OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
test(dedupe): cover cancellation while review output is blocked (#1066)
Dipesh Babu41ce52a18775
diff --git a/sdk/typescript/tests-ts/dedupe-records.test.ts b/sdk/typescript/tests-ts/dedupe-records.test.tsindex 3099188a1..704f5121b 100644--- a/sdk/typescript/tests-ts/dedupe-records.test.ts+++ b/sdk/typescript/tests-ts/dedupe-records.test.ts@@ -1,4 +1,5 @@-import { expect, test, mock } from "bun:test";+import { expect, test, mock, onTestFinished, spyOn } from "bun:test";+import * as records from "../src/deduplication/records.js";import Ajv2020 from "ajv/dist/2020.js";import { DeduplicationReviewError } from "../src/errors.js";import { mkdtemp, readFile, readdir, rm } from "node:fs/promises";@@ -666,6 +667,12 @@ test("disconnect before run and a broken output pipe terminate cleanly", async (input.destroy();});+const emptyRecords = {+ version: 1,+ observations: [],+ candidateRelationships: [],+};+test("a disconnect while flushing the final result does not send a second response", async () => {const inputStream = new PassThrough();const messages: Message[] = [];@@ -677,16 +684,7 @@ test("a disconnect while flushing the final result does not send a second respon},});const done = runRecordsProtocol(inputStream, output);- inputStream.write(- `${JSON.stringify({- ...run,- params: {- version: 1,- observations: [],- candidateRelationships: [],- },- })}\n`,- );+ inputStream.write(`${JSON.stringify({ ...run, params: emptyRecords })}\n`);expect(await done).toBe(0);expect(messages).toMatchObject([{ id: "run-1", result: { status: "completed" } },@@ -694,56 +692,77 @@ test("a disconnect while flushing the final result does not send a second responexpect(messages).toHaveLength(1);});-test.each(["result", "error"] as const)(- "cancellation stops a blocked terminal %s without sending another response",- async (terminal) => {- for (const notification of [false, true]) {+const completed = { result: { status: "completed" } };+for (const trigger of ["signal", "notification"]) {+ test.serial.each([+ ["review request", run.params, { method: "review.run" }],+ ["terminal result", emptyRecords, completed],+ ["terminal result flushed after cancel", emptyRecords, completed],+ ["terminal error", {}, { error: expect.anything() }],+ ] as const)(+ `${trigger} stops a blocked %s`,+ async (stage, params, expected) => {const inputStream = new PassThrough();- const messages: Message[] = [];- let release!: () => void;- const writing = Promise.withResolvers<void>();+ const writing = Promise.withResolvers<() => void>();const output = new Writable({- write(chunk, _encoding, callback) {- messages.push(JSON.parse(chunk.toString()));- release = callback;- writing.resolve();- },+ write: (_chunk, _encoding, flush) => writing.resolve(flush),});+ const writes = spyOn(output, "write");+ let pendingReview: Promise<unknown> | undefined;+ const dedupe = records.deduplicateRecords;+ const operation =+ stage === "review request"+ ? spyOn(records, "deduplicateRecords").mockImplementationOnce(+ (input, options) =>+ dedupe(input, {+ ...options,+ reviewRunner: {+ run: (...args) =>+ (pendingReview = options.reviewRunner.run(...args)),+ },+ }),+ )+ : undefined;const controller = new AbortController();+ let finished = false;+ onTestFinished(() => {+ finished = true;+ operation?.mockRestore();+ writes.mockRestore();+ output.destroy();+ inputStream.destroy();+ });const done = runRecordsProtocol(inputStream, output, controller.signal);- try {+ inputStream.write(`${JSON.stringify({ ...run, params })}\n`);+ const flush = await writing.promise;+ const signal = operation?.mock.calls[0]?.[1].signal;+ const recordsResult = operation?.mock.results[0]?.value;+ if (trigger === "signal") controller.abort("SIGTERM");+ elseinputStream.write(- `${JSON.stringify({- ...run,- params:- terminal === "error"- ? {}- : {- version: 1,- observations: [],- candidateRelationships: [],- },- })}\n`,+ `${JSON.stringify({ jsonrpc: "2.0", method: "cancel", params: { id: run.id } })}\n`,);- await writing.promise;- if (notification)- inputStream.write(- `${JSON.stringify({ jsonrpc: "2.0", method: "cancel", params: { id: run.id } })}\n`,- );- else controller.abort("SIGTERM");- expect(output.destroyed).toBe(true);- expect(await done).toBe(2);- expect(messages).toHaveLength(1);- expect(messages[0]).toHaveProperty(terminal);- } finally {- release();- await done;- inputStream.destroy();- output.destroy();+ if (stage === "terminal result flushed after cancel") flush();+ const code = await done;+ const settled = signal?.aborted+ ? await Promise.allSettled([pendingReview, recordsResult])+ : [];+ // Destroying the blocked stream can resume a test after it timed out.+ if (finished) return;+ expect(code).toBe(2);+ expect(output.destroyed).toBe(true);+ expect(writes).toHaveBeenCalledTimes(1);+ expect(JSON.parse(writes.mock.calls[0]![0])).toMatchObject(expected);+ if (operation) expect(signal?.aborted).toBe(true);+ for (const result of settled) {+ expect(result).toEqual({+ status: "rejected",+ reason: new Error("Deduplication canceled."),+ });}- }- },-);+ },+ );+}test.each(["screening", "pair-review"] as const)("%s refusal stops further reviews and leaves all records unresolved",