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: simplify shared fixtures and message helpers (#1582)

Michael D'Angelo825e7fd059a8
diff --git a/plugins/codex-security/mcp-app/tests/support/json-line-input-source.ts b/plugins/codex-security/mcp-app/tests/support/json-line-input-source.ts
new file mode 100644
index 000000000..cf3325f5a
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/support/json-line-input-source.ts
@@ -0,0 +1,16 @@
+export function jsonLineInputSource(body: string) {
+ return `let buffer = "";
+process.stdin.setEncoding("utf8");
+process.stdin.on("data", (chunk) => {
+ buffer += chunk;
+ while (true) {
+ const newline = buffer.indexOf("\\n");
+ if (newline < 0) return;
+ const line = buffer.slice(0, newline).trim();
+ buffer = buffer.slice(newline + 1);
+ if (!line) continue;
+ const message = JSON.parse(line);
+${body}
+ }
+});`;
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
index 5136feb1c..642ef5229 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
@@ -1,3 +1,4 @@
+import { jsonLineInputSource } from "./support/json-line-input-source.ts";
import { readJson, writeJson } from "./support/json.ts";
import { randomUUID } from "node:crypto";
import { loadWorkbenchProcess } from "./support/workbench-process.ts";
@@ -3319,18 +3320,7 @@ const preflightMarkerPath = process.env.FAKE_CODEX_PREFLIGHT_MARKER ?? ${JSON.st
if (process.argv.includes('app-server')) {
const preflight = { argv: process.argv.slice(2), configPath: process.env.CODEX_SECURITY_CONFIG_PATH, runnerTrackingId: process.env.RUNNER_TRACKING_ID, libraryPath: process.env.LD_LIBRARY_PATH, cwd: process.cwd(), codexHome: process.env.CODEX_HOME, providerKey: process.env.SYNTHETIC_GATEWAY_KEY, providerHeader: process.env.SYNTHETIC_HEADER_VALUE, cacheDirectory: process.env.XDG_CACHE_HOME, gitEnvironment: Object.fromEntries(['PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL'].map(name => [name, process.env[name]])), requests: [] };
writeFileSync(preflightMarkerPath, JSON.stringify(preflight));
- let buffer = '';
- process.stdin.setEncoding('utf8');
- process.stdin.on('data', (chunk) => {
- buffer += chunk;
- while (true) {
- const newline = buffer.indexOf('\\n');
- if (newline < 0) return;
- const line = buffer.slice(0, newline).trim();
- buffer = buffer.slice(newline + 1);
- if (!line) continue;
- const message = JSON.parse(line);
- if (message.method === 'initialized') continue;
+${jsonLineInputSource(` if (message.method === 'initialized') continue;
if (message.method === 'config/read' || message.method === 'permissionProfile/list') {
preflight.requests.push({ method: message.method, cwd: message.params?.cwd });
writeFileSync(preflightMarkerPath, JSON.stringify(preflight));
@@ -3350,9 +3340,7 @@ if (process.argv.includes('app-server')) {
process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Method not found' } }) + '\\n');
continue;
}
- process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\\n');
- }
- });
+ process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\\n');`)}
process.stdin.on('end', () => process.exit(0));
} else {
const stdin = (await process.stdin.toArray()).join('');
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
index eb5c219e6..a77e799c9 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
@@ -1,3 +1,4 @@
+import { jsonLineInputSource } from "./support/json-line-input-source.ts";
import { readJson, readJsonLines } from "./support/json.ts";
import type { ChildProcess, SpawnOptions } from "node:child_process";
interface PreflightFixture {
@@ -1102,7 +1103,6 @@ if (scenario.inheritStdio) {
writeFileSync(scenario.descendantPidPath, String(descendant.pid));
descendant.unref();
}
-let buffer = "";
let catalogIndex = 0;
if (scenario.ignoreTermination) setInterval(() => {}, 1_000);
@@ -1110,20 +1110,8 @@ process.on("SIGTERM", () => {
writeFileSync(scenario.terminatedPath, "SIGTERM");
if (!scenario.ignoreTermination) process.exit(0);
});
-process.stdin.setEncoding("utf8");
-process.stdin.on("data", (chunk) => {
- buffer += chunk;
- while (true) {
- const newline = buffer.indexOf("\\n");
- if (newline < 0) return;
- const line = buffer.slice(0, newline).trim();
- buffer = buffer.slice(newline + 1);
- if (!line) continue;
- const message = JSON.parse(line);
- appendFileSync(scenario.callsPath, JSON.stringify(message) + "\\n");
- handle(message);
- }
-});
+${jsonLineInputSource(` appendFileSync(scenario.callsPath, JSON.stringify(message) + "\\n");
+ handle(message);`)}
process.stdin.on("end", () => {
writeFileSync(scenario.terminatedPath, "stdin-end");
if (!scenario.ignoreTermination) process.exit(0);
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts
index 9ba889938..3424505c7 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts
@@ -1,3 +1,4 @@
+import { jsonLineInputSource } from "./support/json-line-input-source.ts";
import type { PersistedDeepScanWorker } from "../src/deep-scan/types.js";
import { readJson, readJsonLines } from "./support/json.ts";
import { assertNoError, assertFlagPair } from "./assertions.ts";
@@ -1383,18 +1384,7 @@ async function writeFakeCodex(executablePath: string) {
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
if (process.argv.includes('app-server')) {
- let buffer = '';
- process.stdin.setEncoding('utf8');
- process.stdin.on('data', (chunk) => {
- buffer += chunk;
- while (true) {
- const newline = buffer.indexOf('\\n');
- if (newline < 0) return;
- const line = buffer.slice(0, newline).trim();
- buffer = buffer.slice(newline + 1);
- if (!line) continue;
- const message = JSON.parse(line);
- if (message.method === 'initialized') continue;
+${jsonLineInputSource(` if (message.method === 'initialized') continue;
let result;
if (message.method === 'initialize') {
result = { userAgent: 'fixture', codexHome: '/fixture', platformFamily: 'unix', platformOs: 'macos' };
@@ -1408,9 +1398,7 @@ if (process.argv.includes('app-server')) {
process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Method not found' } }) + '\\n');
continue;
}
- process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\\n');
- }
- });
+ process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\\n');`)}
process.stdin.on('end', () => process.exit(0));
} else {
const stdin = (await process.stdin.toArray()).join('');
diff --git a/plugins/codex-security/tests/test_finalize_scan_contract.py b/plugins/codex-security/tests/test_finalize_scan_contract.py
index 2b99427b2..d6365cd14 100644
--- a/plugins/codex-security/tests/test_finalize_scan_contract.py
+++ b/plugins/codex-security/tests/test_finalize_scan_contract.py
@@ -145,11 +145,6 @@ The extraction root is not enforced.
encoding="utf-8",
)
- def write_json(self, relative_path: str, payload: object) -> None:
- path = self.scan_dir / relative_path
- path.parent.mkdir(parents=True, exist_ok=True)
- path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
-
def completion_binding(self) -> dict[str, object]:
return {
"scanId": "scan_001",
diff --git a/plugins/codex-security/tests/test_threat_model_projection.py b/plugins/codex-security/tests/test_threat_model_projection.py
index 31fdfa4c8..3482cb26c 100644
--- a/plugins/codex-security/tests/test_threat_model_projection.py
+++ b/plugins/codex-security/tests/test_threat_model_projection.py
@@ -11,16 +11,14 @@ import unittest.mock
from pathlib import Path
import test_finalize_scan_contract as fixtures
+from workbench_test_support import ScanFixtureTestCase
FINALIZER = fixtures.FINALIZER
-class ThreatModelProjectionTest(unittest.TestCase):
+class ThreatModelProjectionTest(ScanFixtureTestCase):
setUp = fixtures.FinalizeScanContractTest.setUp
- tearDown = fixtures.FinalizeScanContractTest.tearDown
write_scan = fixtures.FinalizeScanContractTest.write_scan
- write_json = fixtures.FinalizeScanContractTest.write_json
- read_json = fixtures.FinalizeScanContractTest.read_json
def test_saves_and_exports_markdown_threat_model_without_changing_authored_body(self) -> None:
body = "# Service Model\n\n| Asset | Owner |\n| --- | --- |\n| Queue | Service |\n\n```text\nA -> B\n```\n"
diff --git a/plugins/codex-security/tests/test_validate_scan_contract.py b/plugins/codex-security/tests/test_validate_scan_contract.py
index b637d3af4..d3ea78bd7 100644
--- a/plugins/codex-security/tests/test_validate_scan_contract.py
+++ b/plugins/codex-security/tests/test_validate_scan_contract.py
@@ -21,9 +21,6 @@ class ValidateScanContractTest(ScanFixtureTestCase):
validator = VALIDATOR
example_scan = EXAMPLE_SCAN
- def write_json(self, name: str, payload: object) -> None:
- (self.scan_dir / name).write_text(json.dumps(payload, indent=2) + "\n")
-
def test_accepts_sealed_contract_without_mutating_it(self) -> None:
before = {
path.name: path.read_bytes() for path in self.scan_dir.iterdir() if path.is_file()
diff --git a/plugins/codex-security/tests/test_validate_tracking_source.py b/plugins/codex-security/tests/test_validate_tracking_source.py
index 0d83bdf35..961887bae 100644
--- a/plugins/codex-security/tests/test_validate_tracking_source.py
+++ b/plugins/codex-security/tests/test_validate_tracking_source.py
@@ -3,7 +3,6 @@ from __future__ import annotations
import copy
import hashlib
import io
-import json
import unittest
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
@@ -25,12 +24,6 @@ class ValidateTrackingSourceTest(ScanFixtureTestCase):
validator = VALIDATOR
example_scan = EXAMPLE_SCAN
- def write_json(self, name: str, payload: object) -> None:
- (self.scan_dir / name).write_text(
- json.dumps(payload, indent=2) + "\n",
- encoding="utf-8",
- )
-
def rewrite_findings_and_seal(self, findings: dict[str, object]) -> None:
self.write_json("findings.json", findings)
manifest = self.read_json("scan-manifest.json")
diff --git a/plugins/codex-security/tests/workbench_test_support.py b/plugins/codex-security/tests/workbench_test_support.py
index 7b7f77242..e234ad2f8 100644
--- a/plugins/codex-security/tests/workbench_test_support.py
+++ b/plugins/codex-security/tests/workbench_test_support.py
@@ -927,5 +927,10 @@ class ScanFixtureTestCase(TestCase):
def read_json(self, name: str) -> dict[str, object]:
return json.loads((self.scan_dir / name).read_text(encoding="utf-8"))
+ def write_json(self, relative_path: str, payload: object) -> None:
+ path = self.scan_dir / relative_path
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
+
def sha256_file(self, name: str) -> str:
return hashlib.sha256((self.scan_dir / name).read_bytes()).hexdigest()
diff --git a/sdk/typescript/scripts/compare-test-reports.mts b/sdk/typescript/scripts/compare-test-reports.mts
index e42476069..75ed2307b 100644
--- a/sdk/typescript/scripts/compare-test-reports.mts
+++ b/sdk/typescript/scripts/compare-test-reports.mts
@@ -12,12 +12,6 @@ type TestRecord = {
attributes: SaxesTagNS["attributes"];
status: TestStatus;
};
-type TestReport = {
- cases: Map<string, number>;
- duration: number;
- failed: boolean;
-};
-
function matchingReports(pattern: string): string[] {
// Python glob treats ** as one component and does not expand braces/extglobs.
const directoriesOnly =
@@ -91,7 +85,7 @@ function seconds(value: string): number {
return Number(number.replace(/inf(?:inity)?/iu, "Infinity"));
}
-function readReport(path: string): TestReport {
+function readReport(path: string) {
const parser = new SaxesParser({ xmlns: true, fileName: path });
let root: SaxesTagNS | undefined;
const stack: Array<TestRecord | undefined> = [];
diff --git a/sdk/typescript/tests-ts/api-post-scan.test.ts b/sdk/typescript/tests-ts/api-post-scan.test.ts
index 793b3e318..47acf2c4f 100644
--- a/sdk/typescript/tests-ts/api-post-scan.test.ts
+++ b/sdk/typescript/tests-ts/api-post-scan.test.ts
@@ -35,11 +35,10 @@ const { cleanup, temporaryDirectory } = createApiTestFixtures();
afterEach(cleanup);
-interface PostScanContext {
- artifactPath: string;
- outside: string;
- scanDir: string;
-}
+type PostScanContext = Pick<
+ Awaited<ReturnType<typeof startPostScan>>,
+ "artifactPath" | "outside" | "scanDir"
+>;
interface PostScanScenario {
artifact: string;
diff --git a/sdk/typescript/tests-ts/auth.test.ts b/sdk/typescript/tests-ts/auth.test.ts
index 9f7d03559..7151c7868 100644
--- a/sdk/typescript/tests-ts/auth.test.ts
+++ b/sdk/typescript/tests-ts/auth.test.ts
@@ -13,7 +13,6 @@ import {
} from "../src/auth.js";
import { PluginBootstrapError } from "../src/index.js";
import { runCodexCommand } from "../src/runtime.js";
-import type { CodexCommand } from "../src/index.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
import { throwing } from "./support/errors.js";
@@ -24,10 +23,7 @@ const { temporaryDirectory, cleanup } = createApiTestFixtures(
afterEach(cleanup);
-async function fakeCodex(): Promise<{
- command: CodexCommand;
- environment: NodeJS.ProcessEnv;
-}> {
+async function fakeCodex() {
const root = await temporaryDirectory();
const script = join(root, "codex.mjs");
await writeFile(
diff --git a/sdk/typescript/tests-ts/bulk-scan-discovery.test.ts b/sdk/typescript/tests-ts/bulk-scan-discovery.test.ts
index 03bff6ef4..acc250004 100644
--- a/sdk/typescript/tests-ts/bulk-scan-discovery.test.ts
+++ b/sdk/typescript/tests-ts/bulk-scan-discovery.test.ts
@@ -104,12 +104,7 @@ function discoveryDependencies(
repositories?: Repository[];
authenticated?: boolean;
} = {},
-): {
- dependencies: BulkScanDiscoveryDependencies;
- prompt: FakePrompt;
- hosts: string[];
- requests: GitHubRequest[];
-} {
+) {
const prompt = new FakePrompt();
const host = options.host ?? "github.com";
const repositories = options.repositories ?? [
@@ -120,114 +115,108 @@ function discoveryDependencies(
const hosts: string[] = [];
const requests: GitHubRequest[] = [];
- return {
+ const dependencies: BulkScanDiscoveryDependencies = {
prompt,
- hosts,
- requests,
- dependencies: {
- prompt,
- now: () => NOW,
- currentDirectory: () => root,
- ...(options.host === undefined ? {} : { githubHost: host }),
- createGitHub: async (githubHost) => {
- hosts.push(githubHost);
- if (options.authenticated === false) {
- throw new Error(
- "GitHub sign-in is required. Run 'gh auth login' first.",
- );
- }
+ now: () => NOW,
+ currentDirectory: () => root,
+ ...(options.host === undefined ? {} : { githubHost: host }),
+ createGitHub: async (githubHost) => {
+ hosts.push(githubHost);
+ if (options.authenticated === false) {
+ throw new Error(
+ "GitHub sign-in is required. Run 'gh auth login' first.",
+ );
+ }
- return new Octokit({
- auth: "test-token",
- baseUrl:
- githubHost === "github.com"
- ? "https://api.github.com"
- : `https://${githubHost}/api/v3`,
- request: {
- fetch: async (
- resource: Parameters<typeof fetch>[0],
- init?: Parameters<typeof fetch>[1],
- ) => {
- const url = new URL(
- resource instanceof Request ? resource.url : resource,
+ return new Octokit({
+ auth: "test-token",
+ baseUrl:
+ githubHost === "github.com"
+ ? "https://api.github.com"
+ : `https://${githubHost}/api/v3`,
+ request: {
+ fetch: async (
+ resource: Parameters<typeof fetch>[0],
+ init?: Parameters<typeof fetch>[1],
+ ) => {
+ const url = new URL(
+ resource instanceof Request ? resource.url : resource,
+ );
+ const path = url.pathname.replace(/^\/api(?:\/v3)?/u, "");
+ const body =
+ init?.body === undefined
+ ? undefined
+ : (JSON.parse(String(init.body)) as {
+ query: string;
+ variables: GitHubRequest["variables"];
+ });
+ requests.push({
+ path,
+ ...(body === undefined
+ ? {}
+ : { query: body.query, variables: body.variables }),
+ });
+
+ if (path === "/user/orgs") {
+ const organizations = options.organizations ?? [];
+ const page = Number(url.searchParams.get("page") ?? "1");
+ const perPage = Number(url.searchParams.get("per_page") ?? "30");
+ const offset = (page - 1) * perPage;
+ const next = new URL(url);
+ next.searchParams.set("page", String(page + 1));
+ return Response.json(
+ organizations
+ .slice(offset, offset + perPage)
+ .map((login) => ({ login })),
+ offset + perPage < organizations.length
+ ? { headers: { link: `<${next.href}>; rel="next"` } }
+ : undefined,
);
- const path = url.pathname.replace(/^\/api(?:\/v3)?/u, "");
- const body =
- init?.body === undefined
- ? undefined
- : (JSON.parse(String(init.body)) as {
- query: string;
- variables: GitHubRequest["variables"];
- });
- requests.push({
- path,
- ...(body === undefined
- ? {}
- : { query: body.query, variables: body.variables }),
- });
-
- if (path === "/user/orgs") {
- const organizations = options.organizations ?? [];
- const page = Number(url.searchParams.get("page") ?? "1");
- const perPage = Number(
- url.searchParams.get("per_page") ?? "30",
- );
- const offset = (page - 1) * perPage;
- const next = new URL(url);
- next.searchParams.set("page", String(page + 1));
- return Response.json(
- organizations
- .slice(offset, offset + perPage)
- .map((login) => ({ login })),
- offset + perPage < organizations.length
- ? { headers: { link: `<${next.href}>; rel="next"` } }
- : undefined,
- );
- }
- if (path === "/user") {
- return Response.json({ login: "personal-account" });
- }
- if (path !== "/graphql" || body === undefined) {
- throw new Error(`Unexpected GitHub request: ${path}`);
- }
-
- const visible = repositories.filter(
- ({ archived, fork, affiliation }) =>
- !archived &&
- !fork &&
- !(
- affiliation === "COLLABORATOR" &&
- /ownerAffiliations:\s*\[OWNER\]/u.test(body.query)
- ),
- );
- const start = Number(body.variables?.cursor ?? 0);
- const page = visible.slice(start, start + 100);
- const next = start + page.length;
- return Response.json({
- data: {
- repositoryOwner: {
- repositories: {
- nodes: page.map((repository) => ({
- nameWithOwner: repository.fullName,
- pushedAt: repository.pushedAt ?? "2026-07-20T00:00:00Z",
- defaultBranchRef: repository.empty
- ? null
- : { target: { oid: REVISION } },
- })),
- pageInfo: {
- hasNextPage: next < visible.length,
- endCursor: next < visible.length ? String(next) : null,
- },
+ }
+ if (path === "/user") {
+ return Response.json({ login: "personal-account" });
+ }
+ if (path !== "/graphql" || body === undefined) {
+ throw new Error(`Unexpected GitHub request: ${path}`);
+ }
+
+ const visible = repositories.filter(
+ ({ archived, fork, affiliation }) =>
+ !archived &&
+ !fork &&
+ !(
+ affiliation === "COLLABORATOR" &&
+ /ownerAffiliations:\s*\[OWNER\]/u.test(body.query)
+ ),
+ );
+ const start = Number(body.variables?.cursor ?? 0);
+ const page = visible.slice(start, start + 100);
+ const next = start + page.length;
+ return Response.json({
+ data: {
+ repositoryOwner: {
+ repositories: {
+ nodes: page.map((repository) => ({
+ nameWithOwner: repository.fullName,
+ pushedAt: repository.pushedAt ?? "2026-07-20T00:00:00Z",
+ defaultBranchRef: repository.empty
+ ? null
+ : { target: { oid: REVISION } },
+ })),
+ pageInfo: {
+ hasNextPage: next < visible.length,
+ endCursor: next < visible.length ? String(next) : null,
},
},
},
- });
- },
+ },
+ });
},
- });
- },
+ },
+ });
},
};
+ return { prompt, hosts, requests, dependencies };
}
describe("bulk scan repository discovery", () => {
diff --git a/sdk/typescript/tests-ts/candidate-normalizer.test.ts b/sdk/typescript/tests-ts/candidate-normalizer.test.ts
index b8a9038e0..64c45c930 100644
--- a/sdk/typescript/tests-ts/candidate-normalizer.test.ts
+++ b/sdk/typescript/tests-ts/candidate-normalizer.test.ts
@@ -18,12 +18,7 @@ const node = Bun.which("node")!;
const helper = join(PLUGIN_ROOT, "mcp", "helpers.mjs");
const roots = createTemporaryDirectoriesSync(true);
type Row = Record<string, unknown>;
-interface Fixture {
- root: string;
- repo: string;
- scope: string;
- output: string;
-}
+type Fixture = ReturnType<typeof fixture>;
const location = (
path = "app/routes.py",
start_line = 2,
@@ -40,7 +35,7 @@ function write(path: string, data: string | Buffer): void {
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, data);
}
-function fixture(): Fixture {
+function fixture() {
const root = roots.create("candidate-normalizer-");
const repo = join(root, "İrepository");
for (const path of [
diff --git a/sdk/typescript/tests-ts/classify-scan-severity.test.ts b/sdk/typescript/tests-ts/classify-scan-severity.test.ts
index 745047201..cf9d3bd01 100644
--- a/sdk/typescript/tests-ts/classify-scan-severity.test.ts
+++ b/sdk/typescript/tests-ts/classify-scan-severity.test.ts
@@ -90,10 +90,7 @@ async function fixture(scanId?: string) {
};
}
-function classifier(
- finding: Finding,
- excluded = false,
-): NonNullable<ClassifySeverityOptions["codex"]> {
+function classifier(finding: Finding, excluded = false) {
return jsonCodex(() => ({
findingId: finding.findingId,
decision: excluded ? "excluded" : "assessed",
@@ -211,21 +208,15 @@ await classifyScanDirectorySeverity(${JSON.stringify(scanDirectory)}, { environm
function recordingClassifier() {
const calls: string[] = [];
const control = { failOn: "", excluded: false };
- const codex: NonNullable<ClassifySeverityOptions["codex"]> = {
- startThread: (thread) => ({
- run: async (prompt, turn) => {
- const { finding } = JSON.parse(prompt.split("\n\n").at(-1)!) as {
- finding: Finding;
- };
- calls.push(finding.findingId);
- if (finding.findingId === control.failOn)
- throw new Error("Interrupted model call");
- return classifier(finding, control.excluded)
- .startThread(thread)
- .run(prompt, turn);
- },
- }),
- };
+ const codex = codexWithRun(async (prompt: string) => {
+ const { finding } = JSON.parse(prompt.split("\n\n").at(-1)!) as {
+ finding: Finding;
+ };
+ calls.push(finding.findingId);
+ if (finding.findingId === control.failOn)
+ throw new Error("Interrupted model call");
+ return classifier(finding, control.excluded).startThread().run();
+ });
return { codex, calls, control };
}
diff --git a/sdk/typescript/tests-ts/cli-scan-patch-settings.test.ts b/sdk/typescript/tests-ts/cli-scan-patch-settings.test.ts
index b0afaee18..8098f1fbf 100644
--- a/sdk/typescript/tests-ts/cli-scan-patch-settings.test.ts
+++ b/sdk/typescript/tests-ts/cli-scan-patch-settings.test.ts
@@ -7,6 +7,7 @@ import { createCliTest } from "./support/cli-run.js";
import { temporaryDirectory } from "./support/temporary-directories.js";
const childSource = `
+import { send, completeTurn } from ${JSON.stringify(new URL("./fixtures/app-server-messages.mjs", import.meta.url).href)};
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { createInterface } from "node:readline";
@@ -14,7 +15,6 @@ let homeConfig = "";
try { homeConfig = readFileSync(join(process.env.CODEX_HOME, "config.toml"), "utf8"); }
catch (error) { if (error.code !== "ENOENT") throw error; }
const receipt = { argv: process.argv.slice(2), homeTier: homeConfig.match(/^service_tier\\s*=\\s*(.*)$/m)?.[1] ?? null };
-const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
for await (const line of createInterface({ input: process.stdin })) {
const request = JSON.parse(line);
if (request.method === "account/login/start") receipt.loginType = request.params.type;
@@ -27,9 +27,7 @@ for await (const line of createInterface({ input: process.stdin })) {
send({ id: request.id, result: { exitCode: 0 } });
} else if (request.method === "turn/start") {
writeFileSync(process.env.SYNTHETIC_PATCH_RECEIPT, JSON.stringify(receipt));
- send({ id: request.id, result: { turn: { id: "synthetic-turn" } } });
- send({ method: "item/completed", params: { threadId: "synthetic-thread", turnId: "synthetic-turn", item: { type: "agentMessage", text: JSON.stringify({ patches: [{ occurrenceId: "occ_1", status: "no_change", files: [], verification: "Synthetic child-process fixture completed." }] }) } } });
- send({ method: "turn/completed", params: { threadId: "synthetic-thread", turn: { id: "synthetic-turn", status: "completed" } } });
+ completeTurn(request, () => JSON.stringify({ patches: [{ occurrenceId: "occ_1", status: "no_change", files: [], verification: "Synthetic child-process fixture completed." }] }), "synthetic-thread");
} else if (request.id !== undefined) send({ id: request.id, result: {} });
}
`;
diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.ts
index e1f353726..52f0f806a 100644
--- a/sdk/typescript/tests-ts/cli.test.ts
+++ b/sdk/typescript/tests-ts/cli.test.ts
@@ -4363,40 +4363,26 @@ describe("CLI", () => {
expect(stderr.text()).not.toContain("warning:");
});
- test("prints trusted cyber access guidance without failing or corrupting JSON scans", async () => {
- const { stdout, stderr, runCli } = createCliTest(main);
-
- const deps = scanDependencies(
- warningResult(
- "Some cybersecurity requests or findings may be refused because your account does not have Trusted Access for Cyber. Apply at https://chatgpt.com/cyber.",
- ),
- );
-
- expect(await runCli(["scan", ".", "--json"], deps)).toBe(0);
- expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
- expect(stderr.text()).toContain(
+ test.each([
+ [
+ "prints trusted cyber access guidance without failing or corrupting JSON scans",
+ "Some cybersecurity requests or findings may be refused because your account does not have Trusted Access for Cyber. Apply at https://chatgpt.com/cyber.",
"codex-security: warning: Some cybersecurity requests or findings may be refused because your account does not have Trusted Access for Cyber.",
- );
- expect(stderr.text()).toContain("Apply at https://chatgpt.com/cyber.");
- });
-
- test("prints unverified trusted cyber access guidance without corrupting JSON scans", async () => {
+ "Apply at https://chatgpt.com/cyber.",
+ ],
+ [
+ "prints unverified trusted cyber access guidance without corrupting JSON scans",
+ "Some cybersecurity requests or findings may be refused because your Trusted Access for Cyber status could not be verified. Check your access or apply at https://chatgpt.com/cyber.",
+ "codex-security: warning: Some cybersecurity requests or findings may be refused because your Trusted Access for Cyber status could not be verified.",
+ "Check your access or apply at https://chatgpt.com/cyber.",
+ ],
+ ] as const)("%s", async (_name, warning, message, guidance) => {
const { stdout, stderr, runCli } = createCliTest(main);
-
- const deps = scanDependencies(
- warningResult(
- "Some cybersecurity requests or findings may be refused because your Trusted Access for Cyber status could not be verified. Check your access or apply at https://chatgpt.com/cyber.",
- ),
- );
-
+ const deps = scanDependencies(warningResult(warning));
expect(await runCli(["scan", ".", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
- expect(stderr.text()).toContain(
- "codex-security: warning: Some cybersecurity requests or findings may be refused because your Trusted Access for Cyber status could not be verified.",
- );
- expect(stderr.text()).toContain(
- "Check your access or apply at https://chatgpt.com/cyber.",
- );
+ expect(stderr.text()).toContain(message);
+ expect(stderr.text()).toContain(guidance);
});
test("prints organizational trusted cyber access guidance without corrupting JSON scans", async () => {
diff --git a/sdk/typescript/tests-ts/config.test.ts b/sdk/typescript/tests-ts/config.test.ts
index 427f57513..51dc1355c 100644
--- a/sdk/typescript/tests-ts/config.test.ts
+++ b/sdk/typescript/tests-ts/config.test.ts
@@ -388,22 +388,20 @@ describe("Codex configuration", () => {
expect(merged["windows"]).toEqual({ sandbox: "elevated" });
});
- test("preserves legacy elevated Windows sandbox overrides", async () => {
- const merged = await mergedCodexConfig({
- codexOverrides: {
+ test.each<{ name: string; overrides: JsonObject; expected: object }>([
+ {
+ name: "preserves legacy elevated Windows sandbox overrides",
+ overrides: {
features: { elevated_windows_sandbox: true },
},
- });
-
- expect(merged).toMatchObject({
- features: { elevated_windows_sandbox: true },
- windows: { sandbox: "elevated" },
- });
- });
-
- test("projects legacy elevated Windows overrides into selected profiles", async () => {
- const merged = await mergedCodexConfig({
- codexOverrides: {
+ expected: {
+ features: { elevated_windows_sandbox: true },
+ windows: { sandbox: "elevated" },
+ },
+ },
+ {
+ name: "projects legacy elevated Windows overrides into selected profiles",
+ overrides: {
profile: "elevated",
profiles: {
elevated: {
@@ -411,22 +409,19 @@ describe("Codex configuration", () => {
},
},
},
- });
-
- expect(merged).toMatchObject({
- windows: { sandbox: "elevated" },
- profiles: {
- elevated: {
- features: { elevated_windows_sandbox: true },
- windows: { sandbox: "elevated" },
+ expected: {
+ windows: { sandbox: "elevated" },
+ profiles: {
+ elevated: {
+ features: { elevated_windows_sandbox: true },
+ windows: { sandbox: "elevated" },
+ },
},
},
- });
- });
-
- test("allows selected profiles to override root elevated sandbox defaults", async () => {
- const merged = await mergedCodexConfig({
- codexOverrides: {
+ },
+ {
+ name: "allows selected profiles to override root elevated sandbox defaults",
+ overrides: {
features: { elevated_windows_sandbox: true },
profile: "restricted",
profiles: {
@@ -435,22 +430,19 @@ describe("Codex configuration", () => {
},
},
},
- });
-
- expect(merged).toMatchObject({
- windows: { sandbox: "elevated" },
- profiles: {
- restricted: {
- features: { elevated_windows_sandbox: false },
- windows: { sandbox: "unelevated" },
+ expected: {
+ windows: { sandbox: "elevated" },
+ profiles: {
+ restricted: {
+ features: { elevated_windows_sandbox: false },
+ windows: { sandbox: "unelevated" },
+ },
},
},
- });
- });
-
- test("gives profile-local Windows sandbox overrides precedence", async () => {
- const merged = await mergedCodexConfig({
- codexOverrides: {
+ },
+ {
+ name: "gives profile-local Windows sandbox overrides precedence",
+ overrides: {
profile: "restricted",
profiles: {
restricted: {
@@ -459,30 +451,29 @@ describe("Codex configuration", () => {
},
},
},
- });
-
- expect(merged).toMatchObject({
- profiles: {
- restricted: {
- features: { elevated_windows_sandbox: true },
- windows: { sandbox: "unelevated" },
+ expected: {
+ profiles: {
+ restricted: {
+ features: { elevated_windows_sandbox: true },
+ windows: { sandbox: "unelevated" },
+ },
},
},
- });
- });
-
- test("gives explicit Windows sandbox overrides precedence", async () => {
- const merged = await mergedCodexConfig({
- codexOverrides: {
+ },
+ {
+ name: "gives explicit Windows sandbox overrides precedence",
+ overrides: {
features: { elevated_windows_sandbox: true },
windows: { sandbox: "unelevated" },
},
- });
-
- expect(merged).toMatchObject({
- features: { elevated_windows_sandbox: true },
- windows: { sandbox: "unelevated" },
- });
+ expected: {
+ features: { elevated_windows_sandbox: true },
+ windows: { sandbox: "unelevated" },
+ },
+ },
+ ])("$name", async ({ overrides, expected }) => {
+ const merged = await mergedCodexConfig({ codexOverrides: overrides });
+ expect(merged).toMatchObject(expected);
});
test("retains the Windows sandbox in the hardened scan profile", async () => {
diff --git a/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts b/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
index 244e01a02..5e26a046c 100644
--- a/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
@@ -1,3 +1,4 @@
+import { readSubprocess } from "./support/shell.js";
import { parseJsonLines } from "./support/json.js";
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
@@ -80,11 +81,7 @@ test("advertises distinct Standard worker and Deep reducer contracts", async ()
stderr: "pipe",
timeout: 30_000,
});
- const [status, stdout, stderr] = await Promise.all([
- child.exited,
- new Response(child.stdout).text(),
- new Response(child.stderr).text(),
- ]);
+ const { status, stdout, stderr } = await readSubprocess(child);
expect(status, stderr).toBe(0);
const response = parseJsonLines<{ id?: number; result?: unknown }>(
stdout,
diff --git a/sdk/typescript/tests-ts/fixtures/app-server-messages.mjs b/sdk/typescript/tests-ts/fixtures/app-server-messages.mjs
new file mode 100644
index 000000000..d4a6f0177
--- /dev/null
+++ b/sdk/typescript/tests-ts/fixtures/app-server-messages.mjs
@@ -0,0 +1,15 @@
+export const send = (value) =>
+ process.stdout.write(`${JSON.stringify(value)}\n`);
+
+export function completeTurn(request, text, threadId) {
+ const turnId = "synthetic-turn";
+ send({ id: request.id, result: { turn: { id: turnId } } });
+ send({
+ method: "item/completed",
+ params: { threadId, turnId, item: { type: "agentMessage", text: text() } },
+ });
+ send({
+ method: "turn/completed",
+ params: { threadId, turn: { id: turnId, status: "completed" } },
+ });
+}
diff --git a/sdk/typescript/tests-ts/fixtures/feedback.mjs b/sdk/typescript/tests-ts/fixtures/feedback.mjs
index 4fadc8807..f274ad094 100644
--- a/sdk/typescript/tests-ts/fixtures/feedback.mjs
+++ b/sdk/typescript/tests-ts/fixtures/feedback.mjs
@@ -1,3 +1,4 @@
+import { send } from "./app-server-messages.mjs";
import { createInterface } from "node:readline";
import { readFile, writeFile } from "node:fs/promises";
@@ -8,7 +9,7 @@ for await (const line of createInterface({ input: process.stdin })) {
const request = JSON.parse(line);
requests.push(request);
if (request.method === "initialize") {
- console.log(JSON.stringify({ id: request.id, result: {} }));
+ send({ id: request.id, result: {} });
} else if (request.method === "feedback/upload") {
const attachments = await Promise.all(
request.params.extraLogFiles.map(async (path) => ({
@@ -22,30 +23,26 @@ for await (const line of createInterface({ input: process.stdin })) {
);
switch (process.env.FEEDBACK_SCENARIO) {
case "error":
- console.log(
- JSON.stringify({
- id: request.id,
- error: { message: "Upload failed" },
- }),
- );
+ send({
+ id: request.id,
+ error: { message: "Upload failed" },
+ });
break;
case "exit":
case "exit-diagnostic":
process.exit(1);
break;
case "missing-id":
- console.log(JSON.stringify({ id: request.id, result: {} }));
+ send({ id: request.id, result: {} });
break;
case "malformed":
console.log("not JSON");
break;
default:
- console.log(
- JSON.stringify({
- id: request.id,
- result: { threadId: "feedback-1" },
- }),
- );
+ send({
+ id: request.id,
+ result: { threadId: "feedback-1" },
+ });
}
}
}
diff --git a/sdk/typescript/tests-ts/fixtures/skill-auth.mjs b/sdk/typescript/tests-ts/fixtures/skill-auth.mjs
index 7f94c5619..6dadf32c6 100644
--- a/sdk/typescript/tests-ts/fixtures/skill-auth.mjs
+++ b/sdk/typescript/tests-ts/fixtures/skill-auth.mjs
@@ -1,10 +1,10 @@
+import { send, completeTurn } from "./app-server-messages.mjs";
import assert from "node:assert/strict";
import { appendFileSync, readFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { parse as parseToml } from "smol-toml";
import { createInterface } from "node:readline";
-const send = (value) => process.stdout.write(`${JSON.stringify(value)}\n`);
let loggedIn = false;
for await (const line of createInterface({ input: process.stdin })) {
const request = JSON.parse(line);
@@ -103,21 +103,6 @@ for await (const line of createInterface({ input: process.stdin })) {
false,
);
}
- send({ id: request.id, result: { turn: { id: "synthetic-turn" } } });
- send({
- method: "item/completed",
- params: {
- threadId: "synthetic-thread",
- turnId: "synthetic-turn",
- item: { type: "agentMessage", text: "Synthetic patch complete" },
- },
- });
- send({
- method: "turn/completed",
- params: {
- threadId: "synthetic-thread",
- turn: { id: "synthetic-turn", status: "completed" },
- },
- });
+ completeTurn(request, () => "Synthetic patch complete", "synthetic-thread");
}
}
diff --git a/sdk/typescript/tests-ts/fixtures/skill-native-config.mjs b/sdk/typescript/tests-ts/fixtures/skill-native-config.mjs
index c927d1789..3c0c4bf12 100644
--- a/sdk/typescript/tests-ts/fixtures/skill-native-config.mjs
+++ b/sdk/typescript/tests-ts/fixtures/skill-native-config.mjs
@@ -1,3 +1,4 @@
+import { send, completeTurn } from "./app-server-messages.mjs";
import { spawn } from "node:child_process";
import { createInterface } from "node:readline";
@@ -7,7 +8,6 @@ const child = spawn(process.env.SYNTHETIC_NATIVE_CODEX, process.argv.slice(2), {
windowsHide: true,
});
child.stderr.pipe(process.stderr);
-const send = (value) => process.stdout.write(JSON.stringify(value) + "\n");
let turnRequest;
let modelProvider;
await Promise.all([
@@ -38,28 +38,17 @@ await Promise.all([
continue;
}
const threadId = turnRequest.params.threadId;
- const turnId = "synthetic-turn";
const config = response.result.config;
- send({ id: turnRequest.id, result: { turn: { id: turnId } } });
- send({
- method: "item/completed",
- params: {
- threadId,
- turnId,
- item: {
- type: "agentMessage",
- text: JSON.stringify({
- modelProvider,
- projectServerConfigured:
- config.mcp_servers?.synthetic !== undefined,
- }),
- },
- },
- });
- send({
- method: "turn/completed",
- params: { threadId, turn: { id: turnId, status: "completed" } },
- });
+ completeTurn(
+ turnRequest,
+ () =>
+ JSON.stringify({
+ modelProvider,
+ projectServerConfigured:
+ config.mcp_servers?.synthetic !== undefined,
+ }),
+ threadId,
+ );
}
})(),
]);
diff --git a/sdk/typescript/tests-ts/fixtures/skill-provider-auth.mjs b/sdk/typescript/tests-ts/fixtures/skill-provider-auth.mjs
index a46879dc5..5bb912e49 100644
--- a/sdk/typescript/tests-ts/fixtures/skill-provider-auth.mjs
+++ b/sdk/typescript/tests-ts/fixtures/skill-provider-auth.mjs
@@ -1,3 +1,4 @@
+import { send, completeTurn } from "./app-server-messages.mjs";
import { appendFileSync, existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { createInterface } from "node:readline";
@@ -7,7 +8,6 @@ const record = (value) =>
process.env.SYNTHETIC_PROVIDER_LOG,
`${JSON.stringify(value)}\n`,
);
-const send = (value) => process.stdout.write(`${JSON.stringify(value)}\n`);
const configPath = join(process.env.CODEX_HOME, "config.toml");
record({
args: process.argv.slice(2),
@@ -50,36 +50,22 @@ if (process.env.SYNTHETIC_SKILL_COMMAND === "validate") {
} else if (request.method === "command/exec") {
send({ id: request.id, result: { exitCode: 0 } });
} else if (request.method === "turn/start") {
- send({ id: request.id, result: { turn: { id: "synthetic-turn" } } });
- send({
- method: "item/completed",
- params: {
- threadId: "synthetic-thread",
- turnId: "synthetic-turn",
- item: {
- type: "agentMessage",
- text:
- process.env.SYNTHETIC_SKILL_COMMAND === "verify-fix"
- ? JSON.stringify({
- results: [
- {
- id: "finding-1",
- status: "fixed",
- evidence: "Synthetic verification",
- },
- ],
- })
- : "Synthetic patch complete",
- },
- },
- });
- send({
- method: "turn/completed",
- params: {
- threadId: "synthetic-thread",
- turn: { id: "synthetic-turn", status: "completed" },
- },
- });
+ completeTurn(
+ request,
+ () =>
+ process.env.SYNTHETIC_SKILL_COMMAND === "verify-fix"
+ ? JSON.stringify({
+ results: [
+ {
+ id: "finding-1",
+ status: "fixed",
+ evidence: "Synthetic verification",
+ },
+ ],
+ })
+ : "Synthetic patch complete",
+ "synthetic-thread",
+ );
}
}
}
diff --git a/sdk/typescript/tests-ts/multiscan.test.ts b/sdk/typescript/tests-ts/multiscan.test.ts
index 5597c39c2..45d6a9753 100644
--- a/sdk/typescript/tests-ts/multiscan.test.ts
+++ b/sdk/typescript/tests-ts/multiscan.test.ts
@@ -51,11 +51,7 @@ const testPosix = process.platform === "win32" ? test.skip : test;
afterEach(cleanup);
-async function fixture(): Promise<{
- root: string;
- input: string;
- output: string;
-}> {
+async function fixture() {
const root = await temporaryDirectory();
return {
root,
@@ -70,11 +66,7 @@ function git(repository: string, ...args: string[]): string {
}).trim();
}
-async function repository(
- root: string,
- name: string,
- objectFormat = "sha1",
-): Promise<{ path: string; revision: string }> {
+async function repository(root: string, name: string, objectFormat = "sha1") {
const path = join(root, name);
await mkdir(join(path, "src"), { recursive: true });
await writeFile(
diff --git a/sdk/typescript/tests-ts/patch-risk-contract.test.ts b/sdk/typescript/tests-ts/patch-risk-contract.test.ts
index 013406bf0..bafe86c16 100644
--- a/sdk/typescript/tests-ts/patch-risk-contract.test.ts
+++ b/sdk/typescript/tests-ts/patch-risk-contract.test.ts
@@ -20,55 +20,7 @@ import {
windowsLoopbackPath,
} from "./windows-helper-location.js";
-interface Assessment {
- [key: string]: unknown;
- schemaVersion: number;
- patch: {
- repository: string;
- sourceType: string;
- base: string;
- head: string;
- changedFiles: string[];
- sha256: string;
- };
- recommendation: string;
- workflowLabel: string;
- impact: { rating: string; rationale: string };
- regressionLikelihood: { rating: string; rationale: string };
- regressionProtection: {
- rating: string;
- rationale: string;
- exactHeadChecksPassed: boolean;
- };
- recoverability: { rating: string; rationale: string };
- confidence: { rating: string; rationale: string };
- applicability: { status: string; rationale: string };
- statusQuoRisk: { rating: string; rationale: string };
- autoMergeExclusions: string[];
- affectedRuntimeRoots: string[];
- materialBoundaries: Array<{
- id: string;
- invariant: string;
- runtimeRoot: string;
- counterexample: string;
- legitimateControl: string;
- result: string;
- }>;
- validation: Array<{
- name: string;
- status: string;
- protects: string;
- }>;
- unknowns: Array<{
- summary: string;
- decisionCritical: boolean;
- }>;
- evidencePlan: Array<{
- question: string;
- action: string;
- outcomes: Record<string, string>;
- }>;
-}
+type Assessment = ReturnType<typeof assessment> & Record<string, unknown>;
const schemaPath = join(
PLUGIN_ROOT,
@@ -85,7 +37,7 @@ afterEach(async () => {
);
});
-function assessment(): Assessment {
+function assessment() {
return {
schemaVersion: 1,
patch: {
@@ -118,7 +70,7 @@ function assessment(): Assessment {
rating: "moderate",
rationale: "The defect remains.",
},
- autoMergeExclusions: [],
+ autoMergeExclusions: [] as string[],
affectedRuntimeRoots: ["service.request"],
materialBoundaries: [
{
@@ -138,8 +90,12 @@ function assessment(): Assessment {
protects: "Changed behavior through the production caller.",
},
],
- unknowns: [],
- evidencePlan: [],
+ unknowns: [] as { summary: string; decisionCritical: boolean }[],
+ evidencePlan: [] as {
+ question: string;
+ action: string;
+ outcomes: Record<string, string>;
+ }[],
};
}
@@ -255,7 +211,7 @@ describe("patch risk assessment contract", () => {
delete (missingRequired as Record<string, unknown>)["patch"];
invalidAssessments.push(missingRequired);
- const additionalProperty = assessment();
+ const additionalProperty: Assessment = assessment();
additionalProperty["unexpected"] = true;
invalidAssessments.push(additionalProperty);
diff --git a/sdk/typescript/tests-ts/publication-integration.test.ts b/sdk/typescript/tests-ts/publication-integration.test.ts
index 9866149bc..84cb63620 100644
--- a/sdk/typescript/tests-ts/publication-integration.test.ts
+++ b/sdk/typescript/tests-ts/publication-integration.test.ts
@@ -23,7 +23,6 @@ import { prepareScanPublication } from "../src/publication.js";
import { recordPublishedIssues } from "../src/publication-store.js";
import type {
CoverageDocument,
- Finding,
FindingsDocument,
ScanManifest,
} from "../src/models.js";
@@ -56,13 +55,7 @@ const { temporaryDirectory, cleanup } = createApiTestFixtures(
"codex-security-publication-integration-",
);
-interface PublicationFixture {
- python: string;
- scanDirectory: string;
- stateDirectory: string;
- environment: NodeJS.ProcessEnv;
- findings: Finding[];
-}
+type PublicationFixture = Awaited<ReturnType<typeof fixture>>;
interface PromptFinding {
findingId: string;
@@ -91,7 +84,7 @@ interface StoredPublication {
afterEach(cleanup);
-async function fixture(count: number): Promise<PublicationFixture> {
+async function fixture(count: number) {
const root = await temporaryDirectory();
const scanDirectory = join(root, "scan");
const stateDirectory = join(root, "state");
diff --git a/sdk/typescript/tests-ts/publication-store.test.ts b/sdk/typescript/tests-ts/publication-store.test.ts
index 136496821..570404a80 100644
--- a/sdk/typescript/tests-ts/publication-store.test.ts
+++ b/sdk/typescript/tests-ts/publication-store.test.ts
@@ -37,12 +37,7 @@ const { temporaryDirectory, cleanup } = createApiTestFixtures(
afterEach(cleanup);
-interface PublicationFixture {
- environment: NodeJS.ProcessEnv;
- publication: PreparedScanPublication;
- python: string;
- stateDirectory: string;
-}
+type PublicationFixture = Awaited<ReturnType<typeof publicationFixture>>;
async function publicationFixture(
options: {
@@ -51,7 +46,7 @@ async function publicationFixture(
seedScan?: boolean;
stateDirectoryName?: string;
} = {},
-): Promise<PublicationFixture> {
+) {
const root = await temporaryDirectory();
const scanDirectory = join(root, "completed-scan");
await mkdir(scanDirectory, { mode: 0o700 });
@@ -62,7 +57,7 @@ async function publicationFixture(
"Publication workbench tests require a Python interpreter.",
);
}
- const environment = {
+ const environment: NodeJS.ProcessEnv = {
...process.env,
CODEX_SECURITY_STATE_DIR: stateDirectory,
PYTHON: python,
diff --git a/sdk/typescript/tests-ts/rank-pool.test.ts b/sdk/typescript/tests-ts/rank-pool.test.ts
index f6e0d8e7e..32fe5ad5d 100644
--- a/sdk/typescript/tests-ts/rank-pool.test.ts
+++ b/sdk/typescript/tests-ts/rank-pool.test.ts
@@ -19,18 +19,7 @@ const node = Bun.which("node")!;
const helper = join(PLUGIN_ROOT, "mcp", "helpers.mjs");
const newline = process.platform === "win32" ? "\r\n" : "\n";
const roots = createTemporaryDirectoriesSync(true);
-interface Worker {
- slot: number;
- input_shards: string[];
- output_shards: string[];
-}
-interface Plan {
- schema_version: number;
- strategy: string;
- shard_count: number;
- ranking_worker_count: number;
- workers: Worker[];
-}
+
const shardName = (index: number, output = false) =>
`rank-shard-${String(index).padStart(4, "0")}.${output ? "output" : "input"}.jsonl`;
const row = (index: number) => ({
@@ -45,6 +34,27 @@ const ranked = (index: number) => ({
include: true,
reason: "runtime surface",
});
+const expectedPlan = {
+ ranking_worker_count: 2,
+ schema_version: 1,
+ shard_count: 5,
+ strategy: "round_robin",
+ workers: [
+ {
+ input_shards: [1, 3, 5].map((index) => shardName(index)),
+ output_shards: [1, 3, 5].map((index) => shardName(index, true)),
+ slot: 1,
+ },
+ {
+ input_shards: [2, 4].map((index) => shardName(index)),
+ output_shards: [2, 4].map((index) => shardName(index, true)),
+ slot: 2,
+ },
+ ],
+};
+type Plan = typeof expectedPlan;
+type Worker = Plan["workers"][number];
+
function fixture(count = 5) {
const root = roots.create("rank-pool-");
const directory = join(root, "rank_shards");
@@ -117,26 +127,8 @@ describe("rank pool helpers", () => {
expect(make(f).stdout).toBe(
`Assigned 5 rank shards to 2 ranking workers in ${f.plan}${newline}`,
);
- const expected: Plan = {
- ranking_worker_count: 2,
- schema_version: 1,
- shard_count: 5,
- strategy: "round_robin",
- workers: [
- {
- input_shards: [1, 3, 5].map((index) => shardName(index)),
- output_shards: [1, 3, 5].map((index) => shardName(index, true)),
- slot: 1,
- },
- {
- input_shards: [2, 4].map((index) => shardName(index)),
- output_shards: [2, 4].map((index) => shardName(index, true)),
- slot: 2,
- },
- ],
- };
expect(readFileSync(f.plan, "utf8")).toBe(
- (JSON.stringify(expected, null, 2) + "\n").replaceAll("\n", newline),
+ (JSON.stringify(expectedPlan, null, 2) + "\n").replaceAll("\n", newline),
);
const first = readFileSync(f.plan);
expect(make(f).status).toBe(0);
diff --git a/sdk/typescript/tests-ts/release-automation.test.ts b/sdk/typescript/tests-ts/release-automation.test.ts
index 45abb90a8..ea87786b9 100644
--- a/sdk/typescript/tests-ts/release-automation.test.ts
+++ b/sdk/typescript/tests-ts/release-automation.test.ts
@@ -782,48 +782,39 @@ describe("protected Git release refs", () => {
).toBe("0.1.2");
});
- test("rejects a release-shaped branch", () => {
- expect(() =>
- releaseTagVersion(
- "branch",
- "refs/heads/npm-v0.1.2",
- "npm-v0.1.2",
- packageJson,
- ),
- ).toThrow("npm releases must be dispatched from a real Git tag.");
- });
-
- test("rejects a mismatched full Git ref", () => {
- expect(() =>
- releaseTagVersion(
- "tag",
- "refs/tags/npm-v0.1.3",
- "npm-v0.1.2",
- packageJson,
- ),
- ).toThrow("npm releases must be dispatched from a real Git tag.");
- });
-
- test("rejects an unstable release tag", () => {
- expect(() =>
- releaseTagVersion(
- "tag",
- "refs/tags/npm-v0.1.2-beta.1",
- "npm-v0.1.2-beta.1",
- packageJson,
- ),
- ).toThrow("Release tags must identify a stable npm-vX.Y.Z version.");
- });
-
- test("rejects a Git tag for a different package version", () => {
- expect(() =>
- releaseTagVersion(
- "tag",
- "refs/tags/npm-v0.1.3",
- "npm-v0.1.3",
- packageJson,
- ),
- ).toThrow("npm release tag must match the stable package version.");
+ test.each([
+ [
+ "rejects a release-shaped branch",
+ "branch",
+ "refs/heads/npm-v0.1.2",
+ "npm-v0.1.2",
+ "npm releases must be dispatched from a real Git tag.",
+ ],
+ [
+ "rejects a mismatched full Git ref",
+ "tag",
+ "refs/tags/npm-v0.1.3",
+ "npm-v0.1.2",
+ "npm releases must be dispatched from a real Git tag.",
+ ],
+ [
+ "rejects an unstable release tag",
+ "tag",
+ "refs/tags/npm-v0.1.2-beta.1",
+ "npm-v0.1.2-beta.1",
+ "Release tags must identify a stable npm-vX.Y.Z version.",
+ ],
+ [
+ "rejects a Git tag for a different package version",
+ "tag",
+ "refs/tags/npm-v0.1.3",
+ "npm-v0.1.3",
+ "npm release tag must match the stable package version.",
+ ],
+ ] as const)("%s", (_name, refType, ref, refName, message) => {
+ expect(() => releaseTagVersion(refType, ref, refName, packageJson)).toThrow(
+ message,
+ );
});
});
diff --git a/sdk/typescript/tests-ts/scan-activity.test.ts b/sdk/typescript/tests-ts/scan-activity.test.ts
index 21399ba5f..99c1f4abb 100644
--- a/sdk/typescript/tests-ts/scan-activity.test.ts
+++ b/sdk/typescript/tests-ts/scan-activity.test.ts
@@ -47,6 +47,17 @@ function sessionEvent(
return { type: "response_item", payload };
}
+function commandActivityCase(
+ _name: string,
+ command: string,
+ repository: string,
+ expected: object,
+) {
+ expect(
+ scanActivityFromEvent(commandEvent("item.started", command), repository),
+ ).toMatchObject(expected);
+}
+
describe("scan activity", () => {
test("reports repository files as soon as a read command starts", () => {
expect(
@@ -222,48 +233,33 @@ describe("scan activity", () => {
}
});
- test("shows exact targeted file listing commands", () => {
- expect(
- scanActivityFromEvent(
- commandEvent(
- "item.started",
- 'rg --files "$CODEX_SECURITY_REPOSITORY/SECURITY.md"',
- ),
- "/code/juice-shop",
- ),
- ).toMatchObject({
- description: 'rg --files "$CODEX_SECURITY_REPOSITORY/SECURITY.md"',
- paths: ["SECURITY.md"],
- });
- });
-
- test("does not present plugin instructions as repository files", () => {
- expect(
- scanActivityFromEvent(
- commandEvent(
- "item.started",
+ test.each([
+ [
+ "shows exact targeted file listing commands",
+ 'rg --files "$CODEX_SECURITY_REPOSITORY/SECURITY.md"',
+ "/code/juice-shop",
+ {
+ description: 'rg --files "$CODEX_SECURITY_REPOSITORY/SECURITY.md"',
+ paths: ["SECURITY.md"],
+ },
+ ],
+ [
+ "does not present plugin instructions as repository files",
+ 'sed -n 1,160p "$CODEX_SECURITY_PLUGIN_ROOT/skills/security-scan/SKILL.md"',
+ "/code/juice-shop",
+ {
+ description:
'sed -n 1,160p "$CODEX_SECURITY_PLUGIN_ROOT/skills/security-scan/SKILL.md"',
- ),
- "/code/juice-shop",
- ),
- ).toMatchObject({
- description:
- 'sed -n 1,160p "$CODEX_SECURITY_PLUGIN_ROOT/skills/security-scan/SKILL.md"',
- paths: [],
- });
- });
-
- test("preserves spaces in quoted repository file paths", () => {
- expect(
- scanActivityFromEvent(
- commandEvent(
- "item.started",
- 'nl -ba "$CODEX_SECURITY_REPOSITORY/routes/login page.ts"',
- ),
- "/code/juice shop",
- ),
- ).toMatchObject({ paths: ["routes/login page.ts"] });
- });
+ paths: [],
+ },
+ ],
+ [
+ "preserves spaces in quoted repository file paths",
+ 'nl -ba "$CODEX_SECURITY_REPOSITORY/routes/login page.ts"',
+ "/code/juice shop",
+ { paths: ["routes/login page.ts"] },
+ ],
+ ] as const)("%s", commandActivityCase);
test("recognizes repository paths generated for the current shell", () => {
const command = `${process.platform === "win32" ? "Get-Content -LiteralPath" : "cat"} ${shellEnvironmentReference("CODEX_SECURITY_REPOSITORY", "/src/app.ts")}`;
@@ -326,41 +322,26 @@ describe("scan activity", () => {
}
});
- test("keeps POSIX environment names case-sensitive", () => {
- expect(
- scanActivityFromEvent(
- commandEvent(
- "item.started",
- 'cat "$codex_security_repository/src/app.ts" "${Codex_Security_Repository}/src/app.ts"',
- ),
- "/code/project",
- ),
- ).toMatchObject({ paths: [] });
- });
-
- test("cleans escaped quotes from real wrapped scan commands", () => {
- expect(
- scanActivityFromEvent(
- commandEvent(
- "item.started",
- 'nl -ba "$CODEX_SECURITY_REPOSITORY/commands.py\\" printf "next" "$CODEX_SECURITY_REPOSITORY/server.py\\"',
- ),
- "/code/juice-shop",
- ),
- ).toMatchObject({ paths: ["commands.py", "server.py"] });
- });
-
- test("never reports paths escaping the scan repository", () => {
- expect(
- scanActivityFromEvent(
- commandEvent(
- "item.started",
- 'cat "$CODEX_SECURITY_REPOSITORY/../credentials.json"',
- ),
- "/code/juice-shop",
- ),
- ).toMatchObject({ paths: [] });
- });
+ test.each([
+ [
+ "keeps POSIX environment names case-sensitive",
+ 'cat "$codex_security_repository/src/app.ts" "${Codex_Security_Repository}/src/app.ts"',
+ "/code/project",
+ { paths: [] },
+ ],
+ [
+ "cleans escaped quotes from real wrapped scan commands",
+ 'nl -ba "$CODEX_SECURITY_REPOSITORY/commands.py\\" printf "next" "$CODEX_SECURITY_REPOSITORY/server.py\\"',
+ "/code/juice-shop",
+ { paths: ["commands.py", "server.py"] },
+ ],
+ [
+ "never reports paths escaping the scan repository",
+ 'cat "$CODEX_SECURITY_REPOSITORY/../credentials.json"',
+ "/code/juice-shop",
+ { paths: [] },
+ ],
+ ] as const)("%s", commandActivityCase);
test("keeps Unix and Windows tool paths inside the repository", () => {
for (const [repository, path, paths] of [
diff --git a/sdk/typescript/tests-ts/scan-recovery.test.ts b/sdk/typescript/tests-ts/scan-recovery.test.ts
index 9be0ad021..ecde3ce77 100644
--- a/sdk/typescript/tests-ts/scan-recovery.test.ts
+++ b/sdk/typescript/tests-ts/scan-recovery.test.ts
@@ -69,14 +69,7 @@ type SarifDocument = {
}>;
};
-type ScanFixture = {
- python: string;
- repository: string;
- stateDir: string;
- scanDir: string;
- scanId: string;
- registration: Record<string, unknown>;
-};
+type ScanFixture = Awaited<ReturnType<typeof startDraftScan>>;
const { temporaryDirectory, cleanup } = createApiTestFixtures(
"codex-security-scan-recovery-",
@@ -99,7 +92,7 @@ async function workbench(
async function startDraftScan(
repositoryKind: "directory" | "clean" | "dirty" | "nested" = "directory",
recipeFromStdin = false,
-): Promise<ScanFixture> {
+) {
const root = await temporaryDirectory();
const python =
process.env["PYTHON"] ?? Bun.which("python3") ?? Bun.which("python");
@@ -148,13 +141,13 @@ async function startDraftScan(
}
}
- const fixture: ScanFixture = {
+ const fixture = {
python: python!,
repository: target,
stateDir: join(root, "state"),
scanDir,
scanId: "",
- registration: {},
+ registration: {} as Record<string, unknown>,
};
const recipe = JSON.stringify({
config: {},
diff --git a/sdk/typescript/tests-ts/support/api-events.ts b/sdk/typescript/tests-ts/support/api-events.ts
index 7303ebf1b..456f5d713 100644
--- a/sdk/typescript/tests-ts/support/api-events.ts
+++ b/sdk/typescript/tests-ts/support/api-events.ts
@@ -114,15 +114,11 @@ export async function* failedEvents(): AsyncGenerator<ThreadEvent> {
}
export function completedCodex(root: string, threadId: string | null = null) {
- return (_options: CodexOptions) => ({
- startThread: () => ({
- id: threadId,
- async runStreamed() {
- await copyCompletedScan(root);
- return { events: completedEvents() };
- },
- }),
- });
+ return (_options: CodexOptions) =>
+ codexFactory(async () => {
+ await copyCompletedScan(root);
+ return { events: completedEvents() };
+ }, threadId)();
}
export function collectObserverErrors(errors: [ScanObserverName, string][]) {
diff --git a/sdk/typescript/tests-ts/support/shell.ts b/sdk/typescript/tests-ts/support/shell.ts
index b33591da8..fa364fd5b 100644
--- a/sdk/typescript/tests-ts/support/shell.ts
+++ b/sdk/typescript/tests-ts/support/shell.ts
@@ -83,7 +83,7 @@ export function nodeCommand() {
}
export async function readSubprocess(
- child: Bun.Subprocess<"ignore", "pipe" | "ignore", "pipe">,
+ child: Bun.Subprocess<Bun.Spawn.Writable, "pipe" | "ignore", "pipe">,
) {
const [status, stdout, stderr] = await Promise.all([
child.exited,
diff --git a/sdk/typescript/tests-ts/support/workbench-fakes.ts b/sdk/typescript/tests-ts/support/workbench-fakes.ts
index 2f460991b..f4699f52b 100644
--- a/sdk/typescript/tests-ts/support/workbench-fakes.ts
+++ b/sdk/typescript/tests-ts/support/workbench-fakes.ts
@@ -52,44 +52,37 @@ export function scriptedWorkbench(
export function checkpointWorkbench(workflowId: string, source: JsonObject) {
const reviews = new Map<string, unknown>();
const saved: JsonObject[] = [];
- const fixture: {
- source: JsonObject;
- saved: JsonObject[];
- run: typeof runWorkbench;
- } = {
- source,
- saved,
- run: (async (_options, args, input) => {
- const payload = request(args, input);
- expect(payload["id"]).toBe(workflowId);
- switch (payload["action"]) {
- case "source":
- expect(payload["repository"]).toBe(fixture.source["repository"]);
- return { source: structuredClone(fixture.source) };
- case "get-review":
- expect(typeof payload["key"]).toBe("string");
- return {
- review: structuredClone(
- reviews.get(payload["key"] as string) ?? null,
- ),
- } as JsonObject;
- case "save-review":
- expect(typeof payload["key"]).toBe("string");
- expect(payload["binding"]).toBeObject();
- expect(payload).toHaveProperty("result");
- saved.push(structuredClone(payload));
- reviews.set(
- payload["key"] as string,
- structuredClone(payload["result"]),
- );
- return {};
- default:
- throw new Error(
- `Unexpected checkpoint request: ${JSON.stringify(payload)}`,
- );
- }
- }) satisfies typeof runWorkbench,
+ const run: typeof runWorkbench = async (_options, args, input) => {
+ const payload = request(args, input);
+ expect(payload["id"]).toBe(workflowId);
+ switch (payload["action"]) {
+ case "source":
+ expect(payload["repository"]).toBe(fixture.source["repository"]);
+ return { source: structuredClone(fixture.source) };
+ case "get-review":
+ expect(typeof payload["key"]).toBe("string");
+ return {
+ review: structuredClone(
+ reviews.get(payload["key"] as string) ?? null,
+ ),
+ } as JsonObject;
+ case "save-review":
+ expect(typeof payload["key"]).toBe("string");
+ expect(payload["binding"]).toBeObject();
+ expect(payload).toHaveProperty("result");
+ saved.push(structuredClone(payload));
+ reviews.set(
+ payload["key"] as string,
+ structuredClone(payload["result"]),
+ );
+ return {};
+ default:
+ throw new Error(
+ `Unexpected checkpoint request: ${JSON.stringify(payload)}`,
+ );
+ }
};
+ const fixture = { source, saved, run };
return fixture;
}
diff --git a/sdk/typescript/tests-ts/trusted-executable.test.ts b/sdk/typescript/tests-ts/trusted-executable.test.ts
index e31f70623..fa7a4039b 100644
--- a/sdk/typescript/tests-ts/trusted-executable.test.ts
+++ b/sdk/typescript/tests-ts/trusted-executable.test.ts
@@ -20,10 +20,7 @@ async function resolveWindowsExecutable(
candidate: string,
path: string,
protectedRoot: string,
-): Promise<{
- executable: string;
- environment: Record<string, string | undefined>;
-} | null> {
+): ReturnType<typeof resolveTrustedExecutable> {
if (process.platform === "win32") {
return await resolveTrustedExecutable(
candidate,
@@ -54,10 +51,9 @@ async function resolveWindowsExecutable(
{ encoding: "utf8" },
);
expect(result.status).toBe(0);
- return JSON.parse(result.stdout) as {
- executable: string;
- environment: Record<string, string | undefined>;
- } | null;
+ return JSON.parse(result.stdout) as Awaited<
+ ReturnType<typeof resolveTrustedExecutable>
+ >;
}
describe("trusted executable resolution", () => {