|
| 1 | +import { chmod, mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { sleep } from "../utils/sleep.js"; |
| 6 | +import { |
| 7 | + disposeActiveTuiFixtures, |
| 8 | + startTuiFixture, |
| 9 | +} from "./tui-pty-harness-fixture-test-support.js"; |
| 10 | + |
| 11 | +const STARTUP_TIMEOUT_MS = 60_000; |
| 12 | +const EXIT_TIMEOUT_MS = 4_000; |
| 13 | +const tempDirs: string[] = []; |
| 14 | + |
| 15 | +async function createCodexFixture(exitMs?: number) { |
| 16 | + const dir = await mkdtemp(path.join(tmpdir(), "openclaw-tui-auth-")); |
| 17 | + tempDirs.push(dir); |
| 18 | + const scriptPath = path.join(dir, "codex-fixture.cjs"); |
| 19 | + await writeFile( |
| 20 | + scriptPath, |
| 21 | + [ |
| 22 | + 'console.log("AUTH_CHILD_STARTED:" + process.pid);', |
| 23 | + exitMs === undefined |
| 24 | + ? "setInterval(() => {}, 1000);" |
| 25 | + : `setTimeout(() => process.exit(0), ${String(exitMs)});`, |
| 26 | + ].join("\n"), |
| 27 | + "utf8", |
| 28 | + ); |
| 29 | + if (process.platform === "win32") { |
| 30 | + await writeFile( |
| 31 | + path.join(dir, "codex.cmd"), |
| 32 | + `@"${process.execPath}" "${scriptPath}" %*\r\n`, |
| 33 | + "utf8", |
| 34 | + ); |
| 35 | + } else { |
| 36 | + const launcherPath = path.join(dir, "codex"); |
| 37 | + await writeFile(launcherPath, `#!/bin/sh\nexec "${process.execPath}" "${scriptPath}" "$@"\n`); |
| 38 | + await chmod(launcherPath, 0o755); |
| 39 | + } |
| 40 | + return { |
| 41 | + pathEnv: `${dir}${path.delimiter}${process.env.PATH ?? ""}`, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +async function waitForProcessExit(pid: number, timeoutMs: number): Promise<void> { |
| 46 | + const deadline = Date.now() + timeoutMs; |
| 47 | + while (Date.now() < deadline) { |
| 48 | + try { |
| 49 | + process.kill(pid, 0); |
| 50 | + } catch { |
| 51 | + return; |
| 52 | + } |
| 53 | + await sleep(10); |
| 54 | + } |
| 55 | + throw new Error(`auth child ${String(pid)} remained alive`); |
| 56 | +} |
| 57 | + |
| 58 | +describe.sequential("TUI auth child lifecycle", () => { |
| 59 | + afterEach(async () => { |
| 60 | + await disposeActiveTuiFixtures(); |
| 61 | + for (const dir of tempDirs.splice(0)) { |
| 62 | + await rm(dir, { recursive: true, force: true }); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + it.skipIf(process.platform === "win32")( |
| 67 | + "terminates the foreground auth child when SIGTERM exits the TUI", |
| 68 | + async () => { |
| 69 | + const auth = await createCodexFixture(); |
| 70 | + const fixture = await startTuiFixture({ |
| 71 | + env: { |
| 72 | + PATH: auth.pathEnv, |
| 73 | + }, |
| 74 | + }); |
| 75 | + await fixture.run.waitForOutput("local ready", STARTUP_TIMEOUT_MS); |
| 76 | + await fixture.run.write("/auth openai\r", { delay: false }); |
| 77 | + await fixture.run.waitForOutput("AUTH_CHILD_STARTED:", STARTUP_TIMEOUT_MS); |
| 78 | + const pidMatch = fixture.run.visibleOutput().match(/AUTH_CHILD_STARTED:(\d+)/u); |
| 79 | + expect(pidMatch).not.toBeNull(); |
| 80 | + const authPid = Number(pidMatch?.[1]); |
| 81 | + |
| 82 | + process.kill(fixture.run.pid, "SIGTERM"); |
| 83 | + |
| 84 | + await expect(fixture.run.waitForExit(EXIT_TIMEOUT_MS)).resolves.toBeDefined(); |
| 85 | + await expect(waitForProcessExit(authPid, 750)).resolves.toBeUndefined(); |
| 86 | + }, |
| 87 | + STARTUP_TIMEOUT_MS + EXIT_TIMEOUT_MS, |
| 88 | + ); |
| 89 | + |
| 90 | + it( |
| 91 | + "resumes the TUI after normal auth completion", |
| 92 | + async () => { |
| 93 | + const auth = await createCodexFixture(50); |
| 94 | + const fixture = await startTuiFixture({ |
| 95 | + env: { |
| 96 | + PATH: auth.pathEnv, |
| 97 | + }, |
| 98 | + }); |
| 99 | + await fixture.run.waitForOutput("local ready", STARTUP_TIMEOUT_MS); |
| 100 | + await fixture.run.write("/auth openai\r", { delay: false }); |
| 101 | + await fixture.run.waitForOutput("auth flow finished for openai", STARTUP_TIMEOUT_MS); |
| 102 | + await fixture.run.write("/gateway-status\r", { delay: false }); |
| 103 | + await fixture.run.waitForOutput("fixture gateway ok", STARTUP_TIMEOUT_MS); |
| 104 | + }, |
| 105 | + STARTUP_TIMEOUT_MS * 2, |
| 106 | + ); |
| 107 | +}); |
0 commit comments