Skip to content

Commit 2923ee4

Browse files
steipetePeter Steinberger
andauthored
test(tui): reuse real gateway PTY fixture (#118567)
* test(tui): reuse real gateway PTY fixture * test(tui): share gateway mock model --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 63651a1 commit 2923ee4

3 files changed

Lines changed: 327 additions & 419 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { describe, expect, it } from "vitest";
2+
import { createDeferred } from "../test-utils/deferred.js";
3+
import { cleanupStartedFixture, createFreshSession } from "./tui-pty-local-test-support.js";
4+
import type { PtyRun } from "./tui-pty-test-support.js";
5+
6+
const SUBMISSION_SETTLE_MS = 150;
7+
const SESSION_ROLLOVER_BUSY_MESSAGE = "abort the current run before /new";
8+
9+
describe("local TUI PTY fixture support", () => {
10+
it("owns late fixture startup without swallowing cleanup failures", async () => {
11+
await expect(cleanupStartedFixture(Promise.reject(new Error("setup failed")))).resolves.toBe(
12+
undefined,
13+
);
14+
15+
const cleanupError = new Error("cleanup failed");
16+
const fixture = {
17+
cleanup: async () => {
18+
throw cleanupError;
19+
},
20+
};
21+
await expect(cleanupStartedFixture(Promise.resolve(fixture))).rejects.toBe(cleanupError);
22+
});
23+
24+
it("does not replay a session rollover when an old busy notice is redrawn", async () => {
25+
const newSessionPrefix = "new session: agent:main:tui-";
26+
const acceptedSession = createDeferred();
27+
const writes: string[] = [];
28+
let output = "";
29+
let acceptanceTimer: ReturnType<typeof setTimeout> | undefined;
30+
const run = {
31+
output: () => output,
32+
visibleOutput: () => output.replace(/\s+/gu, " "),
33+
write: async (data: string) => {
34+
writes.push(data);
35+
if (writes.length === 1) {
36+
output += `${SESSION_ROLLOVER_BUSY_MESSAGE}\n`;
37+
acceptanceTimer = setTimeout(() => {
38+
output += `${newSessionPrefix}accepted\nlocal ready | idle\n`;
39+
acceptedSession.resolve();
40+
}, SUBMISSION_SETTLE_MS + 50);
41+
return;
42+
}
43+
output += `${newSessionPrefix}duplicate\nlocal ready | idle\n`;
44+
},
45+
waitForOutput: async () => output,
46+
waitForExit: async () => ({ exitCode: 0, signal: 0 }),
47+
forceKill: async () => {},
48+
dispose: async () => {},
49+
} satisfies PtyRun;
50+
51+
try {
52+
await createFreshSession(run, newSessionPrefix);
53+
await acceptedSession.promise;
54+
expect(writes).toEqual(["/new\r"]);
55+
} finally {
56+
if (acceptanceTimer) {
57+
clearTimeout(acceptanceTimer);
58+
}
59+
}
60+
});
61+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { waitFor, type PtyRun } from "./tui-pty-test-support.js";
2+
3+
const STARTUP_TIMEOUT_MS = 60_000;
4+
const OUTPUT_TIMEOUT_MS = 120_000;
5+
6+
export async function waitForOutputAfter(
7+
run: PtyRun,
8+
needle: string,
9+
offset: number,
10+
timeoutMs = OUTPUT_TIMEOUT_MS,
11+
) {
12+
await waitFor({
13+
timeoutMs,
14+
read: () => (run.visibleOutput().slice(offset).includes(needle) ? true : null),
15+
onTimeout: () =>
16+
new Error(
17+
`timed out waiting for ${JSON.stringify(needle)} after offset ${offset}\n${run.output()}`,
18+
),
19+
});
20+
}
21+
22+
export function lastOutputIndexAfter(run: PtyRun, needle: string, offset: number): number {
23+
const relativeIndex = run.visibleOutput().slice(offset).lastIndexOf(needle);
24+
return relativeIndex < 0 ? -1 : offset + relativeIndex;
25+
}
26+
27+
export async function createFreshSession(run: PtyRun, newSessionPrefix: string) {
28+
const outputOffset = run.visibleOutput().length;
29+
await run.write("/new\r", { delay: false });
30+
await waitFor({
31+
timeoutMs: STARTUP_TIMEOUT_MS,
32+
read: () => (run.visibleOutput().includes(newSessionPrefix, outputOffset) ? true : null),
33+
onTimeout: () =>
34+
new Error(`timed out creating a fresh session after one submission\n${run.output()}`),
35+
});
36+
const newSessionOffset = run.visibleOutput().lastIndexOf(newSessionPrefix);
37+
// Wait for the accepted session's own idle redraw; older PTY frames can
38+
// replay busy messages and must never cause a second session creation.
39+
await waitForOutputAfter(run, "| idle", newSessionOffset);
40+
}
41+
42+
export async function cleanupStartedFixture(
43+
startup: Promise<{ cleanup: () => Promise<void> }> | undefined,
44+
): Promise<void> {
45+
if (!startup) {
46+
return;
47+
}
48+
let fixture: { cleanup: () => Promise<void> };
49+
try {
50+
fixture = await startup;
51+
} catch {
52+
// The setup hook already reports startup failures. Teardown only owns cleanup.
53+
return;
54+
}
55+
await fixture.cleanup();
56+
}

0 commit comments

Comments
 (0)