Skip to content

Commit bbb9d9c

Browse files
authored
Merge pull request #8987 from uinstinct/wait-tui-test
fix(cli): other tui update tests
2 parents cf80727 + 3db6091 commit bbb9d9c

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

extensions/cli/src/ui/__tests__/TUIChat.editMessage.test.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
import { renderInMode, testBothModes } from "./TUIChat.dualModeHelper.js";
2-
3-
async function waitForCondition(
4-
conditionFn: () => boolean,
5-
timeoutMs = 2000,
6-
intervalMs = 50,
7-
): Promise<void> {
8-
const startTime = Date.now();
9-
while (Date.now() - startTime < timeoutMs) {
10-
if (conditionFn()) {
11-
return;
12-
}
13-
await new Promise((resolve) => setTimeout(resolve, intervalMs));
14-
}
15-
}
2+
import { waitForCondition } from "./TUIChat.testHelper.js";
163

174
/**
185
* Integration tests for the message edit feature in TUIChat

extensions/cli/src/ui/__tests__/TUIChat.fileSearch.test.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { renderInMode, testSingleMode } from "./TUIChat.dualModeHelper.js";
2-
import { waitForNextRender } from "./TUIChat.testHelper.js";
2+
import { waitForCondition } from "./TUIChat.testHelper.js";
33

44
describe("TUIChat - @ File Search Tests", () => {
55
testSingleMode("shows @ character when user types @", "local", async () => {
@@ -34,17 +34,20 @@ describe("TUIChat - @ File Search Tests", () => {
3434
// Type @ followed by text to filter files
3535
stdin.write("@READ");
3636

37-
// Wait for file search to filter and display results
38-
await new Promise((resolve) => setTimeout(resolve, 500));
37+
let frame = lastFrame();
3938

40-
const frame = lastFrame()!;
39+
await waitForCondition(() => {
40+
frame = lastFrame();
41+
return frame?.includes("@READ") ?? false;
42+
});
4143

44+
expect(frame).toBeDefined();
4245
// Should show the typed text
4346
expect(frame).toContain("@READ");
4447

4548
// Should show either navigation hints or at least indicate file search is working
46-
const hasNavigationHints = frame.includes("↑/↓ to navigate");
47-
const hasFileSearch = frame.includes("@READ");
49+
const hasNavigationHints = frame!.includes("↑/↓ to navigate");
50+
const hasFileSearch = frame!.includes("@READ");
4851
expect(hasNavigationHints || hasFileSearch).toBe(true);
4952

5053
// Local mode specific UI expectations
@@ -57,10 +60,13 @@ describe("TUIChat - @ File Search Tests", () => {
5760

5861
// Type multiple @ characters
5962
stdin.write("@@test");
60-
// Wait for UI update
61-
await waitForNextRender();
6263

63-
const frame = lastFrame();
64+
let frame = lastFrame();
65+
66+
await waitForCondition(() => {
67+
frame = lastFrame();
68+
return frame?.includes("@@test") ?? false;
69+
});
6470

6571
// Should handle multiple @ without crashing
6672
expect(frame).toBeDefined();

extensions/cli/src/ui/__tests__/TUIChat.slashCommands.test.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { renderInMode, testBothModes } from "./TUIChat.dualModeHelper.js";
2-
import { waitForNextRender } from "./TUIChat.testHelper.js";
2+
import { waitForCondition, waitForNextRender } from "./TUIChat.testHelper.js";
33

44
describe("TUIChat - Slash Commands Tests", () => {
55
testBothModes("shows slash when user types /", async (mode) => {
@@ -28,9 +28,13 @@ describe("TUIChat - Slash Commands Tests", () => {
2828

2929
// Type /exi to trigger slash command filtering
3030
stdin.write("/exi");
31-
await waitForNextRender();
3231

33-
const frame = lastFrame();
32+
let frame = lastFrame();
33+
34+
await waitForCondition(() => {
35+
frame = lastFrame();
36+
return frame?.includes("/exi") ?? false;
37+
});
3438

3539
// Should show the typed command
3640
expect(frame).toContain("/exi");

extensions/cli/src/ui/__tests__/TUIChat.testHelper.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,24 @@ export function expectNormalMode(frame: string | undefined) {
458458
expect(frame).toContain("Continue CLI");
459459
}
460460

461+
/**
462+
* Helper to wait for a condition to be true
463+
* similar to `waitFor` in testing libraries
464+
*/
465+
export async function waitForCondition(
466+
conditionFn: () => boolean,
467+
timeoutMs = 2000,
468+
intervalMs = 50,
469+
): Promise<void> {
470+
const startTime = Date.now();
471+
while (Date.now() - startTime < timeoutMs) {
472+
if (conditionFn()) {
473+
return;
474+
}
475+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
476+
}
477+
}
478+
461479
// Make runTest available globally for test files
462480
declare global {
463481
var runTest: (

0 commit comments

Comments
 (0)