Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

test(sdk): reduce repeated dashboard and candidate fixtures (#1978)

Michael D'Angelo56a900aa3ea4
diff --git a/plugins/codex-security/mcp-app/tests/test_normalize_candidates.ts b/plugins/codex-security/mcp-app/tests/test_normalize_candidates.ts
index 4a127a770..20d6bd393 100644
--- a/plugins/codex-security/mcp-app/tests/test_normalize_candidates.ts
+++ b/plugins/codex-security/mcp-app/tests/test_normalize_candidates.ts
@@ -5,9 +5,10 @@ import { test } from "node:test";
import { importSource } from "./import-module.ts";
import { temporaryDirectory } from "./support/temporary-directories.ts";
-const { normalizeCandidatesCommand } = await importSource(
- join(import.meta.dirname, "../src/helpers/normalize-candidates.ts"),
-);
+const { normalizeCandidateBatch, normalizeCandidatesCommand } =
+ await importSource(
+ join(import.meta.dirname, "../src/helpers/normalize-candidates.ts"),
+ );
const cases = [
{
@@ -97,3 +98,59 @@ for (const fixture of cases) {
}
});
}
+
+test("reports the candidate field that invalidates the second batch row", async (t) => {
+ const root = await temporaryDirectory("normalize-candidates-");
+ t.after(() => rm(root, { recursive: true, force: true }));
+ const scopePath = join(root, "inventory.txt");
+ await writeFile(scopePath, "source.ts\n");
+ await writeFile(join(root, "source.ts"), "one\ntwo\n");
+ const location = { path: "source.ts", start_line: 2, role: "entrypoint" };
+ const valid = {
+ cwe_ids: ["CWE-89"],
+ locations: [location],
+ summary: "Synthetic candidate",
+ evidence: "Synthetic evidence",
+ };
+
+ for (const [change, error] of [
+ [{ cwe_ids: null }, "cwe_ids: expected an array"],
+ [{ cwe_ids: [12] }, "cwe_ids: expected CWE strings"],
+ [
+ { cwe_ids: ["SQL injection"] },
+ 'cwe_ids: unsupported value "SQL injection"',
+ ],
+ [{ cwe_ids: ["CWE-0"] }, 'cwe_ids: unsupported value "CWE-0"'],
+ [{ locations: [] }, "locations: expected a non-empty array"],
+ [{ locations: [null] }, "locations: expected location objects"],
+ [
+ { locations: [{ ...location, end_line: 1 }] },
+ "end_line: must be greater than or equal to start_line",
+ ],
+ [
+ { locations: [{ ...location, role: "rootControl" }] },
+ "role: unsupported value rootControl",
+ ],
+ [
+ { locations: [{ ...location, line: 2 }] },
+ "locations: unsupported fields line",
+ ],
+ [{ disposition: "reportable" }, "unsupported fields disposition"],
+ [{ candidate_id: " " }, "candidate_id: expected a non-empty string"],
+ [{ summary: " \t\r\n" }, "summary: expected a non-empty string"],
+ [{ evidence: null }, "evidence: expected a non-empty string"],
+ [{ context: " " }, "context: expected a non-empty string"],
+ [{ instance: false }, "instance: expected a non-empty string"],
+ ] as const) {
+ assert.throws(
+ () =>
+ normalizeCandidateBatch({
+ repoRoot: root,
+ scopePath,
+ allowMissing: false,
+ candidates: [valid, { ...valid, ...change }],
+ }),
+ { message: `discovery candidates: candidate input row 2: ${error}` },
+ );
+ }
+});
diff --git a/sdk/typescript/tests-ts/candidate-normalizer.test.ts b/sdk/typescript/tests-ts/candidate-normalizer.test.ts
index 64c45c930..4c683c279 100644
--- a/sdk/typescript/tests-ts/candidate-normalizer.test.ts
+++ b/sdk/typescript/tests-ts/candidate-normalizer.test.ts
@@ -372,27 +372,9 @@ describe("built candidate normalizer", () => {
);
});
- test("reports invalid fields with the input row and preserves existing output", () => {
+ test("reports rejected candidate input rows without replacing existing output", () => {
const f = fixture();
const cases: [Row, string][] = [
- [candidate(undefined, { cwe_ids: null }), "cwe_ids: expected an array"],
- [
- candidate(undefined, { cwe_ids: [12] }),
- "cwe_ids: expected CWE strings",
- ],
- [
- candidate(undefined, { cwe_ids: ["SQL injection"] }),
- "cwe_ids: unsupported value",
- ],
- [
- candidate(undefined, { cwe_ids: ["CWE-0"] }),
- "cwe_ids: unsupported value",
- ],
- [candidate([]), "locations: expected a non-empty array"],
- [
- candidate([null as unknown as Row]),
- "locations: expected location objects",
- ],
[candidate([location("app/missing.py")]), "missing.py"],
[
candidate([location("../outside.py")]),
@@ -407,43 +389,10 @@ describe("built candidate normalizer", () => {
candidate([location("app/routes.py", 6)]),
"line range 6-6 exceeds app/routes.py:5",
],
- [candidate([{ ...location(), end_line: 1 }]), "greater than or equal"],
- [
- candidate([location("app/routes.py", 1, "rootControl")]),
- "role: unsupported value",
- ],
- [
- candidate([{ ...location(), line: 2 }]),
- "locations: unsupported fields line",
- ],
[
candidate(undefined, { technically_validated: true }),
"unsupported fields technically_validated",
],
- [
- candidate(undefined, { disposition: "reportable" }),
- "unsupported fields disposition",
- ],
- [
- candidate(undefined, { candidate_id: " " }),
- "candidate_id: expected a non-empty string",
- ],
- [
- candidate(undefined, { summary: " \t\r\n" }),
- "summary: expected a non-empty string",
- ],
- [
- candidate(undefined, { evidence: null }),
- "evidence: expected a non-empty string",
- ],
- [
- candidate(undefined, { context: " " }),
- "context: expected a non-empty string",
- ],
- [
- candidate(undefined, { instance: false }),
- "instance: expected a non-empty string",
- ],
];
write(f.output, "previous output\n");
for (const [row, message] of cases) {
diff --git a/sdk/typescript/tests-ts/scan-dashboard.test.ts b/sdk/typescript/tests-ts/scan-dashboard.test.ts
index 3995d2f67..750441dfe 100644
--- a/sdk/typescript/tests-ts/scan-dashboard.test.ts
+++ b/sdk/typescript/tests-ts/scan-dashboard.test.ts
@@ -346,27 +346,29 @@ describe("live scan dashboard", () => {
)(
"keeps %s intact at ASCII wrapping boundaries with code=%p",
(cluster, code) => {
- for (let padding = 0; padding < 40; padding++) {
- const stderr = capture(true);
- const dashboard = createDashboard({
- ...stderr.stream,
- columns: 40,
- rows: 24,
- });
- dashboard.start();
+ const stderr = capture(true);
+ const dashboard = createDashboard({
+ ...stderr.stream,
+ columns: 40,
+ rows: 160,
+ });
+ const lines = Array.from(
+ { length: 40 },
+ (_, padding) => "x".repeat(padding) + cluster + "yyy",
+ );
+ dashboard.start();
+ try {
dashboard.record({
id: "mixed-code",
kind: "message",
status: "completed",
- description:
- (code ? "```text\n" : "") +
- "x".repeat(padding) +
- cluster +
- "yyy" +
- (code ? "\n```" : ""),
+ description: (code ? ["```text", ...lines, "```"] : lines).join("\n"),
paths: [],
});
- expect(lastFrame(stderr)).toContain(cluster);
+ const frame = lastFrame(stderr);
+ expect(frame.replace(/\s+/gu, "")).toContain(lines.join(""));
+ expect(frame.split(cluster).length - 1).toBe(lines.length);
+ } finally {
dashboard.stop();
}
},
@@ -1194,39 +1196,39 @@ describe("live scan dashboard", () => {
"preserves Escape-prefixed cancellation and chunk ownership in %s",
async (mode) => {
jest.useFakeTimers();
- for (const chunks of [
- ["\u001B", "\u0003"],
- ["\u001B\u0003"],
- ["\u001B", "\u001B"],
- ["\u001B\u001B"],
- ["\u001B[", "\u0003"],
- ["\u001B[\u0003"],
- ["\u001BO", "\u0003"],
- ["\u001BO\u0003"],
- ["\u001B", "\u0003d"],
- ["\u001B\u0003d"],
- ["\u001B", "\u001B\u0003"],
- ["\u001B\u001B", "\u0003"],
- ["\u001B\u001B\u0003"],
- ["\u001B", "\u001B\u0003d"],
- ["\u001B\u001B[\u0003"],
- ["\u001B", "\u001B[\u0003"],
- ["\u001B\u001B", "[\u0003"],
- ["\u001B\u001B[", "\u0003"],
- ["\u001B", "\u001B", "[\u0003"],
- ["\u001B", "\u001B[", "\u0003"],
- ["\u001B\u001B", "[", "\u0003"],
- ["\u001B", "\u001B", "[", "\u0003"],
- ...Array.from({ length: 8 }, (_, index) => {
- const escapes = "\u001B".repeat(index + 1);
- return [
- ["\u001B", `${escapes}\u0003`],
- [escapes, "\u001B\u0003"],
- [escapes, "\u0003"],
- [`${escapes}\u0003`],
+ // The scan and budget handlers differ in who owns the rest of a chunk
+ // after Escape. Both component views use the ordinary key decoder.
+ const layouts = mode.startsWith("component")
+ ? [["\u001B\u0003"], ["\u001B", "\u0003d"], ["\u001B", "\u001B"]]
+ : [
+ ["\u001B", "\u001B"],
+ ["\u001B\u001B"],
+ ["\u001B[", "\u0003"],
+ ["\u001B[\u0003"],
+ ["\u001BO", "\u0003"],
+ ["\u001BO\u0003"],
+ ["\u001B", "\u0003d"],
+ ["\u001B\u0003d"],
+ ["\u001B", "\u001B\u0003d"],
+ ["\u001B\u001B[\u0003"],
+ ["\u001B", "\u001B[\u0003"],
+ ["\u001B\u001B", "[\u0003"],
+ ["\u001B\u001B[", "\u0003"],
+ ["\u001B", "\u001B", "[\u0003"],
+ ["\u001B", "\u001B[", "\u0003"],
+ ["\u001B\u001B", "[", "\u0003"],
+ ["\u001B", "\u001B", "[", "\u0003"],
+ ...Array.from({ length: 8 }, (_, index) => {
+ const escapes = "\u001B".repeat(index + 1);
+ return [
+ ...(index === 0 ? [] : [["\u001B", `${escapes}\u0003`]]),
+ [escapes, "\u001B\u0003"],
+ [escapes, "\u0003"],
+ [`${escapes}\u0003`],
+ ];
+ }).flat(),
];
- }).flat(),
- ]) {
+ for (const chunks of layouts) {
const stderr = capture(true);
const input = new DashboardTestInput();
input.isRaw = true;
@@ -1241,8 +1243,9 @@ describe("live scan dashboard", () => {
onInterrupt,
});
let answer: number | undefined | "pending" = "pending";
+ // Exercise bytewise UTF-8 before the split and coalesced following-key cases.
const pasted =
- mode === "budget"
+ mode === "budget" && chunks.at(-1)?.endsWith("d")
? Array.from(Buffer.from("é🙂界"), (byte) => Uint8Array.of(byte))
: [];
dashboard.start();
@@ -1273,7 +1276,7 @@ describe("live scan dashboard", () => {
answer = value;
});
for (const chunk of pasted) input.emit("data", chunk);
- if (mode === "budget") {
+ if (pasted.length > 0) {
expect(lastFrame(stderr)).toContain("é🙂界");
expect(lastFrame(stderr)).not.toContain("\uFFFD");
}