OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
test(sdk): share setup for scan budgets and cleanup (#1376)
mldangelo-oai8c67135bd0e3
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.tsindex edddd868d..1142ba24c 100644--- a/sdk/typescript/tests-ts/api.test.ts+++ b/sdk/typescript/tests-ts/api.test.ts@@ -4161,35 +4161,27 @@ describe("CodexSecurity orchestration", () => {const warnings: string[] = [];let turns = 0;- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- ...(setupFails- ? {- prepareScanArtifactRestorer: async () => fail(failureMessage),- }- : {}),- runWorkbench: recordingWorkbench(commands),- createCodex: () => ({- startThread: () => ({- id: "thread-1",- async runStreamed() {- turns += 1;- if (turns === 1) {- await copyCompletedScan(root);- return { events: completedEvents() };- }- if (setupFails) {- throw new Error("post-scan turn started after setup failed");- }+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ ...(setupFails+ ? {+ prepareScanArtifactRestorer: async () => fail(failureMessage),+ }+ : {}),+ runWorkbench: recordingWorkbench(commands),+ createCodex: codexFactory(async () => {+ turns += 1;+ if (turns === 1) {+ await copyCompletedScan(root);+ return { events: completedEvents() };+ }+ if (setupFails) {+ throw new Error("post-scan turn started after setup failed");+ }- return { events: failedEvents() };- },- }),- }),- },- );+ return { events: failedEvents() };+ }, "thread-1"),+ });await expect(client.run(repository, {@@ -4230,63 +4222,55 @@ describe("CodexSecurity orchestration", () => {const scanFails = outcome === "failed";const controller = new AbortController();- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- createCodex: () => ({- startThread: () => ({- id: "thread-1",- async runStreamed(prompt: string) {- prompts.push(prompt);- await writeUsageSession(codexHome, "thread-1", {});- await writeUsageSession(- codexHome,- `worker-${prompts.length}`,- {},- { parent: "thread-1" },- );- if (prompts.length === 1 && !scanFails) {- await copyCompletedScan(root);- const coveragePath = join(scanDir, "coverage.json");- const original = await readFile(coveragePath, "utf8");- const coverage = original.replace(- '"completeness": "complete"',- `"completeness": "${outcome}"`,- );- const manifestPath = join(scanDir, "scan-manifest.json");- await writeFile(coveragePath, coverage);- await writeFile(- manifestPath,- (await readFile(manifestPath, "utf8")).replace(- hash("sha256", original),- hash("sha256", coverage),- ),- );- return { events: completedEvents() };- }- if (prompts.length === 2 && cancelFollowUp) controller.abort();- if (prompts.length === 2 && !followUpFails) {- return { events: completedEvents() };- }- async function* failedEvents(): AsyncGenerator<ThreadEvent> {- yield { type: "thread.started", thread_id: "thread-1" };- yield {- type: "turn.failed",- error: {- message:- prompts.length === 1- ? "The scan failed."- : "The post-scan instructions failed.",- },- };- }- return { events: failedEvents() };+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ createCodex: codexFactory(async (prompt: string) => {+ prompts.push(prompt);+ await writeUsageSession(codexHome, "thread-1", {});+ await writeUsageSession(+ codexHome,+ `worker-${prompts.length}`,+ {},+ { parent: "thread-1" },+ );+ if (prompts.length === 1 && !scanFails) {+ await copyCompletedScan(root);+ const coveragePath = join(scanDir, "coverage.json");+ const original = await readFile(coveragePath, "utf8");+ const coverage = original.replace(+ '"completeness": "complete"',+ `"completeness": "${outcome}"`,+ );+ const manifestPath = join(scanDir, "scan-manifest.json");+ await writeFile(coveragePath, coverage);+ await writeFile(+ manifestPath,+ (await readFile(manifestPath, "utf8")).replace(+ hash("sha256", original),+ hash("sha256", coverage),+ ),+ );+ return { events: completedEvents() };+ }+ if (prompts.length === 2 && cancelFollowUp) controller.abort();+ if (prompts.length === 2 && !followUpFails) {+ return { events: completedEvents() };+ }+ async function* failedEvents(): AsyncGenerator<ThreadEvent> {+ yield { type: "thread.started", thread_id: "thread-1" };+ yield {+ type: "turn.failed",+ error: {+ message:+ prompts.length === 1+ ? "The scan failed."+ : "The post-scan instructions failed.",},- }),- }),- },- );+ };+ }+ return { events: failedEvents() };+ }, "thread-1"),+ });const result = client.run(repository, {postScanPrompt: "Record the scan cost.",@@ -4333,45 +4317,30 @@ describe("CodexSecurity orchestration", () => {let originalFindings: string;let turns = 0;let closing: Promise<void> | undefined;- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- prepareScanArtifactRestorer: async () => ({- restore: async (name, contents) => {- if (scenario === "restore failure")- throw new Error("write failed");- await writeFile(join(scanDir, name), contents);- },- }),- createCodex: () => ({- startThread: () => ({- id: "thread-1",- async runStreamed() {- if (++turns === 1) {- await copyCompletedScan(root);- originalFindings = await readFile(- join(scanDir, "findings.json"),- "utf8",- );- } else {- await writeFile(- join(scanDir, "findings.json"),- '{"unfinished":',- );- await writeFile(- join(scanDir, "report.md"),- "unfinished report",- );- if (scenario === "close") closing = client.close();- else controller.abort();- }- return { events: completedEvents() };- },- }),- }),- },- );+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ prepareScanArtifactRestorer: async () => ({+ restore: async (name, contents) => {+ if (scenario === "restore failure") throw new Error("write failed");+ await writeFile(join(scanDir, name), contents);+ },+ }),+ createCodex: codexFactory(async () => {+ if (++turns === 1) {+ await copyCompletedScan(root);+ originalFindings = await readFile(+ join(scanDir, "findings.json"),+ "utf8",+ );+ } else {+ await writeFile(join(scanDir, "findings.json"), '{"unfinished":');+ await writeFile(join(scanDir, "report.md"), "unfinished report");+ if (scenario === "close") closing = client.close();+ else controller.abort();+ }+ return { events: completedEvents() };+ }, "thread-1"),+ });const result = client.run(repository, {postScanPrompt: "Draft confirmed fixes.",signal: controller.signal,@@ -4445,16 +4414,13 @@ describe("CodexSecurity orchestration", () => {};});let budgetSignal: AbortSignal | undefined;- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- runWorkbench: recordingWorkbench(commands),- createCodex: () => ({- startThread,- }),- },- );+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ runWorkbench: recordingWorkbench(commands),+ createCodex: () => ({+ startThread,+ }),+ });const keepAlive = setTimeout(() => {}, 10_000);try {const result = await client.run(repository, {@@ -4515,61 +4481,51 @@ describe("CodexSecurity orchestration", () => {if (cost.inputTokens === 950) nextCost.resolve();});let budgetSignal: AbortSignal | undefined;- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- runWorkbench: async (_options, args, input) => {- commands.push(args);- if (scenario === "save-failed" && args[0] === "set-scan-cost-limit")- throw new Error("Synthetic save failure");- if (scenario === "saving" && args[0] === "set-scan-cost-limit")- await lateAnswer.promise;- return mockWorkbench(args, input);- },- createCodex: () => ({- startThread: () => ({- id: null,- async runStreamed(- _input: string,- { signal }: { signal: AbortSignal },- ) {- async function* events(): AsyncGenerator<ThreadEvent> {- yield { type: "thread.started", thread_id: "scan-thread" };- const path = await writeUsageSession(- codexHome,- "scan-thread",- { input_tokens: 900, output_tokens: 0 },- );- await requestStarted.promise;- if (scenario === "completed") {- await copyCompletedScan(root);- yield {- type: "turn.completed",- usage: {- input_tokens: 900,- cached_input_tokens: 0,- cache_write_input_tokens: 0,- output_tokens: 0,- reasoning_output_tokens: 0,- },- };- return;- }- await appendUsage(path, 950);- await nextCost.promise;- if (scenario === "canceled") controller.abort();- else await appendUsage(path, 1_200);- await (signal.aborted ? undefined : once(signal, "abort"));- await appendUsage(path, 2_000);- throw new DOMException("aborted", "AbortError");- }- return { events: events() };- },- }),- }),+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ runWorkbench: async (_options, args, input) => {+ commands.push(args);+ if (scenario === "save-failed" && args[0] === "set-scan-cost-limit")+ throw new Error("Synthetic save failure");+ if (scenario === "saving" && args[0] === "set-scan-cost-limit")+ await lateAnswer.promise;+ return mockWorkbench(args, input);},- );+ createCodex: codexFactory(+ async (_input: string, { signal }: { signal: AbortSignal }) => {+ async function* events(): AsyncGenerator<ThreadEvent> {+ yield { type: "thread.started", thread_id: "scan-thread" };+ const path = await writeUsageSession(codexHome, "scan-thread", {+ input_tokens: 900,+ output_tokens: 0,+ });+ await requestStarted.promise;+ if (scenario === "completed") {+ await copyCompletedScan(root);+ yield {+ type: "turn.completed",+ usage: {+ input_tokens: 900,+ cached_input_tokens: 0,+ cache_write_input_tokens: 0,+ output_tokens: 0,+ reasoning_output_tokens: 0,+ },+ };+ return;+ }+ await appendUsage(path, 950);+ await nextCost.promise;+ if (scenario === "canceled") controller.abort();+ else await appendUsage(path, 1_200);+ await (signal.aborted ? undefined : once(signal, "abort"));+ await appendUsage(path, 2_000);+ throw new DOMException("aborted", "AbortError");+ }+ return { events: events() };+ },+ ),+ });const keepAlive = setTimeout(() => {}, 10_000);try {const scan = client.run(repository, {@@ -4662,14 +4618,11 @@ describe("CodexSecurity orchestration", () => {cached_input_tokens: 200,output_tokens: 30,})!;- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- runWorkbench: recordingWorkbench(commands),- createCodex: codexFactory(runStreamed),- },- );+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ runWorkbench: recordingWorkbench(commands),+ createCodex: codexFactory(runStreamed),+ });// The fake Codex stream has no process handle to keep its unref'ed poll alive.const keepEventLoopAlive = setTimeout(() => {}, 10_000);@@ -4749,50 +4702,47 @@ describe("CodexSecurity orchestration", () => {return { events: events() };},);- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- runWorkbench: async (- _options: unknown,- args: readonly string[],- input?: string,- ): Promise<JsonObject> => {- commands.push(args);- if (args[0] !== "complete-budget-exhausted-scan") {- return mockWorkbench(args, input);- }- if (completion === "unavailable") {- throw new Error("Deep Scan discovery has not completed.");- }- await copyCompletedScan(root);- const coveragePath = join(scanDir, "coverage.json");- const coverage = JSON.parse(await readFile(coveragePath, "utf8"));- coverage.mode = "deep_repository";- coverage.completeness =- completion === "invalid" ? "complete" : "partial";- if (completion === "partial") {- coverage.deferred.push({- id: "budget-exhausted",- reason: "The scan reached its configured cost limit.",- });- }- const coverageBytes = `${JSON.stringify(coverage)}\n`;- await writeFile(coveragePath, coverageBytes);- const manifestPath = join(scanDir, "scan-manifest.json");- const manifest = JSON.parse(await readFile(manifestPath, "utf8"));- const artifact = manifest.scan.artifacts.find(- (item: { path: string }) => item.path === "coverage.json",- );- artifact.sha256 = hash("sha256", coverageBytes);- await writeFile(manifestPath, `${JSON.stringify(manifest)}\n`);- return {- scan: { warnings: [args[args.indexOf("--message") + 1]!] },- };- },- createCodex: codexFactory(runStreamed),+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ runWorkbench: async (+ _options: unknown,+ args: readonly string[],+ input?: string,+ ): Promise<JsonObject> => {+ commands.push(args);+ if (args[0] !== "complete-budget-exhausted-scan") {+ return mockWorkbench(args, input);+ }+ if (completion === "unavailable") {+ throw new Error("Deep Scan discovery has not completed.");+ }+ await copyCompletedScan(root);+ const coveragePath = join(scanDir, "coverage.json");+ const coverage = JSON.parse(await readFile(coveragePath, "utf8"));+ coverage.mode = "deep_repository";+ coverage.completeness =+ completion === "invalid" ? "complete" : "partial";+ if (completion === "partial") {+ coverage.deferred.push({+ id: "budget-exhausted",+ reason: "The scan reached its configured cost limit.",+ });+ }+ const coverageBytes = `${JSON.stringify(coverage)}\n`;+ await writeFile(coveragePath, coverageBytes);+ const manifestPath = join(scanDir, "scan-manifest.json");+ const manifest = JSON.parse(await readFile(manifestPath, "utf8"));+ const artifact = manifest.scan.artifacts.find(+ (item: { path: string }) => item.path === "coverage.json",+ );+ artifact.sha256 = hash("sha256", coverageBytes);+ await writeFile(manifestPath, `${JSON.stringify(manifest)}\n`);+ return {+ scan: { warnings: [args[args.indexOf("--message") + 1]!] },+ };},- );+ createCodex: codexFactory(runStreamed),+ });const keepAlive = setTimeout(() => {}, 10_000);try {const result = client.run(repository, {@@ -4841,33 +4791,30 @@ describe("CodexSecurity orchestration", () => {const { root, repository, codexHome, scanDir } = await scanDirectories();const warnings = mock((_warning: string) => {});const commands: Array<readonly string[]> = [];- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- runWorkbench: recordingWorkbench(commands),- createCodex: (options: CodexOptions) => ({- startThread(threadOptions: Parameters<Codex["startThread"]>[0]) {- const thread = new Codex({- ...options,- codexPathOverride: process.execPath,- }).startThread(threadOptions);- const executable = thread as unknown as {- _exec: { run(): AsyncGenerator<string> };- };- executable._exec.run = async function* () {- await copyCompletedScan(root);- yield JSON.stringify({- type: "thread.started",- thread_id: "scan-thread",- });- yield JSON.stringify({ type: "turn.completed", usage: null });- };- return thread;- },- }),- },- );+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ runWorkbench: recordingWorkbench(commands),+ createCodex: (options: CodexOptions) => ({+ startThread(threadOptions: Parameters<Codex["startThread"]>[0]) {+ const thread = new Codex({+ ...options,+ codexPathOverride: process.execPath,+ }).startThread(threadOptions);+ const executable = thread as unknown as {+ _exec: { run(): AsyncGenerator<string> };+ };+ executable._exec.run = async function* () {+ await copyCompletedScan(root);+ yield JSON.stringify({+ type: "thread.started",+ thread_id: "scan-thread",+ });+ yield JSON.stringify({ type: "turn.completed", usage: null });+ };+ return thread;+ },+ }),+ });const result = await client.run(repository, {maxCostUsd: 1,@@ -4934,62 +4881,59 @@ describe("CodexSecurity orchestration", () => {let workbenchGit = "";let prompt = "";let recipe: unknown;- const client = new TestClient(- {},- {- prepareRuntime: runtimePreparer(codexHome, () => ({- environment: {- PATH: [knowledgeBaseBin, trustedBin, dirname(trustedGit)].join(- delimiter,- ),- GIT_SSH_COMMAND: "synthetic-ssh --fixture",- },- })),- resolvePluginPython: async () => "/managed/python",- prepareOutputDir: async () => scanDir,- repositoryRevision: async () => "deadbeef",- runWorkbench: async (- workbenchOptions: WorkbenchCommandOptions,- args: readonly string[],- input?: string,- ): Promise<JsonObject> => {- workbenchGit =- workbenchOptions.environment["CODEX_SECURITY_GIT"] ?? "";- if (args[0] !== "register-cli-scan")- return mockWorkbench(args, input);- expect(JSON.parse(input!).userContext).toBe(scanPrompt);- recipe = JSON.parse(input!).recipe;- return mockScanRegistration(args, input);+ const client = TestClient.withDependencies({+ prepareRuntime: runtimePreparer(codexHome, () => ({+ environment: {+ PATH: [knowledgeBaseBin, trustedBin, dirname(trustedGit)].join(+ delimiter,+ ),+ GIT_SSH_COMMAND: "synthetic-ssh --fixture",},- createCodex: (options: CodexOptions) => ({- startThread: () => ({- id: null,- async runStreamed(input: string) {- prompt = input;- expect(options.env?.["CODEX_SECURITY_GIT"]).toBe(expectedGit);- expect(options.env?.["PATH"]?.split(delimiter)).not.toContain(- knowledgeBaseBin,- );- expect(options.env?.["GIT_SSH_COMMAND"]).toBe(- "synthetic-ssh --fixture",+ })),+ resolvePluginPython: async () => "/managed/python",+ prepareOutputDir: async () => scanDir,+ repositoryRevision: async () => "deadbeef",+ runWorkbench: async (+ workbenchOptions: WorkbenchCommandOptions,+ args: readonly string[],+ input?: string,+ ): Promise<JsonObject> => {+ workbenchGit =+ workbenchOptions.environment["CODEX_SECURITY_GIT"] ?? "";+ if (args[0] !== "register-cli-scan")+ return mockWorkbench(args, input);+ expect(JSON.parse(input!).userContext).toBe(scanPrompt);+ recipe = JSON.parse(input!).recipe;+ return mockScanRegistration(args, input);+ },+ createCodex: (options: CodexOptions) => ({+ startThread: () => ({+ id: null,+ async runStreamed(input: string) {+ prompt = input;+ expect(options.env?.["CODEX_SECURITY_GIT"]).toBe(expectedGit);+ expect(options.env?.["PATH"]?.split(delimiter)).not.toContain(+ knowledgeBaseBin,+ );+ expect(options.env?.["GIT_SSH_COMMAND"]).toBe(+ "synthetic-ssh --fixture",+ );+ if (kind === "standalone-file")+ expect(options.env?.["PATH"]?.split(delimiter)).toContain(+ trustedBin,);- if (kind === "standalone-file")- expect(options.env?.["PATH"]?.split(delimiter)).toContain(- trustedBin,- );- knowledgeDirectory =- options.env?.["CODEX_SECURITY_KNOWLEDGE_BASE"] ?? "";- const [document] = await readdir(knowledgeDirectory);- expect(- await readFile(join(knowledgeDirectory, document!), "utf8"),- ).toBe(context);- await copyCompletedScan(root);- return { events: completedEvents() };- },- }),+ knowledgeDirectory =+ options.env?.["CODEX_SECURITY_KNOWLEDGE_BASE"] ?? "";+ const [document] = await readdir(knowledgeDirectory);+ expect(+ await readFile(join(knowledgeDirectory, document!), "utf8"),+ ).toBe(context);+ await copyCompletedScan(root);+ return { events: completedEvents() };+ },}),- },- );+ }),+ });await expect(client.run(repository, {@@ -5025,23 +4969,20 @@ describe("CodexSecurity orchestration", () => {await writeFile(knowledgeBase, "Authorization boundaries are in scope.\n");let knowledgeDirectory = "";let prompt = "";- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- createCodex: (options: CodexOptions) => ({- startThread: () => ({- id: null,- async runStreamed(input: string) {- prompt = input;- knowledgeDirectory =- options.env?.["CODEX_SECURITY_KNOWLEDGE_BASE"] ?? "";- throw new Error("scan failed");- },- }),+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ createCodex: (options: CodexOptions) => ({+ startThread: () => ({+ id: null,+ async runStreamed(input: string) {+ prompt = input;+ knowledgeDirectory =+ options.env?.["CODEX_SECURITY_KNOWLEDGE_BASE"] ?? "";+ throw new Error("scan failed");+ },}),- },- );+ }),+ });await expect(client.run(repository, {@@ -5070,34 +5011,26 @@ describe("CodexSecurity orchestration", () => {CODEX_SECURITY_STATE_DIR: stateDirectory,};const commands: Array<readonly string[]> = [];- const client = new TestClient(- {},- {- environment,- prepareRuntime: runtimePreparer(codexHome, () => ({ environment })),- resolvePluginPython: async () => python!,- prepareOutputDir: async () => scanDir,- repositoryRevision: async () => "deadbeef",- runWorkbench: async (- options: Parameters<typeof runWorkbench>[0],- args: readonly string[],- input?: string,- ): Promise<JsonObject> => {- commands.push(args);- const result = await runWorkbench(options, args, input);- if (args[0] === "fail-scan") {- throw new Error("failure recording also failed");- }- return result;- },- createCodex: () => ({- startThread: () => ({- id: null,- runStreamed: async () => fail("original scan failure"),- }),- }),+ const client = TestClient.withDependencies({+ environment,+ prepareRuntime: runtimePreparer(codexHome, () => ({ environment })),+ resolvePluginPython: async () => python!,+ prepareOutputDir: async () => scanDir,+ repositoryRevision: async () => "deadbeef",+ runWorkbench: async (+ options: Parameters<typeof runWorkbench>[0],+ args: readonly string[],+ input?: string,+ ): Promise<JsonObject> => {+ commands.push(args);+ const result = await runWorkbench(options, args, input);+ if (args[0] === "fail-scan") {+ throw new Error("failure recording also failed");+ }+ return result;},- );+ createCodex: codexFactory(async () => fail("original scan failure")),+ });await expect(client.run(repository)).rejects.toThrow("original scan failure",);@@ -5143,39 +5076,31 @@ describe("CodexSecurity orchestration", () => {client_secret_value: "SYNTHETIC correct horse battery staple",});const originalFailure = `request failed: token=SYNTHETIC_TOKEN ${quotedCredential}`;- const client = new TestClient(- {},- {- environment,- prepareRuntime: runtimePreparer(codexHome, () => ({ environment })),- resolvePluginPython: async () => python!,- prepareOutputDir: async () => scanDir,- repositoryRevision: async () => "deadbeef",- runWorkbench: async (- options: Parameters<typeof runWorkbench>[0],- args: readonly string[],- input?: string,- ): Promise<JsonObject> => {- commands.push(args);- return await runWorkbench(options, args, input);- },- createCodex: () => ({- startThread: () => ({- id: null,- async runStreamed() {- async function* failingEvents(): AsyncGenerator<ThreadEvent> {- yield { type: "thread.started", thread_id: "failed-thread" };- yield {- type: "error",- message: originalFailure,- };- }- return { events: failingEvents() };- },- }),- }),+ const client = TestClient.withDependencies({+ environment,+ prepareRuntime: runtimePreparer(codexHome, () => ({ environment })),+ resolvePluginPython: async () => python!,+ prepareOutputDir: async () => scanDir,+ repositoryRevision: async () => "deadbeef",+ runWorkbench: async (+ options: Parameters<typeof runWorkbench>[0],+ args: readonly string[],+ input?: string,+ ): Promise<JsonObject> => {+ commands.push(args);+ return await runWorkbench(options, args, input);},- );+ createCodex: codexFactory(async () => {+ async function* failingEvents(): AsyncGenerator<ThreadEvent> {+ yield { type: "thread.started", thread_id: "failed-thread" };+ yield {+ type: "error",+ message: originalFailure,+ };+ }+ return { events: failingEvents() };+ }),+ });await expect(client.run(repository)).rejects.toThrow(originalFailure);const failure = commands.find((args) => args[0] === "fail-scan");@@ -5205,28 +5130,25 @@ describe("CodexSecurity orchestration", () => {const stateDirectory = join(root, "state");await mkdir(repository);await mkdir(codexHome);- const client = new TestClient(- {},- {- environment: { CODEX_SECURITY_STATE_DIR: stateDirectory },- prepareRuntime: async () => preparedRuntime(codexHome),- resolvePluginPython: async () => "/managed/python",- repositoryRevision: async () => "deadbeef",- createCodex: (options: CodexOptions) => ({- startThread: () => ({- id: null,- async runStreamed() {- const scanDir = options.env?.["CODEX_SECURITY_SCAN_DIR"];- if (scanDir === undefined)- throw new Error("missing scan directory");- await cp(EXAMPLE, scanDir, { recursive: true });- await writeFile(join(scanDir, "report.md"), "# Scan report\n");- return { events: completedEvents() };- },- }),+ const client = TestClient.withDependencies({+ environment: { CODEX_SECURITY_STATE_DIR: stateDirectory },+ prepareRuntime: async () => preparedRuntime(codexHome),+ resolvePluginPython: async () => "/managed/python",+ repositoryRevision: async () => "deadbeef",+ createCodex: (options: CodexOptions) => ({+ startThread: () => ({+ id: null,+ async runStreamed() {+ const scanDir = options.env?.["CODEX_SECURITY_SCAN_DIR"];+ if (scanDir === undefined)+ throw new Error("missing scan directory");+ await cp(EXAMPLE, scanDir, { recursive: true });+ await writeFile(join(scanDir, "report.md"), "# Scan report\n");+ return { events: completedEvents() };+ },}),- },- );+ }),+ });const result = await client.run(repository);expect(@@ -5255,15 +5177,12 @@ describe("CodexSecurity orchestration", () => {linkedState,join(linkedState, "repository", "missing", "state"),]) {- const client = new TestClient(- {},- {- environment: { CODEX_SECURITY_STATE_DIR: stateDirectory },- prepareRuntime: async () => fail("Runtime must not start"),- resolvePluginPython: async () => "/managed/python",- createCodex: codexMustNotStart,- },- );+ const client = TestClient.withDependencies({+ environment: { CODEX_SECURITY_STATE_DIR: stateDirectory },+ prepareRuntime: async () => fail("Runtime must not start"),+ resolvePluginPython: async () => "/managed/python",+ createCodex: codexMustNotStart,+ });for (const operation of ["preflight", "run"] as const) {await expect(@@ -5278,13 +5197,10 @@ describe("CodexSecurity orchestration", () => {test("rejects reruns when the original plugin version is unavailable", async () => {const { repository, codexHome } = await runtimeDirectories();- const client = new TestClient(- {},- {- prepareRuntime: async () => preparedRuntime(codexHome),- createCodex: codexMustNotStart,- },- );+ const client = TestClient.withDependencies({+ prepareRuntime: async () => preparedRuntime(codexHome),+ createCodex: codexMustNotStart,+ });await expect(client.run(repository, { expectedPluginVersion: "0.0.1" }),@@ -5624,22 +5540,19 @@ describe("CodexSecurity orchestration", () => {await mkdir(pluginRoot, { recursive: true });await mkdir(scanDir, { mode: 0o700 });const createCodex = mock(throwing("Codex should not start"));- const client = new TestClient(- {},- {- prepareRuntime: runtimePreparer(codexHome, () => ({- plugin: {- ...preparedRuntime(codexHome).plugin,- pluginRoot,- installedRoot: pluginRoot,- },- })),- resolvePluginPython: async () => "/managed/python",- prepareOutputDir: async () => scanDir,- repositoryRevision: async () => "deadbeef",- createCodex,- },- );+ const client = TestClient.withDependencies({+ prepareRuntime: runtimePreparer(codexHome, () => ({+ plugin: {+ ...preparedRuntime(codexHome).plugin,+ pluginRoot,+ installedRoot: pluginRoot,+ },+ })),+ resolvePluginPython: async () => "/managed/python",+ prepareOutputDir: async () => scanDir,+ repositoryRevision: async () => "deadbeef",+ createCodex,+ });await expect(client.run(repository)).rejects.toThrow("Shell-visible plugin root must be outside CODEX_HOME",@@ -6051,27 +5964,24 @@ describe("CodexSecurity orchestration", () => {const { root, repository, codexHome, scanDir } = await scanDirectories();await writeFile(join(repository, "target.ts"), "export {};\n");let targetPathsFile: string | null = null;- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- createCodex: (options: CodexOptions) => ({- startThread: () => ({- id: null,- async runStreamed() {- const path = options.env?.["CODEX_SECURITY_TARGET_PATHS_FILE"];- if (typeof path !== "string") {- throw new Error("missing target paths file");- }- targetPathsFile = path;- expect(existsSync(path)).toBe(true);- await copyCompletedScan(root);- return { events: completedEvents() };- },- }),+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ createCodex: (options: CodexOptions) => ({+ startThread: () => ({+ id: null,+ async runStreamed() {+ const path = options.env?.["CODEX_SECURITY_TARGET_PATHS_FILE"];+ if (typeof path !== "string") {+ throw new Error("missing target paths file");+ }+ targetPathsFile = path;+ expect(existsSync(path)).toBe(true);+ await copyCompletedScan(root);+ return { events: completedEvents() };+ },}),- },- );+ }),+ });await expect(client.run(repository, { target: ["target.ts"] }),@@ -6102,13 +6012,10 @@ describe("CodexSecurity orchestration", () => {git("branch", base);git("branch", head);const runStreamed = mock(async (_input: string) => fail("prompt captured"));- const client = new TestClient(- {},- {- ...scanRuntimeDependencies(codexHome, scanDir),- createCodex: codexFactory(runStreamed),- },- );+ const client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ createCodex: codexFactory(runStreamed),+ });const revision = gitText(["rev-parse", "HEAD"], { cwd: repository }).trim();await expect(@@ -6207,7 +6114,7 @@ describe("CodexSecurity orchestration", () => {);git("checkout", "-q", "main");- const client = new TestClient({}, {});+ const client = TestClient.withDependencies({});await expect(client.run(repository, {target: DiffTarget.refs({ base: "main", head: "feature" }),