From c1ac9fc845ee370a1d13a2dfb49348d872c57854 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Wed, 3 Dec 2025 18:14:27 +0530 Subject: [PATCH 1/3] refactor `waitForCondition` into file --- .../ui/__tests__/TUIChat.editMessage.test.tsx | 15 +-------------- .../cli/src/ui/__tests__/TUIChat.testHelper.ts | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/extensions/cli/src/ui/__tests__/TUIChat.editMessage.test.tsx b/extensions/cli/src/ui/__tests__/TUIChat.editMessage.test.tsx index 73af37d50..2a537cf3d 100644 --- a/extensions/cli/src/ui/__tests__/TUIChat.editMessage.test.tsx +++ b/extensions/cli/src/ui/__tests__/TUIChat.editMessage.test.tsx @@ -1,18 +1,5 @@ import { renderInMode, testBothModes } from "./TUIChat.dualModeHelper.js"; - -async function waitForCondition( - conditionFn: () => boolean, - timeoutMs = 2000, - intervalMs = 50, -): Promise { - const startTime = Date.now(); - while (Date.now() - startTime < timeoutMs) { - if (conditionFn()) { - return; - } - await new Promise((resolve) => setTimeout(resolve, intervalMs)); - } -} +import { waitForCondition } from "./TUIChat.testHelper.js"; /** * Integration tests for the message edit feature in TUIChat diff --git a/extensions/cli/src/ui/__tests__/TUIChat.testHelper.ts b/extensions/cli/src/ui/__tests__/TUIChat.testHelper.ts index 37ebb5c3e..00a0e16a3 100644 --- a/extensions/cli/src/ui/__tests__/TUIChat.testHelper.ts +++ b/extensions/cli/src/ui/__tests__/TUIChat.testHelper.ts @@ -458,6 +458,24 @@ export function expectNormalMode(frame: string | undefined) { expect(frame).toContain("Continue CLI"); } +/** + * Helper to wait for a condition to be true + * similar to `waitFor` in testing libraries + */ +export async function waitForCondition( + conditionFn: () => boolean, + timeoutMs = 2000, + intervalMs = 50, +): Promise { + const startTime = Date.now(); + while (Date.now() - startTime < timeoutMs) { + if (conditionFn()) { + return; + } + await new Promise((resolve) => setTimeout(resolve, intervalMs)); + } +} + // Make runTest available globally for test files declare global { var runTest: ( From 50f38286c21c98a5bf193216f60fc2abcb032155 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Wed, 3 Dec 2025 18:21:46 +0530 Subject: [PATCH 2/3] update with waitForCondition --- .../cli/src/ui/__tests__/TUIChat.fileSearch.test.tsx | 10 +++++----- .../src/ui/__tests__/TUIChat.slashCommands.test.tsx | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/extensions/cli/src/ui/__tests__/TUIChat.fileSearch.test.tsx b/extensions/cli/src/ui/__tests__/TUIChat.fileSearch.test.tsx index b38a90cb6..fbed0423a 100644 --- a/extensions/cli/src/ui/__tests__/TUIChat.fileSearch.test.tsx +++ b/extensions/cli/src/ui/__tests__/TUIChat.fileSearch.test.tsx @@ -1,5 +1,5 @@ import { renderInMode, testSingleMode } from "./TUIChat.dualModeHelper.js"; -import { waitForNextRender } from "./TUIChat.testHelper.js"; +import { waitForCondition } from "./TUIChat.testHelper.js"; describe("TUIChat - @ File Search Tests", () => { testSingleMode("shows @ character when user types @", "local", async () => { @@ -34,11 +34,10 @@ describe("TUIChat - @ File Search Tests", () => { // Type @ followed by text to filter files stdin.write("@READ"); - // Wait for file search to filter and display results - await new Promise((resolve) => setTimeout(resolve, 500)); - const frame = lastFrame()!; + waitForCondition(() => lastFrame()!.includes("@READ")); + // Should show the typed text expect(frame).toContain("@READ"); @@ -58,10 +57,11 @@ describe("TUIChat - @ File Search Tests", () => { // Type multiple @ characters stdin.write("@@test"); // Wait for UI update - await waitForNextRender(); const frame = lastFrame(); + await waitForCondition(() => lastFrame()?.includes("@@test") ?? false); + // Should handle multiple @ without crashing expect(frame).toBeDefined(); expect(frame).toContain("@@test"); diff --git a/extensions/cli/src/ui/__tests__/TUIChat.slashCommands.test.tsx b/extensions/cli/src/ui/__tests__/TUIChat.slashCommands.test.tsx index 1f63f3814..b3630d2b6 100644 --- a/extensions/cli/src/ui/__tests__/TUIChat.slashCommands.test.tsx +++ b/extensions/cli/src/ui/__tests__/TUIChat.slashCommands.test.tsx @@ -1,5 +1,5 @@ import { renderInMode, testBothModes } from "./TUIChat.dualModeHelper.js"; -import { waitForNextRender } from "./TUIChat.testHelper.js"; +import { waitForCondition, waitForNextRender } from "./TUIChat.testHelper.js"; describe("TUIChat - Slash Commands Tests", () => { testBothModes("shows slash when user types /", async (mode) => { @@ -28,10 +28,11 @@ describe("TUIChat - Slash Commands Tests", () => { // Type /exi to trigger slash command filtering stdin.write("/exi"); - await waitForNextRender(); const frame = lastFrame(); + waitForCondition(() => lastFrame()?.includes("/exi") ?? false); + // Should show the typed command expect(frame).toContain("/exi"); From 3db6091dc5db43338141f719cbf75a1bf43ea6ae Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Wed, 3 Dec 2025 18:41:19 +0530 Subject: [PATCH 3/3] update lastFrame --- .../ui/__tests__/TUIChat.fileSearch.test.tsx | 20 ++++++++++++------- .../__tests__/TUIChat.slashCommands.test.tsx | 7 +++++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/extensions/cli/src/ui/__tests__/TUIChat.fileSearch.test.tsx b/extensions/cli/src/ui/__tests__/TUIChat.fileSearch.test.tsx index fbed0423a..b469b2725 100644 --- a/extensions/cli/src/ui/__tests__/TUIChat.fileSearch.test.tsx +++ b/extensions/cli/src/ui/__tests__/TUIChat.fileSearch.test.tsx @@ -34,16 +34,20 @@ describe("TUIChat - @ File Search Tests", () => { // Type @ followed by text to filter files stdin.write("@READ"); - const frame = lastFrame()!; + let frame = lastFrame(); - waitForCondition(() => lastFrame()!.includes("@READ")); + await waitForCondition(() => { + frame = lastFrame(); + return frame?.includes("@READ") ?? false; + }); + expect(frame).toBeDefined(); // Should show the typed text expect(frame).toContain("@READ"); // Should show either navigation hints or at least indicate file search is working - const hasNavigationHints = frame.includes("↑/↓ to navigate"); - const hasFileSearch = frame.includes("@READ"); + const hasNavigationHints = frame!.includes("↑/↓ to navigate"); + const hasFileSearch = frame!.includes("@READ"); expect(hasNavigationHints || hasFileSearch).toBe(true); // Local mode specific UI expectations @@ -56,11 +60,13 @@ describe("TUIChat - @ File Search Tests", () => { // Type multiple @ characters stdin.write("@@test"); - // Wait for UI update - const frame = lastFrame(); + let frame = lastFrame(); - await waitForCondition(() => lastFrame()?.includes("@@test") ?? false); + await waitForCondition(() => { + frame = lastFrame(); + return frame?.includes("@@test") ?? false; + }); // Should handle multiple @ without crashing expect(frame).toBeDefined(); diff --git a/extensions/cli/src/ui/__tests__/TUIChat.slashCommands.test.tsx b/extensions/cli/src/ui/__tests__/TUIChat.slashCommands.test.tsx index b3630d2b6..6f44933b6 100644 --- a/extensions/cli/src/ui/__tests__/TUIChat.slashCommands.test.tsx +++ b/extensions/cli/src/ui/__tests__/TUIChat.slashCommands.test.tsx @@ -29,9 +29,12 @@ describe("TUIChat - Slash Commands Tests", () => { // Type /exi to trigger slash command filtering stdin.write("/exi"); - const frame = lastFrame(); + let frame = lastFrame(); - waitForCondition(() => lastFrame()?.includes("/exi") ?? false); + await waitForCondition(() => { + frame = lastFrame(); + return frame?.includes("/exi") ?? false; + }); // Should show the typed command expect(frame).toContain("/exi");