chore: update slashCommands tests with waitForCondition

This commit is contained in:
uinstinct 2025-12-05 13:48:49 +05:30
parent 37b4702d25
commit bf2a8a4add

View file

@ -1,5 +1,5 @@
import { renderInMode, testBothModes } from "./TUIChat.dualModeHelper.js";
import { waitForCondition, waitForNextRender } from "./TUIChat.testHelper.js";
import { waitForCondition } from "./TUIChat.testHelper.js";
describe("TUIChat - Slash Commands Tests", () => {
testBothModes("shows slash when user types /", async (mode) => {
@ -7,9 +7,12 @@ describe("TUIChat - Slash Commands Tests", () => {
// Type / to trigger slash command
stdin.write("/");
await waitForNextRender();
const frame = lastFrame();
let frame = "";
await waitForCondition(() => {
frame = lastFrame() ?? "";
return frame.includes("/");
});
// Should show the slash character
expect(frame).toContain("/");
@ -51,14 +54,21 @@ describe("TUIChat - Slash Commands Tests", () => {
testBothModes("handles tab key after slash command", async (mode) => {
const { lastFrame, stdin } = renderInMode(mode);
// Type /exi and then tab
stdin.write("/exi");
await waitForNextRender();
let frame = "";
await waitForCondition(() => {
frame = lastFrame() ?? "";
return frame.includes("/exi");
});
stdin.write("\t");
await waitForNextRender();
const frameAfterTab = lastFrame();
let frameAfterTab = "";
await waitForCondition(() => {
frameAfterTab = lastFrame() ?? "";
return frameAfterTab.length > 0;
});
// Should not crash after tab
expect(frameAfterTab).toBeDefined();
@ -77,11 +87,13 @@ describe("TUIChat - Slash Commands Tests", () => {
testBothModes("shows slash command menu when typing /", async (mode) => {
const { lastFrame, stdin } = renderInMode(mode);
// Type just /
stdin.write("/");
await waitForNextRender();
const frame = lastFrame();
let frame = "";
await waitForCondition(() => {
frame = lastFrame() ?? "";
return frame.includes("/");
});
// Should show the slash
expect(frame).toContain("/");
@ -111,9 +123,18 @@ describe("TUIChat - Slash Commands Tests", () => {
// Type a complete command name first
stdin.write("/title");
await waitForNextRender();
const frameAfterCommand = lastFrame();
let frameAfterCommand = lastFrame();
await waitForCondition(() => {
frameAfterCommand = lastFrame();
return (
frameAfterCommand?.includes(
mode === "remote" ? "Remote Mode" : "/title",
) ?? false
);
});
if (mode === "remote") {
// In remote mode, /title might not be a valid command, so just check we're in remote mode
expect(frameAfterCommand).toContain("Remote Mode");
@ -124,9 +145,12 @@ describe("TUIChat - Slash Commands Tests", () => {
// Now add a space and arguments
stdin.write(" My Session Title");
await waitForNextRender();
const frameAfterArgs = lastFrame();
let frameAfterArgs = lastFrame();
await waitForCondition(() => {
frameAfterArgs = lastFrame() ?? "";
return frameAfterArgs.includes("My Session Title");
});
// Check that the UI is still functional after adding arguments
if (mode === "remote") {
@ -145,19 +169,27 @@ describe("TUIChat - Slash Commands Tests", () => {
async (mode) => {
const { lastFrame, stdin } = renderInMode(mode);
// Type a complete command with arguments
stdin.write("/title Test Session");
await waitForNextRender();
const frameBeforeEnter = lastFrame();
let frameBeforeEnter = lastFrame();
await waitForCondition(() => {
frameBeforeEnter = lastFrame() ?? "";
return (
frameBeforeEnter.includes("/title") &&
frameBeforeEnter.includes("Test Session")
);
});
expect(frameBeforeEnter).toContain("/title");
expect(frameBeforeEnter).toContain("Test Session");
// Press Enter - this should execute the command, not try to autocomplete
stdin.write("\r");
await waitForNextRender();
const frameAfterEnter = lastFrame();
let frameAfterEnter = lastFrame();
await waitForCondition(() => {
frameAfterEnter = lastFrame() ?? "";
return frameAfterEnter.length > 0;
});
// Should not crash and should clear the input (or show command execution)
expect(frameAfterEnter).toBeDefined();