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: share direct Deep Scan contracts with Node (#1622)

Michael D'Angelo84b46e8324eb
diff --git a/sdk/typescript/tests-ts/api-direct-deep-scan.test.ts b/sdk/typescript/tests-ts/api-direct-deep-scan.test.ts
index 95daf13bc..c4c63321e 100644
--- a/sdk/typescript/tests-ts/api-direct-deep-scan.test.ts
+++ b/sdk/typescript/tests-ts/api-direct-deep-scan.test.ts
@@ -2,10 +2,10 @@ import { mkdir, readFile, writeFile } from "node:fs/promises";
import { hash } from "node:crypto";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
-import { fileURLToPath, pathToFileURL } from "node:url";
+import { fileURLToPath } from "node:url";
import { delimiter, join } from "node:path";
import { afterEach, expect, mock, test } from "bun:test";
-import { copyCompletedScan, PLUGIN_ROOT } from "./plugin-root.js";
+import { copyCompletedScan } from "./plugin-root.js";
import { TestClient, mockWorkbench } from "./support/api-client.js";
import {
completedEvents,
@@ -220,16 +220,16 @@ test.each(["configured", "managed"] as const)(
},
);
-test("Node retains the direct Deep Scan capability", async () => {
+test("Node runs the direct Deep Scan contracts", async () => {
if (
runTestInSubprocess(
import.meta.path,
- "Node retains the direct Deep Scan capability",
+ "Node runs the direct Deep Scan contracts",
)
)
return;
const root = await temporaryDirectory();
- const source = new URL("../src/deep-scan.ts", import.meta.url);
+ const source = new URL("./direct-deep-scan.test.ts", import.meta.url);
const built = await Bun.build({
entrypoints: [fileURLToPath(source)],
target: "node",
@@ -239,11 +239,8 @@ test("Node retains the direct Deep Scan capability", async () => {
expect(built.success).toBe(true);
const module = join(root, "deep-scan.mjs");
await writeFile(module, await built.outputs[0]!.text());
- await promisify(execFile)("node", [
- "--input-type=module",
- "--eval",
- `import assert from "node:assert/strict";
- const { supportsDirectDeepScan } = await import(${JSON.stringify(pathToFileURL(module).href)});
- assert.equal(await supportsDirectDeepScan(${JSON.stringify(PLUGIN_ROOT)}), true);`,
- ]);
+ await promisify(execFile)("node", ["--test", module], {
+ timeout: Number(process.env["CODEX_SECURITY_TEST_TIMEOUT_MS"] ?? "30000"),
+ windowsHide: true,
+ });
});
diff --git a/sdk/typescript/tests-ts/direct-deep-scan.test.ts b/sdk/typescript/tests-ts/direct-deep-scan.test.ts
index 2302b8422..82f4a798e 100644
--- a/sdk/typescript/tests-ts/direct-deep-scan.test.ts
+++ b/sdk/typescript/tests-ts/direct-deep-scan.test.ts
@@ -1,7 +1,15 @@
-import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises";
+import assert from "node:assert/strict";
+import {
+ copyFile,
+ mkdir,
+ readFile,
+ realpath,
+ writeFile,
+} from "node:fs/promises";
import { join } from "node:path";
-import { afterEach, expect, test } from "bun:test";
-import { runDeepScan } from "../src/deep-scan.js";
+import { afterEach, test } from "node:test";
+import { setTimeout } from "node:timers/promises";
+import { runDeepScan, supportsDirectDeepScan } from "../src/deep-scan.js";
import { ScanCostTracker } from "../src/cost.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
@@ -49,6 +57,7 @@ const lines = createInterface({input: process.stdin});
const request = JSON.parse((await lines[Symbol.asyncIterator]().next()).value);
writeFileSync(process.env.SYNTHETIC_RECEIPT, JSON.stringify({
request, argv: process.argv.slice(2), key: process.env.CODEX_API_KEY,
+ execPath: process.execPath, bun: process.versions.bun,
awsProfile: process.env.AWS_PROFILE, snapshot: process.env.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH,
}));
if (process.env.SYNTHETIC_WAIT === '1') {
@@ -73,7 +82,7 @@ test("isolates direct engine credentials and runtime snapshots across concurrent
const receipt = join(root, `${name}.json`);
await mkdir(scanDir);
const environment = {
- PATH: process.env["PATH"]!,
+ PATH: process.versions["bun"] ? process.env["PATH"]! : "",
CODEX_HOME: join(root, "codex-home"),
SYNTHETIC_RECEIPT: receipt,
SYNTHETIC_THREAD_ID: `thread-${name}`,
@@ -108,33 +117,39 @@ test("isolates direct engine credentials and runtime snapshots across concurrent
}))
collected.push(event);
const saved = JSON.parse(await readFile(receipt, "utf8"));
- expect(saved).toMatchObject({
+ assert.partialDeepStrictEqual(saved, {
key: `synthetic-key-${name}`,
awsProfile: `profile-${name}`,
snapshot: environment.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH,
- argv: ["--deep-scan-engine"],
request: {
scanId: `scan-${name}`,
threadId: `thread-${name}`,
- permissionProfile: {
- commandArgs: ["--config", `model_provider="provider_${name}"`],
- configOverrides: [
- 'model="synthetic-model"',
- 'permissions.scan.filesystem={":root"="read"}',
- "features.plugins=false",
- ],
- },
},
});
- expect(environment).toEqual(before);
+ assert.deepEqual(saved.argv, ["--deep-scan-engine"]);
+ assert.deepEqual(saved.request.permissionProfile, {
+ commandArgs: ["--config", `model_provider="provider_${name}"`],
+ configOverrides: [
+ 'model="synthetic-model"',
+ 'permissions.scan.filesystem={":root"="read"}',
+ "features.plugins=false",
+ ],
+ });
+ assert.equal(saved.bun, undefined);
+ if (!process.versions["bun"])
+ assert.equal(
+ await realpath(saved.execPath),
+ await realpath(process.execPath),
+ );
+ assert.deepEqual(environment, before);
return collected;
}),
);
for (const run of events)
- expect(run.map((event) => event.type)).toEqual([
- "thread.started",
- "turn.completed",
- ]);
+ assert.deepEqual(
+ run.map((event) => event.type),
+ ["thread.started", "turn.completed"],
+ );
});
test("preserves complete worker usage and cost after direct engine completion", async () => {
@@ -198,7 +213,7 @@ test("preserves complete worker usage and cost after direct engine completion",
signal: new AbortController().signal,
codexOptions: {
env: {
- PATH: process.env["PATH"]!,
+ PATH: process.versions["bun"] ? process.env["PATH"]! : "",
CODEX_HOME: codexHome,
CODEX_SECURITY_SCAN_DIR: scanDir,
SYNTHETIC_RECEIPT: join(root, "engine.json"),
@@ -206,7 +221,7 @@ test("preserves complete worker usage and cost after direct engine completion",
},
},
});
- expect((await events.next()).value).toMatchObject({
+ assert.partialDeepStrictEqual((await events.next()).value, {
type: "thread.started",
thread_id: "engine-session",
});
@@ -218,9 +233,9 @@ test("preserves complete worker usage and cost after direct engine completion",
tracker.start("engine-session");
const running = await tracker.refresh();
const completed = (await events.next()).value;
- expect(completed?.type).toBe("turn.completed");
+ assert.equal(completed?.type, "turn.completed");
const final = await tracker.stop(completed?.["usage"]);
- expect(running.usage).toEqual({
+ assert.deepEqual(running.usage, {
input_tokens: 1_500,
cached_input_tokens: 250,
cache_write_input_tokens: 140,
@@ -228,10 +243,10 @@ test("preserves complete worker usage and cost after direct engine completion",
reasoning_output_tokens: 0,
total_tokens: 1_660,
});
- expect(running.cost).not.toBeNull();
- expect(final).toEqual(running);
- expect(final.cost?.cacheWriteInputTokensReported).toBeUndefined();
- expect((await events.next()).done).toBe(true);
+ assert.notEqual(running.cost, null);
+ assert.deepEqual(final, running);
+ assert.equal(final.cost?.cacheWriteInputTokensReported, undefined);
+ assert.equal((await events.next()).done, true);
});
test("waits for the direct engine to stop when the scan is canceled", async () => {
@@ -249,7 +264,7 @@ test("waits for the direct engine to stop when the scan is canceled", async () =
codexOptions: {
codexPathOverride: "/synthetic/codex",
env: {
- PATH: process.env["PATH"]!,
+ PATH: process.versions["bun"] ? process.env["PATH"]! : "",
CODEX_HOME: join(root, "codex-home"),
SYNTHETIC_RECEIPT: receipt,
SYNTHETIC_THREAD_ID: "cancel-session",
@@ -257,7 +272,9 @@ test("waits for the direct engine to stop when the scan is canceled", async () =
},
},
});
- expect((await events.next()).value).toMatchObject({ type: "thread.started" });
+ assert.partialDeepStrictEqual((await events.next()).value, {
+ type: "thread.started",
+ });
const running = events.next();
const outcome = running.then(
() => undefined,
@@ -265,12 +282,19 @@ test("waits for the direct engine to stop when the scan is canceled", async () =
);
try {
while (!(await readFile(receipt).catch(() => undefined)))
- await Bun.sleep(10);
+ await setTimeout(10);
controller.abort();
- expect(await outcome).toBeInstanceOf(Error);
- expect(await readFile(receipt + ".stopped", "utf8")).toBe("stopped");
+ assert.ok((await outcome) instanceof Error);
+ assert.equal(await readFile(receipt + ".stopped", "utf8"), "stopped");
} finally {
controller.abort();
await outcome;
}
});
+
+test("selects the direct engine only in Node", async () => {
+ assert.equal(
+ await supportsDirectDeepScan(PLUGIN_ROOT),
+ !process.versions["bun"],
+ );
+});
diff --git a/sdk/typescript/tests-ts/support/temporary-directories.ts b/sdk/typescript/tests-ts/support/temporary-directories.ts
index 80ac0a7a8..4bd0866e5 100644
--- a/sdk/typescript/tests-ts/support/temporary-directories.ts
+++ b/sdk/typescript/tests-ts/support/temporary-directories.ts
@@ -1,5 +1,6 @@
import { mkdtempSync, realpathSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
+import { setTimeout } from "node:timers/promises";
import { join } from "node:path";
import { rm } from "node:fs/promises";
import {
@@ -16,7 +17,7 @@ export async function removeTemporaryDirectory(path: string): Promise<void> {
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "EBUSY" || attempt === 10)
throw error;
- await Bun.sleep(100 * (attempt + 1));
+ await setTimeout(100 * (attempt + 1));
}
}
}
diff --git a/sdk/typescript/tests-ts/support/usage-rollout.ts b/sdk/typescript/tests-ts/support/usage-rollout.ts
index 68fcf63c7..87030c736 100644
--- a/sdk/typescript/tests-ts/support/usage-rollout.ts
+++ b/sdk/typescript/tests-ts/support/usage-rollout.ts
@@ -1,7 +1,7 @@
import { mkdir, writeFile } from "node:fs/promises";
import { runNodePython } from "./python-probe.js";
import { join } from "node:path";
-import { expect } from "bun:test";
+import assert from "node:assert/strict";
export const scanThreadId = "scan-thread";
export const lowerUuid7Turn = "019f9e4d-b3ba-7000-8000-000000000001";
@@ -92,7 +92,7 @@ export function readPythonRolloutUsage(
rolloutPath: string,
): unknown {
const python = Bun.which("python3") ?? Bun.which("python");
- expect(python).not.toBeNull();
+ assert.notEqual(python, null);
const probe = [
"import json, sys",
"from datetime import datetime, timezone",
@@ -116,8 +116,8 @@ export function readPythonRolloutUsage(
scanThreadId,
]);
- expect(result.error).toBeUndefined();
- expect(result.status, result.stderr).toBe(0);
+ assert.equal(result.error, undefined);
+ assert.equal(result.status, 0, result.stderr);
return JSON.parse(result.stdout) as unknown;
}