|
| 1 | +import { expect } from "vitest"; |
| 2 | +import { |
| 3 | + type StartTuiPtyFixture, |
| 4 | + waitForSynchronizedFrameRows, |
| 5 | +} from "./tui-pty-harness-assertion-test-support.js"; |
| 6 | +import { objectFieldEquals, readFixtureLog } from "./tui-pty-harness-fixture-test-support.js"; |
| 7 | + |
| 8 | +const slashHelpMarkers = |
| 9 | + "Slash commands:,/help,/verbose <on|off|full>,/reasoning <on|off|stream>,/goal,/goal start <objective>,/btw <side question>,/queue,/stop,/exit".split( |
| 10 | + ",", |
| 11 | + ); |
| 12 | +const countHistoryLoads = async (logPath: string) => |
| 13 | + (await readFixtureLog(logPath)).filter((entry) => entry.method === "loadHistory").length; |
| 14 | +const rowsInclude = (rows: string[], ...texts: string[]) => |
| 15 | + rows.some((row) => texts.every((text) => row.includes(text))); |
| 16 | + |
| 17 | +export async function exerciseTuiCommandSurface( |
| 18 | + startFixture: StartTuiPtyFixture, |
| 19 | + surface: "slash-commands" | "pickers" | "settings", |
| 20 | + startupTimeoutMs: number, |
| 21 | +) { |
| 22 | + const fixture = await startFixture({ |
| 23 | + env: { |
| 24 | + OPENCLAW_TUI_PTY_COLS: "100", |
| 25 | + OPENCLAW_TUI_PTY_ROWS: "24", |
| 26 | + ...(surface === "pickers" ? { OPENCLAW_TUI_PTY_PICKER_FIXTURE: "1" } : {}), |
| 27 | + }, |
| 28 | + }); |
| 29 | + try { |
| 30 | + const waitForRows = (predicate: Parameters<typeof waitForSynchronizedFrameRows>[1]) => |
| 31 | + waitForSynchronizedFrameRows(fixture.run, predicate, startupTimeoutMs); |
| 32 | + await fixture.run.waitForOutput("local ready", startupTimeoutMs); |
| 33 | + if (surface === "slash-commands") { |
| 34 | + await fixture.run.write("/help\r", { delay: false }); |
| 35 | + for (const text of slashHelpMarkers) { |
| 36 | + await fixture.run.waitForOutput(text, startupTimeoutMs); |
| 37 | + } |
| 38 | + await waitForRows((rows) => |
| 39 | + ["/settings", "/exit"].every((text) => rows.some((row) => row.trim() === text)), |
| 40 | + ); |
| 41 | + await fixture.run.write("/gateway-status\r", { delay: false }); |
| 42 | + await fixture.waitForLogEntry((entry) => entry.method === "getGatewayStatus"); |
| 43 | + await waitForRows((rows) => rows.some((row) => row.trim() === "fixture gateway ok")); |
| 44 | + return; |
| 45 | + } |
| 46 | + if (surface === "pickers") { |
| 47 | + await fixture.run.write("/models\r", { delay: false }); |
| 48 | + await fixture.waitForLogEntry((entry) => entry.method === "listModels"); |
| 49 | + await waitForRows((rows) => rows.some((row) => row.includes("Fixture 2"))); |
| 50 | + await fixture.run.write("\x1b[B\r", { delay: false }); |
| 51 | + await fixture.waitForLogEntry( |
| 52 | + (entry) => |
| 53 | + entry.method === "patchSession" && |
| 54 | + objectFieldEquals(entry, "model", "fixture-provider/fixture-model-2"), |
| 55 | + ); |
| 56 | + await fixture.run.write("/sessions\r", { delay: false }); |
| 57 | + await fixture.waitForLogEntry( |
| 58 | + (entry) => entry.method === "listSessions" && objectFieldEquals(entry, "purpose", "picker"), |
| 59 | + ); |
| 60 | + await waitForRows((rows) => rows.some((row) => row.includes("Picker target"))); |
| 61 | + await fixture.run.write("\x1b[B\r", { delay: false }); |
| 62 | + await fixture.waitForLogEntry( |
| 63 | + (entry) => |
| 64 | + entry.method === "loadHistory" && |
| 65 | + objectFieldEquals(entry, "sessionKey", "agent:main:picker-target"), |
| 66 | + ); |
| 67 | + await fixture.run.write("picker selection proof\r", { delay: false }); |
| 68 | + const sent = await fixture.waitForLogEntry( |
| 69 | + (entry) => |
| 70 | + entry.method === "sendChat" && |
| 71 | + objectFieldEquals(entry, "message", "picker selection proof"), |
| 72 | + ); |
| 73 | + expect(sent.payload).toMatchObject({ sessionKey: "agent:main:picker-target" }); |
| 74 | + await waitForRows((rows) => rowsInclude(rows, "PTY_RESPONSE: picker selection proof")); |
| 75 | + return; |
| 76 | + } |
| 77 | + const initialHistoryLoads = await countHistoryLoads(fixture.logPath); |
| 78 | + await fixture.run.write("/settings\r", { delay: false }); |
| 79 | + await waitForRows( |
| 80 | + (rows) => |
| 81 | + rowsInclude(rows, "Tool output", "collapsed") && rowsInclude(rows, "Show thinking", "off"), |
| 82 | + ); |
| 83 | + await fixture.run.write("\r", { delay: false }); |
| 84 | + await waitForRows((rows) => rowsInclude(rows, "Tool output", "expanded")); |
| 85 | + expect(await countHistoryLoads(fixture.logPath)).toBe(initialHistoryLoads); |
| 86 | + await fixture.run.write("\x1b[B\r", { delay: false }); |
| 87 | + await waitForRows((rows) => rowsInclude(rows, "Show thinking", "on")); |
| 88 | + await expect |
| 89 | + .poll(() => countHistoryLoads(fixture.logPath), { timeout: startupTimeoutMs }) |
| 90 | + .toBe(initialHistoryLoads + 1); |
| 91 | + } finally { |
| 92 | + await fixture.cleanup(); |
| 93 | + } |
| 94 | +} |
0 commit comments