|
| 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 | +}); |
0 commit comments