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): share setup for scan and follow-up scenarios (#1375)

mldangelo-oaiedff3cd4b5ce
diff --git a/sdk/typescript/tests-ts/api-events.test.ts b/sdk/typescript/tests-ts/api-events.test.ts
index e59eda880..d90e14063 100644
--- a/sdk/typescript/tests-ts/api-events.test.ts
+++ b/sdk/typescript/tests-ts/api-events.test.ts
@@ -62,8 +62,6 @@ function tacToolCall(
async function* tacEvents(
items: readonly McpToolCallItem[],
): AsyncGenerator<ThreadEvent> {
- yield { type: "thread.started", thread_id: "thread-1" };
- yield { type: "turn.started" };
for (const item of items) {
yield { type: "item.completed", item };
}
@@ -71,16 +69,6 @@ async function* tacEvents(
type: "item.completed",
item: { id: "message-1", type: "agent_message", text: "scan complete" },
};
- yield {
- type: "turn.completed",
- usage: {
- input_tokens: 10,
- cached_input_tokens: 2,
- cache_write_input_tokens: 0,
- output_tokens: 3,
- reasoning_output_tokens: 1,
- },
- };
}
function runTacEvents(
@@ -91,7 +79,7 @@ function runTacEvents(
onTrustedAccessStatus?: (status: ScanTrustedAccessStatus) => void,
authentication?: ScanAuthentication,
): ReturnType<typeof runEvents> {
- return runEvents(scanDir, tacEvents(items), {
+ return runEvents(scanDir, completedEvents("thread-1", tacEvents(items)), {
authentication,
onObserverError,
onTrustedAccessStatus,
@@ -285,7 +273,7 @@ describe("one-shot scan events", () => {
const statuses: ScanTrustedAccessStatus[] = [];
const result = await runEvents(
scanDir,
- tacEvents([tacToolCall(status)]),
+ completedEvents("thread-1", tacEvents([tacToolCall(status)])),
{
authentication,
modelProvider: "amazon-bedrock",
@@ -475,7 +463,10 @@ describe("one-shot scan events", () => {
};
const result = await runScanEvents({
thread: { id: null },
- events: tacEvents([tacToolCall("not_granted")]),
+ events: completedEvents(
+ "thread-1",
+ tacEvents([tacToolCall("not_granted")]),
+ ),
signal: new AbortController().signal,
scanDir,
pluginRoot: PLUGIN_ROOT,
@@ -1005,8 +996,6 @@ describe("one-shot scan events", () => {
const scanDir = await copyCompletedScan(await temporaryDirectory());
const statuses: ScanWorkerStatus[] = [];
async function* workerEvents(): AsyncGenerator<ThreadEvent> {
- yield { type: "thread.started", thread_id: "thread-1" };
- yield { type: "turn.started" };
yield {
type: "item.completed",
item: {
@@ -1038,20 +1027,10 @@ describe("one-shot scan events", () => {
text: 'CODEX_SECURITY_WORKER_STATUS {"phase":"ranking","planned":6,"started":3}',
},
};
- yield {
- type: "turn.completed",
- usage: {
- input_tokens: 10,
- cached_input_tokens: 2,
- cache_write_input_tokens: 0,
- output_tokens: 3,
- reasoning_output_tokens: 1,
- },
- };
}
await expect(
- runEvents(scanDir, workerEvents(), {
+ runEvents(scanDir, completedEvents("thread-1", workerEvents()), {
onWorkerStatus: (status) => statuses.push(status),
}),
).resolves.toBeDefined();
@@ -1066,8 +1045,6 @@ describe("one-shot scan events", () => {
const activities: ScanActivity[] = [];
async function* activityEvents(): AsyncGenerator<ThreadEvent> {
- yield { type: "thread.started", thread_id: "thread-1" };
- yield { type: "turn.started" };
for (const [type, status] of [
["item.started", "in_progress"],
["item.completed", "completed"],
@@ -1083,20 +1060,10 @@ describe("one-shot scan events", () => {
},
};
}
- yield {
- type: "turn.completed",
- usage: {
- input_tokens: 10,
- cached_input_tokens: 2,
- cache_write_input_tokens: 0,
- output_tokens: 3,
- reasoning_output_tokens: 1,
- },
- };
}
await expect(
- runEvents(scanDir, activityEvents(), {
+ runEvents(scanDir, completedEvents("thread-1", activityEvents()), {
onActivity: (activity) => activities.push(activity),
}),
).resolves.toBeDefined();
@@ -1123,8 +1090,6 @@ describe("one-shot scan events", () => {
const activities: ScanActivity[] = [];
async function* reasoningEvents(): AsyncGenerator<ThreadEvent> {
- yield { type: "thread.started", thread_id: "thread-1" };
- yield { type: "turn.started" };
yield {
type: "item.completed",
item: {
@@ -1135,19 +1100,9 @@ describe("one-shot scan events", () => {
"**Planning batch file size verification and progress output**",
},
};
- yield {
- type: "turn.completed",
- usage: {
- input_tokens: 10,
- cached_input_tokens: 2,
- cache_write_input_tokens: 0,
- output_tokens: 3,
- reasoning_output_tokens: 1,
- },
- };
}
- await runEvents(scanDir, reasoningEvents(), {
+ await runEvents(scanDir, completedEvents("thread-1", reasoningEvents()), {
onActivity: (activity) => activities.push(activity),
});
@@ -1171,8 +1126,6 @@ describe("one-shot scan events", () => {
const updates: ScanProgress[] = [];
async function* progressEvents(): AsyncGenerator<ThreadEvent> {
- yield { type: "thread.started", thread_id: "thread-1" };
- yield { type: "turn.started" };
for (const text of [
'CODEX_SECURITY_SCAN_PROGRESS {"phase":"discovery","filesCompleted":0,"filesTotal":8}',
'CODEX_SECURITY_SCAN_PROGRESS {"phase":"discovery","filesCompleted":3,"filesTotal":8}',
@@ -1187,20 +1140,10 @@ describe("one-shot scan events", () => {
},
};
}
- yield {
- type: "turn.completed",
- usage: {
- input_tokens: 10,
- cached_input_tokens: 2,
- cache_write_input_tokens: 0,
- output_tokens: 3,
- reasoning_output_tokens: 1,
- },
- };
}
await expect(
- runEvents(scanDir, progressEvents(), {
+ runEvents(scanDir, completedEvents("thread-1", progressEvents()), {
onProgress: (progress) => updates.push(progress),
}),
).resolves.toBeDefined();
diff --git a/sdk/typescript/tests-ts/api-post-scan.test.ts b/sdk/typescript/tests-ts/api-post-scan.test.ts
index 36a6d4665..793b3e318 100644
--- a/sdk/typescript/tests-ts/api-post-scan.test.ts
+++ b/sdk/typescript/tests-ts/api-post-scan.test.ts
@@ -12,7 +12,7 @@ import {
writeFile,
} from "node:fs/promises";
import { dirname, join } from "node:path";
-import type { ThreadEvent } from "@openai/codex-sdk";
+import type { ThreadEvent, TurnOptions } from "@openai/codex-sdk";
import { afterEach, describe, expect, spyOn, test } from "bun:test";
import {
@@ -24,6 +24,7 @@ import { writeThreatModel } from "../src/artifact-export.js";
import { copyCompletedScan, PLUGIN_ROOT } from "./plugin-root.js";
import { TestClient } from "./support/api-client.js";
import {
+ codexFactory,
completedEvents,
failedPostScanEvents,
preparedRuntime,
@@ -82,64 +83,54 @@ async function startPostScan(scenario: PostScanScenario) {
}
let turns = 0;
let original = Buffer.alloc(0);
- const client = new TestClient(
- {},
- {
- prepareRuntime: async () => runtime,
- resolvePluginPython: async () => python!,
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
- prepareScanArtifactRestorer: async (...args) => {
- const restorer = await prepareScanArtifactRestorer(...args);
- return scenario.wrapRestorer?.(restorer, context) ?? restorer;
- },
- createCodex: () => ({
- startThread: () => ({
- id: "thread-1",
- async runStreamed(_input, options) {
- expect(options.cyberAccessProgram).toBe("daybreak_blue");
- turns += 1;
- if (turns === 1) {
- await copyCompletedScan(root);
- if (scenario.initialContents !== undefined) {
- await mkdir(dirname(artifactPath), { recursive: true });
- await writeFile(artifactPath, scenario.initialContents);
- const manifestPath = join(scanDir, "scan-manifest.json");
- const manifest = JSON.parse(
- await readFile(manifestPath, "utf8"),
- );
- if (scenario.threatModel !== undefined)
- manifest.scan.threatModel = scenario.threatModel;
- if (scenario.artifact !== "threatmodel.md")
- manifest.scan.artifacts.push({
- path: scenario.artifact,
- sha256: hash("sha256", await readFile(artifactPath)),
- mediaType: scenario.artifact.endsWith(".bin")
- ? "application/octet-stream"
- : "application/json",
- });
- await writeFile(manifestPath, JSON.stringify(manifest));
- if (scenario.threatModel !== undefined)
- await writeThreatModel(scanDir, {
- pluginRoot: runtime.plugin.installedRoot,
- pythonPath: python!,
- protectedRoot: repository,
- });
- }
- original = await readFile(artifactPath);
- return { events: completedEvents() };
- }
- if (scenario.artifact === "threatmodel.md")
- original = await readFile(artifactPath);
- await scenario.mutate(context);
- return {
- events: scenario.followUpEvents?.() ?? failedPostScanEvents(),
- };
- },
- }),
- }),
+ const client = TestClient.withDependencies({
+ prepareRuntime: async () => runtime,
+ resolvePluginPython: async () => python!,
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ prepareScanArtifactRestorer: async (...args) => {
+ const restorer = await prepareScanArtifactRestorer(...args);
+ return scenario.wrapRestorer?.(restorer, context) ?? restorer;
},
- );
+ createCodex: codexFactory(async (_input: string, options: TurnOptions) => {
+ expect(options.cyberAccessProgram).toBe("daybreak_blue");
+ turns += 1;
+ if (turns === 1) {
+ await copyCompletedScan(root);
+ if (scenario.initialContents !== undefined) {
+ await mkdir(dirname(artifactPath), { recursive: true });
+ await writeFile(artifactPath, scenario.initialContents);
+ const manifestPath = join(scanDir, "scan-manifest.json");
+ const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
+ if (scenario.threatModel !== undefined)
+ manifest.scan.threatModel = scenario.threatModel;
+ if (scenario.artifact !== "threatmodel.md")
+ manifest.scan.artifacts.push({
+ path: scenario.artifact,
+ sha256: hash("sha256", await readFile(artifactPath)),
+ mediaType: scenario.artifact.endsWith(".bin")
+ ? "application/octet-stream"
+ : "application/json",
+ });
+ await writeFile(manifestPath, JSON.stringify(manifest));
+ if (scenario.threatModel !== undefined)
+ await writeThreatModel(scanDir, {
+ pluginRoot: runtime.plugin.installedRoot,
+ pythonPath: python!,
+ protectedRoot: repository,
+ });
+ }
+ original = await readFile(artifactPath);
+ return { events: completedEvents() };
+ }
+ if (scenario.artifact === "threatmodel.md")
+ original = await readFile(artifactPath);
+ await scenario.mutate(context);
+ return {
+ events: scenario.followUpEvents?.() ?? failedPostScanEvents(),
+ };
+ }, "thread-1"),
+ });
const scan = client.run(repository, {
postScanPrompt: "Draft confirmed fixes.",
cyberAccessProgram: "daybreak_blue",
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index 3efcb70e1..edddd868d 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -143,62 +143,53 @@ test.each(["completed", "receipt-lost", "scan-interrupted", "prompt-files"])(
const makeClient = async (attempt: number) => {
const codexHome = join(root, `codex-home-${attempt}`);
await mkdir(codexHome);
- return new TestClient(
- {},
- {
- environment,
- ...scanRuntimeDependencies(codexHome, scanDir),
- runWorkbench: async (options, args, input) => {
- if (args[0] === "finding-workflow") {
- const payload = JSON.parse(input!);
- if (
- loseReceipt &&
- payload.action === "complete" &&
- payload.stage === "scan"
- ) {
- loseReceipt = false;
- throw new Error("Synthetic receipt write failure");
- }
- const state = await runWorkbench(options, args, input);
- if (scenario === "prompt-files" && payload.action === "begin")
- await rm(promptFile);
- return state;
- }
- if (args[0] === "get-scan")
- return {
- scan: {
- progress: { status: completed ? "complete" : "failed" },
- continuationThreadId: "thread-1",
- },
- };
- if (args[0] === "register-cli-scan") {
- expect(JSON.parse(input!).workflowId).toBe(workflowId);
- const registration = mockScanRegistration(args, input);
- await new FindingWorkflow(workflowId, environment).bind({
- scanId: registration["scanId"] as string,
- scanDir,
- });
- return registration;
+ return TestClient.withDependencies({
+ environment,
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: async (options, args, input) => {
+ if (args[0] === "finding-workflow") {
+ const payload = JSON.parse(input!);
+ if (
+ loseReceipt &&
+ payload.action === "complete" &&
+ payload.stage === "scan"
+ ) {
+ loseReceipt = false;
+ throw new Error("Synthetic receipt write failure");
}
- if (args[0] === "complete-scan") completed = true;
- return mockWorkbench(args, input);
- },
- createCodex: () => ({
- startThread: () => ({
- id: "thread-1",
- async runStreamed(input: string) {
- modelCalls++;
- if (scenario === "prompt-files")
- expect(input).toContain(scanPrompt);
- if (scenario === "scan-interrupted" && modelCalls === 1)
- throw new Error("Synthetic interrupted scan");
- await copyCompletedScan(root);
- return { events: completedEvents() };
+ const state = await runWorkbench(options, args, input);
+ if (scenario === "prompt-files" && payload.action === "begin")
+ await rm(promptFile);
+ return state;
+ }
+ if (args[0] === "get-scan")
+ return {
+ scan: {
+ progress: { status: completed ? "complete" : "failed" },
+ continuationThreadId: "thread-1",
},
- }),
- }),
+ };
+ if (args[0] === "register-cli-scan") {
+ expect(JSON.parse(input!).workflowId).toBe(workflowId);
+ const registration = mockScanRegistration(args, input);
+ await new FindingWorkflow(workflowId, environment).bind({
+ scanId: registration["scanId"] as string,
+ scanDir,
+ });
+ return registration;
+ }
+ if (args[0] === "complete-scan") completed = true;
+ return mockWorkbench(args, input);
},
- );
+ createCodex: codexFactory(async (input: string) => {
+ modelCalls++;
+ if (scenario === "prompt-files") expect(input).toContain(scanPrompt);
+ if (scenario === "scan-interrupted" && modelCalls === 1)
+ throw new Error("Synthetic interrupted scan");
+ await copyCompletedScan(root);
+ return { events: completedEvents() };
+ }, "thread-1"),
+ });
};
const first = await makeClient(1);
let original: Record<string, unknown> | undefined;
@@ -264,21 +255,18 @@ test.each(["ambient settings", "shipped defaults"])(
CODEX_HOME: codexHome,
CODEX_SECURITY_STATE_DIR: join(root, "state"),
};
- const client = new TestClient(
- {},
- {
- environment,
- runWorkbench: async (options, args, input) => {
- if (
- args[0] === "finding-workflow" &&
- JSON.parse(input!).action === "begin"
- ) {
- throw new Error("Synthetic stop before scan");
- }
- return runWorkbench(options, args, input);
- },
+ const client = TestClient.withDependencies({
+ environment,
+ runWorkbench: async (options, args, input) => {
+ if (
+ args[0] === "finding-workflow" &&
+ JSON.parse(input!).action === "begin"
+ ) {
+ throw new Error("Synthetic stop before scan");
+ }
+ return runWorkbench(options, args, input);
},
- );
+ });
const defaults = DEFAULT_DEEP_SCAN_SETTINGS as { workers: number };
const originalWorkers = defaults.workers;
const request = { workflowId: "deep-resume", mode: "deep" } as const;
@@ -581,7 +569,7 @@ describe("CodexSecurity finding validation", () => {
test("rejects invalid inputs, unsafe output, and cancellation before preparing credentials", async () => {
const repositoryPath = await temporaryDirectory();
const prepareRuntime = mock(async () => fail("runtime must not start"));
- await using client = new TestClient({}, { prepareRuntime });
+ await using client = TestClient.withDependencies({ prepareRuntime });
const options = { repositoryPath, finding: "Candidate" };
for (const finding of ["", " \n", null, []]) {
await expect(
@@ -666,22 +654,18 @@ describe("CodexSecurity orchestration", () => {
await mkdir(codexHome);
const runtimeEnvironment = { codex_safety_identifier: "ambient-user" };
const received: Array<CodexOptions> = [];
- const clients = [0, 1].map(
- () =>
- new TestClient(
- {},
- {
- environment: { OPENAI_API_KEY: "synthetic-key" },
- prepareRuntime: runtimePreparer(codexHome, () => ({
- environment: runtimeEnvironment,
- })),
- resolvePluginPython: async () => "/managed/python",
- createCodex: (options) => {
- received.push(options);
- throw new Error("captured scan");
- },
- },
- ),
+ const clients = [0, 1].map(() =>
+ TestClient.withDependencies({
+ environment: { OPENAI_API_KEY: "synthetic-key" },
+ prepareRuntime: runtimePreparer(codexHome, () => ({
+ environment: runtimeEnvironment,
+ })),
+ resolvePluginPython: async () => "/managed/python",
+ createCodex: (options) => {
+ received.push(options);
+ throw new Error("captured scan");
+ },
+ }),
);
try {
await Promise.all(
@@ -728,7 +712,7 @@ describe("CodexSecurity orchestration", () => {
test.each(["", " ", "a".repeat(65), "id\0suffix"])(
"rejects invalid safety identifier %j before preparing the runtime",
async (identifier) => {
- const client = new TestClient({}, {});
+ const client = TestClient.withDependencies({});
await expect(
client.run("/unused", { safetyIdentifier: identifier }),
).rejects.toThrow("safetyIdentifier must");
@@ -990,12 +974,9 @@ describe("CodexSecurity orchestration", () => {
const repository = join(root, "repository");
await mkdir(repository);
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ prepareRuntime,
+ });
await expect(
client.preflight(repository, { maxCostUsd: 5 }),
@@ -1041,12 +1022,9 @@ describe("CodexSecurity orchestration", () => {
const repository = join(root, "repository");
await mkdir(repository);
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ prepareRuntime,
+ });
await expect(
client.preflight(repository, {
@@ -1113,13 +1091,10 @@ describe("CodexSecurity orchestration", () => {
await writeFile(invalidDocument, "not a PDF");
await writeFile(unsupportedDocument, new Uint8Array([0, 1, 2]));
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- environment: { OPENAI_API_KEY: "must-not-be-used" },
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ environment: { OPENAI_API_KEY: "must-not-be-used" },
+ prepareRuntime,
+ });
await expect(
client.preflight(repository, { knowledgeBasePaths: [knowledgeBase] }),
@@ -1195,13 +1170,10 @@ describe("CodexSecurity orchestration", () => {
[{}, { method: "stored_credentials", verified: false }],
] as const) {
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- environment,
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ environment,
+ prepareRuntime,
+ });
const preflight = await client.preflight(repository);
const authentication: ScanAuthentication = preflight.authentication;
@@ -1218,16 +1190,13 @@ describe("CodexSecurity orchestration", () => {
const repository = join(root, "repository");
await mkdir(repository);
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- environment: {
- OPENAI_API_KEY: "synthetic-openai-key",
- CODEX_API_KEY: "synthetic-codex-key",
- },
- prepareRuntime,
+ const client = TestClient.withDependencies({
+ environment: {
+ OPENAI_API_KEY: "synthetic-openai-key",
+ CODEX_API_KEY: "synthetic-codex-key",
},
- );
+ prepareRuntime,
+ });
await expect(
client.preflight(repository, { auth: "chatgpt" }),
@@ -1270,14 +1239,11 @@ describe("CodexSecurity orchestration", () => {
const repository = join(root, "repository");
await mkdir(repository);
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- environment:
- field === "auth" ? { OPENAI_API_KEY: "synthetic-openai-key" } : {},
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ environment:
+ field === "auth" ? { OPENAI_API_KEY: "synthetic-openai-key" } : {},
+ prepareRuntime,
+ });
const options = { [field]: value } as unknown as ScanOptions;
await expect(client.preflight(repository, options)).rejects.toThrow(
@@ -1294,13 +1260,10 @@ describe("CodexSecurity orchestration", () => {
const repository = join(root, "repository");
await mkdir(repository);
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- environment: { OPENAI_API_KEY: " " },
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ environment: { OPENAI_API_KEY: " " },
+ prepareRuntime,
+ });
await expect(
client.preflight(repository, { auth: "api-key" }),
@@ -1320,29 +1283,26 @@ describe("CodexSecurity orchestration", () => {
let pythonEnvironment: Record<string, string | undefined> | undefined;
const onAuthentication =
mock<(authentication: ScanAuthentication) => void>();
- const client = new TestClient(
- {},
- {
+ const client = TestClient.withDependencies({
+ environment: {
+ OPENAI_API_KEY: "synthetic-openai-key",
+ Codex_Api_Key: "synthetic-codex-key",
+ },
+ prepareRuntime: runtimePreparer(codexHome, () => ({
environment: {
- OPENAI_API_KEY: "synthetic-openai-key",
- Codex_Api_Key: "synthetic-codex-key",
- },
- prepareRuntime: runtimePreparer(codexHome, () => ({
- environment: {
- CODEX_HOME: codexHome,
- OpenAi_Api_Key: "synthetic-forwarded-openai-key",
- codex_api_key: "synthetic-forwarded-codex-key",
- },
- })),
- resolvePluginPython: async (options) => {
- pythonEnvironment = options?.environment;
- return "/managed/python";
+ CODEX_HOME: codexHome,
+ OpenAi_Api_Key: "synthetic-forwarded-openai-key",
+ codex_api_key: "synthetic-forwarded-codex-key",
},
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
- createCodex,
+ })),
+ resolvePluginPython: async (options) => {
+ pythonEnvironment = options?.environment;
+ return "/managed/python";
},
- );
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ createCodex,
+ });
await expect(
client.run(repository, {
@@ -1667,22 +1627,15 @@ describe("CodexSecurity orchestration", () => {
prepareRuntime: runtimePreparer(codexHome, () => ({
deepScanConfigPath: join(codexHome, "deep-scan.toml"),
})),
- createCodex: () => ({
- startThread: () => ({
- id: null,
- async runStreamed(prompt) {
- expect(prompt).toContain(
- "Amazon Bedrock with AWS authentication",
- );
- expect(prompt).toContain(
- "Skip the ChatGPT account Daybreak access advisory",
- );
- expect(prompt).toContain(
- "Report any actual provider error unchanged",
- );
- throw new Error("Bedrock prompt captured");
- },
- }),
+ createCodex: codexFactory(async (prompt: string) => {
+ expect(prompt).toContain("Amazon Bedrock with AWS authentication");
+ expect(prompt).toContain(
+ "Skip the ChatGPT account Daybreak access advisory",
+ );
+ expect(prompt).toContain(
+ "Report any actual provider error unchanged",
+ );
+ throw new Error("Bedrock prompt captured");
}),
},
);
@@ -1893,16 +1846,13 @@ describe("CodexSecurity orchestration", () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
await mkdir(repository);
- const client = new TestClient(
- {},
- {
- environment: {
- AWS_BEARER_TOKEN_BEDROCK: "synthetic-bedrock-bearer",
- AWS_ACCESS_KEY_ID: "synthetic-aws-access-key",
- AWS_SECRET_ACCESS_KEY: "synthetic-aws-secret-key",
- },
+ const client = TestClient.withDependencies({
+ environment: {
+ AWS_BEARER_TOKEN_BEDROCK: "synthetic-bedrock-bearer",
+ AWS_ACCESS_KEY_ID: "synthetic-aws-access-key",
+ AWS_SECRET_ACCESS_KEY: "synthetic-aws-secret-key",
},
- );
+ });
expect((await client.preflight(repository)).authentication).toEqual({
method: "stored_credentials",
@@ -1914,15 +1864,12 @@ describe("CodexSecurity orchestration", () => {
test("isolates authentication observer failures from scan startup", async () => {
const { root, repository, codexHome } = await runtimeDirectories();
const observerErrors: Array<[ScanObserverName, string]> = [];
- const client = new TestClient(
- {},
- {
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- repositoryRevision: async () => null,
- createCodex: codexFactory(scanDidNotStart),
- },
- );
+ const client = TestClient.withDependencies({
+ prepareRuntime: async () => preparedRuntime(codexHome),
+ resolvePluginPython: async () => "/managed/python",
+ repositoryRevision: async () => null,
+ createCodex: codexFactory(scanDidNotStart),
+ });
await expect(
client.run(repository, {
@@ -1984,15 +1931,12 @@ describe("CodexSecurity orchestration", () => {
);
const onAuthentication =
mock<(authentication: ScanAuthentication) => void>();
- const client = new TestClient(
- {},
- {
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- repositoryRevision: async () => null,
- createCodex: codexFactory(scanDidNotStart),
- },
- );
+ const client = TestClient.withDependencies({
+ prepareRuntime: async () => preparedRuntime(codexHome),
+ resolvePluginPython: async () => "/managed/python",
+ repositoryRevision: async () => null,
+ createCodex: codexFactory(scanDidNotStart),
+ });
await expect(
client.run(repository, {
@@ -2026,12 +1970,9 @@ describe("CodexSecurity orchestration", () => {
await mkdir(output, { mode: 0o700 });
await writeFile(join(output, "previous.txt"), "previous scan\n");
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ prepareRuntime,
+ });
const preflight = await client.preflight(repository, {
outputDir: output,
@@ -2072,25 +2013,21 @@ describe("CodexSecurity orchestration", () => {
let archived: string | undefined;
let registration: readonly string[] | undefined;
const observerErrors: Array<[ScanObserverName, string]> = [];
- const client = new TestClient(
- {},
- {
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- repositoryRevision: async () => null,
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- if (args[0] !== "register-cli-scan")
- return mockWorkbench(args, input);
- registration = args;
- return mockScanRegistration(args, input);
- },
- createCodex: codexFactory(scanDidNotStart),
+ const client = TestClient.withDependencies({
+ prepareRuntime: async () => preparedRuntime(codexHome),
+ resolvePluginPython: async () => "/managed/python",
+ repositoryRevision: async () => null,
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ if (args[0] !== "register-cli-scan") return mockWorkbench(args, input);
+ registration = args;
+ return mockScanRegistration(args, input);
},
- );
+ createCodex: codexFactory(scanDidNotStart),
+ });
await expect(
client.run(repository, {
@@ -2126,30 +2063,27 @@ describe("CodexSecurity orchestration", () => {
await mkdir(codexHome);
let failedCleanupPath: string | undefined;
const warnings = mock((_warning: string) => {});
- const client = new TestClient(
- {},
- {
- environment: { OPENAI_API_KEY: "test-key" },
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- repositoryRevision: async () => "deadbeef",
- createCodex: (options: CodexOptions) => ({
- startThread: () => ({
- id: null,
- async runStreamed() {
- // Cleanup removes the target paths file with a non-recursive rm, so
- // replacing that file with a directory makes cleanup reject on every
- // platform.
- failedCleanupPath =
- options.env?.["CODEX_SECURITY_TARGET_PATHS_FILE"];
- await rm(failedCleanupPath!, { force: true });
- await mkdir(failedCleanupPath!);
- throw new Error("the model refused the scan");
- },
- }),
+ const client = TestClient.withDependencies({
+ environment: { OPENAI_API_KEY: "test-key" },
+ prepareRuntime: async () => preparedRuntime(codexHome),
+ resolvePluginPython: async () => "/managed/python",
+ repositoryRevision: async () => "deadbeef",
+ createCodex: (options: CodexOptions) => ({
+ startThread: () => ({
+ id: null,
+ async runStreamed() {
+ // Cleanup removes the target paths file with a non-recursive rm, so
+ // replacing that file with a directory makes cleanup reject on every
+ // platform.
+ failedCleanupPath =
+ options.env?.["CODEX_SECURITY_TARGET_PATHS_FILE"];
+ await rm(failedCleanupPath!, { force: true });
+ await mkdir(failedCleanupPath!);
+ throw new Error("the model refused the scan");
+ },
}),
- },
- );
+ }),
+ });
await expect(
client.run(repository, {
@@ -2175,12 +2109,9 @@ describe("CodexSecurity orchestration", () => {
await mkdir(repository);
await writeFile(join(repository, "preserved.txt"), "preserved\n");
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ prepareRuntime,
+ });
await expect(
client.run(repository, { outputDir: join(repository, "scan") }),
@@ -2219,12 +2150,9 @@ describe("CodexSecurity orchestration", () => {
const repository = join(root, "repository");
await mkdir(repository);
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ prepareRuntime,
+ });
for (const separator of ["\n", "\u0085", "\u2028", "\u2029"]) {
await expect(
@@ -2273,12 +2201,9 @@ describe("CodexSecurity orchestration", () => {
const output = join(worktree, "scan");
await mkdir(repository, { recursive: true });
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ prepareRuntime,
+ });
await expect(
client.run(repository, { outputDir: output }),
@@ -2303,12 +2228,9 @@ describe("CodexSecurity orchestration", () => {
const previous = process.env[temporaryVariable];
process.env[temporaryVariable] = temporaryRoot;
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ prepareRuntime,
+ });
try {
await expect(client.run(repository)).rejects.toMatchObject({
@@ -2339,13 +2261,10 @@ describe("CodexSecurity orchestration", () => {
"GIT_REPLACE_REF_BASE",
]) {
const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {},
- {
- environment: { [name.toLowerCase()]: join(root, "override") },
- prepareRuntime,
- },
- );
+ const client = TestClient.withDependencies({
+ environment: { [name.toLowerCase()]: join(root, "override") },
+ prepareRuntime,
+ });
await expect(client.preflight(repository)).rejects.toThrow(
`${name.toLowerCase()} is not supported`,
@@ -2419,18 +2338,15 @@ describe("CodexSecurity orchestration", () => {
await mkdir(codexHome);
const originalCwd = process.cwd();
process.chdir(initial);
- const client = new TestClient(
- {},
- {
- prepareRuntime: async () => {
- process.chdir(elsewhere);
- return preparedRuntime(codexHome);
- },
- resolvePluginPython: async () => "/managed/python",
- repositoryRevision: async () => null,
- createCodex: () => fail("Codex reached"),
+ const client = TestClient.withDependencies({
+ prepareRuntime: async () => {
+ process.chdir(elsewhere);
+ return preparedRuntime(codexHome);
},
- );
+ resolvePluginPython: async () => "/managed/python",
+ repositoryRevision: async () => null,
+ createCodex: () => fail("Codex reached"),
+ });
try {
await expect(
@@ -2695,32 +2611,29 @@ describe("CodexSecurity orchestration", () => {
};
});
- const client = new TestClient(
- {},
- {
- ...scanRuntimeDependencies(codexHome, scanDir),
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- if (args[0] === "register-cli-scan") {
- return {
- ...mockScanRegistration(args, input),
- targetRevision: "cafebabe",
- contract: {
- target: {
- allowedKinds: ["git_worktree"],
- requiredSnapshotDigest: TEST_SNAPSHOT_DIGEST,
- },
+ const client = TestClient.withDependencies({
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ if (args[0] === "register-cli-scan") {
+ return {
+ ...mockScanRegistration(args, input),
+ targetRevision: "cafebabe",
+ contract: {
+ target: {
+ allowedKinds: ["git_worktree"],
+ requiredSnapshotDigest: TEST_SNAPSHOT_DIGEST,
},
- };
- }
- return mockWorkbench(args, input);
- },
- createCodex,
+ },
+ };
+ }
+ return mockWorkbench(args, input);
},
- );
+ createCodex,
+ });
await expect(client.run(repository)).rejects.toThrow(
"target contract captured",
@@ -2758,39 +2671,36 @@ describe("CodexSecurity orchestration", () => {
].join("\n"),
);
let recipe: Record<string, unknown> | undefined;
- const client = new TestClient(
- {},
- {
- environment: { CODEX_HOME: ambientHome },
- prepareRuntime: async () => {
- await writeFile(
- join(ambientHome, "codex-security", "config.toml"),
- "[deep_scan]\nstop_after_no_new = 99\n",
- );
- return preparedRuntime(codexHome);
- },
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- if (args[0] !== "register-cli-scan") {
- return {
- scanId: "scan_example_001",
- targetId: "target_sha256_example",
- falsePositives: [],
- };
- }
- expect(JSON.parse(input!).userContext).toBeUndefined();
- recipe = JSON.parse(input!).recipe;
- return mockScanRegistration(args, input);
- },
- createCodex: codexFactory(deepSettingsCaptured),
+ const client = TestClient.withDependencies({
+ environment: { CODEX_HOME: ambientHome },
+ prepareRuntime: async () => {
+ await writeFile(
+ join(ambientHome, "codex-security", "config.toml"),
+ "[deep_scan]\nstop_after_no_new = 99\n",
+ );
+ return preparedRuntime(codexHome);
},
- );
+ resolvePluginPython: async () => "/managed/python",
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ if (args[0] !== "register-cli-scan") {
+ return {
+ scanId: "scan_example_001",
+ targetId: "target_sha256_example",
+ falsePositives: [],
+ };
+ }
+ expect(JSON.parse(input!).userContext).toBeUndefined();
+ recipe = JSON.parse(input!).recipe;
+ return mockScanRegistration(args, input);
+ },
+ createCodex: codexFactory(deepSettingsCaptured),
+ });
await expect(
client.run(repository, {
@@ -2835,46 +2745,38 @@ describe("CodexSecurity orchestration", () => {
const { repository, codexHome, scanDir } = await scanDirectories();
const updates: DeepScanProgress[] = [];
const environment = { CODEX_CLI_PATH: process.execPath };
- const client = new TestClient(
- {},
- {
- environment,
- prepareRuntime: runtimePreparer(codexHome, () => ({ environment })),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
- resolveCodexCommand: () => ({ command: process.execPath }),
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- if (args[0] === "get-scan") {
- return {
- scan: {
- progress: {
- independentReviews: {
- completed: 3,
- active: 2,
- maximum: 40,
- },
+ const client = TestClient.withDependencies({
+ environment,
+ prepareRuntime: runtimePreparer(codexHome, () => ({ environment })),
+ resolvePluginPython: async () => "/managed/python",
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ resolveCodexCommand: () => ({ command: process.execPath }),
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ if (args[0] === "get-scan") {
+ return {
+ scan: {
+ progress: {
+ independentReviews: {
+ completed: 3,
+ active: 2,
+ maximum: 40,
},
},
- };
- }
- return mockWorkbench(args, input);
- },
- createCodex: () => ({
- startThread: () => ({
- id: null,
- async runStreamed() {
- await Bun.sleep(0);
- throw new Error("deep progress captured");
},
- }),
- }),
+ };
+ }
+ return mockWorkbench(args, input);
},
- );
+ createCodex: codexFactory(async () => {
+ await Bun.sleep(0);
+ throw new Error("deep progress captured");
+ }),
+ });
await expect(
client.run(repository, {
@@ -2903,17 +2805,14 @@ describe("CodexSecurity orchestration", () => {
join(ambientHome, "codex-security", "config.toml"),
"[deep_scan]\nworkers = 5\n",
);
- const client = new TestClient(
- {},
- {
- environment: {
- CODEX_HOME: "~\\ambient-home",
- USERPROFILE: root,
- },
- ...scanRuntimeDependencies(codexHome, scanDir),
- createCodex: codexFactory(deepSettingsCaptured),
+ const client = TestClient.withDependencies({
+ environment: {
+ CODEX_HOME: "~\\ambient-home",
+ USERPROFILE: root,
},
- );
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ createCodex: codexFactory(deepSettingsCaptured),
+ });
await expect(client.run(repository, { mode: "deep" })).rejects.toThrow(
"deep scan settings captured",
@@ -2957,14 +2856,11 @@ describe("CodexSecurity orchestration", () => {
"[deep_scan]\nworkers = 5\n[other]\nenabled = true\n",
);
- const client = new TestClient(
- {},
- {
- environment: { CODEX_HOME: ambientHome },
- ...scanRuntimeDependencies(runtimeHome, scanDir),
- createCodex: codexFactory(deepSettingsCaptured),
- },
- );
+ const client = TestClient.withDependencies({
+ environment: { CODEX_HOME: ambientHome },
+ ...scanRuntimeDependencies(runtimeHome, scanDir),
+ createCodex: codexFactory(deepSettingsCaptured),
+ });
await expect(
client.run(repository, { mode: "deep", workers: 2 }),
@@ -3032,19 +2928,16 @@ describe("CodexSecurity orchestration", () => {
await writeFile(configPath, originalConfiguration);
await mkdir(scanDir, { mode: 0o700 });
- await using client = new TestClient(
- {},
- {
- environment: {
- CODEX_HOME:
- homeKind === "relative"
- ? relative(process.cwd(), codexHome)
- : codexHome,
- },
- ...scanRuntimeDependencies(codexHome, scanDir),
- createCodex: codexFactory(deepSettingsCaptured),
+ await using client = TestClient.withDependencies({
+ environment: {
+ CODEX_HOME:
+ homeKind === "relative"
+ ? relative(process.cwd(), codexHome)
+ : codexHome,
},
- );
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ createCodex: codexFactory(deepSettingsCaptured),
+ });
await expect(
client.run(repository, {
@@ -3109,14 +3002,11 @@ describe("CodexSecurity orchestration", () => {
await writeFile(configPath, originalConfiguration);
await mkdir(scanDir, { mode: 0o700 });
- await using client = new TestClient(
- {},
- {
- environment: { CODEX_HOME: codexHome.toUpperCase() },
- ...scanRuntimeDependencies(codexHome, scanDir),
- createCodex: codexFactory(deepSettingsCaptured),
- },
- );
+ await using client = TestClient.withDependencies({
+ environment: { CODEX_HOME: codexHome.toUpperCase() },
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ createCodex: codexFactory(deepSettingsCaptured),
+ });
await expect(client.run(repository, { mode: "deep" })).rejects.toThrow(
"deep scan settings captured",
@@ -3134,24 +3024,21 @@ describe("CodexSecurity orchestration", () => {
test("rejects a scan registration without an authoritative target contract", async () => {
const { repository, codexHome, scanDir } = await scanDirectories();
- const client = new TestClient(
- {},
- {
- ...scanRuntimeDependencies(codexHome, scanDir),
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> =>
- args[0] === "register-cli-scan"
- ? {
- ...mockScanRegistration(args, input),
- contract: { target: { allowedKinds: [] } },
- }
- : {},
- createCodex: codexMustNotStart,
- },
- );
+ const client = TestClient.withDependencies({
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> =>
+ args[0] === "register-cli-scan"
+ ? {
+ ...mockScanRegistration(args, input),
+ contract: { target: { allowedKinds: [] } },
+ }
+ : {},
+ createCodex: codexMustNotStart,
+ });
await expect(client.run(repository)).rejects.toThrow(
"invalid scan registration",
@@ -3163,24 +3050,21 @@ describe("CodexSecurity orchestration", () => {
const { root, repository, codexHome, scanDir } = await scanDirectories();
const commands: string[] = [];
- const client = new TestClient(
- {},
- {
- ...scanRuntimeDependencies(codexHome, scanDir),
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- commands.push(args[0]!);
- if (args[0] === "prepare-scan-completion") {
- await writeFile(join(scanDir, "findings.json"), "corrupted\n");
- }
- return mockWorkbench(args, input);
- },
- createCodex: completedCodex(root),
+ const client = TestClient.withDependencies({
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ commands.push(args[0]!);
+ if (args[0] === "prepare-scan-completion") {
+ await writeFile(join(scanDir, "findings.json"), "corrupted\n");
+ }
+ return mockWorkbench(args, input);
},
- );
+ createCodex: completedCodex(root),
+ });
await expect(client.run(repository)).rejects.toThrow();
expect(commands).toEqual([
@@ -3199,32 +3083,29 @@ describe("CodexSecurity orchestration", () => {
const terminalFailure =
"Deep Scan reached its discovery limit after worker policy refusals.";
- const client = new TestClient(
- {},
- {
- ...scanRuntimeDependencies(codexHome, scanDir),
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- commands.push(args[0]!);
- if (args[0] === "prepare-scan-completion") {
- throw new Error("Only a running scan can be completed.");
- }
- if (args[0] === "get-scan") {
- return {
- scan: {
- failureMessage: terminalFailure,
- progress: { status: "failed" },
- },
- };
- }
- return mockWorkbench(args, input);
- },
- createCodex: completedCodex(root),
+ const client = TestClient.withDependencies({
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ commands.push(args[0]!);
+ if (args[0] === "prepare-scan-completion") {
+ throw new Error("Only a running scan can be completed.");
+ }
+ if (args[0] === "get-scan") {
+ return {
+ scan: {
+ failureMessage: terminalFailure,
+ progress: { status: "failed" },
+ },
+ };
+ }
+ return mockWorkbench(args, input);
},
- );
+ createCodex: completedCodex(root),
+ });
await expect(client.run(repository, { mode: "deep" })).rejects.toThrow(
terminalFailure,
@@ -3244,21 +3125,18 @@ describe("CodexSecurity orchestration", () => {
await writeFile(join(codexHome, "sessions"), "not a directory");
const commands: string[] = [];
const warnings: string[] = [];
- const client = new TestClient(
- {},
- {
- ...scanRuntimeDependencies(codexHome, scanDir),
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- commands.push(args[0]!);
- return mockWorkbench(args, input);
- },
- createCodex: completedCodex(root),
+ const client = TestClient.withDependencies({
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ commands.push(args[0]!);
+ return mockWorkbench(args, input);
},
- );
+ createCodex: completedCodex(root),
+ });
const scan = client.run(repository, {
...(enforceCostLimit ? { maxCostUsd: 1 } : {}),
@@ -3355,78 +3233,70 @@ describe("CodexSecurity orchestration", () => {
const workers: ScanWorkerEvent[] = [];
const observerErrors: ScanObserverName[] = [];
const usage = { input_tokens: 100, output_tokens: 10 };
- const client = new TestClient(
- {},
- {
- ...scanRuntimeDependencies(codexHome, scanDir),
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> =>
- args[0] === "register-cli-scan"
- ? { ...mockScanRegistration(args, input), scopeFileCount: 1_258 }
- : mockWorkbench(args, input),
- createCodex: () => ({
- startThread: () => ({
- id: null,
- async runStreamed() {
- await copyCompletedScan(root);
- await Promise.all([
- writeUsageSession(codexHome, "thread-1", usage),
- writeUsageSession(codexHome, "worker-thread", usage, {
- parent: "thread-1",
- parentField: "parent_thread_id",
- }),
- writeUsageSession(codexHome, "unrelated-thread", usage),
- ]);
- const marker = (
- phase: ScanProgress["phase"],
- filesCompleted: number,
- filesTotal: number,
- ): string =>
- `CODEX_SECURITY_SCAN_PROGRESS ${JSON.stringify({
- phase,
- filesCompleted,
- filesTotal,
- })}`;
- for (const [threadId, text] of [
- ["worker-thread", marker("discovery", 3, 1_249)],
- ["worker-thread", marker("discovery", 2, 2)],
- ["worker-thread", marker("discovery", 7, 1_259)],
- ["unrelated-thread", marker("discovery", 7, 1_258)],
- ["worker-thread", marker("discovery", 1_250, 1_249)],
- ["worker-thread", marker("discovery", 1_249, 1_249)],
- ["worker-thread", marker("validation", 1_249, 1_249)],
- ] as const) {
- await appendFile(
- join(
- codexHome,
- "sessions",
- "2026",
- "07",
- "26",
- `rollout-${threadId}.jsonl`,
- ),
- `${JSON.stringify({
- type: "response_item",
- payload: {
- type: "custom_tool_call_output",
- status: "completed",
- output: [
- { type: "input_text", text: "Reviewed file batch." },
- { type: "input_text", text },
- ],
- },
- })}\n`,
- );
- }
- return { events: completedEvents() };
- },
+ const client = TestClient.withDependencies({
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> =>
+ args[0] === "register-cli-scan"
+ ? { ...mockScanRegistration(args, input), scopeFileCount: 1_258 }
+ : mockWorkbench(args, input),
+ createCodex: codexFactory(async () => {
+ await copyCompletedScan(root);
+ await Promise.all([
+ writeUsageSession(codexHome, "thread-1", usage),
+ writeUsageSession(codexHome, "worker-thread", usage, {
+ parent: "thread-1",
+ parentField: "parent_thread_id",
}),
- }),
- },
- );
+ writeUsageSession(codexHome, "unrelated-thread", usage),
+ ]);
+ const marker = (
+ phase: ScanProgress["phase"],
+ filesCompleted: number,
+ filesTotal: number,
+ ): string =>
+ `CODEX_SECURITY_SCAN_PROGRESS ${JSON.stringify({
+ phase,
+ filesCompleted,
+ filesTotal,
+ })}`;
+ for (const [threadId, text] of [
+ ["worker-thread", marker("discovery", 3, 1_249)],
+ ["worker-thread", marker("discovery", 2, 2)],
+ ["worker-thread", marker("discovery", 7, 1_259)],
+ ["unrelated-thread", marker("discovery", 7, 1_258)],
+ ["worker-thread", marker("discovery", 1_250, 1_249)],
+ ["worker-thread", marker("discovery", 1_249, 1_249)],
+ ["worker-thread", marker("validation", 1_249, 1_249)],
+ ] as const) {
+ await appendFile(
+ join(
+ codexHome,
+ "sessions",
+ "2026",
+ "07",
+ "26",
+ `rollout-${threadId}.jsonl`,
+ ),
+ `${JSON.stringify({
+ type: "response_item",
+ payload: {
+ type: "custom_tool_call_output",
+ status: "completed",
+ output: [
+ { type: "input_text", text: "Reviewed file batch." },
+ { type: "input_text", text },
+ ],
+ },
+ })}\n`,
+ );
+ }
+ return { events: completedEvents() };
+ }),
+ });
const result = await client.run(repository, {
onProgress: (progress) => updates.push(progress),
@@ -3561,44 +3431,36 @@ describe("CodexSecurity orchestration", () => {
const commands: Array<readonly string[]> = [];
let prompt = "";
let feedback = "";
- const client = new TestClient(
- {},
- {
- ...scanRuntimeDependencies(codexHome, scanDir),
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- commands.push(args);
- if (args[0] === "get-scan-feedback") {
- return {
- scanId: "scan_example_001",
- targetId: "target_sha256_example",
- falsePositives: [falsePositive],
- };
- }
- if (args[0] === "list-global-findings") {
- return args.includes("--offset")
- ? { findings: [{ findingId: "second" }], nextOffset: null }
- : { findings: [previousFinding], nextOffset: 1 };
- }
- return mockWorkbench(args, input);
- },
- createCodex: () => ({
- startThread: () => ({
- id: null,
- async runStreamed(input: string) {
- prompt = input;
- feedback = await readFile(feedbackPath, "utf8");
- await expect(readFile(previousFindingsPath)).rejects.toThrow();
- await copyCompletedScan(root);
- return { events: completedEvents() };
- },
- }),
- }),
+ const client = TestClient.withDependencies({
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ commands.push(args);
+ if (args[0] === "get-scan-feedback") {
+ return {
+ scanId: "scan_example_001",
+ targetId: "target_sha256_example",
+ falsePositives: [falsePositive],
+ };
+ }
+ if (args[0] === "list-global-findings") {
+ return args.includes("--offset")
+ ? { findings: [{ findingId: "second" }], nextOffset: null }
+ : { findings: [previousFinding], nextOffset: 1 };
+ }
+ return mockWorkbench(args, input);
},
- );
+ createCodex: codexFactory(async (input: string) => {
+ prompt = input;
+ feedback = await readFile(feedbackPath, "utf8");
+ await expect(readFile(previousFindingsPath)).rejects.toThrow();
+ await copyCompletedScan(root);
+ return { events: completedEvents() };
+ }),
+ });
const result = await client.run(repository);
expect(result.threadId).toBe("thread-1");
@@ -3658,33 +3520,30 @@ describe("CodexSecurity orchestration", () => {
await mkdir(scanDir, { mode: 0o700 });
const runtime = preparedRuntime(codexHome);
const commands: string[] = [];
- const client = new TestClient(
- {},
- {
- prepareRuntime: async () => ({
- ...runtime,
- plugin: {
- ...runtime.plugin,
- pluginRoot,
- marketplaceRoot: pluginRoot,
- installedRoot: pluginRoot,
- },
- }),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- commands.push(args[0]!);
- return args[0] === "register-cli-scan"
- ? mockScanRegistration(args, input)
- : {};
+ const client = TestClient.withDependencies({
+ prepareRuntime: async () => ({
+ ...runtime,
+ plugin: {
+ ...runtime.plugin,
+ pluginRoot,
+ marketplaceRoot: pluginRoot,
+ installedRoot: pluginRoot,
},
+ }),
+ resolvePluginPython: async () => "/managed/python",
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ commands.push(args[0]!);
+ return args[0] === "register-cli-scan"
+ ? mockScanRegistration(args, input)
+ : {};
},
- );
+ });
await expect(client.run(repository)).rejects.toThrow(
"Installed plugin is missing scan skill: security-scan",
@@ -3710,40 +3569,32 @@ describe("CodexSecurity orchestration", () => {
await mkdir(codexHome);
await mkdir(scanDir, { mode: 0o700 });
let prompt = "";
- const client = new TestClient(
- {},
- {
- ...scanRuntimeDependencies(codexHome, scanDir),
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- if (args[0] === "register-cli-scan") {
- return { ...mockScanRegistration(args, input), scanId };
- }
- if (args[0] === "get-scan-feedback") {
- return {
- scanId,
- targetId: "target_sha256_example",
- falsePositives: withFeedback
- ? [{ reason: "The finding is no longer reproducible." }]
- : [],
- };
- }
- return {};
- },
- createCodex: () => ({
- startThread: () => ({
- id: null,
- async runStreamed(input: string) {
- prompt = input;
- throw new Error("prompt captured");
- },
- }),
- }),
+ const client = TestClient.withDependencies({
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ if (args[0] === "register-cli-scan") {
+ return { ...mockScanRegistration(args, input), scanId };
+ }
+ if (args[0] === "get-scan-feedback") {
+ return {
+ scanId,
+ targetId: "target_sha256_example",
+ falsePositives: withFeedback
+ ? [{ reason: "The finding is no longer reproducible." }]
+ : [],
+ };
+ }
+ return {};
},
- );
+ createCodex: codexFactory(async (input: string) => {
+ prompt = input;
+ throw new Error("prompt captured");
+ }),
+ });
await expect(client.run(repository, { mode })).rejects.toThrow(
"prompt captured",
@@ -4213,22 +4064,19 @@ describe("CodexSecurity orchestration", () => {
for (const feedback of invalidFeedback) {
const { repository, codexHome, scanDir } = await scanDirectories();
- const client = new TestClient(
- {},
- {
- ...scanRuntimeDependencies(codexHome, scanDir),
- runWorkbench: async (
- _options: unknown,
- args: readonly string[],
- input?: string,
- ): Promise<JsonObject> => {
- return args[0] === "get-scan-feedback"
- ? feedback
- : mockWorkbench(args, input);
- },
- createCodex: () => fail("Invalid feedback must not start Codex."),
+ const client = TestClient.withDependencies({
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ): Promise<JsonObject> => {
+ return args[0] === "get-scan-feedback"
+ ? feedback
+ : mockWorkbench(args, input);
},
- );
+ createCodex: () => fail("Invalid feedback must not start Codex."),
+ });
await expect(client.run(repository)).rejects.toThrow(
"invalid false-positive feedback",
diff --git a/sdk/typescript/tests-ts/support/api-client.ts b/sdk/typescript/tests-ts/support/api-client.ts
index c2cfe732a..94cfae4d8 100644
--- a/sdk/typescript/tests-ts/support/api-client.ts
+++ b/sdk/typescript/tests-ts/support/api-client.ts
@@ -58,6 +58,10 @@ export function mockWorkbench(
}
export class TestClient extends CodexSecurity {
+ static withDependencies(dependencies: Partial<ClientArguments[1]>) {
+ return new TestClient({}, dependencies);
+ }
+
public constructor(
config: ClientArguments[0],
dependencies: Partial<ClientArguments[1]>,
diff --git a/sdk/typescript/tests-ts/support/api-events.ts b/sdk/typescript/tests-ts/support/api-events.ts
index f22b01223..1a27f25a0 100644
--- a/sdk/typescript/tests-ts/support/api-events.ts
+++ b/sdk/typescript/tests-ts/support/api-events.ts
@@ -50,13 +50,17 @@ type ScanEventOptions = Omit<
export async function* completedEvents(
threadId = "thread-1",
+ events?: AsyncIterable<ThreadEvent>,
): AsyncGenerator<ThreadEvent> {
yield { type: "thread.started", thread_id: threadId };
yield { type: "turn.started" };
- yield {
- type: "item.completed",
- item: { id: "message-1", type: "agent_message", text: "scan complete" },
- };
+ if (events) yield* events;
+ else {
+ yield {
+ type: "item.completed",
+ item: { id: "message-1", type: "agent_message", text: "scan complete" },
+ };
+ }
yield {
type: "turn.completed",
usage: {
@@ -118,9 +122,12 @@ export function collectObserverErrors(errors: [ScanObserverName, string][]) {
};
}
-export function codexFactory<Run>(runStreamed: Run) {
+export function codexFactory<Run>(
+ runStreamed: Run,
+ threadId: string | null = null,
+) {
return () => ({
- startThread: () => ({ id: null, runStreamed }),
+ startThread: () => ({ id: threadId, runStreamed }),
});
}