|
| 1 | +import childProcess from "node:child_process"; |
| 2 | +import { syncBuiltinESMExports } from "node:module"; |
1 | 3 | import assert from "node:assert/strict"; |
2 | 4 | import test from "node:test"; |
3 | 5 |
|
|
6 | 8 | ghJsonWithRetry, |
7 | 9 | ghJsonWithRetryAsync, |
8 | 10 | ghSpawn, |
| 11 | + ghText, |
| 12 | + ghTextAsync, |
9 | 13 | githubLimitedPagePath, |
10 | 14 | githubPaginatedPath, |
11 | 15 | } from "../../dist/repair/github-cli.js"; |
@@ -183,3 +187,71 @@ test("public read throttles fall back once to the target App token", async () => |
183 | 187 | } |
184 | 188 | } |
185 | 189 | }); |
| 190 | + |
| 191 | +test("GitHub CLI deadlines stop stalled children and preserve successful output", async () => { |
| 192 | + const env = { |
| 193 | + GH_BIN: process.execPath, |
| 194 | + GH_BIN_ARGS: JSON.stringify(["--eval", "setTimeout(() => {}, 10_000)", "--"]), |
| 195 | + }; |
| 196 | + const options = { env, timeoutMs: 100 }; |
| 197 | + assert.throws(() => ghText(["api", "user"], options), { code: "ETIMEDOUT" }); |
| 198 | + await assert.rejects(ghTextAsync(["api", "user"], options), { |
| 199 | + killed: true, |
| 200 | + signal: "SIGTERM", |
| 201 | + }); |
| 202 | + assert.equal(ghSpawn(["api", "user"], options).error?.code, "ETIMEDOUT"); |
| 203 | + |
| 204 | + env.GH_BIN_ARGS = JSON.stringify(["--eval", "process.stdout.write('ok')", "--"]); |
| 205 | + assert.equal(ghText(["api", "user"], { env, timeoutMs: 2_000 }), "ok"); |
| 206 | + assert.equal(await ghTextAsync(["api", "user"], { env, timeoutMs: 2_000 }), "ok"); |
| 207 | + assert.equal(ghSpawn(["api", "user"], { env, timeoutMs: 2_000 }).stdout, "ok"); |
| 208 | +}); |
| 209 | + |
| 210 | +test("GitHub CLI deadline selection follows the child environment and explicit budget", (t) => { |
| 211 | + let observedTimeout: number | undefined; |
| 212 | + t.mock.method(childProcess, "spawnSync", (_file, _args, options) => { |
| 213 | + observedTimeout = options.timeout; |
| 214 | + return { status: 0, stdout: "", stderr: "" }; |
| 215 | + }); |
| 216 | + syncBuiltinESMExports(); |
| 217 | + t.after(() => { |
| 218 | + t.mock.restoreAll(); |
| 219 | + syncBuiltinESMExports(); |
| 220 | + }); |
| 221 | + const previous = process.env.CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS; |
| 222 | + process.env.CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS = "90000"; |
| 223 | + t.after(() => { |
| 224 | + if (previous === undefined) delete process.env.CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS; |
| 225 | + else process.env.CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS = previous; |
| 226 | + }); |
| 227 | + const cases = [ |
| 228 | + { env: {}, expected: 90_000 }, |
| 229 | + { env: { CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS: "45000" }, expected: 45_000 }, |
| 230 | + { env: { CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS: "10" }, expected: 30_000 }, |
| 231 | + { |
| 232 | + env: { |
| 233 | + CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS: undefined, |
| 234 | + CLAWSWEEPER_NETWORK_COMMAND_TIMEOUT_MS: "60000", |
| 235 | + }, |
| 236 | + expected: 60_000, |
| 237 | + }, |
| 238 | + { |
| 239 | + env: { |
| 240 | + CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS: undefined, |
| 241 | + CLAWSWEEPER_NETWORK_COMMAND_TIMEOUT_MS: undefined, |
| 242 | + }, |
| 243 | + expected: 120_000, |
| 244 | + }, |
| 245 | + ...["", "invalid", "0", "-1", "Infinity"].map((value) => ({ |
| 246 | + env: { CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS: value }, |
| 247 | + expected: 120_000, |
| 248 | + })), |
| 249 | + { timeoutMs: 250, env: {}, expected: 250 }, |
| 250 | + { timeoutMs: 0.5, env: {}, expected: 1 }, |
| 251 | + ...[0, -1, NaN, Infinity].map((timeoutMs) => ({ timeoutMs, env: {}, expected: 90_000 })), |
| 252 | + ]; |
| 253 | + for (const { expected, ...options } of cases) { |
| 254 | + ghSpawn(["api", "user"], { ...options, env: { ...options.env, GH_BIN: process.execPath } }); |
| 255 | + assert.equal(observedTimeout, expected); |
| 256 | + } |
| 257 | +}); |
0 commit comments