|
1 | 1 | // Native hook relay CLI tests cover relay command registration and runtime delegation. |
| 2 | +import { PassThrough } from "node:stream"; |
2 | 3 | import { describe, expect, it, vi } from "vitest"; |
3 | 4 | import { |
4 | 5 | createReadableTextStream, |
|
8 | 9 |
|
9 | 10 | describe("native hook relay CLI", () => { |
10 | 11 | it("reads Codex hook JSON from stdin and forwards it to the gateway relay", async () => { |
11 | | - const callGateway = vi.fn(async () => ({ stdout: "", stderr: "", exitCode: 0 })); |
| 12 | + const callGateway = vi.fn(async (_opts: unknown) => ({ stdout: "", stderr: "", exitCode: 0 })); |
12 | 13 | const stdout = createWritableTextBuffer(); |
13 | 14 | const stderr = createWritableTextBuffer(); |
14 | 15 |
|
@@ -50,9 +51,14 @@ describe("native hook relay CLI", () => { |
50 | 51 | tool_input: { command: "pnpm test" }, |
51 | 52 | }, |
52 | 53 | }, |
53 | | - timeoutMs: 1234, |
| 54 | + timeoutMs: expect.any(Number), |
| 55 | + signal: expect.any(AbortSignal), |
54 | 56 | scopes: ["operator.admin"], |
55 | 57 | }); |
| 58 | + const call = callGateway.mock.calls[0]?.[0] as { timeoutMs?: number } | undefined; |
| 59 | + expect(call).toBeDefined(); |
| 60 | + expect(call?.timeoutMs).toBeGreaterThan(0); |
| 61 | + expect(call?.timeoutMs).toBeLessThanOrEqual(1234); |
56 | 62 | }); |
57 | 63 |
|
58 | 64 | it("renders provider-compatible stdout, stderr, and exit code from the gateway response", async () => { |
@@ -260,6 +266,115 @@ describe("native hook relay CLI", () => { |
260 | 266 | expect(callGateway).not.toHaveBeenCalled(); |
261 | 267 | }); |
262 | 268 |
|
| 269 | + it.each([ |
| 270 | + { |
| 271 | + event: "pre_tool_use", |
| 272 | + preToolUseUnavailable: "noop", |
| 273 | + stdout: null, |
| 274 | + }, |
| 275 | + { |
| 276 | + event: "pre_tool_use", |
| 277 | + stdout: { |
| 278 | + hookSpecificOutput: { |
| 279 | + hookEventName: "PreToolUse", |
| 280 | + permissionDecision: "deny", |
| 281 | + permissionDecisionReason: "Native hook relay timed out", |
| 282 | + }, |
| 283 | + }, |
| 284 | + }, |
| 285 | + { |
| 286 | + event: "permission_request", |
| 287 | + stdout: { |
| 288 | + hookSpecificOutput: { |
| 289 | + hookEventName: "PermissionRequest", |
| 290 | + decision: { |
| 291 | + behavior: "deny", |
| 292 | + message: "Native hook relay timed out", |
| 293 | + }, |
| 294 | + }, |
| 295 | + }, |
| 296 | + }, |
| 297 | + { |
| 298 | + event: "post_tool_use", |
| 299 | + stdout: null, |
| 300 | + }, |
| 301 | + ])( |
| 302 | + "bounds valid $event hook input that never reaches EOF", |
| 303 | + async (testCase) => { |
| 304 | + const invokeBridge = vi.fn(); |
| 305 | + const callGateway = vi.fn(); |
| 306 | + const stdin = createHeldOpenTextStream("{}"); |
| 307 | + const stdout = createWritableTextBuffer(); |
| 308 | + const stderr = createWritableTextBuffer(); |
| 309 | + |
| 310 | + const exitCode = await runNativeHookRelayCli( |
| 311 | + { |
| 312 | + provider: "codex", |
| 313 | + relayId: "relay-1", |
| 314 | + generation: "generation-1", |
| 315 | + event: testCase.event, |
| 316 | + preToolUseUnavailable: testCase.preToolUseUnavailable, |
| 317 | + timeout: "25", |
| 318 | + }, |
| 319 | + { |
| 320 | + stdin, |
| 321 | + stdout, |
| 322 | + stderr, |
| 323 | + invokeBridge: invokeBridge as never, |
| 324 | + callGateway: callGateway as never, |
| 325 | + }, |
| 326 | + ); |
| 327 | + |
| 328 | + expect(exitCode).toBe(0); |
| 329 | + if (testCase.stdout) { |
| 330 | + expect(JSON.parse(stdout.text())).toEqual(testCase.stdout); |
| 331 | + } else { |
| 332 | + expect(stdout.text()).toBe(""); |
| 333 | + } |
| 334 | + expect(stderr.text()).toContain("native hook relay timed out"); |
| 335 | + expect(stdin.destroyed).toBe(true); |
| 336 | + expect(invokeBridge).not.toHaveBeenCalled(); |
| 337 | + expect(callGateway).not.toHaveBeenCalled(); |
| 338 | + }, |
| 339 | + 1_000, |
| 340 | + ); |
| 341 | + |
| 342 | + it("applies the relay deadline to gateway fallback", async () => { |
| 343 | + const invokeBridge = vi.fn(async () => { |
| 344 | + throw new Error("bridge unavailable"); |
| 345 | + }); |
| 346 | + const callGateway = vi.fn(async () => await new Promise<never>(() => {})); |
| 347 | + const stdout = createWritableTextBuffer(); |
| 348 | + const stderr = createWritableTextBuffer(); |
| 349 | + |
| 350 | + const exitCode = await runNativeHookRelayCli( |
| 351 | + { |
| 352 | + provider: "codex", |
| 353 | + relayId: "relay-1", |
| 354 | + generation: "generation-1", |
| 355 | + event: "post_tool_use", |
| 356 | + timeout: "25", |
| 357 | + }, |
| 358 | + { |
| 359 | + stdin: createReadableTextStream("{}"), |
| 360 | + stdout, |
| 361 | + stderr, |
| 362 | + invokeBridge: invokeBridge as never, |
| 363 | + callGateway: callGateway as never, |
| 364 | + }, |
| 365 | + ); |
| 366 | + |
| 367 | + expect(exitCode).toBe(0); |
| 368 | + expect(stdout.text()).toBe(""); |
| 369 | + expect(stderr.text()).toContain("native hook relay timed out"); |
| 370 | + expect(callGateway).toHaveBeenCalledWith( |
| 371 | + expect.objectContaining({ |
| 372 | + method: "nativeHook.invoke", |
| 373 | + signal: expect.any(AbortSignal), |
| 374 | + }), |
| 375 | + ); |
| 376 | + }, 1_000); |
| 377 | + |
263 | 378 | it("rejects oversized hook input without touching the gateway", async () => { |
264 | 379 | const callGateway = vi.fn(); |
265 | 380 | const stderr = createWritableTextBuffer(); |
@@ -417,3 +532,9 @@ describe("native hook relay CLI", () => { |
417 | 532 | expect(stderr.text()).toContain("native hook relay unavailable"); |
418 | 533 | }); |
419 | 534 | }); |
| 535 | + |
| 536 | +function createHeldOpenTextStream(text: string): PassThrough { |
| 537 | + const stream = new PassThrough(); |
| 538 | + stream.write(text); |
| 539 | + return stream; |
| 540 | +} |
0 commit comments