Skip to content

Commit 633526f

Browse files
saravmajesticclaude
andcommitted
test(auth): harden the sign-in cancel test and run it in the subprocess pass
- Skip under `OPENCODE_SKIP_SUBPROCESS` and list it in CI's bounded `--max-concurrency=2` subprocess pass, like `cliIt` tests - Skip on Windows, which has no POSIX signals - Timeout messages now include the output captured at the time of failure, and timers and polling loops stop when the test ends Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bb89009 commit 633526f

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ jobs:
337337
# pass is isolated from the 11k main-suite tests so 2-per-worker is plenty bounded.
338338
# (Bun 1.3.x can segfault during cleanup after all tests pass — we parse the summary, not the
339339
# exit code. --timeout 90000 gives headroom for slow fs/spawn tests.)
340-
SUBPROCESS_PATHS="test/cli/acp/ test/cli/smokes/ test/cli/serve/ test/cli/run/run-process.test.ts test/cli/mcp-add.test.ts test/cli/help/help-snapshots.test.ts"
340+
SUBPROCESS_PATHS="test/cli/acp/ test/cli/smokes/ test/cli/serve/ test/cli/run/run-process.test.ts test/cli/mcp-add.test.ts test/cli/help/help-snapshots.test.ts test/cli/auth-login-cancel.test.ts"
341341
342342
OPENCODE_SKIP_SUBPROCESS=1 bun test --timeout 90000 > /tmp/test-main.txt 2>&1 || true
343343
bun test --timeout 90000 --max-concurrency=2 $SUBPROCESS_PATHS > /tmp/test-sub.txt 2>&1 || true

‎packages/opencode/test/cli/auth-login-cancel.test.ts‎

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,26 @@ afterAll(async () => {
6262
await fs.rm(root, { recursive: true, force: true })
6363
})
6464

65-
const withTimeout = <T>(promise: Promise<T>, ms: number, label: string) =>
66-
Promise.race([
67-
promise,
68-
new Promise<never>((_, reject) => setTimeout(() => reject(new Error(`${label} after ${ms}ms`)), ms)),
69-
])
65+
// Fails with `what()` evaluated at timeout, so the message carries the output captured so far.
66+
async function waitFor(check: () => boolean, ms: number, what: () => string) {
67+
const deadline = Date.now() + ms
68+
while (!check()) {
69+
if (Date.now() > deadline) throw new Error(`${what()} (after ${ms}ms)`)
70+
await Bun.sleep(50)
71+
}
72+
}
73+
74+
async function exitWithin<T>(exited: Promise<T>, ms: number, what: string) {
75+
let timer: ReturnType<typeof setTimeout> | undefined
76+
const timeout = new Promise<never>((_, reject) => {
77+
timer = setTimeout(() => reject(new Error(`${what} after ${ms}ms`)), ms)
78+
})
79+
return Promise.race([exited, timeout]).finally(() => clearTimeout(timer))
80+
}
7081

71-
describe("auth login cancel", () => {
82+
// Spawns the real CLI, so like cliIt tests it runs only in CI's bounded subprocess pass.
83+
// Windows has no POSIX signals or Ctrl+C-as-SIGINT through a PTY.
84+
describe.skipIf(!!process.env.OPENCODE_SKIP_SUBPROCESS || process.platform === "win32")("auth login cancel", () => {
7285
test("Ctrl+C in a terminal exits 130", async () => {
7386
const child = ptySpawn(process.execPath, argv, { name: "xterm-256color", cols: 120, rows: 40, cwd: root, env })
7487
let output = ""
@@ -77,15 +90,13 @@ describe("auth login cancel", () => {
7790
})
7891
const exited = new Promise<{ exitCode: number }>((resolve) => child.onExit(resolve))
7992
try {
80-
await withTimeout(
81-
(async () => {
82-
while (!output.includes(WAITING)) await Bun.sleep(50)
83-
})(),
93+
await waitFor(
94+
() => output.includes(WAITING),
8495
60_000,
85-
`never printed "${WAITING}": ${output}`,
96+
() => `never printed "${WAITING}": ${output}`,
8697
)
8798
child.write("\x03")
88-
const { exitCode } = await withTimeout(exited, 10_000, "still running after Ctrl+C")
99+
const { exitCode } = await exitWithin(exited, 10_000, "still running after Ctrl+C")
89100
expect(exitCode).toBe(130)
90101
expect(output).toContain("Sign-in canceled.")
91102
} finally {
@@ -101,27 +112,24 @@ describe("auth login cancel", () => {
101112
stdout: "pipe",
102113
stderr: "pipe",
103114
})
104-
try {
115+
let output = ""
116+
const decoder = new TextDecoder()
117+
const drained = (async () => {
105118
const reader = proc.stdout.getReader()
106-
const decoder = new TextDecoder()
107-
let output = ""
108-
await withTimeout(
109-
(async () => {
110-
while (!output.includes(WAITING)) {
111-
const { value, done } = await reader.read()
112-
if (done) throw new Error(`exited before waiting: ${output}`)
113-
output += decoder.decode(value)
114-
}
115-
})(),
119+
for (let next = await reader.read(); !next.done; next = await reader.read()) output += decoder.decode(next.value)
120+
})()
121+
try {
122+
await waitFor(
123+
() => output.includes(WAITING),
116124
60_000,
117-
`never printed "${WAITING}": ${output}`,
125+
() => `never printed "${WAITING}": ${output}`,
118126
)
119-
reader.releaseLock()
120127
proc.kill("SIGINT")
121-
expect(await withTimeout(proc.exited, 10_000, "still running after SIGINT")).toBe(130)
128+
expect(await exitWithin(proc.exited, 10_000, "still running after SIGINT")).toBe(130)
122129
expect(await new Response(proc.stderr).text()).toContain("Sign-in canceled.")
123130
} finally {
124131
proc.kill("SIGKILL")
132+
await drained
125133
}
126134
}, 90_000)
127135
})

0 commit comments

Comments
 (0)