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
refactor(cli): simplify command setup and fixtures (#1208)
mldangelo-oaiba9aa374886f
diff --git a/sdk/typescript/src/cli-help.ts b/sdk/typescript/src/cli-help.tsindex 9669cde9b..6eef178b1 100644--- a/sdk/typescript/src/cli-help.ts+++ b/sdk/typescript/src/cli-help.ts@@ -186,8 +186,7 @@ function optionRow(row: Row, command: string): Row {if (choices.length > 1) {description += `${description.endsWith(".") ? "" : "."} Choices: ${choices.join(", ")}.`;}- const value = VALUE_LABELS[name] ?? (type === "number" ? "count" : name);- return `<${value}>`;+ return `<${VALUE_LABELS[name] ?? (type === "number" ? "count" : name)}>`;});label = label.replace(/^--([\w-]+), (-\w)/u, "$2, --$1");if (command === "scan" && !/default:/iu.test(description)) {diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 1624d8445..5c564b20e 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -1,4 +1,5 @@#!/usr/bin/env node+import { isNonEmptyString } from "./value.js";import {execFile as execFileCallback,@@ -84,7 +85,7 @@ import {environmentEntry,readCodexHomeConfig,} from "./auth.js";-import { loadContract } from "./contract.js";+import { loadContract, sha256Text } from "./contract.js";import { isRecord as isJsonObject } from "./record.js";import { suggestOwnersInternal } from "./suggest-owners.js";import { parseImportedFindings } from "./findings-import.js";@@ -712,10 +713,7 @@ class PublicationProgressPresenter {this.#repository,)) {const item = (event.event as Record<string, unknown>)["item"];- const tool =- typeof item === "object" && item !== null && !Array.isArray(item)- ? (item as Record<string, unknown>)["tool"]- : undefined;+ const tool = isJsonObject(item) ? item["tool"] : undefined;const hidesShellCommand =activity.kind === "command" ||(activity.kind === "tool" &&@@ -868,19 +866,14 @@ class FindingProgressPresenter {public observe(event: Readonly<Record<string, unknown>>): void {const method = event["method"];const params = event["params"];- if (- typeof params !== "object" ||- params === null ||- Array.isArray(params)- ) {+ if (!isJsonObject(params)) {return;}- const values = params as Record<string, unknown>;let normalized: Record<string, unknown>;if (method === "item/reasoning/summaryTextDelta") {- const id = values["itemId"];- const delta = values["delta"];+ const id = params["itemId"];+ const delta = params["delta"];if (typeof id !== "string" || typeof delta !== "string") return;if (this.#dashboard === null) return;const text = `${this.#reasoning.get(id) ?? ""}${delta}`;@@ -890,29 +883,28 @@ class FindingProgressPresenter {item: { id, type: "reasoning", text },};} else if (method === "item/started" || method === "item/completed") {- const item = values["item"];- if (typeof item !== "object" || item === null || Array.isArray(item)) {+ const item = params["item"];+ if (!isJsonObject(item)) {return;}- const current = item as Record<string, unknown>;- const type = current["type"];+ const type = item["type"];let converted: Record<string, unknown>;if (type === "commandExecution") {- converted = { ...current, type: "command_execution" };+ converted = { ...item, type: "command_execution" };} else if (type === "mcpToolCall") {- converted = { ...current, type: "mcp_tool_call" };+ converted = { ...item, type: "mcp_tool_call" };} else if (type === "agentMessage") {- if (current["phase"] !== "commentary") return;- converted = { ...current, type: "agent_message" };+ if (item["phase"] !== "commentary") return;+ converted = { ...item, type: "agent_message" };} else if (type === "reasoning") {- const summary = current["summary"];+ const summary = item["summary"];const text = Array.isArray(summary)? summary.filter((entry): entry is string => typeof entry === "string").join("\n")- : current["text"];+ : item["text"];if (typeof text !== "string") return;- converted = { ...current, text };+ converted = { ...item, text };} else {return;}@@ -991,10 +983,9 @@ type DeepCliOptionName = Extract<readonly [string, string, string, string]>[0];const DEEP_SCAN_OPTION_SCHEMAS = Object.fromEntries(- DEEP_SCAN_SETTINGS.filter(([, , , flag]) => flag !== null).map(([name]) => [- name,- DeepScanSettingsSchema.shape[name],- ]),+ DEEP_SCAN_SETTINGS.flatMap(([name, , , flag]) =>+ flag !== null ? [[name, DeepScanSettingsSchema.shape[name]]] : [],+ ),) as Pick<typeof DeepScanSettingsSchema.shape, DeepCliOptionName>;export function resolveCliPath(directory: string, value: string): AbsolutePath {@@ -1308,14 +1299,13 @@ const DEFAULT_DEPENDENCIES: CliDependencies = {...exportEnvironment(),CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(),};- const python = await resolvePluginPython({- configuredPath: pythonPath,- environment,- signal,- });return await runWorkbench({- python,+ python: await resolvePluginPython({+ configuredPath: pythonPath,+ environment,+ signal,+ }),pluginRoot: await bundledPluginRoot(),environment,signal,@@ -1329,6 +1319,16 @@ const DEFAULT_DEPENDENCIES: CliDependencies = {matchScanFindingsInternal(input, options, { surface: "cli" }),};+function signalHandlers(+ dependencies: CliDependencies,+ operation: "add" | "remove",+ interrupt: () => void,+ terminate: () => void,+): void {+ dependencies[`${operation}SignalListener`]("SIGINT", interrupt);+ dependencies[`${operation}SignalListener`]("SIGTERM", terminate);+}+export async function runCodexSkillCommand(args: readonly string[],output?: SkillCommandOutput,@@ -1812,7 +1812,7 @@ export async function main(: undefined;let exitCode = 0;let frameworkExit: number | undefined;- let frameworkOutput = "";+ const frameworkCapture = captureOutput();let streamedLogs: Awaited<ReturnType<typeof readSavedScanLogs>> | undefined;let renderedHistory: string | undefined;let renderedPublication: string | undefined;@@ -1892,11 +1892,9 @@ export async function main(const onInterrupt = (): void => cancel("SIGINT");const onTerminate = (): void => cancel("SIGTERM");const removeListeners = (): void => {- dependencies.removeSignalListener("SIGINT", onInterrupt);- dependencies.removeSignalListener("SIGTERM", onTerminate);+ signalHandlers(dependencies, "remove", onInterrupt, onTerminate);};- dependencies.addSignalListener("SIGINT", onInterrupt);- dependencies.addSignalListener("SIGTERM", onTerminate);+ signalHandlers(dependencies, "add", onInterrupt, onTerminate);let previousProgress = "";try {const result = await operation({@@ -1923,8 +1921,7 @@ export async function main(return result;} catch (error) {const interrupted = controller.signal.reason;- exitCode =- interrupted === "SIGINT" ? 130 : interrupted === "SIGTERM" ? 143 : 2;+ exitCode = interruptedExitCode(controller.signal) ?? 2;const message =interrupted === "SIGINT"? "Finding matching canceled by Ctrl-C. Saved comparisons are preserved."@@ -2355,8 +2352,7 @@ export async function main(const sourcePath = imported["sourcePath"];const format = imported["format"];if (- typeof sourcePath !== "string" ||- sourcePath.length === 0 ||+ !isNonEmptyString(sourcePath) ||(format !== "csv" && format !== "json")) {throw new CodexSecurityError(@@ -2657,8 +2653,7 @@ export async function main(const onTerminate = (): void => cancel("SIGTERM");const removeSignalListeners = (): void => {if (!observingSignals) return;- dependencies.removeSignalListener("SIGINT", onInterrupt);- dependencies.removeSignalListener("SIGTERM", onTerminate);+ signalHandlers(dependencies, "remove", onInterrupt, onTerminate);observingSignals = false;};try {@@ -2748,8 +2743,7 @@ export async function main(): undefined;if (options.to !== "linear") {- dependencies.addSignalListener("SIGINT", onInterrupt);- dependencies.addSignalListener("SIGTERM", onTerminate);+ signalHandlers(dependencies, "add", onInterrupt, onTerminate);observingSignals = true;}if (csvPath !== undefined) {@@ -2808,16 +2802,13 @@ export async function main(listedScans.map(async (scan) => {if (!isJsonObject(scan)) return undefined;const directory = scan["scanDir"];- if (typeof directory !== "string" || directory.length === 0) {+ if (!isNonEmptyString(directory)) {return undefined;}const metadata = await lstat(resolveCliPath(currentDirectory, directory),).catch(() => undefined);- return metadata?.isDirectory() === true &&- !metadata.isSymbolicLink()- ? scan- : undefined;+ return metadata?.isDirectory() === true ? scan : undefined;}),)).filter((scan): scan is JsonObject => scan !== undefined);@@ -2837,10 +2828,8 @@ export async function main(const scanId = scan["scanId"];const directory = scan["scanDir"];if (- typeof scanId !== "string" ||- scanId.length === 0 ||- typeof directory !== "string" ||- directory.length === 0 ||+ !isNonEmptyString(scanId) ||+ !isNonEmptyString(directory) ||!isJsonObject(progress) ||progress["status"] !== "complete") {@@ -3064,8 +3053,7 @@ export async function main(publicationRepository,);presentation = progress;- dependencies.addSignalListener("SIGINT", onInterrupt);- dependencies.addSignalListener("SIGTERM", onTerminate);+ signalHandlers(dependencies, "add", onInterrupt, onTerminate);observingSignals = true;if (!options.dryRun) {progress.start();@@ -3631,20 +3619,9 @@ export async function main(} = resolveScanSettings(project,{- auth: options.auth,+ ...pickScanSettings(options),target: scope.target,- cyberAccessProgram: options.cyberAccessProgram,knowledgeBasePaths: options.knowledgeBase,- scanPromptFile: options.scanPromptFile,- validationPromptFile: options.validationPromptFile,- postScanPromptFile: options.postScanPromptFile,- mode: options.mode,- workers: options.workers,- subagents: options.subagents,- stopAfterNoNew: options.stopAfterNoNew,- maxDiscoveryRuns: options.maxDiscoveryRuns,- maxTimeHours: options.maxTimeHours,- outputDir: options.outputDir,failureSeverity: options.failOnSeverity,maxCostUsd: options.maxCost,codexOverrides: parseCodexOverrides(@@ -3854,7 +3831,7 @@ export async function main(errorOutput.write(`codex-security: ${signal === "SIGINT" || signal === "SIGTERM" ? "Owner suggestions canceled." : diagnosticValue(error)}\n`,);- exitCode = signal === "SIGINT" ? 130 : signal === "SIGTERM" ? 143 : 2;+ exitCode = interruptedExitCode(controller.signal) ?? 2;return undefined;} finally {removeSignals();@@ -3952,7 +3929,7 @@ export async function main(errorOutput.write(`codex-security: ${signal === "SIGINT" || signal === "SIGTERM" ? "Severity classification canceled." : diagnosticValue(error)}\n`,);- exitCode = signal === "SIGINT" ? 130 : signal === "SIGTERM" ? 143 : 2;+ exitCode = interruptedExitCode(controller.signal) ?? 2;return undefined;} finally {removeSignals();@@ -4078,7 +4055,7 @@ export async function main(: diagnosticValue(error)}\n`,);- exitCode = signal === "SIGINT" ? 130 : signal === "SIGTERM" ? 143 : 2;+ exitCode = interruptedExitCode(controller.signal) ?? 2;return undefined;} finally {removeSignals();@@ -4194,9 +4171,7 @@ export async function main(};const onInterrupt = (): void => controller.abort("SIGINT");const onTerminate = (): void => controller.abort("SIGTERM");-- dependencies.addSignalListener("SIGINT", onInterrupt);- dependencies.addSignalListener("SIGTERM", onTerminate);+ signalHandlers(dependencies, "add", onInterrupt, onTerminate);try {const directory = dependencies.currentDirectory();const repository = resolveCliPath(directory, args.repository ?? ".");@@ -4207,12 +4182,8 @@ export async function main(const resolved = resolveScanSettings(project,{- auth: options.auth,- cyberAccessProgram: options.cyberAccessProgram,- outputDir: options.outputDir,+ ...pickScanSettings({ ...options, workers: undefined }),knowledgeBasePaths: options.knowledgeBase,- scanPromptFile: options.scanPromptFile,- postScanPromptFile: options.postScanPromptFile,maxCostUsd: options.maxCost,codexOverrides: parseCodexOverrides(options.codex,@@ -4354,8 +4325,7 @@ export async function main(errorOutput.write(`codex-security: ${errorMessage(error)}\n`);} finally {stopDashboard();- dependencies.removeSignalListener("SIGINT", onInterrupt);- dependencies.removeSignalListener("SIGTERM", onTerminate);+ signalHandlers(dependencies, "remove", onInterrupt, onTerminate);}},})@@ -4471,12 +4441,8 @@ export async function main(dependencies,);const overrides = {- mode: options.mode,- outputDir: options.outputDir,+ ...pickScanSettings({ ...options, workers: undefined }),knowledgeBasePaths: options.knowledgeBase,- scanPromptFile: options.scanPromptFile,- validationPromptFile: options.validationPromptFile,- postScanPromptFile: options.postScanPromptFile,maxCostUsd: options.maxCost,};const resolved = resolveScanSettings(@@ -4970,21 +4936,8 @@ export async function main(const environment =imports.length === 0? undefined- : Object.fromEntries(- Object.entries(dependencies.environment).filter(- ([name]) =>- !/^(?:CODEX_SECURITY_)?LINEAR_(?:API_KEY|ACCESS_TOKEN)$/iu.test(- name,- ),- ),- );- let response = "";- const verificationOutput: Writable = {- write(value: string | Uint8Array): boolean {- response += value.toString();- return true;- },- };+ : withoutLinearCredentials(dependencies.environment);+ const response = captureOutput();const progress = new FindingProgressPresenter(errorOutput,dependencies,@@ -4997,7 +4950,7 @@ export async function main("verify-fix",selected === undefined ? [...positionals, ...imports] : [],options,- verificationOutput,+ response.stream,errorOutput,dependencies,{@@ -5018,7 +4971,7 @@ export async function main(let reported: unknown;try {- reported = JSON.parse(response);+ reported = JSON.parse(response.text());} catch {throw new CodexSecurityError("Verification results were not valid JSON.",@@ -5332,14 +5285,7 @@ export async function main(const environment =imports.length === 0? undefined- : Object.fromEntries(- Object.entries(dependencies.environment).filter(- ([name]) =>- !/^(?:CODEX_SECURITY_)?LINEAR_(?:API_KEY|ACCESS_TOKEN)$/iu.test(- name,- ),- ),- );+ : withoutLinearCredentials(dependencies.environment);const patchGitBase =options.assessPatchRisk || options.createPr? await snapshotPatchTree(repository, dependencies)@@ -5354,17 +5300,12 @@ export async function main(dependencies,);}- let report = "";+ const report = captureOutput();exitCode = await runSkill("fix-finding",[...positionals, ...imports],options,- {- write: (value) => {- report += value.toString();- return true;- },- },+ report.stream,errorOutput,dependencies,{@@ -5374,7 +5315,7 @@ export async function main(validationPrompt,},);- if (!jsonOutput) output.write(report);+ if (!jsonOutput) output.write(report.text());const files = await changedPatchFiles(repository,patchBase,@@ -5430,7 +5371,7 @@ export async function main(if (structuredOutput)return {...patchResult,- ...(jsonOutput ? { report } : {}),+ ...(jsonOutput ? { report: report.text() } : {}),};} catch (error) {if (exitCode === 0) exitCode = 2;@@ -5543,14 +5484,13 @@ export async function main(if (authentication.method === "api_key") {const configuredApiKeyVariables = Object.entries(dependencies.environment,- )- .filter(- ([name, value]) =>- value?.trim() &&- (name.toUpperCase() === "OPENAI_API_KEY" ||- name.toUpperCase() === "CODEX_API_KEY"),- )- .map(([name]) => name);+ ).flatMap(([name, value]) =>+ value?.trim() &&+ (name.toUpperCase() === "OPENAI_API_KEY" ||+ name.toUpperCase() === "CODEX_API_KEY")+ ? name+ : [],+ );const loginWarning = options.withAccessToken? `Access-token login succeeded, but noninteractive scans will use ${authentication.source}.\n`: "ChatGPT login succeeded. Interactive scans will ask which account to use; " +@@ -5895,9 +5835,7 @@ export async function main(: [argument],),{- stdout: (value) => {- frameworkOutput += value;- },+ stdout: frameworkCapture.stream.write,exit: (code) => {frameworkExit = code;},@@ -5909,6 +5847,7 @@ export async function main(} finally {updateController.abort();}+ const frameworkOutput = frameworkCapture.text();if (notice !== undefined) errorOutput.write(formatUpdateNotice(notice));if (frameworkExit !== undefined) {if (policyFullOutput || patchStructuredError || scanStructuredError) {@@ -5976,7 +5915,7 @@ async function runScanImport(: errorMessage(error);errorOutput.write(`codex-security: ${diagnosticValue(message)}\n`);return {- exitCode: signal === "SIGINT" ? 130 : signal === "SIGTERM" ? 143 : 2,+ exitCode: interruptedExitCode(controller.signal) ?? 2,error: message,};} finally {@@ -6077,7 +6016,7 @@ async function prepareScanArgumentsFromRecipe();}const repository = recipe["repository"];- if (typeof repository !== "string" || repository.length === 0) {+ if (!isNonEmptyString(repository)) {throw new CodexSecurityError("The saved scan recipe does not contain a repository.",);@@ -6087,12 +6026,7 @@ async function prepareScanArgumentsFromRecipe(throw new CodexSecurityError("The saved scan recipe contains no target.");}const paths = target["paths"];- if (- !Array.isArray(paths) ||- !paths.every(- (path): path is string => typeof path === "string" && path.length > 0,- )- ) {+ if (!Array.isArray(paths) || !paths.every(isNonEmptyString)) {throw new CodexSecurityError("The saved scan recipe contains invalid paths.",);@@ -6100,9 +6034,7 @@ async function prepareScanArgumentsFromRecipe(const knowledgeBasePaths = recipe["knowledgeBasePaths"] ?? [];if (!Array.isArray(knowledgeBasePaths) ||- !knowledgeBasePaths.every(- (path): path is string => typeof path === "string" && path.length > 0,- )+ !knowledgeBasePaths.every(isNonEmptyString)) {throw new CodexSecurityError("The saved scan recipe contains invalid knowledge base paths.",@@ -6141,7 +6073,7 @@ async function prepareScanArgumentsFromRecipe();}const head = target["headRef"];- if (head !== undefined && (typeof head !== "string" || head.length === 0)) {+ if (head !== undefined && !isNonEmptyString(head)) {throw new CodexSecurityError("The saved scan recipe has an invalid Git head.",);@@ -6681,9 +6613,8 @@ async function selectSavedFindings(}const selected = findings.filter((finding) => {- const triage = finding["triage"] as JsonObject | undefined;return (- triage?.["status"] !== "closed" &&+ (finding["triage"] as JsonObject | undefined)?.["status"] !== "closed" &&(identifiers.length === 0 ||identifiers.includes(finding.occurrenceId) ||identifiers.includes(finding.findingId)) &&@@ -6758,10 +6689,9 @@ function directPatchDigest(positionals: readonly string[],imports: readonly ImportedIssue[],): string {- return `issues-${createHash("sha256")- .update(JSON.stringify([...positionals, ...imports.map(({ id }) => id)]))- .digest("hex")- .slice(0, 12)}`;+ return `issues-${sha256Text(+ JSON.stringify([...positionals, ...imports.map(({ id }) => id)]),+ ).slice(0, 12)}`;}async function publishPatchBranch(@@ -7173,18 +7103,12 @@ async function assessPatchRisk(for await (const chunk of createReadStream(patchPath)) {digest.update(chunk);}- let report = "";- const stdout: Writable = {- write(value: string | Uint8Array): boolean {- report += value.toString();- return true;- },- };+ const report = captureOutput();const status = await runSkill("assess-patch-risk",[],request.configuration,- stdout,+ report.stream,stderr,dependencies,{@@ -7207,7 +7131,7 @@ async function assessPatchRisk(`Patch risk assessment exited with status ${status}.`,);}- return { report: report.trim() };+ return { report: report.text().trim() };} finally {await rm(root, { recursive: true, force: true });}@@ -7270,13 +7194,7 @@ async function runFindingPatches();const patches: FindingPatch[] = [];for (const finding of selected.findings) {- let response = "";- const stdout: Writable = {- write(value: string | Uint8Array): boolean {- response += value.toString();- return true;- },- };+ const response = captureOutput();const instruction = options.findingInstructions?.[finding.occurrenceId];const progress = new FindingProgressPresenter(stderr,@@ -7300,7 +7218,7 @@ async function runFindingPatches("fix-finding",[],configuration,- stdout,+ response.stream,patchErrors,dependencies,{@@ -7336,7 +7254,7 @@ async function runFindingPatches(patch = failed(`Patch command exited with status ${status}.`);} else {try {- const reported = JSON.parse(response) as { patches?: unknown };+ const reported = JSON.parse(response.text()) as { patches?: unknown };const entries = Array.isArray(reported?.patches)? reported.patches: [];@@ -7384,6 +7302,19 @@ async function runFindingPatches(return patches;}+function captureOutput() {+ let text = "";+ return {+ stream: {+ write(value: string | Uint8Array): boolean {+ text += value.toString();+ return true;+ },+ },+ text: () => text,+ };+}+async function runSkill(skill: "validation" | "fix-finding" | "verify-fix" | "assess-patch-risk",inputs: readonly (string | ImportedIssue)[],@@ -7682,13 +7613,11 @@ export async function readSkillCommandOutput(): Promise<{message?: string;error?: string;- malformed: boolean;completed?: boolean;sandboxUnavailable?: boolean;}> {let message: string | undefined;let error: string | undefined;- let malformed = false;let threadId: string | undefined;let turnId: string | undefined;let completed = false;@@ -7743,11 +7672,9 @@ export async function readSkillCommandOutput(try {event = JSON.parse(line);} catch {- malformed = true;continue;}if (typeof event !== "object" || event === null) {- malformed = true;continue;}const value = event as Record<string, unknown>;@@ -7967,7 +7894,6 @@ export async function readSkillCommandOutput(return {...(message === undefined ? {} : { message }),...(error === undefined ? {} : { error }),- malformed,...(appServer === undefined ? {} : { completed }),...(sandboxUnavailable ? { sandboxUnavailable } : {}),};@@ -8243,11 +8169,9 @@ async function executeScan(const onInterrupt = signalListener("SIGINT");const onTerminate = signalListener("SIGTERM");const removeSignalListeners = (): void => {- dependencies.removeSignalListener("SIGINT", onInterrupt);- dependencies.removeSignalListener("SIGTERM", onTerminate);+ signalHandlers(dependencies, "remove", onInterrupt, onTerminate);};- dependencies.addSignalListener("SIGINT", onInterrupt);- dependencies.addSignalListener("SIGTERM", onTerminate);+ signalHandlers(dependencies, "add", onInterrupt, onTerminate);let security: Pick<CodexSecurity, "run" | "preflight" | "close"> | null =null;@@ -9039,12 +8963,10 @@ function authenticationFailureMessage();}if (authentication?.method === "api_key") {- const openAiKey =- authentication.source === "OPENAI_API_KEY" ||- authentication.source === "CODEX_API_KEY";return (`Authentication failed using ${authentication.source}. ` +- (openAiKey+ (authentication.source === "OPENAI_API_KEY" ||+ authentication.source === "CODEX_API_KEY"? "Retry with '--auth chatgpt' or provide a valid API key.": `Provide a valid ${authentication.source} for the selected provider.`));@@ -9350,20 +9272,18 @@ function protectedRootErrorMessage(? "Scan artifacts cannot be written inside the protected scan root.": "Temporary and runtime files cannot be created inside the protected scan root.";const suggestion = suggestedOutputDirectory(error.protectedRoot);- const recovery =+ return [+ `${description} must be outside the scanned directory and any enclosing Git worktree.`,+ ` Resolved path: ${error.outputDirectory}`,+ ` Protected root: ${error.protectedRoot}`,+ ` Reason: ${reason}`,error.pathKind === "output"? suggestion === undefined? "Choose a private output directory outside the protected root.": `Re-run with --output-dir ${quoteCliPath(suggestion)}.`: suggestion === undefined? "Set TMPDIR (or TEMP on Windows) to a writable directory outside the protected root."- : `Set TMPDIR (or TEMP on Windows) to ${quoteCliPath(suggestion)} after creating that directory.`;- return [- `${description} must be outside the scanned directory and any enclosing Git worktree.`,- ` Resolved path: ${error.outputDirectory}`,- ` Protected root: ${error.protectedRoot}`,- ` Reason: ${reason}`,- recovery,+ : `Set TMPDIR (or TEMP on Windows) to ${quoteCliPath(suggestion)} after creating that directory.`,].join("\n");}@@ -9819,3 +9739,14 @@ function listenForAbort(dependencies.removeSignalListener("SIGTERM", onTerminate);};}++function withoutLinearCredentials(+ environment: NodeJS.ProcessEnv,+): NodeJS.ProcessEnv {+ return Object.fromEntries(+ Object.entries(environment).filter(+ ([name]) =>+ !/^(?:CODEX_SECURITY_)?LINEAR_(?:API_KEY|ACCESS_TOKEN)$/iu.test(name),+ ),+ );+}diff --git a/sdk/typescript/tests-ts/cli-authentication.test.ts b/sdk/typescript/tests-ts/cli-authentication.test.tsindex 45dcb44d9..351de8567 100644--- a/sdk/typescript/tests-ts/cli-authentication.test.ts+++ b/sdk/typescript/tests-ts/cli-authentication.test.ts@@ -1,9 +1,10 @@+import { parseJsonLines } from "./support/json.js";+import { resolving } from "./support/promises.js";import { spawnSync } from "node:child_process";import { existsSync } from "node:fs";import {chmod,mkdir,- mkdtemp,realpath,readFile,rm,@@ -11,10 +12,9 @@ import {symlink,writeFile,} from "node:fs/promises";-import { tmpdir } from "node:os";import { join } from "node:path";import { fileURLToPath, pathToFileURL } from "node:url";-import { afterEach, beforeEach, describe, expect, test } from "bun:test";+import { afterEach, beforeEach, describe, expect, test, mock } from "bun:test";import { parse as parseToml } from "smol-toml";import { main, runCodexSkillCommand } from "../src/cli.js";import {@@ -29,18 +29,23 @@ import {setCodexSecurityCredentialLogout,} from "../src/runtime.js";import {+ savedRecipe,capture,dependencies as cliDependencies,FakeSignals,fakePreflight,fakeResult,+ fakeSecurity,+ failingSecurity,} from "./cli-fixtures.js";+import { temporaryDirectory } from "./support/temporary-directories.js";+import { throwing } from "./support/errors.js";let stateDirectory: string;beforeEach(async () => {stateDirectory = await realpath(- await mkdtemp(join(tmpdir(), "codex-security-cli-authentication-")),+ await temporaryDirectory("codex-security-cli-authentication-"),);});@@ -77,16 +82,11 @@ describe("CLI authentication", () => {const deps = dependencies();deps.prepareAuthenticationHome = async () =>join(stateDirectory, "codex-home");- let forwarded: readonly string[] | undefined;- deps.createSecurity = () => {- throw new Error("must not initialize Codex Security");- };- deps.runCodex = async (args) => {- forwarded = args;- return 17;- };+ const runCodex = mock<typeof deps.runCodex>(resolving(17));+ deps.createSecurity = throwing("must not initialize Codex Security");+ deps.runCodex = runCodex;expect(await main(argv, stdout.stream, stderr.stream, deps)).toBe(17);- expect(forwarded).toEqual([argv[0], ...argv.slice(1)]);+ expect(runCodex.mock.lastCall?.[0]).toEqual([argv[0], ...argv.slice(1)]);expect(stdout.text()).toBe("");expect(stderr.text()).toBe("");}@@ -124,7 +124,7 @@ describe("CLI authentication", () => {"validates and canonicalizes the credential home for status and logout",async () => {const root = await realpath(- await mkdtemp(join(tmpdir(), "codex-security-cli-managed-auth-")),+ await temporaryDirectory("codex-security-cli-managed-auth-"),);try {const actualState = join(root, "actual-state");@@ -339,7 +339,7 @@ describe("CLI authentication", () => {[["scan", "--auth", "auto"], "auto"],[["scan"], "auto"],] as const) {- let selected: ScanOptions["auth"];+ const onTurn = mock((_repository: string, { auth }: ScanOptions) => auth);const stderr = capture();expect(@@ -349,13 +349,11 @@ describe("CLI authentication", () => {stderr.stream,dependencies({environment: { OPENAI_API_KEY: "synthetic-private-key" },- onTurn: (_repository, options) => {- selected = (options as ScanOptions).auth;- },+ onTurn,}),),).toBe(0);- expect(selected).toBe(expected);+ expect(onTurn.mock.results.at(-1)?.value).toBe(expected);expect(stderr.text()).not.toContain("synthetic-private-key");}});@@ -379,18 +377,15 @@ describe("CLI authentication", () => {const stdout = capture();const stderr = capture(false);const deps = dependencies({ environment });- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onAuthentication?.({method: "aws_credentials",source,verified: false,});return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(@@ -463,18 +458,15 @@ describe("CLI authentication", () => {const deps = dependencies({environment: { AWS_BEARER_TOKEN_BEDROCK: "synthetic-bedrock-bearer" },});- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onAuthentication?.({method: "aws_credentials",source: "AWS_BEARER_TOKEN_BEDROCK",verified: false,});throw new CodexSecurityError(detail);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(@@ -622,22 +614,13 @@ describe("CLI authentication", () => {[["scans", "rerun", "scan-original", "--verbose"], "api-key"],] as const) {const stderr = capture(true);- let selected: ScanOptions["auth"];+ const onTurn = mock((_repository: string, { auth }: ScanOptions) => auth);let question = "";let choices: readonly { label: string; value: string }[] = [];const deps = dependencies({environment: { OPENAI_API_KEY: "sk-proj-SYNTHETIC_SECRET_123" },- onTurn: (_repository, options) => {- selected = (options as ScanOptions).auth;- },- onWorkbench: () => ({- recipe: {- repository: "/original/repository",- target: { kind: "repository", paths: [] },- mode: "standard",- config: {},- },- }),+ onTurn,+ onWorkbench: () => savedRecipe(),});deps.hasStoredChatGPTSignIn = async () => true;deps.scanAuthenticationPrompt = {@@ -653,7 +636,7 @@ describe("CLI authentication", () => {};expect(await main(argv, capture().stream, stderr.stream, deps)).toBe(0);- expect(selected).toBe(selection);+ expect(onTurn.mock.results.at(-1)?.value).toBe(selection);expect(question).toBe("How would you like to authenticate this scan?");expect(choices).toEqual([{ label: "ChatGPT subscription", value: "chatgpt" },@@ -670,23 +653,20 @@ describe("CLI authentication", () => {for (const stage of ["status", "prompt"] as const) {const signals = new FakeSignals();const signalName = stage === "status" ? "SIGTERM" : "SIGINT";- let observedSignal: AbortSignal | undefined;- let initialized = false;+ const interrupt = mock((_signal?: AbortSignal) => {+ signals.emit(signalName);+ return new Promise<never>(() => {});+ });+ const createSecurity = mock(+ throwing("must not initialize a cancelled scan"),+ );const deps = dependencies({signals,environment: { OPENAI_API_KEY: "synthetic-private-key" },});- deps.createSecurity = () => {- initialized = true;- throw new Error("must not initialize a cancelled scan");- };- const interrupt = <Value>(signal?: AbortSignal): Promise<Value> => {- observedSignal = signal;- signals.emit(signalName);- return new Promise(() => {});- };+ deps.createSecurity = createSecurity;deps.hasStoredChatGPTSignIn = (signal) =>- stage === "status" ? interrupt<boolean>(signal) : Promise.resolve(true);+ stage === "status" ? interrupt(signal) : Promise.resolve(true);deps.scanAuthenticationPrompt = {isInteractive: () => true,select: <Value extends string>(@@ -694,14 +674,14 @@ describe("CLI authentication", () => {_options: readonly { label: string; value: Value }[],_presentation?: { header?: string },signal?: AbortSignal,- ) => interrupt<Value>(signal),+ ) => interrupt(signal),};expect(await main(["scan"], capture().stream, capture(true).stream, deps),).toBe(signalName === "SIGTERM" ? 143 : 130);- expect(observedSignal?.aborted).toBe(true);- expect(initialized).toBe(false);+ expect(interrupt.mock.lastCall?.[0]?.aborted).toBe(true);+ expect(createSecurity).not.toHaveBeenCalled();expect(signals.listeners.get("SIGINT")?.size).toBe(0);expect(signals.listeners.get("SIGTERM")?.size).toBe(0);}@@ -794,16 +774,14 @@ describe("CLI authentication", () => {]) {const stdout = capture();const stderr = capture(scenario.terminal);- let selected: ScanOptions["auth"];+ const onTurn = mock((_repository: string, { auth }: ScanOptions) => auth);let prompts = 0;- let discoveries = 0;+ const hasStoredChatGPTSignIn = mock(resolving(scenario.stored));const deps = dependencies({environment: scenario.key? { OPENAI_API_KEY: "synthetic-private-key" }: {},- onTurn: (_repository, options) => {- selected = (options as ScanOptions).auth;- },+ onTurn,onWorkbench: (args): JsonObject =>args[0] === "list-scans"? { scans: [{ scanId: "scan-original" }] }@@ -819,10 +797,7 @@ describe("CLI authentication", () => {},},});- deps.hasStoredChatGPTSignIn = async () => {- discoveries += 1;- return scenario.stored;- };+ deps.hasStoredChatGPTSignIn = hasStoredChatGPTSignIn;deps.scanAuthenticationPrompt = {isInteractive: () => scenario.inputInteractive !== false,select: async <Value extends string>(@@ -839,12 +814,12 @@ describe("CLI authentication", () => {).toBe(0);expect(prompts).toBe(0);if (scenario.argv.includes("--json") || scenario.argv.includes("jsonl")) {- expect(discoveries).toBe(0);+ expect(hasStoredChatGPTSignIn).toHaveBeenCalledTimes(0);expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());expect(stderr.text()).not.toMatch(/\x1b\[/u);}if (!scenario.argv.includes("--dry-run")) {- expect(selected).toBe(+ expect(onTurn.mock.results.at(-1)?.value).toBe(scenario.recipeAuth ?? scenario.expectedAuth ?? "auto",);}@@ -855,9 +830,7 @@ describe("CLI authentication", () => {test("rejects explicit API-key authentication before initializing a scan when no key is set", async () => {const stderr = capture();const deps = dependencies();- deps.createSecurity = () => {- throw new Error("must not initialize Codex Security");- };+ deps.createSecurity = throwing("must not initialize Codex Security");expect(await main(@@ -941,7 +914,7 @@ describe("CLI authentication", () => {});test("keeps delegated credentials in the configured Codex home", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-login-home-"));+ const root = await temporaryDirectory("codex-security-login-home-");const repository = join(root, "repository");const relativeHome = join(repository, ".codex-security-home");const tildeHome = join(root, ".codex-security-home");@@ -1035,8 +1008,8 @@ describe("CLI authentication", () => {const stdout = capture();const stderr = capture(true);const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onAuthentication?.({method: "api_key",source: "OPENAI_API_KEY",@@ -1044,10 +1017,7 @@ describe("CLI authentication", () => {});options?.onScanStarted?.();return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(["scan", "--json"], stdout.stream, stderr.stream, deps),@@ -1076,18 +1046,15 @@ describe("CLI authentication", () => {const stdout = capture();const stderr = capture(false);const deps = dependencies({ environment });- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onAuthentication?.({method: "api_key",source,verified: false,});throw new CodexSecurityError(detail);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(["scan"], stdout.stream, stderr.stream, deps)).toBe(2,@@ -1183,18 +1150,15 @@ describe("CLI authentication", () => {const stdout = capture();const stderr = capture(false);const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onAuthentication?.({method: "api_key",source: "OPENAI_API_KEY",verified: false,});return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(["scan", "--json"], stdout.stream, stderr.stream, deps),@@ -1219,13 +1183,7 @@ describe("CLI authentication", () => {] as const) {const stderr = capture(false);const deps = dependencies({ environment });- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(message);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () => failingSecurity(message);expect(await main(["scan"], capture().stream, stderr.stream, deps)).toBe(2,@@ -1251,14 +1209,11 @@ describe("CLI authentication", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onAuthentication?.(authentication);return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(["scan", "--json"], stdout.stream, stderr.stream, deps),@@ -1294,7 +1249,7 @@ describe("CLI authentication", () => {test("recognizes existing ambient Codex authentication on a fresh state directory during login status", async () => {const root = await realpath(- await mkdtemp(join(tmpdir(), "codex-security-cli-ambient-auth-")),+ await temporaryDirectory("codex-security-cli-ambient-auth-"),);try {const ambientHome = join(root, "ambient-codex");@@ -1332,7 +1287,7 @@ describe("CLI authentication", () => {test("does not import ambient Codex authentication during login status after explicit logout", async () => {const root = await realpath(- await mkdtemp(join(tmpdir(), "codex-security-cli-ambient-logout-")),+ await temporaryDirectory("codex-security-cli-ambient-logout-"),);try {const ambientHome = join(root, "ambient-codex");@@ -2277,10 +2232,7 @@ describe("skill authentication", () => {: "Authentication failed using OPENAI_API_KEY",);else expect(stderr.text()).toBe("Patch applied. Files changed: 1.\n");- const requests = (await readFile(requestLog, "utf8"))- .trim()- .split("\n")- .map((line) => JSON.parse(line));+ const requests = parseJsonLines(await readFile(requestLog, "utf8"));const methods = requests.map((request) => request.method);if (!loginFailure) {expect(diff --git a/sdk/typescript/tests-ts/cli-classify-severity.test.ts b/sdk/typescript/tests-ts/cli-classify-severity.test.tsindex 06f1d5e54..76e09824a 100644--- a/sdk/typescript/tests-ts/cli-classify-severity.test.ts+++ b/sdk/typescript/tests-ts/cli-classify-severity.test.ts@@ -1,5 +1,6 @@+import { rejecting } from "./support/errors.js";import { resolve } from "node:path";-import { expect, test } from "bun:test";+import { expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";@@ -88,11 +89,8 @@ test("classify-severity accepts external scan directories and defaults to existitest("classify-severity rejects missing or conflicting selectors and surfaces SDK errors", async () => {const deps = dependencies();- let calls = 0;- deps.classifyScanSeverity = async () => {- calls++;- throw new Error("The scan is incomplete");- };+ const classifyScanSeverity = mock(rejecting("The scan is incomplete"));+ deps.classifyScanSeverity = classifyScanSeverity;for (const args of [[], ["--scan", "latest", "--scan-dir", "saved"]]) {expect(await main(@@ -103,7 +101,7 @@ test("classify-severity rejects missing or conflicting selectors and surfaces SD),).toBe(2);}- expect(calls).toBe(0);+ expect(classifyScanSeverity).toHaveBeenCalledTimes(0);const stderr = capture();expect(await main(@@ -113,7 +111,7 @@ test("classify-severity rejects missing or conflicting selectors and surfaces SDdeps,),).toBe(2);- expect(stderr.text()).toContain("The scan is incomplete");+ expect(stderr.text()).toBe("codex-security: The scan is incomplete\n");});test.each([diff --git a/sdk/typescript/tests-ts/cli-cloud-publish.test.ts b/sdk/typescript/tests-ts/cli-cloud-publish.test.tsindex 242bcc654..1d050bb31 100644--- a/sdk/typescript/tests-ts/cli-cloud-publish.test.ts+++ b/sdk/typescript/tests-ts/cli-cloud-publish.test.ts@@ -1,7 +1,8 @@+import { resolving } from "./support/promises.js";import { mkdir, mkdtemp, realpath, rm, symlink } from "node:fs/promises";import { homedir, tmpdir } from "node:os";import { join, resolve } from "node:path";-import { afterEach, describe, expect, test } from "bun:test";+import { afterEach, describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import type { JsonObject } from "../src/index.js";import {@@ -9,8 +10,10 @@ import {dependencies,FakeSignals,SYNTHETIC_CREDENTIALS,+ mustNotInitializeCodex,} from "./cli-fixtures.js";-import { createTemporaryDirectories } from "./support/temporary-directories.js";+import { createApiTestFixtures } from "./support/temporary-directories.js";+import { rejecting, throwing } from "./support/errors.js";const receipt = {scanId: "scan-1",@@ -18,11 +21,14 @@ const receipt = {findingCount: 1,};-const temporaryDirectories = createTemporaryDirectories({ canonical: false });-afterEach(temporaryDirectories.cleanup);+const { temporaryDirectory, cleanup } = createApiTestFixtures(+ "cloud-saved-scans-",+ false,+);+afterEach(cleanup);async function savedScansFixture() {- const root = await temporaryDirectories.create("cloud-saved-scans-");+ const root = await temporaryDirectory();const scans = await Promise.all([1, 2, 3].map(async (index) => {const scanDir = join(root, `scan ${index}`);@@ -68,9 +74,7 @@ describe("publish scan to Cloud", () => {test("passes --dry-run through when publishing a findings CSV", async () => {const deps = dependencies({currentDirectory: "/workspace/repository",- onWorkbench: () => {- throw new Error("unexpected scan lookup");- },+ onWorkbench: throwing("unexpected scan lookup"),});let publishedPath = "";deps.publishFindingsCsvToCloud = async (path, options) => {@@ -127,11 +131,8 @@ describe("publish scan to Cloud", () => {],])("rejects combining --csv with %s", async (_name, args, message) => {const deps = dependencies();- let publications = 0;- deps.publishFindingsCsvToCloud = async () => {- publications++;- return receipt;- };+ const publishFindingsCsvToCloud = mock(resolving(receipt));+ deps.publishFindingsCsvToCloud = publishFindingsCsvToCloud;const stderr = capture();expect(await main(@@ -141,7 +142,7 @@ describe("publish scan to Cloud", () => {deps,),).toBe(2);- expect(publications).toBe(0);+ expect(publishFindingsCsvToCloud).toHaveBeenCalledTimes(0);expect(stderr.text()).toContain(message);});@@ -202,11 +203,8 @@ describe("publish scan to Cloud", () => {if (failure === "incomplete") second!.progress.status = "running";if (failure === "unavailable")await rm(second!.scanDir, { recursive: true });- let uploads = 0;- deps.publishScanToCloud = async () => {- uploads++;- return receipt;- };+ const publishScanToCloud = mock(resolving(receipt));+ deps.publishScanToCloud = publishScanToCloud;const stderr = capture();expect(await main(@@ -225,7 +223,7 @@ describe("publish scan to Cloud", () => {deps,),).toBe(2);- expect(uploads).toBe(0);+ expect(publishScanToCloud).toHaveBeenCalledTimes(0);expect(stderr.text()).toMatch(/not found|not complete|artifacts or run a new scan/,);@@ -236,9 +234,7 @@ describe("publish scan to Cloud", () => {test("rejects mixed ID and directory selectors before reading history", async () => {const deps = dependencies({- onWorkbench: () => {- throw new Error("unexpected lookup");- },+ onWorkbench: throwing("unexpected lookup"),});const stderr = capture();expect(@@ -267,9 +263,7 @@ describe("publish scan to Cloud", () => {let selections = 0;deps.publishPrompt = {isInteractive: () => true,- select: async () => {- throw new Error("unexpected single-select prompt");- },+ select: unexpectedSingleSelect,checkbox: async <Value extends string>(_question: string,choices: readonly { label: string; value: Value }[],@@ -322,20 +316,15 @@ describe("publish scan to Cloud", () => {signals.remove(signal, listener);deps.publishPrompt = {isInteractive: () => true,- select: async () => {- throw new Error("unexpected single-select prompt");- },+ select: unexpectedSingleSelect,checkbox: async (_question, _choices, _presentation, signal) => {signals.emit("SIGINT");signal!.throwIfAborted();return [];},};- let uploads = 0;- deps.publishScanToCloud = async () => {- uploads++;- return receipt;- };+ const publishScanToCloud = mock(resolving(receipt));+ deps.publishScanToCloud = publishScanToCloud;expect(await main(["publish", "scan", "--to", "cloud"],@@ -344,7 +333,7 @@ describe("publish scan to Cloud", () => {deps,),).toBe(130);- expect(uploads).toBe(0);+ expect(publishScanToCloud).toHaveBeenCalledTimes(0);expect([...signals.listeners.values()].every((listeners) => listeners.size === 0,@@ -438,16 +427,10 @@ describe("publish scan to Cloud", () => {test("publishes multiple explicit scans in order and deduplicates resolved paths", async () => {for (const dryRun of [false, true]) {const deps = dependencies({- onWorkbench: () => {- throw new Error("must not inspect scan history");- },+ onWorkbench: unexpectedScanHistory,});- deps.createSecurity = () => {- throw new Error("must not initialize Codex");- };- deps.publishScan = async () => {- throw new Error("must not publish to Linear");- };+ deps.createSecurity = mustNotInitializeCodex;+ deps.publishScan = rejecting("must not publish to Linear");const directories = ["scan one", "scan-two"].map((path) =>resolve(deps.currentDirectory(), path),);@@ -635,11 +618,8 @@ describe("publish scan to Cloud", () => {test("rejects multiple scans for Linear before publishing any findings", async () => {const deps = dependencies();- let calls = 0;- deps.publishScan = async () => {- calls++;- throw new Error("unexpected publication");- };+ const publishScan = mock(rejecting("unexpected publication"));+ deps.publishScan = publishScan;const stdout = capture();const stderr = capture();expect(@@ -661,7 +641,7 @@ describe("publish scan to Cloud", () => {deps,),).toBe(2);- expect(calls).toBe(0);+ expect(publishScan).toHaveBeenCalledTimes(0);expect(stdout.text()).toBe("");expect(stderr.text()).toContain("Multiple scan directories");});@@ -675,16 +655,10 @@ describe("publish scan to Cloud", () => {const deps = dependencies({currentDirectory,environment: { CODEX_SECURITY_LINEAR_API_KEY: " " },- onWorkbench: () => {- throw new Error("must not inspect scan history");- },+ onWorkbench: unexpectedScanHistory,});- deps.createSecurity = () => {- throw new Error("must not initialize Codex");- };- deps.publishScan = async () => {- throw new Error("must not publish to Linear");- };+ deps.createSecurity = mustNotInitializeCodex;+ deps.publishScan = rejecting("must not publish to Linear");let calls = 0;const result = dryRun? {@@ -746,15 +720,10 @@ describe("publish scan to Cloud", () => {},]) {const deps = dependencies({- onWorkbench: () => {- throw new Error("must not inspect scan history");- },+ onWorkbench: unexpectedScanHistory,});- const calls: string[] = [];- deps.publishScanToCloud = async (directory) => {- calls.push(directory);- return receipt;- };+ const calls = mock(resolving<typeof receipt, [string]>(receipt));+ deps.publishScanToCloud = calls;const stdout = capture();expect(await main(@@ -764,7 +733,7 @@ describe("publish scan to Cloud", () => {deps,),).toBe(0);- expect(calls).toEqual(expected);+ expect(calls.mock.calls.map(([value]) => value)).toEqual(expected);expect(JSON.parse(stdout.text())).toEqual(expected.length === 1? receipt@@ -778,7 +747,7 @@ describe("publish scan to Cloud", () => {});test("publishes a scan once through canonical and directory-linked paths", async () => {- const root = await temporaryDirectories.create("cloud-publish-links-");+ const root = await temporaryDirectory("cloud-publish-links-");const scans = join(root, "scans");const scanDir = join(scans, "completed-scan");const linkedScans = join(root, "linked-scans");@@ -789,16 +758,11 @@ describe("publish scan to Cloud", () => {process.platform === "win32" ? "junction" : "dir",);const canonicalScan = await realpath(scanDir);- const calls: string[] = [];+ const calls = mock(resolving<typeof receipt, [string]>(receipt));const deps = dependencies({- onWorkbench: () => {- throw new Error("must not inspect scan history");- },+ onWorkbench: unexpectedScanHistory,});- deps.publishScanToCloud = async (directory) => {- calls.push(directory);- return receipt;- };+ deps.publishScanToCloud = calls;const stdout = capture();expect(await main(@@ -818,7 +782,7 @@ describe("publish scan to Cloud", () => {deps,),).toBe(0);- expect(calls).toEqual([canonicalScan]);+ expect(calls.mock.calls.map(([value]) => value)).toEqual([canonicalScan]);expect(JSON.parse(stdout.text())).toEqual(receipt);});@@ -833,11 +797,8 @@ describe("publish scan to Cloud", () => {["scan-one", "scan-two"],]) {const deps = dependencies();- let calls = 0;- deps.publishScanToCloud = async () => {- calls++;- return receipt;- };+ const publishScanToCloud = mock(resolving(receipt));+ deps.publishScanToCloud = publishScanToCloud;const stdout = capture();const stderr = capture();expect(@@ -848,7 +809,7 @@ describe("publish scan to Cloud", () => {deps,),).toBe(2);- expect(calls).toBe(0);+ expect(publishScanToCloud).toHaveBeenCalledTimes(0);expect(stdout.text()).toBe("");expect(stderr.text()).not.toBe("");}@@ -877,9 +838,7 @@ describe("publish scan to Cloud", () => {let selections = 0;deps.publishPrompt = {isInteractive: () => true,- select: async () => {- throw new Error("unexpected single-select prompt");- },+ select: unexpectedSingleSelect,checkbox: async (_question, choices, presentation) => {selections++;const directories: string[] = choices.map(({ value }) => value);@@ -913,13 +872,9 @@ describe("publish scan to Cloud", () => {const deps = dependencies();deps.publishPrompt = {isInteractive: () => false,- select: async () => {- throw new Error("unexpected picker");- },- };- deps.publishScanToCloud = async () => {- throw new Error("unexpected publication");+ select: rejecting("unexpected picker"),};+ deps.publishScanToCloud = rejecting("unexpected publication");const stderr = capture();expect(await main(@@ -943,11 +898,8 @@ describe("publish scan to Cloud", () => {["--skip-existing"],]) {const deps = dependencies();- let calls = 0;- deps.publishScanToCloud = async () => {- calls++;- return receipt;- };+ const publishScanToCloud = mock(resolving(receipt));+ deps.publishScanToCloud = publishScanToCloud;const stdout = capture();const stderr = capture();expect(@@ -965,7 +917,7 @@ describe("publish scan to Cloud", () => {deps,),).toBe(2);- expect(calls).toBe(0);+ expect(publishScanToCloud).toHaveBeenCalledTimes(0);expect(stdout.text()).toBe("");expect(stderr.text()).toContain("cannot be combined with Linear options");}@@ -975,9 +927,7 @@ describe("publish scan to Cloud", () => {for (const flag of ["--help", "--schema", "--llms", "--llms-full"]) {const stdout = capture();const deps = dependencies();- deps.publishScanToCloud = async () => {- throw new Error("unexpected publication");- };+ deps.publishScanToCloud = rejecting("unexpected publication");expect(await main(["publish", "scan", flag],@@ -992,11 +942,9 @@ describe("publish scan to Cloud", () => {test("reports original publication failures without claiming success", async () => {const deps = dependencies();- deps.publishScanToCloud = async () => {- throw new Error(- `Cloud failed: ${SYNTHETIC_CREDENTIALS}\u001b[2J\ncontinued`,- );- };+ deps.publishScanToCloud = rejecting(+ `Cloud failed: ${SYNTHETIC_CREDENTIALS}\u001b[2J\ncontinued`,+ );const stdout = capture();const stderr = capture();expect(@@ -1050,11 +998,9 @@ describe("publish scan to Cloud", () => {environment: { CODEX_SECURITY_LINEAR_API_KEY: "synthetic-linear-key" },});let now = 0;- let forced = false;+ const forceExit = mock();deps.now = () => now;- deps.forceExit = () => {- forced = true;- };+ deps.forceExit = forceExit;deps.publishScanToCloud = async (_directory, options) => {signals.emit(signal);expect(options?.signal?.aborted).toBe(true);@@ -1073,7 +1019,7 @@ describe("publish scan to Cloud", () => {deps,),).toBe(code);- expect(forced).toBe(false);+ expect(forceExit).not.toHaveBeenCalled();expect(stdout.text()).toBe("");expect(stderr.text()).toContain(signal === "SIGINT" ? "canceled" : "terminated",@@ -1087,3 +1033,7 @@ describe("publish scan to Cloud", () => {}});});++const unexpectedSingleSelect = rejecting("unexpected single-select prompt");++const unexpectedScanHistory = throwing("must not inspect scan history");diff --git a/sdk/typescript/tests-ts/cli-dedupe.test.ts b/sdk/typescript/tests-ts/cli-dedupe.test.tsindex 8832e2bad..305c6cf54 100644--- a/sdk/typescript/tests-ts/cli-dedupe.test.ts+++ b/sdk/typescript/tests-ts/cli-dedupe.test.ts@@ -1,6 +1,7 @@-import { expect, test } from "bun:test";+import { expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";+import { throwing, rejecting } from "./support/errors.js";const args = ["dedupe",@@ -33,10 +34,7 @@ test.each([false, true])(deps.deduplicateScan = async () => result;const stdout = capture();const stderr = capture();- if (brokenLog)- stderr.stream.write = () => {- throw new Error("Synthetic logging failure");- };+ if (brokenLog) stderr.stream.write = throwing("Synthetic logging failure");expect(await main(args, stdout.stream, stderr.stream, deps)).toBe(0);expect(JSON.parse(stdout.text())).toEqual(result);if (!brokenLog) {@@ -161,11 +159,10 @@ test.each(["0", "-1", "1.5", "NaN", "Infinity", "9007199254740992"])("dedupe rejects invalid concurrency %s before calling the SDK",async (value) => {const deps = dependencies();- let called = false;- deps.deduplicateScan = async () => {- called = true;- throw new Error("Invalid concurrency must not reach the SDK");- };+ const deduplicateScan = mock(+ rejecting("Invalid concurrency must not reach the SDK"),+ );+ deps.deduplicateScan = deduplicateScan;const stderr = capture();expect(await main(@@ -176,7 +173,7 @@ test.each(["0", "-1", "1.5", "NaN", "Infinity", "9007199254740992"])(),).toBe(2);expect(stderr.text()).toContain("concurrency");- expect(called).toBe(false);+ expect(deduplicateScan).not.toHaveBeenCalled();},);@@ -225,11 +222,8 @@ test("dedupe help and schema expose concurrency and its default", async () => {test("dedupe requires both explicit inputs and reports SDK failures", async () => {const deps = dependencies();- let called = false;- deps.deduplicateScan = async () => {- called = true;- throw new Error("Finding has not been indexed");- };+ const deduplicateScan = mock(rejecting("Finding has not been indexed"));+ deps.deduplicateScan = deduplicateScan;for (const flags of [[],["--scan", "latest"],@@ -244,12 +238,12 @@ test("dedupe requires both explicit inputs and reports SDK failures", async () =),).not.toBe(0);}- expect(called).toBe(false);+ expect(deduplicateScan).not.toHaveBeenCalled();const stdout = capture();const stderr = capture();expect(await main(args, stdout.stream, stderr.stream, deps)).toBe(2);expect(stdout.text()).toBe("");- expect(stderr.text()).toContain("Finding has not been indexed");+ expect(stderr.text()).toBe("codex-security: Finding has not been indexed\n");});test("dedupe forwards cancellation and removes signal handlers", async () => {diff --git a/sdk/typescript/tests-ts/cli-deep-scan-summary.test.ts b/sdk/typescript/tests-ts/cli-deep-scan-summary.test.tsindex 592e6071f..e8df74826 100644--- a/sdk/typescript/tests-ts/cli-deep-scan-summary.test.ts+++ b/sdk/typescript/tests-ts/cli-deep-scan-summary.test.ts@@ -1,7 +1,8 @@-import { describe, expect, test } from "bun:test";+import { describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import type { JsonObject } from "../src/config.js";import { capture, dependencies, fakeResult } from "./cli-fixtures.js";+import { throwing } from "./support/errors.js";const cappedState: JsonObject = {terminalReason: "capped",@@ -100,9 +101,7 @@ describe("deep scan completion summary", () => {cached_input_tokens: 200,output_tokens: 30,}),- onWorkbench: () => {- throw new Error("The exceeded cost limit is already known");- },+ onWorkbench: throwing("The exceeded cost limit is already known"),},["--mode", "deep", "--max-cost", "0.004"],);@@ -126,18 +125,15 @@ describe("deep scan completion summary", () => {);test("leaves standard scan summaries unchanged", async () => {- let reads = 0;+ const onWorkbench = mock<() => {}>().mockReturnValue({});const text = await summary({result: fakeResult(),- onWorkbench: () => {- reads++;- return {};- },+ onWorkbench,},[],);- expect(reads).toBe(0);+ expect(onWorkbench).toHaveBeenCalledTimes(0);expect(text).not.toContain("STOPPED");});});diff --git a/sdk/typescript/tests-ts/cli-diagnostics.test.ts b/sdk/typescript/tests-ts/cli-diagnostics.test.tsindex 21de96f3c..6d2e97e9a 100644--- a/sdk/typescript/tests-ts/cli-diagnostics.test.ts+++ b/sdk/typescript/tests-ts/cli-diagnostics.test.ts@@ -3,11 +3,13 @@ import { describe, expect, test } from "bun:test";import { main } from "../src/cli.js";import { CodexSecurityError, OutputDirectoryError } from "../src/errors.js";import {+ warningResult,capture,dependencies,- fakePreflight,fakeResult,+ fakeSecurity,} from "./cli-fixtures.js";+import { throwing } from "./support/errors.js";describe("CLI diagnostics", () => {test.each([@@ -62,9 +64,7 @@ describe("CLI diagnostics", () => {async ({ command, args, structured }) => {const message ="Operation failed: token=SYNTHETIC_VALUE\u001b[2J\ncontinued\r\ttail";- const fail = () => {- throw new Error(message);- };+ const fail = throwing(message);const deps = dependencies({ onCodex: fail, onRepositoryCommand: fail });deps.classifyScanSeverity = fail;deps.deduplicateScan = fail;@@ -99,13 +99,8 @@ describe("CLI diagnostics", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw failure;- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () =>+ fakeSecurity((Promise.reject<never>).bind(Promise, failure));expect(await main(@@ -178,8 +173,8 @@ describe("CLI diagnostics", () => {const deps = dependencies({environment: { ...mode.environment, NO_COLOR: "1" },});- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onAuthentication?.({method: "api_key",source: "OPENAI_API_KEY",@@ -190,10 +185,7 @@ describe("CLI diagnostics", () => {options?.onWarning?.(warning);options?.onObserverError?.("onWorkerStatus", new Error(observer));return result;- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(@@ -229,14 +221,7 @@ describe("CLI diagnostics", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {- options?.onWarning?.(warning, { kind: "target_changed" });- return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () => fakeSecurity(warningResult(warning, true));expect(await main(["scan", ".", "--json", "--verbose"],@@ -255,8 +240,8 @@ describe("CLI diagnostics", () => {const stdout = capture();const stderr = capture(true);const deps = dependencies({ environment: { NO_COLOR: "1" } });- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onScanStarted?.();options?.onActivity?.({id: "synthetic-command",@@ -267,10 +252,7 @@ describe("CLI diagnostics", () => {});options?.onObserverError?.("onWorkerStatus", new Error(observer));return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(["scan", "."], stdout.stream, stderr.stream, deps)).toBe(0,diff --git a/sdk/typescript/tests-ts/cli-export.test.ts b/sdk/typescript/tests-ts/cli-export.test.tsindex 0571292e4..a7962bdf8 100644--- a/sdk/typescript/tests-ts/cli-export.test.ts+++ b/sdk/typescript/tests-ts/cli-export.test.ts@@ -1,7 +1,7 @@+import { resolving } from "./support/promises.js";import { createHash } from "node:crypto";import {chmod,- cp,lstat,mkdir,mkdtemp,@@ -16,7 +16,7 @@ import { tmpdir } from "node:os";import { join } from "node:path";import { Writable } from "node:stream";import { pathToFileURL } from "node:url";-import { describe, expect, test } from "bun:test";+import { describe, expect, test, mock } from "bun:test";import { exportEnvironment, main } from "../src/cli.js";import type { JsonObject } from "../src/config.js";import {@@ -29,15 +29,15 @@ import {SYNTHETIC_CREDENTIALS,capture,dependencies,+ mustNotInitializeCodex,} from "./cli-fixtures.js";-import { PLUGIN_ROOT } from "./plugin-root.js";+import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";+import { temporaryDirectory } from "./support/temporary-directories.js";import { runCommand } from "./support/shell.js";async function copyCompletedScan(root: string): Promise<string> {const scan = join(root, "scan");- await cp(join(PLUGIN_ROOT, "examples", "completed-scan"), scan, {- recursive: true,- });+ await copyCompletedScanFixture(scan);if (process.platform !== "win32") await chmod(scan, 0o700);return scan;}@@ -71,9 +71,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => {- throw new Error("must not initialize Codex");- };+ deps.createSecurity = mustNotInitializeCodex;expect(await main(["export", "scan", "--export-format", format, "--output", "-"],@@ -456,9 +454,7 @@ describe("CLI", () => {test.skipIf(process.platform === "win32")("streams a large stdout export through a slow destination without buffering or status noise",async () => {- const root = await mkdtemp(- join(tmpdir(), "codex-security-export-stream-"),- );+ const root = await temporaryDirectory("codex-security-export-stream-");const fakePython = join(root, "fake-python");const expectedBytes = 2 * 1024 * 1024;await writeFile(@@ -473,7 +469,7 @@ describe("CLI", () => {);let bytes = 0;let writes = 0;- let drains = 0;+ const drains = mock(() => {});let emptyWrites = 0;const stdout = new Writable({highWaterMark: 32 * 1024,@@ -484,9 +480,7 @@ describe("CLI", () => {setTimeout(callback, 1);},});- stdout.on("drain", () => {- drains += 1;- });+ stdout.on("drain", drains);const stderr = capture();try {@@ -508,7 +502,7 @@ describe("CLI", () => {).toBe(0);expect(bytes).toBe(expectedBytes);expect(writes).toBeGreaterThan(1);- expect(drains).toBeGreaterThan(0);+ expect(drains.mock.calls.length).toBeGreaterThan(0);expect(emptyWrites).toBe(0);expect(stderr.text()).toBe("");@@ -548,9 +542,7 @@ describe("CLI", () => {test.skipIf(process.platform === "win32")(`terminates a stdout exporter promptly when ${failure}`,async () => {- const root = await mkdtemp(- join(tmpdir(), "codex-security-export-fail-"),- );+ const root = await temporaryDirectory("codex-security-export-fail-");const fakePython = join(root, "fake-python");await writeFile(fakePython,@@ -611,7 +603,7 @@ describe("CLI", () => {test.skipIf(process.platform === "win32")("terminates a stdout exporter promptly when the destination fails under backpressure",async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-export-fail-"));+ const root = await temporaryDirectory("codex-security-export-fail-");const fakePython = join(root, "fake-python");await writeFile(fakePython,@@ -672,7 +664,7 @@ describe("CLI", () => {["partial", false],["unknown", false],] as const)("exports %s, deferred: %j", async (completeness, hasDeferred) => {- const directory = await mkdtemp(join(tmpdir(), "codex-security-export-"));+ const directory = await temporaryDirectory("codex-security-export-");try {const scan = await copyCompletedScan(directory);const manifestPath = join(scan, "scan-manifest.json");@@ -781,7 +773,7 @@ describe("CLI", () => {});test("expands home-relative export paths", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-export-home-"));+ const root = await temporaryDirectory("codex-security-export-home-");const home = join(root, "home");const currentDirectory = join(root, "current");const previousHome = process.env["HOME"];@@ -795,16 +787,9 @@ describe("CLI", () => {process.env["HOME"] = home;process.env["USERPROFILE"] = home;- const exports: Array<{- scanDir: string;- output: string;- sourceRoot?: string;- }> = [];const deps = dependencies({ currentDirectory });- deps.exportFindings = async (arguments_) => {- exports.push(arguments_);- return undefined;- };+ const exports = mock<typeof deps.exportFindings>(resolving(undefined));+ deps.exportFindings = exports;expect(await main([@@ -822,7 +807,7 @@ describe("CLI", () => {deps,),).toBe(0);- expect(exports).toEqual([+ expect(exports.mock.calls.map(([value]) => value)).toEqual([expect.objectContaining({scanDir: await realpath(scan),output: join(await realpath(home), "findings.sarif"),@@ -839,9 +824,7 @@ describe("CLI", () => {});test("explains a missing export-output directory", async () => {- const root = await mkdtemp(- join(tmpdir(), "codex-security-export-missing-"),- );+ const root = await temporaryDirectory("codex-security-export-missing-");try {const output = join(root, "reports", "results.sarif");const stderr = capture();@@ -864,7 +847,7 @@ describe("CLI", () => {});test("rejects a repository-controlled output symlink without following it", async () => {- const directory = await mkdtemp(join(tmpdir(), "codex-security-export-"));+ const directory = await temporaryDirectory("codex-security-export-");try {const scan = await copyCompletedScan(directory);const outside = join(directory, "outside.txt");@@ -890,7 +873,7 @@ describe("CLI", () => {});test("passes the canonical scan directory to the exporter", async () => {- const directory = await mkdtemp(join(tmpdir(), "codex-security-export-"));+ const directory = await temporaryDirectory("codex-security-export-");try {const actual = join(directory, "actual");const linked = join(directory, "linked");@@ -920,7 +903,7 @@ describe("CLI", () => {});test("creates the optional scan-local exports directory", async () => {- const directory = await mkdtemp(join(tmpdir(), "codex-security-export-"));+ const directory = await temporaryDirectory("codex-security-export-");try {const scan = await copyCompletedScan(directory);const output = join(scan, "exports", "results.sarif");@@ -940,7 +923,7 @@ describe("CLI", () => {});test("exports through a symlinked output parent", async () => {- const directory = await mkdtemp(join(tmpdir(), "codex-security-export-"));+ const directory = await temporaryDirectory("codex-security-export-");try {const scan = await copyCompletedScan(directory);const actualOutput = join(directory, "actual-output");@@ -974,7 +957,7 @@ describe("CLI", () => {});test("rejects a symlinked output parent inside the scan directory", async () => {- const directory = await mkdtemp(join(tmpdir(), "codex-security-export-"));+ const directory = await temporaryDirectory("codex-security-export-");try {const scan = join(directory, "scan");const outside = join(directory, "outside");@@ -1016,7 +999,7 @@ describe("CLI", () => {});test("rejects a repository-controlled output-directory symlink", async () => {- const directory = await mkdtemp(join(tmpdir(), "codex-security-export-"));+ const directory = await temporaryDirectory("codex-security-export-");try {const scan = join(directory, "scan");const repository = join(directory, "repo");diff --git a/sdk/typescript/tests-ts/cli-feedback.test.ts b/sdk/typescript/tests-ts/cli-feedback.test.tsindex e66c7975f..71be83331 100644--- a/sdk/typescript/tests-ts/cli-feedback.test.ts+++ b/sdk/typescript/tests-ts/cli-feedback.test.ts@@ -1,13 +1,14 @@+import { PLUGIN_ROOT } from "./plugin-root.js";import { spawnSync } from "node:child_process";-import { mkdir, mkdtemp, realpath, rm } from "node:fs/promises";-import { tmpdir } from "node:os";+import { mkdir, rm } from "node:fs/promises";import { join } from "node:path";import { expect, mock, test } from "bun:test";import { main } from "../src/cli.js";import type { JsonObject } from "../src/config.js";import { resolvePluginPython, runWorkbench } from "../src/runtime.js";import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";-import { PLUGIN_ROOT } from "./plugin-root.js";+import { temporaryDirectory } from "./support/temporary-directories.js";+import { rejecting, throwing } from "./support/errors.js";async function run(args: string[], deps = dependencies()) {const stdout = capture();@@ -22,9 +23,7 @@ async function run(args: string[], deps = dependencies()) {}test("feedback selects the newest saved scan when an older scan is still running", async () => {- const root = await realpath(- await mkdtemp(join(tmpdir(), "feedback-history-")),- );+ const root = await temporaryDirectory("feedback-history-", true);try {const python = await resolvePluginPython();const repository = join(root, "repository");@@ -170,13 +169,9 @@ for (const args of []) {test(`feedback rejects invalid arguments ${JSON.stringify(args)}`, async () => {const deps = dependencies({- onWorkbench: () => {- throw new Error("Must not read scans");- },+ onWorkbench: throwing("Must not read scans"),});- deps.sendFeedback = async () => {- throw new Error("Must not upload");- };+ deps.sendFeedback = rejecting("Must not upload");const result = await run(args, deps);expect(result.code).toBe(2);expect(result.stdout).toBe("");@@ -192,9 +187,7 @@ for (const missingScan of [false, true]) {return { scans: [] };},});- const upload = mock(async () => {- throw new Error("Upload failed");- });+ const upload = mock(rejecting("Upload failed"));deps.sendFeedback = upload;const result = await run([diff --git a/sdk/typescript/tests-ts/cli-fixtures.ts b/sdk/typescript/tests-ts/cli-fixtures.tsindex 185bd26f9..367a0bc40 100644--- a/sdk/typescript/tests-ts/cli-fixtures.ts+++ b/sdk/typescript/tests-ts/cli-fixtures.ts@@ -14,11 +14,16 @@ import type {ScanWorkerStatus,SeverityLevel,} from "../src/index.js";-import { ScanResult } from "../src/index.js";+import { CodexSecurityError, ScanResult } from "../src/index.js";import type { UpdateNotice } from "../src/version.js";+import { throwing } from "./support/errors.js";type MainDependencies = NonNullable<Parameters<typeof main>[3]>;+export type OnCodex = (+ ...arguments_: Parameters<MainDependencies["runCodex"]>+) => number | Promise<number>;+export const SYNTHETIC_CREDENTIALS = ["sk-proj-SYNTHETIC_KEY_123","Bearer SYNTHETIC_TOKEN_123",@@ -71,7 +76,7 @@ export const SYNTHETIC_CREDENTIALS = ["https://example.test/?redirect_uri=https%3A%2F%2Finner.test%2Fcb%3Frefresh_token%3DSYNTHETIC_NESTED_REFRESH_123%26password%3DSYNTHETIC_NESTED_PASSWORD_123%26safe%3D1",].join(" ");-export function capture(isTTY = false): {+export function capture(isTTY: boolean | null = false): {stream: Pick<NodeJS.WriteStream, "write"> &Partial<Pick<NodeJS.WriteStream, "isTTY">>;text(): string;@@ -79,7 +84,7 @@ export function capture(isTTY = false): {let value = "";return {stream: {- isTTY,+ ...(isTTY === null ? {} : { isTTY }),write(chunk: string | Uint8Array): boolean {value += chunk.toString();return true;@@ -103,6 +108,18 @@ export function fakePreflight(};}+export const savedRecipe = (+ config: JsonObject = {},+ target: JsonObject = { kind: "repository", paths: [] },+) => ({+ recipe: {+ repository: "/original/repository",+ target,+ mode: "standard",+ config,+ },+});+export function fakeResult(severityLevels: readonly SeverityLevel[] = [],completeness: CoverageDocument["completeness"] = "complete",@@ -186,13 +203,11 @@ export class FakeSignals {export function dependencies(options: {onConfig?: (config: CodexSecurityConfig) => void;- onTurn?: (repository: string, options: unknown) => void;+ onTurn?: (repository: string, options: ScanOptions) => void;onRun?: () => void;onInterrupt?: () => void;onClose?: () => void | Promise<void>;- onCodex?: (- ...arguments_: Parameters<MainDependencies["runCodex"]>- ) => number | Promise<number>;+ onCodex?: OnCodex;linearClient?: MainDependencies["linearClient"];importGitHubAlerts?: MainDependencies["importGitHubAlerts"];onRepositoryCommand?: (@@ -257,7 +272,7 @@ export function dependencies(checkForUpdate: async (signal) => await options.onUpdateCheck?.(signal),currentDirectory: () => options.currentDirectory ?? "/current/repository",now: () => 0,- setInterval: () => ({}) as NodeJS.Timeout,+ setInterval: fakeInterval,clearInterval: () => {},addSignalListener: (signal, listener) => signals.add(signal, listener),removeSignalListener: (signal, listener) =>@@ -296,3 +311,28 @@ export function dependencies(),};}++export const mustNotInitializeCodex = throwing("must not initialize Codex");++export function fakeSecurity(run: CodexSecurity["run"]) {+ return { run, preflight: async () => fakePreflight(), close: async () => {} };+}++export function failingSecurity(message: string) {+ return fakeSecurity(async () => {+ throw new CodexSecurityError(message);+ });+}++export function fakeInterval(_callback: () => void): NodeJS.Timeout {+ return {} as NodeJS.Timeout;+}++export const warningResult =+ (message: string, targetChanged = false) =>+ async (_repository: string, options?: ScanOptions) => {+ if (targetChanged)+ options?.onWarning?.(message, { kind: "target_changed" });+ else options?.onWarning?.(message);+ return fakeResult();+ };diff --git a/sdk/typescript/tests-ts/cli-history-paths.test.ts b/sdk/typescript/tests-ts/cli-history-paths.test.tsindex 8a8c9372c..aea34277f 100644--- a/sdk/typescript/tests-ts/cli-history-paths.test.ts+++ b/sdk/typescript/tests-ts/cli-history-paths.test.ts@@ -1,7 +1,7 @@import { mkdir, mkdtemp, realpath, rm } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";-import { expect, test } from "bun:test";+import { expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import { capture, dependencies } from "./cli-fixtures.js";@@ -16,7 +16,9 @@ test.skipIf(process.platform !== "win32")(const ambiguous = join(root, "history.");await Promise.all([mkdir(scanRoot), mkdir(ambiguous)]);expect(await realpath(scanRoot)).not.toBe(await realpath(ambiguous));- let workbenchCalls = 0;+ const onWorkbench = mock(() => {+ return { scans: [] };+ });const stderr = capture();expect(@@ -26,14 +28,11 @@ test.skipIf(process.platform !== "win32")(stderr.stream,dependencies({currentDirectory: root,- onWorkbench: () => {- workbenchCalls += 1;- return { scans: [] };- },+ onWorkbench,}),),).toBe(2);- expect(workbenchCalls).toBe(0);+ expect(onWorkbench).toHaveBeenCalledTimes(0);expect(stderr.text()).toContain("Windows-ambiguous components");} finally {await rm(root, { recursive: true, force: true });diff --git a/sdk/typescript/tests-ts/cli-launcher.test.ts b/sdk/typescript/tests-ts/cli-launcher.test.tsindex 9cc72797a..f45ca00ba 100644--- a/sdk/typescript/tests-ts/cli-launcher.test.ts+++ b/sdk/typescript/tests-ts/cli-launcher.test.ts@@ -1,23 +1,16 @@-import {- copyFile,- mkdir,- mkdtemp,- rm,- symlink,- writeFile,-} from "node:fs/promises";-import { tmpdir } from "node:os";+import { copyFile, mkdir, rm, symlink, writeFile } from "node:fs/promises";import { join } from "node:path";import { describe, expect, test } from "bun:test";import { VERSION } from "../src/index.js";import { SYNTHETIC_CREDENTIALS } from "./cli-fixtures.js";import { runCommand } from "./support/shell.js";+import { temporaryDirectory } from "./support/temporary-directories.js";const packageRoot = join(import.meta.dir, "..");describe("CLI launcher", () => {test("runs through an installed npm-style bin symlink", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-cli-bin-"));+ const root = await temporaryDirectory("codex-security-cli-bin-");try {const launcher = join(packageRoot, "src", "cli.ts");const bin =@@ -40,7 +33,7 @@ describe("CLI launcher", () => {});test("maps unexpected source-entrypoint failures to exit 2", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-cli-failure-"));+ const root = await temporaryDirectory("codex-security-cli-failure-");try {const preload = join(root, "unavailable-cwd.mjs");await writeFile(@@ -64,9 +57,7 @@ describe("CLI launcher", () => {});test("maps installed-launcher failures to a fixed startup error", async () => {- const root = await mkdtemp(- join(tmpdir(), "codex-security-cli-bin-failure-"),- );+ const root = await temporaryDirectory("codex-security-cli-bin-failure-");try {const launcher = join(root, "bin", "codex-security.mjs");await mkdir(join(root, "bin"), { recursive: true });diff --git a/sdk/typescript/tests-ts/cli-patch-results.test.ts b/sdk/typescript/tests-ts/cli-patch-results.test.tsindex 0c50188e1..1b5a3a738 100644--- a/sdk/typescript/tests-ts/cli-patch-results.test.ts+++ b/sdk/typescript/tests-ts/cli-patch-results.test.ts@@ -100,15 +100,7 @@ lines.on("line", (line) => {const outcome = await fixture.patch(["Synthetic issue", "--external-sandbox"],{- onCodex: (_args, output, environment) =>- runCodexSkillCommand(- ["-e", source],- output,- {- command: process.execPath,- },- environment,- ),+ onCodex: nodeCodex(source),},);expect(outcome.status).toBe(0);@@ -269,15 +261,7 @@ lines.on("line", (line) => {const outcome = await fixture.patch(["Synthetic issue", "--full-output"],{- onCodex: (_args, output, environment) =>- runCodexSkillCommand(- ["-e", source],- output,- {- command: process.execPath,- },- environment,- ),+ onCodex: nodeCodex(source),},);expect(outcome.status).toBe(2);@@ -293,3 +277,13 @@ lines.on("line", (line) => {},);});++function nodeCodex(source: string): NonNullable<FixtureOptions["onCodex"]> {+ return (_args, output, environment) =>+ runCodexSkillCommand(+ ["-e", source],+ output,+ { command: process.execPath },+ environment,+ );+}diff --git a/sdk/typescript/tests-ts/cli-patch-trust.test.ts b/sdk/typescript/tests-ts/cli-patch-trust.test.tsindex 1dcb24aba..3e7ce3a58 100644--- a/sdk/typescript/tests-ts/cli-patch-trust.test.ts+++ b/sdk/typescript/tests-ts/cli-patch-trust.test.ts@@ -1,15 +1,7 @@import { execFileSync, spawn } from "node:child_process";import { once } from "node:events";import { existsSync } from "node:fs";-import {- mkdir,- mkdtemp,- readFile,- realpath,- rm,- writeFile,-} from "node:fs/promises";-import { tmpdir } from "node:os";+import { mkdir, readFile, rm, writeFile } from "node:fs/promises";import { join } from "node:path";import { createInterface } from "node:readline";import { Writable } from "node:stream";@@ -18,6 +10,7 @@ import { parse as parseToml } from "smol-toml";import { readSkillCommandOutput } from "../src/cli.js";import { writeCodexConfig } from "../src/config.js";import { resolveCodexCommand } from "../src/runtime.js";+import { temporaryDirectory } from "./support/temporary-directories.js";test.each([[undefined, "patch"],@@ -28,9 +21,7 @@ test.each([] as const)("preserves project trust and protects verification (%s, %s)",async (trust, mode) => {- const root = await realpath(- await mkdtemp(join(tmpdir(), "codex-security-patch-trust-")),- );+ const root = await temporaryDirectory("codex-security-patch-trust-", true);const repository = join(root, "repository");const codexHome = join(root, "codex-home");const marker = join(root, "mcp-started");@@ -219,8 +210,9 @@ test.each([test.each(["openai", undefined])("app-server preserves explicit and native providers during ephemeral API-key login (%s)",async (selectedProvider) => {- const codexHome = await realpath(- await mkdtemp(join(tmpdir(), "codex-security-key-login-")),+ const codexHome = await temporaryDirectory(+ "codex-security-key-login-",+ true,);const stored = JSON.stringify({auth_mode: "apikey",diff --git a/sdk/typescript/tests-ts/cli-patch.test.ts b/sdk/typescript/tests-ts/cli-patch.test.tsindex c44517c3f..7687f796a 100644--- a/sdk/typescript/tests-ts/cli-patch.test.ts+++ b/sdk/typescript/tests-ts/cli-patch.test.ts@@ -1,15 +1,11 @@+import { gitText } from "./support/shell.js";+import { emptyPage } from "./support/linear-pagination.js";+import { resolving } from "./support/promises.js";import { parse as parseToml } from "smol-toml";-import { describe, expect, test } from "bun:test";+import { describe, expect, test, mock } from "bun:test";import { execFileSync } from "node:child_process";-import { createHash } from "node:crypto";-import {- mkdir,- mkdtemp,- readFile,- rm,- symlink,- writeFile,-} from "node:fs/promises";+import { hash } from "node:crypto";+import { mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { dirname, join, relative, resolve } from "node:path";import { Writable } from "node:stream";@@ -18,6 +14,8 @@ import type { Finding, JsonObject, SeverityLevel } from "../src/index.js";import { main } from "../src/cli.js";import type { LinearClientFactory } from "../src/linear.js";import { capture, dependencies, fakeResult } from "./cli-fixtures.js";+import { temporaryDirectory } from "./support/temporary-directories.js";+import { throwing } from "./support/errors.js";const CURRENT_REPOSITORY = resolve("/current/repository");const SAVED_REPOSITORY = resolve("/saved/repository");@@ -42,11 +40,12 @@ function resultWithFindings(severities: readonly SeverityLevel[]) {function savedScan(result: ReturnType<typeof resultWithFindings>,scanId = "scan-1",+ targetPath = SAVED_REPOSITORY,): JsonObject {return {scan: {scanId,- targetPath: SAVED_REPOSITORY,+ targetPath,findings: result.findings.findings as unknown as JsonObject[],},};@@ -153,7 +152,8 @@ describe("scan and patch workflow", () => {let snapshotHadProgress = false;let resultSnapshotHadProgress = false;let modelStarted = false;- let timers = 0;+ const setInterval = mock(() => ({}) as NodeJS.Timeout);+ const clearInterval = mock();const current = dependencies({result,onWorkbench: () => savedScan(result),@@ -165,7 +165,8 @@ describe("scan and patch workflow", () => {if (failSnapshot) throw new Error("Baseline snapshot failed.");}if (args.includes("add") && modelStarted)- resultSnapshotHadProgress = timers > 0;+ resultSnapshotHadProgress =+ setInterval.mock.calls.length > clearInterval.mock.calls.length;return args.includes("--name-only") ? "src/finding-1.ts\0" : "";},onCodex: (args, output) => {@@ -174,13 +175,8 @@ describe("scan and patch workflow", () => {return 0;},});- current.setInterval = () => {- timers += 1;- return {} as NodeJS.Timeout;- };- current.clearInterval = () => {- timers -= 1;- };+ current.setInterval = setInterval;+ current.clearInterval = clearInterval;const status = await main(["scan", "--patch", "--patch-severity", "high"],@@ -193,7 +189,9 @@ describe("scan and patch workflow", () => {expect(resultSnapshotHadProgress).toBe(!failSnapshot);expect(modelStarted).toBe(!failSnapshot);expect(status).toBe(failSnapshot ? 2 : 0);- expect(timers).toBe(0);+ expect(setInterval.mock.calls.length).toBe(+ clearInterval.mock.calls.length,+ );if (failSnapshot)expect(stderr.text()).toContain("Baseline snapshot failed.");},@@ -253,22 +251,19 @@ describe("scan and patch workflow", () => {const stderr = capture(true);Object.assign(stderr.stream, { columns: 36 });let now = 0;- let tick: (() => void) | undefined;const current = dependencies({result,onWorkbench: () => savedScan(result),onCodex: (args, output) => {now = 84_000;- tick?.();+ setIntervalMock.mock.lastCall?.[0]?.();expect(completePatches(args, output)[0]!.title).toBe(title);return 0;},});+ const setIntervalMock = mock(current.setInterval);current.now = () => now;- current.setInterval = (callback) => {- tick = callback;- return {} as NodeJS.Timeout;- };+ current.setInterval = setIntervalMock;current.clearInterval = () => {};expect(@@ -397,7 +392,8 @@ describe("scan and patch workflow", () => {test("stops patch progress on interruption or an agent error", async () => {for (const status of [130, "error"] as const) {const result = resultWithFindings(["high", "high"]);- let timers = 0;+ const setInterval = mock(() => ({}) as NodeJS.Timeout);+ const clearInterval = mock();const outcome = await runWorkflow(["patch", "--scan", "scan-1", "--json"],{@@ -411,18 +407,15 @@ describe("scan and patch workflow", () => {{interactive: true,configure: (current) => {- current.setInterval = () => {- timers += 1;- return {} as NodeJS.Timeout;- };- current.clearInterval = () => {- timers -= 1;- };+ current.setInterval = setInterval;+ current.clearInterval = clearInterval;},},);expect(outcome.exitCode).not.toBe(0);- expect(timers).toBe(0);+ expect(setInterval.mock.calls.length).toBe(+ clearInterval.mock.calls.length,+ );expect(outcome.stderr).toContain("\u001B[?25h");expect(outcome.stderr).not.toContain("Patching 2/2");expect(outcome.stderr).toContain(@@ -444,7 +437,7 @@ describe("scan and patch workflow", () => {test.each(["literal", "file", "linear"])("passes custom validation instructions to the %s patch task",async (source) => {- const repository = await mkdtemp(join(tmpdir(), "patch-validation-"));+ const repository = await temporaryDirectory("patch-validation-");const validation ="Start the local app. Exercise the fix and a legitimate request. Stop the app.\n";try {@@ -478,10 +471,7 @@ describe("scan and patch workflow", () => {title: "Synthetic security issue",description: "Synthetic issue details",url: "https://linear.app/example/issue/SEC-123",- comments: async () => ({- nodes: [],- pageInfo: { hasNextPage: false },- }),+ comments: emptyPage,}),}) as unknown as ReturnType<LinearClientFactory>,onCodex: (_args, output) => {@@ -515,7 +505,7 @@ describe("scan and patch workflow", () => {);test("reads validation from the invocation directory once for all saved findings", async () => {- const directory = await mkdtemp(join(tmpdir(), "saved-patch-validation-"));+ const directory = await temporaryDirectory("saved-patch-validation-");const repository = join(directory, "repository");const validation = "Build the app and run the regression tests.\n";const result = resultWithFindings(["high", "medium"]);@@ -534,13 +524,7 @@ describe("scan and patch workflow", () => {],{currentDirectory: directory,- onWorkbench: () => ({- scan: {- scanId: "scan-1",- targetPath: repository,- findings: result.findings.findings as unknown as JsonObject[],- },- }),+ onWorkbench: () => savedScan(result, "scan-1", repository),onCodex: async (args, output) => {calls++;expect(output?.appServer?.directory).toBe(repository);@@ -575,7 +559,7 @@ describe("scan and patch workflow", () => {])("checks the invocation checkout boundary for %s prompts from a %s",async (kind, invocation) => {- const root = await mkdtemp(join(tmpdir(), "patch-prompt-boundary-"));+ const root = await temporaryDirectory("patch-prompt-boundary-");const checkout = join(root, "invocation");const directory =invocation === "root" ? checkout : join(checkout, "nested", "cwd");@@ -617,13 +601,7 @@ describe("scan and patch workflow", () => {],{currentDirectory: directory,- onWorkbench: () => ({- scan: {- scanId: "scan-1",- targetPath: repository,- findings: result.findings.findings as unknown as JsonObject[],- },- }),+ onWorkbench: () => savedScan(result, "scan-1", repository),onCodex: (args, output) => {started = true;completePatches(args, output);@@ -644,14 +622,12 @@ describe("scan and patch workflow", () => {test.each(["missing", "empty", "directory"])("rejects a %s validation prompt before starting a patch",async (kind) => {- const directory = await mkdtemp(- join(tmpdir(), "invalid-patch-validation-"),- );+ const directory = await temporaryDirectory("invalid-patch-validation-");try {const path = join(directory, "validation.md");if (kind === "empty") await writeFile(path, " \n");if (kind === "directory") await mkdir(path);- let started = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const outcome = await runWorkflow(["patch",@@ -662,14 +638,11 @@ describe("scan and patch workflow", () => {],{currentDirectory: directory,- onCodex: () => {- started = true;- return 0;- },+ onCodex,},);expect(outcome.exitCode).toBe(2);- expect(started).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();expect(JSON.parse(outcome.stdout)).toMatchObject({ok: false,applied: false,@@ -802,9 +775,7 @@ describe("scan and patch workflow", () => {});test("assesses only changes made during a literal patch run", async () => {- const directory = await mkdtemp(- join(tmpdir(), "codex-security-patch-risk-"),- );+ const directory = await temporaryDirectory("codex-security-patch-risk-");const repository = join(directory, "repository");await mkdir(repository, { recursive: true });const git = repositoryGit(repository);@@ -879,16 +850,7 @@ describe("scan and patch workflow", () => {output?.stdout.write("Patch complete.");return 0;},- onRepositoryCommand: (command, args, workingDirectory, options) => {- expect(command).toBe("git");- const result = execFileSync("git", args, {- cwd: workingDirectory,- encoding: "utf8",- env: { ...process.env, ...options?.environment },- stdio: ["ignore", "pipe", "pipe"],- });- return options?.trim === false ? result : result.trim();- },+ onRepositoryCommand: runGitRepositoryCommand,},);@@ -900,8 +862,8 @@ describe("scan and patch workflow", () => {});test("creates a draft pull request with the Linear patch-risk summary", async () => {- const directory = await mkdtemp(- join(tmpdir(), "codex-security-linear-patch-pr-"),+ const directory = await temporaryDirectory(+ "codex-security-linear-patch-pr-",);const repository = join(directory, "repository");const remote = join(directory, "remote.git");@@ -988,13 +950,12 @@ describe("scan and patch workflow", () => {) => {expect(workingDirectory).toBe(repository);if (command === "git") {- const result = execFileSync("git", args, {- cwd: repository,- encoding: "utf8",- env: { ...process.env, ...commandOptions?.environment },- stdio: ["ignore", "pipe", "pipe"],- });- return commandOptions?.trim === false ? result : result.trim();+ return runGitRepositoryCommand(+ command,+ args,+ workingDirectory,+ commandOptions,+ );}if (args[1] === "list") return "";pullRequestArguments = args;@@ -1039,9 +1000,7 @@ describe("scan and patch workflow", () => {});test("assesses a patch larger than the repository command buffer", async () => {- const directory = await mkdtemp(- join(tmpdir(), "codex-security-large-patch-"),- );+ const directory = await temporaryDirectory("codex-security-large-patch-");const repository = join(directory, "repository");await mkdir(repository, { recursive: true });const git = repositoryGit(repository);@@ -1071,9 +1030,7 @@ describe("scan and patch workflow", () => {) as { path: string; sha256: string };const patch = await readFile(artifact.path);expect(patch.byteLength).toBeGreaterThan(1024 * 1024);- expect(createHash("sha256").update(patch).digest("hex")).toBe(- artifact.sha256,- );+ expect(hash("sha256", patch)).toBe(artifact.sha256);output.stdout.write(patchRiskAssessment().report);return 0;}@@ -1084,16 +1041,7 @@ describe("scan and patch workflow", () => {output?.stdout.write("Patch complete.");return 0;},- onRepositoryCommand: (command, args, workingDirectory, options) => {- expect(command).toBe("git");- const result = execFileSync("git", args, {- cwd: workingDirectory,- encoding: "utf8",- env: { ...process.env, ...options?.environment },- stdio: ["ignore", "pipe", "pipe"],- });- return options?.trim === false ? result : result.trim();- },+ onRepositoryCommand: runGitRepositoryCommand,},);@@ -1364,7 +1312,7 @@ describe("scan and patch workflow", () => {});test("publishes only verified patch files and preserves unrelated staged changes", async () => {- const directory = await mkdtemp(join(tmpdir(), "codex-security-patch-pr-"));+ const directory = await temporaryDirectory("codex-security-patch-pr-");const repository = join(directory, "repository");const remote = join(directory, "remote.git");const url = "https://github.example.test/example/repository/pull/15";@@ -1411,13 +1359,7 @@ describe("scan and patch workflow", () => {{currentDirectory: repository,result,- onWorkbench: () => ({- scan: {- scanId: "scan",- targetPath: repository,- findings: result.findings.findings as unknown as JsonObject[],- },- }),+ onWorkbench: () => savedScan(result, "scan", repository),onCodex: async (args, output) => {if (output?.appServer?.prompt.includes(@@ -1446,9 +1388,7 @@ describe("scan and patch workflow", () => {expect(artifact.sourceType).toBe("patch_file");expect(artifact.changedFiles).toEqual(["src/finding-1.ts"]);expect(patch.toString()).toEndWith("+fixed \n");- expect(createHash("sha256").update(patch).digest("hex")).toBe(- artifact.sha256,- );+ expect(hash("sha256", patch)).toBe(artifact.sha256);output.stdout.write(patchRiskAssessment().report);return 0;}@@ -1467,13 +1407,12 @@ describe("scan and patch workflow", () => {) => {expect(workingDirectory).toBe(repository);if (command === "git") {- const result = execFileSync("git", args, {- cwd: repository,- encoding: "utf8",- env: { ...process.env, ...commandOptions?.environment },- stdio: ["ignore", "pipe", "pipe"],- });- return commandOptions?.trim === false ? result : result.trim();+ return runGitRepositoryCommand(+ command,+ args,+ workingDirectory,+ commandOptions,+ );}githubCommands.push([...args]);if (args[1] === "list") return "";@@ -1537,9 +1476,7 @@ describe("scan and patch workflow", () => {])("resumes %s publication after %s fails without patching again",async (provider, failure) => {- const directory = await mkdtemp(- join(tmpdir(), "codex-security-pr-retry-"),- );+ const directory = await temporaryDirectory("codex-security-pr-retry-");const repository = join(directory, "repository");const remote = join(directory, "remote.git");const branch = "codex-security/patch-scan-1";@@ -1572,13 +1509,7 @@ describe("scan and patch workflow", () => {const fixtures: Parameters<typeof dependencies>[0] = {currentDirectory: repository,- onWorkbench: () => ({- scan: {- scanId: "scan-1",- targetPath: repository,- findings: result.findings.findings as unknown as JsonObject[],- },- }),+ onWorkbench: () => savedScan(result, "scan-1", repository),onCodex: async (args, output) => {modelCalls += 1;await writeFile(join(repository, "src", "finding-1.ts"), "fixed\n");@@ -1667,14 +1598,11 @@ describe("scan and patch workflow", () => {test("refuses to resume a missing or changed patch commit", async () => {for (const saved of ["", "saved-commit"]) {- let modelCalls = 0;+ const onCodex = mock<() => number>().mockReturnValue(0);const outcome = await runWorkflow(["patch", "--resume-pr", "codex-security/patch-scan-1"],{- onCodex: () => {- modelCalls += 1;- return 0;- },+ onCodex,onRepositoryCommand: (command, args) => {expect(command).toBe("git");return args[0] === "config" ? saved : "changed-commit";@@ -1685,7 +1613,7 @@ describe("scan and patch workflow", () => {expect(outcome.stderr).toContain(saved ? "changed since verification" : "No verified patch commit",);- expect(modelCalls).toBe(0);+ expect(onCodex).toHaveBeenCalledTimes(0);}});@@ -1700,23 +1628,19 @@ describe("scan and patch workflow", () => {["--external-sandbox"],["occ_1"],]) {- let commandStarted = false;+ const onCodex = mock<() => number>().mockReturnValue(0);+ const onRepositoryCommand = mock<() => string>().mockReturnValue("");const outcome = await runWorkflow(["patch", "--resume-pr", "codex-security/patch-scan-1", ...input],{- onCodex: () => {- commandStarted = true;- return 0;- },- onRepositoryCommand: () => {- commandStarted = true;- return "";- },+ onCodex,+ onRepositoryCommand,},);expect(outcome.exitCode).toBe(2);expect(outcome.stderr).toContain("--resume-pr cannot be combined");- expect(commandStarted).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();+ expect(onRepositoryCommand).not.toHaveBeenCalled();}});@@ -1846,18 +1770,15 @@ describe("scan and patch workflow", () => {);test("does not patch incomplete scans or allow patching during a dry run", async () => {- let invoked = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const incomplete = resultWithFindings(["high"]);incomplete.coverage.completeness = "partial";const partial = await runWorkflow(["scan", "--patch", "--json"], {result: incomplete,- onCodex: () => {- invoked = true;- return 0;- },+ onCodex,});expect(partial.exitCode).toBe(2);- expect(invoked).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();const dryRun = await runWorkflow(["scan", "--patch", "--dry-run"]);expect(dryRun.exitCode).toBe(2);@@ -1963,8 +1884,8 @@ describe("scan and patch workflow", () => {test("does not offer patch review when there are no actionable findings", async () => {for (const severities of [[], ["informational"]] as const) {- let offered = false;- let opened = false;+ const confirmPatchReview = mock(resolving(true));+ const patchEditor = mock(resolving(null));const outcome = await runWorkflow(["scan"],{@@ -1974,14 +1895,8 @@ describe("scan and patch workflow", () => {{interactive: true,configure: (value) => {- value.confirmPatchReview = async () => {- offered = true;- return true;- };- value.patchEditor = async () => {- opened = true;- return null;- };+ value.confirmPatchReview = confirmPatchReview;+ value.patchEditor = patchEditor;},},);@@ -1989,8 +1904,8 @@ describe("scan and patch workflow", () => {expect(outcome.exitCode).toBe(0);expect(outcome.stderr).toContain(`FINDINGS ${severities.length}`);expect(outcome.stderr).not.toContain("Review and patch these findings?");- expect(offered).toBe(false);- expect(opened).toBe(false);+ expect(confirmPatchReview).not.toHaveBeenCalled();+ expect(patchEditor).not.toHaveBeenCalled();}});@@ -2263,11 +2178,9 @@ describe("scan and patch workflow", () => {{result,onWorkbench: () => savedScan(result),- onRepositoryCommand: () => {- throw new Error(- "GitHub rejected github_pat_SYNTHETIC_SECRET_123\u001b[2J\ncontinued",- );- },+ onRepositoryCommand: throwing(+ "GitHub rejected github_pat_SYNTHETIC_SECRET_123\u001b[2J\ncontinued",+ ),},);@@ -2394,7 +2307,7 @@ describe("scan and patch workflow", () => {expect(scan.exitCode).toBe(2);expect(scan.stderr).toContain("--create-pr requires --patch");- const directory = await mkdtemp(join(tmpdir(), "codex-security-dirty-pr-"));+ const directory = await temporaryDirectory("codex-security-dirty-pr-");const git = repositoryGit(directory);try {git("init", "--initial-branch=main");@@ -2404,32 +2317,20 @@ describe("scan and patch workflow", () => {git("add", "--", "app.ts");git("commit", "-m", "Initial synthetic checkout");await writeFile(join(directory, "app.ts"), "user change\n");- let started = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const literal = await runWorkflow(["patch", "Synthetic security issue", "--create-pr"],{currentDirectory: directory,- onCodex: () => {- started = true;- return 0;- },- onRepositoryCommand: (command, args, workingDirectory, options) => {- expect(command).toBe("git");- const result = execFileSync("git", args, {- cwd: workingDirectory,- encoding: "utf8",- env: { ...process.env, ...options?.environment },- stdio: ["ignore", "pipe", "pipe"],- });- return options?.trim === false ? result : result.trim();- },+ onCodex,+ onRepositoryCommand: runGitRepositoryCommand,},);expect(literal.exitCode).toBe(2);expect(literal.stderr).toContain("Pull request creation for supplied issues requires a clean working tree.",);- expect(started).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();} finally {await rm(directory, { recursive: true, force: true });}@@ -2444,3 +2345,15 @@ function repositoryGit(repository: string) {stdio: ["ignore", "pipe", "pipe"],}).trim();}++const runGitRepositoryCommand: NonNullable<+ NonNullable<Parameters<typeof dependencies>[0]>["onRepositoryCommand"]+> = (command, args, workingDirectory, options) => {+ expect(command).toBe("git");+ const result = gitText(args, {+ cwd: workingDirectory,+ env: { ...process.env, ...options?.environment },+ stdio: ["ignore", "pipe", "pipe"],+ });+ return options?.trim === false ? result : result.trim();+};diff --git a/sdk/typescript/tests-ts/cli-policy.test.ts b/sdk/typescript/tests-ts/cli-policy.test.tsindex 7c0ef75df..708aada88 100644--- a/sdk/typescript/tests-ts/cli-policy.test.ts+++ b/sdk/typescript/tests-ts/cli-policy.test.ts@@ -1,7 +1,7 @@import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";import { dirname, join } from "node:path";import { Writable } from "node:stream";-import { afterEach, describe, expect, test } from "bun:test";+import { afterEach, describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import {securityPolicyDiff,@@ -14,32 +14,24 @@ import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";import {POLICY,PYTHON,- policyFixture,+ createPolicyTestFixtures,stageResult,} from "./support/security-policy.js";+import { rejecting, throwing } from "./support/errors.js";-const fixtures: Awaited<ReturnType<typeof policyFixture>>[] = [];-async function fixture() {- const f = await policyFixture();- fixtures.push(f);- return f;-}-afterEach(async () => {- await Promise.all(fixtures.splice(0).map((f) => f.cleanup()));-});+const { fixture, cleanup } = createPolicyTestFixtures();+afterEach(cleanup);function prompt(overrides: Partial<PolicyPrompt> = {}): PolicyPrompt {return {isInteractive: () => false,- input: async () => {- throw new Error("Unexpected input prompt");- },+ input: rejecting("Unexpected input prompt"),...overrides,};}function policyDependencies(- f: Awaited<ReturnType<typeof policyFixture>>,+ f: Awaited<ReturnType<typeof fixture>>,options: {draft?: SecurityPolicyDraft;prompt?: PolicyPrompt;@@ -155,8 +147,8 @@ describe("policy CLI", () => {};const stdout = capture();const stderr = capture();- let closed = false;- let config: unknown;+ const onClose = mock();+ const onConfig = mock<(config: unknown) => void>();expect(await main([@@ -173,12 +165,8 @@ describe("policy CLI", () => {stderr.stream,policyDependencies(f, {draft,- onClose: () => {- closed = true;- },- onConfig: (value) => {- config = value;- },+ onClose,+ onConfig,}),),).toBe(0);@@ -190,14 +178,14 @@ describe("policy CLI", () => {expect(stderr.text()).toContain("$1.00–$2.00 (standard, context unknown)");expect(stderr.text()).toContain("[1/3]");expect(stderr.text()).not.toContain("+Requests must be authorized");- expect(config).toMatchObject({+ expect(onConfig.mock.lastCall?.[0]).toMatchObject({codexOverrides: {model: "gpt-5.6-terra",model_reasoning_effort: "high",},});expect(await readdir(f.repository)).toEqual([]);- expect(closed).toBe(true);+ expect(onClose).toHaveBeenCalled();});test("offers the scan credential chooser before interactive policy generation", async () => {@@ -306,13 +294,11 @@ describe("policy CLI", () => {for (const phase of ["status", "prompt"] as const) {const f = await fixture();const signals = new FakeSignals();- let initialized = false;+ const onConfig = mock();const deps = policyDependencies(f, {signals,prompt: prompt({ isInteractive: () => true }),- onConfig: () => {- initialized = true;- },+ onConfig,});deps.environment = { OPENAI_API_KEY: "synthetic-private-key" };deps.hasStoredChatGPTSignIn = async (signal) => {@@ -339,7 +325,7 @@ describe("policy CLI", () => {expect(await main(["policy"], capture().stream, capture(true).stream, deps),).toBe(143);- expect(initialized).toBe(false);+ expect(onConfig).not.toHaveBeenCalled();expect(await readdir(f.outputDir)).toEqual([]);expect(signals.listeners.get("SIGTERM")?.size).toBe(0);}@@ -381,9 +367,7 @@ describe("policy CLI", () => {["policy", "--headless", "--json"],stdout.stream,{- write: () => {- throw new Error("Progress output failed");- },+ write: throwing("Progress output failed"),},policyDependencies(f),),@@ -402,9 +386,7 @@ describe("policy CLI", () => {stdout.stream,stderr.stream,policyDependencies(f, {- onClose: () => {- throw new Error("synthetic cleanup failure");- },+ onClose: throwing("synthetic cleanup failure"),}),),).toBe(0);@@ -474,9 +456,7 @@ describe("policy CLI", () => {stderr.stream,policyDependencies(f, {draft,- onPreview: () => {- throw new Error("Python preview failed");- },+ onPreview: throwing("Python preview failed"),}),),).toBe(0);@@ -507,7 +487,10 @@ describe("policy CLI", () => {test("asks owner questions and previews the exact draft without writing source", async () => {const f = await fixture();const stderr = capture(true);- let asked = 0;+ const input = mock(async (question: string) => {+ expect(question).toContain("internet-facing");+ return "Private service";+ });expect(await main(["policy"],@@ -516,16 +499,12 @@ describe("policy CLI", () => {policyDependencies(f, {prompt: prompt({isInteractive: () => true,- input: async (question) => {- asked++;- expect(question).toContain("internet-facing");- return "Private service";- },+ input,}),}),),).toBe(0);- expect(asked).toBe(1);+ expect(input).toHaveBeenCalledTimes(1);expect(stderr.text()).toContain("--- /dev/null");expect(stderr.text()).toContain("+Requests must be authorized");expect(stderr.text()).toContain("Owner review:");@@ -539,12 +518,7 @@ describe("policy CLI", () => {test("preserves significant trailing spaces in the proposed diff", async () => {const f = await fixture();const draft = await f.generate({- run: async (stage) => ({- ...stageResult(stage),- ...(stage === "policy"- ? { markdown: "# Policy\n\nLast line \n" }- : {}),- }),+ run: async (stage) => stageResult(stage, "# Policy\n\nLast line \n"),});const stderr = capture(true);expect(@@ -567,9 +541,7 @@ describe("policy CLI", () => {const f = await fixture();const stdout = capture();const deps = policyDependencies(f, {- onGenerate: () => {- throw new Error("Must not generate");- },+ onGenerate: throwing("Must not generate"),});expect(await main(@@ -592,7 +564,7 @@ describe("policy CLI", () => {const f = await fixture();const signals = new FakeSignals();const stdout = capture();- let closed = false;+ const onClose = mock();expect(await main(["policy", "--dry-run", "--json"],@@ -605,14 +577,12 @@ describe("policy CLI", () => {expect(options.signal?.aborted).toBe(true);if (cooperative) options.signal!.throwIfAborted();},- onClose: () => {- closed = true;- },+ onClose,}),),).toBe(exitCode);expect(stdout.text()).toBe("");- expect(closed).toBe(true);+ expect(onClose).toHaveBeenCalled();expect(signals.listeners.get(signal)?.size).toBe(0);expect(await readdir(f.outputDir)).toEqual([]);}@@ -623,10 +593,7 @@ describe("policy CLI", () => {const f = await fixture();const markdown = `${POLICY.trimEnd()} `;const draft = await f.generate({- run: async (stage) => ({- ...stageResult(stage),- ...(stage === "policy" ? { markdown } : {}),- }),+ run: async (stage) => stageResult(stage, markdown),});const stdout = capture();expect(@@ -746,12 +713,7 @@ describe("policy CLI", () => {await mkdir(join(f.repository, scope));const draft = await f.generate({path: scope,- run: async (stage) => ({- ...stageResult(stage),- ...(stage === "policy"- ? { markdown: content, reviewNotes: [note] }- : {}),- }),+ run: async (stage) => stageResult(stage, content, note),});const deps = policyDependencies(f, { draft });const create = deps.createPolicySecurity;@@ -834,12 +796,11 @@ describe("policy CLI", () => {test("keeps policy argument and schema errors in full-output stdout", async () => {const f = await fixture();- let initialized = false;+ const createPolicySecurity = mock(+ throwing("Validation must finish before initializing Codex"),+ );const deps = policyDependencies(f);- deps.createPolicySecurity = () => {- initialized = true;- throw new Error("Validation must finish before initializing Codex");- };+ deps.createPolicySecurity = createPolicySecurity;for (const [args, message] of [[["policy", "--write"], "Unknown flag"],[["policy", "--path"], "Missing value"],@@ -866,25 +827,21 @@ describe("policy CLI", () => {expect(stderr.text()).not.toContain('"ok": false');}}- expect(initialized).toBe(false);+ expect(createPolicySecurity).not.toHaveBeenCalled();expect(await readdir(f.repository)).toEqual([]);});test("returns a full-output error when policy setup fails", async () => {const f = await fixture();const deps = policyDependencies(f);- deps.currentDirectory = () => {- throw new Error("Working directory is unavailable");- };+ deps.currentDirectory = throwing("Working directory is unavailable");const stdout = capture();expect(await main(["policy", "--json", "--full-output"],stdout.stream,{- write: () => {- throw new Error("Diagnostic output failed");- },+ write: throwing("Diagnostic output failed"),},deps,),@@ -930,7 +887,7 @@ describe("policy CLI", () => {test("returns the interrupt exit code and removes signal listeners", async () => {const f = await fixture();const signals = new FakeSignals();- let closed = false;+ const onClose = mock();expect(await main(["policy", "--headless"],@@ -938,9 +895,7 @@ describe("policy CLI", () => {capture().stream,policyDependencies(f, {signals,- onClose: () => {- closed = true;- },+ onClose,onGenerate: (_repository, options) => {signals.emit("SIGINT");options.signal!.throwIfAborted();@@ -948,7 +903,7 @@ describe("policy CLI", () => {}),),).toBe(130);- expect(closed).toBe(true);+ expect(onClose).toHaveBeenCalled();expect([...signals.listeners.values()].every((listeners) => listeners.size === 0,diff --git a/sdk/typescript/tests-ts/cli-project-config.test.ts b/sdk/typescript/tests-ts/cli-project-config.test.tsindex c28c05764..03e135995 100644--- a/sdk/typescript/tests-ts/cli-project-config.test.ts+++ b/sdk/typescript/tests-ts/cli-project-config.test.ts@@ -1,16 +1,7 @@-import {- mkdir,- mkdtemp,- readFile,- realpath,- rm,- symlink,- writeFile,-} from "node:fs/promises";+import { mkdir, readFile, symlink, writeFile } from "node:fs/promises";import { execFileSync } from "node:child_process";-import { tmpdir } from "node:os";import { join } from "node:path";-import { afterEach, expect, test } from "bun:test";+import { afterEach, expect, test, mock } from "bun:test";import { CodexSecurity, type ScanOptions } from "../src/api.js";import { main } from "../src/cli.js";import type { CodexSecurityConfig, JsonObject } from "../src/config.js";@@ -19,24 +10,24 @@ import { readProjectConfig } from "../src/project-config.js";import {capture,dependencies,- fakePreflight,fakeResult,+ fakeSecurity,} from "./cli-fixtures.js";+import { createApiTestFixtures } from "./support/temporary-directories.js";+import { rejecting, throwing } from "./support/errors.js";-const directories: string[] = [];-afterEach(async () => {- await Promise.all(- directories- .splice(0)- .map((path) => rm(path, { recursive: true, force: true })),- );-});+const { temporaryDirectory, cleanup } = createApiTestFixtures(+ "cli-project-config-",+);+afterEach(cleanup);++const cliClientFactory =+ (deps: ConstructorParameters<typeof CodexSecurity>[1]) =>+ (config: CodexSecurityConfig) =>+ new CodexSecurity(config, { ...deps }, { surface: "cli" });async function fixture(input: ProjectConfigInput | string) {- const root = await realpath(- await mkdtemp(join(tmpdir(), "cli-project-config-")),- );- directories.push(root);+ const root = await temporaryDirectory();const repository = join(root, "repository");const configDirectory = join(root, "settings");await mkdir(repository);@@ -67,9 +58,7 @@ test.each([const output = capture();const deps = dependencies({currentDirectory: input.root,- onConfig: () => {- throw new Error("No runtime for init");- },+ onConfig: throwing("No runtime for init"),});expect(await main(args, output.stream, capture().stream, deps)).toBe(0);const path = join(input.root, file ?? "codex-security.yaml");@@ -111,9 +100,7 @@ test("info resolves a config and its sources without a target, prompt reads, ordependencies({currentDirectory: input.configDirectory,environment: { CODEX_HOME: home },- onConfig: () => {- throw new Error("No runtime for info");- },+ onConfig: throwing("No runtime for info"),}),),).toBe(0);@@ -177,7 +164,7 @@ test("an explicit config flag overrides the operator-selected environment file",[[], "gpt-5.6-terra"],[["-c", override], "gpt-5.6-sol"],] as const) {- let selected: CodexSecurityConfig | undefined;+ const onConfig = mock<(config: CodexSecurityConfig) => void>();expect(await main(["scan", ...flags, "--json"],@@ -186,12 +173,11 @@ test("an explicit config flag overrides the operator-selected environment file",dependencies({currentDirectory: input.repository,environment: { CODEX_SECURITY_PROJECT_CONFIG: input.config },- onConfig: (config) => {- selected = config;- },+ onConfig,}),),).toBe(0);+ const selected = onConfig.mock.lastCall?.[0];expect(selected?.codexOverrides?.["model"]).toBe(expected);}});@@ -202,7 +188,7 @@ test.each([undefined, "standard", "daybreak_red"] as const)(const input = await fixture({scan: { cyber_access_program: "daybreak_blue" },});- let selected: ScanOptions | undefined;+ const onTurn = mock<(_target: string, options: ScanOptions) => void>();expect(await main([@@ -219,19 +205,18 @@ test.each([undefined, "standard", "daybreak_red"] as const)(capture().stream,dependencies({currentDirectory: input.repository,- onTurn: (_target, options) => {- selected = options as ScanOptions;- },+ onTurn,}),),).toBe(0);+ const selected = onTurn.mock.lastCall?.[1];expect(selected?.cyberAccessProgram).toBe(override ?? "daybreak_blue");},);test("a missing operator-selected environment file fails without falling back", async () => {const input = await fixture({});- let initialized = false;+ const onConfig = mock();expect(await main(["scan", "--json"],@@ -242,13 +227,11 @@ test("a missing operator-selected environment file fails without falling back",environment: {CODEX_SECURITY_PROJECT_CONFIG: join(input.root, "missing.yaml"),},- onConfig: () => {- initialized = true;- },+ onConfig,}),),).toBe(2);- expect(initialized).toBe(false);+ expect(onConfig).not.toHaveBeenCalled();});test.each([123, "", " "])(@@ -257,7 +240,7 @@ test.each([123, "", " "])(const input = await fixture({codex: { profile: "selected", profiles: { selected: { model } } },});- let initialized = false;+ const onConfig = mock();const stderr = capture();expect(await main(@@ -266,16 +249,14 @@ test.each([123, "", " "])(stderr.stream,dependencies({currentDirectory: input.repository,- onConfig: () => {- initialized = true;- },+ onConfig,}),),).toBe(2);expect(stderr.text()).toContain("--model must be a nonempty string when using --provider amazon-bedrock",);- expect(initialized).toBe(false);+ expect(onConfig).not.toHaveBeenCalled();},);@@ -284,7 +265,7 @@ test("rerun rejects a blank replacement when scan instructions are required", asconst prompt = join(input.root, "empty.md");await writeFile(prompt, " \n");const stderr = capture();- let initialized = false;+ const onConfig = mock();expect(await main(["scans", "rerun", "saved", "--scan-prompt-file", prompt, "--json"],@@ -301,14 +282,12 @@ test("rerun rejects a blank replacement when scan instructions are required", asrequiresScanPrompt: true,},}),- onConfig: () => {- initialized = true;- },+ onConfig,}),),).toBe(2);expect(stderr.text()).toContain("--scan-prompt-file must not be empty");- expect(initialized).toBe(false);+ expect(onConfig).not.toHaveBeenCalled();});test.each(["instructions_file", "validation_file"] as const)(@@ -327,7 +306,7 @@ test.each(["instructions_file", "validation_file"] as const)(csv,`id,repository,revision\nsource,${input.repository},${"a".repeat(40)}\n`,);- let initialized = false;+ const onConfig = mock(throwing("Runtime must not start"));const stderr = capture();const exit = await main(["bulk-scan", csv, "-c", input.config, "--json"],@@ -335,17 +314,14 @@ test.each(["instructions_file", "validation_file"] as const)(stderr.stream,dependencies({currentDirectory: input.repository,- onConfig: () => {- initialized = true;- throw new Error("Runtime must not start");- },+ onConfig,}),);expect(exit).toBe(2);expect(stderr.text()).toContain("Input files must not follow repository directory links",);- expect(initialized).toBe(false);+ expect(onConfig).not.toHaveBeenCalled();},);@@ -364,10 +340,7 @@ test("bulk scans apply config and linked operator prompts, preserve CSV scope ovoutput: { directory: "../batch-results" },};const input = await fixture(config);- const promptDirectory = await realpath(- await mkdtemp(join(tmpdir(), "bulk-operator-prompts-")),- );- directories.push(promptDirectory);+ const promptDirectory = await temporaryDirectory("bulk-operator-prompts-");await symlink(promptDirectory,join(input.configDirectory, "linked"),@@ -421,26 +394,22 @@ test("bulk scans apply config and linked operator prompts, preserve CSV scope ovconst deps = dependencies({ currentDirectory: input.root });deps.createSecurity = (native) => {expect(native.codexOverrides?.["model"]).toBe("gpt-5.6-terra");- return {- preflight: async () => fakePreflight(),- close: async () => {},- run: async (_repository, options = {}) => {- selected.push(options);- const result = fakeResult(["high"]);- await mkdir(options.outputDir!, { recursive: true });- for (const [name, content] of Object.entries({- "scan-manifest.json": result.manifest,- "findings.json": result.findings,- "coverage.json": result.coverage,- "report.md": "Synthetic report.",- }))- await writeFile(- join(options.outputDir!, name),- typeof content === "string" ? content : JSON.stringify(content),- );- return result;- },- };+ return fakeSecurity(async (_repository, options = {}) => {+ selected.push(options);+ const result = fakeResult(["high"]);+ await mkdir(options.outputDir!, { recursive: true });+ for (const [name, content] of Object.entries({+ "scan-manifest.json": result.manifest,+ "findings.json": result.findings,+ "coverage.json": result.coverage,+ "report.md": "Synthetic report.",+ }))+ await writeFile(+ join(options.outputDir!, name),+ typeof content === "string" ? content : JSON.stringify(content),+ );+ return result;+ });};const args = ["bulk-scan",@@ -521,7 +490,7 @@ test.each(["standard", "deep"] as const)(join(input.configDirectory, "validation.md"),"Validate synthetic boundaries.",);- let selected: ScanOptions | undefined;+ const onTurn = mock<(_target: string, options: ScanOptions) => void>();const stdout = capture();const stderr = capture();const result = fakeResult(["high"], "complete", {@@ -547,12 +516,11 @@ test.each(["standard", "deep"] as const)(currentDirectory: input.root,result,costUpdates: [result.cost!],- onTurn: (_repository, options) => {- selected = options as ScanOptions;- },+ onTurn,}),),).toBe(1);+ const selected = onTurn.mock.lastCall?.[1];expect(selected).toMatchObject({mode,target: ["lib"],@@ -589,9 +557,8 @@ test("actual CLI parsing preserves file values when flags are absent", async ()policy: { fail_on_severity: "high" },codex: { model: "gpt-5.6-terra", model_reasoning_effort: "high" },});- let selected: ScanOptions | undefined;- let native: CodexSecurityConfig | undefined;- let repository: string | undefined;+ const onTurn = mock<(_target: string, options: ScanOptions) => void>();+ const onConfig = mock<(config: CodexSecurityConfig) => void>();const stdout = capture();const stderr = capture();expect(@@ -602,17 +569,15 @@ test("actual CLI parsing preserves file values when flags are absent", async ()dependencies({currentDirectory: input.repository,environment: { OPENAI_API_KEY: "synthetic-test-key" },- onConfig: (value) => {- native = value;- },- onTurn: (target, value) => {- repository = target;- selected = value as ScanOptions;- },+ onConfig,+ onTurn,result: fakeResult(["high"]),}),),).toBe(1);+ const repository = onTurn.mock.lastCall?.[0];+ const selected = onTurn.mock.lastCall?.[1];+ const native = onConfig.mock.lastCall?.[0];expect(repository).toBe(input.repository);expect(selected).toMatchObject({auth: "api-key",@@ -650,8 +615,8 @@ test("CLI values override matching file values, including native objects and lissynthetic_setting: { enabled: true, names: ["first"] },},});- let selected: ScanOptions | undefined;- let native: CodexSecurityConfig | undefined;+ const onTurn = mock<(_target: string, options: ScanOptions) => void>();+ const onConfig = mock<(config: CodexSecurityConfig) => void>();expect(await main([@@ -684,15 +649,13 @@ test("CLI values override matching file values, including native objects and liscapture().stream,dependencies({currentDirectory: input.repository,- onConfig: (value) => {- native = value;- },- onTurn: (_target, value) => {- selected = value as ScanOptions;- },+ onConfig,+ onTurn,}),),).toBe(0);+ const selected = onTurn.mock.lastCall?.[1];+ const native = onConfig.mock.lastCall?.[0];expect(selected).toMatchObject({auth: "auto",mode: "deep",@@ -728,7 +691,7 @@ test("rerun accepts replacement scan and validation files relative to the invocavalidationMode: "custom",cyberAccessProgram: "daybreak_blue",};- let selected: ScanOptions | undefined;+ const onTurn = mock<(_target: string, options: ScanOptions) => void>();const stderr = capture();expect(await main(@@ -747,12 +710,11 @@ test("rerun accepts replacement scan and validation files relative to the invocadependencies({currentDirectory: input.configDirectory,onWorkbench: async () => ({ recipe }),- onTurn: (_target, options) => {- selected = options as ScanOptions;- },+ onTurn,}),),).toBe(0);+ const selected = onTurn.mock.lastCall?.[1];expect(selected).toMatchObject({scanPrompt: "Review the synthetic boundary.",validationPrompt: "Validate the synthetic boundary.",@@ -785,7 +747,7 @@ test.each([const input = await fixture({scan: { scope: structuredClone(scope) },} as ProjectConfigInput);- let selected: ScanOptions | undefined;+ const onTurn = mock<(_target: string, options: ScanOptions) => void>();const stderr = capture();expect(await main(@@ -794,12 +756,11 @@ test.each([stderr.stream,dependencies({currentDirectory: input.repository,- onTurn: (_target, value) => {- selected = value as ScanOptions;- },+ onTurn,}),),).toBe(0);+ const selected = onTurn.mock.lastCall?.[1];if (typeof target === "string" || Array.isArray(target))expect(selected?.target).toEqual(target);else expect(selected?.target).toMatchObject(target);@@ -819,7 +780,7 @@ test.each(["rejects incompatible explicit overrides: %j",async (flags, message) => {const input = await fixture({});- let initialized = false;+ const onConfig = mock();const stderr = capture();expect(await main(@@ -828,14 +789,12 @@ test.each([stderr.stream,dependencies({currentDirectory: input.repository,- onConfig: () => {- initialized = true;- },+ onConfig,}),),).toBe(2);expect(stderr.text()).toContain(message);- expect(initialized).toBe(false);+ expect(onConfig).not.toHaveBeenCalled();},);@@ -846,7 +805,7 @@ test("selecting standard mode leaves inactive file deep settings out of the actideep: { workers: 8, stop_after_consecutive_errors: 2 },},});- let selected: ScanOptions | undefined;+ const onTurn = mock<(_target: string, options: ScanOptions) => void>();expect(await main(["scan", "-c", input.config, "--mode", "standard", "--json"],@@ -854,12 +813,11 @@ test("selecting standard mode leaves inactive file deep settings out of the acticapture().stream,dependencies({currentDirectory: input.repository,- onTurn: (_target, value) => {- selected = value as ScanOptions;- },+ onTurn,}),),).toBe(0);+ const selected = onTurn.mock.lastCall?.[1];expect(selected?.mode).toBe("standard");expect(selected?.workers).toBeUndefined();expect(selected?.stopAfterConsecutiveErrors).toBeUndefined();@@ -881,7 +839,7 @@ test("file prompts use the config directory and CLI prompt overrides use the invjoin(input.repository, "validate.md"),"Synthetic CLI validation.",);- let selected: ScanOptions | undefined;+ const onTurn = mock<(_target: string, options: ScanOptions) => void>();expect(await main([@@ -896,12 +854,11 @@ test("file prompts use the config directory and CLI prompt overrides use the invcapture().stream,dependencies({currentDirectory: input.repository,- onTurn: (_target, value) => {- selected = value as ScanOptions;- },+ onTurn,}),),).toBe(0);+ const selected = onTurn.mock.lastCall?.[1];expect(selected).toMatchObject({scanPrompt: "Synthetic file instructions.",validationPrompt: "Synthetic CLI validation.",@@ -912,7 +869,7 @@ test("an explicit file can supply the model required by a provider override", asconst input = await fixture({codex: { model: "synthetic-model" },});- let native: CodexSecurityConfig | undefined;+ const onConfig = mock<(config: CodexSecurityConfig) => void>();expect(await main(["scan", "-c", input.config, "--provider", "amazon-bedrock", "--json"],@@ -920,12 +877,11 @@ test("an explicit file can supply the model required by a provider override", ascapture().stream,dependencies({currentDirectory: input.repository,- onConfig: (value) => {- native = value;- },+ onConfig,}),),).toBe(0);+ const native = onConfig.mock.lastCall?.[0];expect(native?.codexOverrides).toMatchObject({model: "synthetic-model",model_provider: "amazon-bedrock",@@ -945,7 +901,7 @@ test.each(["file", "CLI"])(},},});- let native: CodexSecurityConfig | undefined;+ const onConfig = mock<(config: CodexSecurityConfig) => void>();expect(await main([@@ -961,12 +917,11 @@ test.each(["file", "CLI"])(capture().stream,dependencies({currentDirectory: input.repository,- onConfig: (value) => {- native = value;- },+ onConfig,}),),).toBe(0);+ const native = onConfig.mock.lastCall?.[0];expect(native?.codexOverrides).toMatchObject({profile: "review",profiles: { review: { model: "synthetic-profile-model" } },@@ -982,7 +937,7 @@ test("an unselected file profile does not satisfy the provider model requirement},});const stderr = capture();- let initialized = false;+ const onConfig = mock();expect(await main(["scan", "-c", input.config, "--provider", "amazon-bedrock", "--json"],@@ -990,13 +945,11 @@ test("an unselected file profile does not satisfy the provider model requirementstderr.stream,dependencies({currentDirectory: input.repository,- onConfig: () => {- initialized = true;- },+ onConfig,}),),).toBe(2);- expect(initialized).toBe(false);+ expect(onConfig).not.toHaveBeenCalled();expect(stderr.text()).toContain("--model is required when using --provider amazon-bedrock",);@@ -1009,7 +962,7 @@ test.each([])("malformed unselected files do not affect %j", async ({ argv }) => {const input = await fixture("scan: [");await writeFile(join(input.repository, "codex-security.yaml"), "scan: [");- let initialized = false;+ const onConfig = mock();expect(await main([...argv],@@ -1017,13 +970,11 @@ test.each([capture().stream,dependencies({currentDirectory: input.repository,- onConfig: () => {- initialized = true;- },+ onConfig,}),),).toBe(0);- expect(initialized).toBe(false);+ expect(onConfig).not.toHaveBeenCalled();});test.each(["--help", "--schema"])(@@ -1048,7 +999,7 @@ test.each(["--help", "--schema"])(test("a malformed selected file fails before constructing a client", async () => {const input = await fixture("scan: [");- let initialized = false;+ const onConfig = mock();const stdout = capture();const stderr = capture();expect(@@ -1058,13 +1009,11 @@ test("a malformed selected file fails before constructing a client", async () =>stderr.stream,dependencies({currentDirectory: input.repository,- onConfig: () => {- initialized = true;- },+ onConfig,}),),).toBe(2);- expect(initialized).toBe(false);+ expect(onConfig).not.toHaveBeenCalled();expect(stderr.text()).toContain("Cannot parse project configuration");expect(JSON.parse(stdout.text())).toEqual({status: "failed",@@ -1101,27 +1050,17 @@ test("dry-run uses the real SDK without initializing its runtime and reports pro};const stdout = capture();const stderr = capture();- let initialized = false;+ const createCodex = mock(throwing("No inference in dry-run"));+ const prepareRuntime = mock(rejecting("No runtime in dry-run"));const deps = dependencies({currentDirectory: input.repository,environment,});- deps.createSecurity = (config) =>- new CodexSecurity(- config,- {- environment,- createCodex: () => {- initialized = true;- throw new Error("No inference in dry-run");- },- prepareRuntime: async () => {- initialized = true;- throw new Error("No runtime in dry-run");- },- },- { surface: "cli" },- );+ deps.createSecurity = cliClientFactory({+ environment,+ createCodex,+ prepareRuntime,+ });expect(await main([@@ -1140,7 +1079,8 @@ test("dry-run uses the real SDK without initializing its runtime and reports prodeps,),).toBe(0);- expect(initialized).toBe(false);+ expect(createCodex).not.toHaveBeenCalled();+ expect(prepareRuntime).not.toHaveBeenCalled();expect(JSON.parse(stdout.text())).toMatchObject({dryRun: true,repository: input.repository,@@ -1186,27 +1126,17 @@ test.each([CODEX_SECURITY_STATE_DIR: join(input.root, "state"),};const stderr = capture();- let initialized = false;+ const createCodex = mock(throwing("No inference"));+ const prepareRuntime = mock(rejecting("No runtime"));const deps = dependencies({currentDirectory: input.repository,environment,});- deps.createSecurity = (configuration) =>- new CodexSecurity(- configuration,- {- environment,- createCodex: () => {- initialized = true;- throw new Error("No inference");- },- prepareRuntime: async () => {- initialized = true;- throw new Error("No runtime");- },- },- { surface: "cli" },- );+ deps.createSecurity = cliClientFactory({+ environment,+ createCodex,+ prepareRuntime,+ });expect(await main(["scan", "-c", input.config, "--dry-run", "--json"],@@ -1216,7 +1146,8 @@ test.each([),).toBe(2);expect(stderr.text().toLowerCase()).toContain(message.toLowerCase());- expect(initialized).toBe(false);+ expect(createCodex).not.toHaveBeenCalled();+ expect(prepareRuntime).not.toHaveBeenCalled();},);@@ -1239,7 +1170,7 @@ test("rerun restores all saved deep settings and authentication without loadingdeepScan: deep,deepScanResolved: true,};- let selected: ScanOptions | undefined;+ const onTurn = mock<(_target: string, options: ScanOptions) => void>();expect(await main(["scans", "rerun", "saved", "--json"],@@ -1252,12 +1183,11 @@ test("rerun restores all saved deep settings and authentication without loadingCODEX_SECURITY_PROJECT_CONFIG: input.config,},onWorkbench: async () => ({ recipe }),- onTurn: (_target, value) => {- selected = value as ScanOptions;- },+ onTurn,}),),).toBe(0);+ const selected = onTurn.mock.lastCall?.[1];expect(selected).toMatchObject({...deep,auth: "api-key",@@ -1283,7 +1213,7 @@ test.each([...extra,};const stderr = capture();- let ran = false;+ const onRun = mock();expect(await main(["scans", "rerun", "saved", "--json"],@@ -1292,13 +1222,11 @@ test.each([dependencies({currentDirectory: input.repository,onWorkbench: async () => ({ recipe }),- onRun: () => {- ran = true;- },+ onRun,}),),).toBe(2);expect(stderr.text()).toContain(message);- expect(ran).toBe(false);+ expect(onRun).not.toHaveBeenCalled();},);diff --git a/sdk/typescript/tests-ts/cli-publish.test.ts b/sdk/typescript/tests-ts/cli-publish.test.tsindex 33e79894f..07070a2e8 100644--- a/sdk/typescript/tests-ts/cli-publish.test.ts+++ b/sdk/typescript/tests-ts/cli-publish.test.ts@@ -2,11 +2,12 @@ import { mkdir, rm, symlink, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { dirname, join, resolve } from "node:path";import { stripVTControlCharacters } from "node:util";-import { afterEach, describe, expect, test } from "bun:test";+import { afterEach, describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import type { CheckScanPublicationResult } from "../src/publish.js";import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";-import { createTemporaryDirectories } from "./support/temporary-directories.js";+import { createApiTestFixtures } from "./support/temporary-directories.js";+import { fail } from "./support/errors.js";const DESTINATION_OPTIONS = ["--to",@@ -16,21 +17,13 @@ const DESTINATION_OPTIONS = ["--linear-project","project-from-flags",] as const;-const temporaryDirectories = createTemporaryDirectories({ canonical: false });+const { temporaryDirectory: publicationDirectory, cleanup } =+ createApiTestFixtures("codex-security-cli-publication-", false);-afterEach(temporaryDirectories.cleanup);--async function publicationDirectory(): Promise<string> {- const directory = await temporaryDirectories.create(- "codex-security-cli-publication-",- );- return directory;-}+afterEach(cleanup);async function publicationScanDirectories(count: number): Promise<string[]> {- const root = await temporaryDirectories.create(- "codex-security-publish-picker-",- );+ const root = await publicationDirectory("codex-security-publish-picker-");return Promise.all(Array.from({ length: count }, async (_, index) => {const directory = join(root, `scan-${index}`);@@ -199,9 +192,7 @@ describe("publish scan to custom", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.publishScanToCustom = async () => {- throw new Error("must not publish");- };+ deps.publishScanToCustom = async () => fail("must not publish");expect(await main(["publish", "scan", "completed-scan", ...flags, "--json"],@@ -282,9 +273,7 @@ describe("publish check", () => {CODEX_SECURITY_LINEAR_TEAM: "environment-team",},});- deps.publishScan = async () => {- throw new Error("Check must not publish.");- };+ deps.publishScan = async () => fail("Check must not publish.");deps.checkScanPublication = async (directory, options) => {expect(directory).toBe(resolve(currentDirectory, "completed-scan"));expect(options).toEqual({@@ -325,12 +314,9 @@ describe("publish check", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.checkScanPublication = async () => {- throw new Error("The selected project is unavailable.");- };- deps.publishScan = async () => {- throw new Error("Check must not publish.");- };+ deps.checkScanPublication = async () =>+ fail("The selected project is unavailable.");+ deps.publishScan = async () => fail("Check must not publish.");expect(await main([@@ -366,29 +352,27 @@ describe("publish check", () => {environment: { CODEX_SECURITY_LINEAR_API_KEY: "synthetic-key" },});let now = 0;- let forced = false;+ const forceExit = mock();deps.now = () => now;- deps.forceExit = () => {- forced = true;- };- const { promise: operationStarted, resolve: started } =- Promise.withResolvers<void>();- const { promise: cleanup, resolve: finishCleanup } =- Promise.withResolvers<void>();+ deps.forceExit = forceExit;+ const operationStarted = Promise.withResolvers<void>();+ const cleanup = Promise.withResolvers<void>();const operation = async (_directory: string,options: { signal?: AbortSignal },): Promise<never> => {expect(options.signal).toBeInstanceOf(AbortSignal);- started();- await cleanup;+ operationStarted.resolve();+ await cleanup.promise;expect(options.signal?.reason).toBe(signal);options.signal!.throwIfAborted();throw new Error("Expected cancellation.");};deps.publishScan = operation;deps.checkScanPublication = operation;- let finished = false;+ const observeFinished = mock((status: number) => {+ return status;+ });const running = main(["publish",@@ -401,17 +385,14 @@ describe("publish check", () => {stdout.stream,stderr.stream,deps,- ).then((status) => {- finished = true;- return status;- });- await operationStarted;+ ).then(observeFinished);+ await operationStarted.promise;signals.emit(signal);- expect(finished).toBe(false);+ expect(observeFinished).not.toHaveBeenCalled();now = 500;signals.emit(signal);- expect(forced).toBe(false);- finishCleanup();+ expect(forceExit).not.toHaveBeenCalled();+ cleanup.resolve();expect(await running).toBe(expectedCode);expect(stdout.text()).toBe("");expect(stderr.text()).toContain(@@ -576,13 +557,10 @@ describe("publish scan", () => {{ scanDirectory: string; options: Record<string, unknown> } | undefined;const deps = dependencies({currentDirectory,- onWorkbench: () => {- throw new Error("scan history must not be inspected");- },+ onWorkbench: () => fail("scan history must not be inspected"),});- deps.createSecurity = () => {- throw new Error("a new security scan must not be started");- };+ deps.createSecurity = () =>+ fail("a new security scan must not be started");deps.publishScan = async (scanDirectory, options) => {invocation = { scanDirectory, options: { ...options } };return publicationResult();@@ -708,9 +686,7 @@ describe("publish scan", () => {const deps = dependencies({environment: { CODEX_SECURITY_LINEAR_API_KEY: key },});- deps.publishScan = async () => {- throw new Error(`Linear rejected ${key}.`);- };+ deps.publishScan = async () => fail(`Linear rejected ${key}.`);expect(await main(@@ -778,10 +754,8 @@ describe("publish scan", () => {const signals = new FakeSignals();const events: string[] = [];let now = 0;- const { promise: publicationStarted, resolve: enteredPublication } =- Promise.withResolvers<void>();- const { promise: recoveryFinished, resolve: finishRecovery } =- Promise.withResolvers<void>();+ const publicationStarted = Promise.withResolvers<void>();+ const recoveryFinished = Promise.withResolvers<void>();const deps = dependencies({ signals });deps.environment["CODEX_SECURITY_LINEAR_API_KEY"] = "synthetic-key";deps.now = () => now;@@ -793,8 +767,8 @@ describe("publish scan", () => {});signals.emit(signal);expect(events).toEqual([`aborted ${signal}`]);- enteredPublication();- await recoveryFinished;+ publicationStarted.resolve();+ await recoveryFinished.promise;events.push("recovered created issues");throw new Error("The publication handoff remains at /tmp/synthetic-handoff; recover it before retrying to avoid creating duplicate issues.",@@ -807,7 +781,7 @@ describe("publish scan", () => {stderr.stream,deps,);- await publicationStarted;+ await publicationStarted.promise;expect(signals.listeners.get("SIGINT")?.size).toBe(1);expect(signals.listeners.get("SIGTERM")?.size).toBe(1);expect(stdout.text()).toBe("");@@ -818,7 +792,7 @@ describe("publish scan", () => {signals.emit(signal);expect(events).toEqual([`aborted ${signal}`, `forced ${signal}`]);expect(stderr.text()).toContain("reconcile retained Linear");- finishRecovery();+ recoveryFinished.resolve();expect(await publishing).toBe(expectedCode);@@ -1098,7 +1072,7 @@ describe("publish scan", () => {const stdout = capture(scenario.interactive);const stderr = capture();const deps = dependencies({ environment: scenario.environment });- deps.publishScan = async () => publicationResult();+ deps.publishScan = successfulPublication<[]>;expect(await main(@@ -1270,7 +1244,7 @@ describe("publish scan", () => {[];let header: string | undefined;let workbenchArguments: readonly string[] | undefined;- let publishedDirectory: string | undefined;+ const publishScan = mock(successfulPublication<[string]>);const deps = dependencies({onWorkbench: (args) => {workbenchArguments = args;@@ -1334,10 +1308,7 @@ describe("publish scan", () => {return options[1]!.value;},};- deps.publishScan = async (scanDirectory) => {- publishedDirectory = scanDirectory;- return publicationResult();- };+ deps.publishScan = publishScan;expect(await main(@@ -1371,7 +1342,7 @@ describe("publish scan", () => {expect(choices.every((choice) => !choice.label.includes("unavailable")),).toBe(true);- expect(publishedDirectory).toBe(selectedDirectory);+ expect(publishScan.mock.lastCall?.[0]).toBe(selectedDirectory);expect(JSON.parse(stdout.text())).toEqual(publicationResult());expect(stderr.text()).toContain("\u001B[?1049h\u001B[?25l");expect(stripVTControlCharacters(stderr.text())).toContain(@@ -1510,7 +1481,7 @@ describe("publish scan", () => {return options[0]!.value;},};- deps.publishScan = async () => publicationResult();+ deps.publishScan = successfulPublication<[]>;expect(await main(@@ -1655,7 +1626,7 @@ describe("publish scan", () => {return options[0]!.value;},};- deps.publishScan = async () => publicationResult();+ deps.publishScan = successfulPublication<[]>;expect(await main(@@ -1886,16 +1857,7 @@ describe("publish scan", () => {const stdout = capture();const stderr = capture(true);const deps = dependencies({ environment });- deps.publishScan = async (_scanDirectory, options) => {- options.onProgress?.({ type: "started", scanId: "scan-123", total: 1 });- options.onProgress?.({- type: "completed",- created: 1,- failed: 0,- total: 1,- });- return publicationResult();- };+ deps.publishScan = publishWithProgress;expect(await main(@@ -1939,10 +1901,8 @@ describe("publish scan", () => {};const listeners = new Map<string, () => void>();const removed: string[] = [];- const { promise: publicationStarted, resolve: enteredPublication } =- Promise.withResolvers<void>();- const { promise: recoveryFinished, resolve: finishRecovery } =- Promise.withResolvers<void>();+ const publicationStarted = Promise.withResolvers<void>();+ const recoveryFinished = Promise.withResolvers<void>();const deps = dependencies();deps.addSignalListener = (name, listener) => {listeners.set(name, listener);@@ -1963,15 +1923,17 @@ describe("publish scan", () => {total: 1,});listeners.get(signal)!();- enteredPublication();- await recoveryFinished;+ publicationStarted.resolve();+ await recoveryFinished.promise;events.push("recovered created issues");throw new Error("The publication handoff remains at /tmp/synthetic-handoff; recover it before retrying to avoid creating duplicate issues.",);};- let finished = false;+ const observeFinished = mock((status: number) => {+ return status;+ });const publishing = main(["publish",@@ -1983,12 +1945,9 @@ describe("publish scan", () => {stdout.stream,stderr,deps,- ).then((status) => {- finished = true;- return status;- });- await publicationStarted;- expect(finished).toBe(false);+ ).then(observeFinished);+ await publicationStarted.promise;+ expect(observeFinished).not.toHaveBeenCalled();expect(listeners.size).toBe(2);expect(events).toEqual(interactive@@ -1997,7 +1956,7 @@ describe("publish scan", () => {);expect(captured.text()).not.toContain(expectedMessage);expect(stdout.text()).toBe("");- finishRecovery();+ recoveryFinished.resolve();expect(await publishing).toBe(expectedCode);expect(events).toEqual(@@ -2040,16 +1999,7 @@ describe("publish scan", () => {},};const deps = dependencies();- deps.publishScan = async (_scanDirectory, options) => {- options.onProgress?.({ type: "started", scanId: "scan-123", total: 1 });- options.onProgress?.({- type: "completed",- created: 1,- failed: 0,- total: 1,- });- return publicationResult();- };+ deps.publishScan = publishWithProgress;expect(await main(@@ -2098,7 +2048,7 @@ describe("publish scan", () => {const stdout = capture();const stderr = capture(true);let offered: readonly { label: string; value: string }[] = [];- let publishedDirectory: string | undefined;+ const publishScan = mock(successfulPublication<[string]>);const deps = dependencies({currentDirectory: directory,onWorkbench: () => ({@@ -2122,10 +2072,7 @@ describe("publish scan", () => {return choices[1]!.value;},};- deps.publishScan = async (scanDirectory) => {- publishedDirectory = scanDirectory;- return publicationResult();- };+ deps.publishScan = publishScan;expect(await main(@@ -2143,7 +2090,7 @@ describe("publish scan", () => {expect.stringContaining("first-scan"),expect.stringContaining("selected-scan"),]);- expect(publishedDirectory).toBe(selectedDirectory);+ expect(publishScan.mock.lastCall?.[0]).toBe(selectedDirectory);expect(JSON.parse(stdout.text())).toEqual(publicationResult());expect(stripVTControlCharacters(stderr.text())).toContain("CODEX SECURITY · PUBLISH · selected-scan",@@ -2154,13 +2101,12 @@ describe("publish scan", () => {test("requires an interactive terminal when no scan directory is supplied", async () => {const stdout = capture();const stderr = capture();- let listed = false;- let published = false;+ const onWorkbench = mock(() => {+ return { scans: [] };+ });+ const publishScan = mock(successfulPublication<[]>);const deps = dependencies({- onWorkbench: () => {- listed = true;- return { scans: [] };- },+ onWorkbench,});deps.publishPrompt = {isInteractive: () => false,@@ -2169,10 +2115,7 @@ describe("publish scan", () => {options: readonly { label: string; value: Value }[],): Promise<Value> => options[0]!.value,};- deps.publishScan = async () => {- published = true;- return publicationResult();- };+ deps.publishScan = publishScan;expect(await main(@@ -2187,15 +2130,15 @@ describe("publish scan", () => {);expect(stderr.text()).toContain("codex-security publish scan");expect(stdout.text()).toBe("");- expect(listed).toBe(false);- expect(published).toBe(false);+ expect(onWorkbench).not.toHaveBeenCalled();+ expect(publishScan).not.toHaveBeenCalled();});test("fails clearly when no completed scans are available", async () => {const stdout = capture();const stderr = capture(true);let prompted = false;- let published = false;+ const publishScan = mock(successfulPublication<[]>);const deps = dependencies({ onWorkbench: () => ({ scans: [] }) });deps.publishPrompt = {isInteractive: () => true,@@ -2207,10 +2150,7 @@ describe("publish scan", () => {return options[0]!.value;},};- deps.publishScan = async () => {- published = true;- return publicationResult();- };+ deps.publishScan = publishScan;expect(await main(@@ -2225,7 +2165,7 @@ describe("publish scan", () => {);expect(stdout.text()).toBe("");expect(prompted).toBe(false);- expect(published).toBe(false);+ expect(publishScan).not.toHaveBeenCalled();});test("does not offer completed history when every scan directory is unavailable", async () => {@@ -2242,7 +2182,7 @@ describe("publish scan", () => {const stdout = capture();const stderr = capture(true);let prompted = false;- let published = false;+ const publishScan = mock(successfulPublication<[]>);const deps = dependencies({onWorkbench: () => ({scans: [@@ -2266,10 +2206,7 @@ describe("publish scan", () => {return choices[0]!.value;},};- deps.publishScan = async () => {- published = true;- return publicationResult();- };+ deps.publishScan = publishScan;expect(await main(@@ -2284,7 +2221,7 @@ describe("publish scan", () => {);expect(stdout.text()).toBe("");expect(prompted).toBe(false);- expect(published).toBe(false);+ expect(publishScan).not.toHaveBeenCalled();});test("requires an explicit supported destination and team with valid optional flags", async () => {@@ -2342,17 +2279,14 @@ describe("publish scan", () => {for (const [argv, expected] of cases) {const stdout = capture();const stderr = capture();- let published = false;+ const publishScan = mock(successfulPublication<[]>);const deps = dependencies();- deps.publishScan = async () => {- published = true;- return publicationResult();- };+ deps.publishScan = publishScan;expect(await main(argv, stdout.stream, stderr.stream, deps)).toBe(2);expect(stderr.text()).toContain(expected);expect(stdout.text()).toBe("");- expect(published).toBe(false);+ expect(publishScan).not.toHaveBeenCalled();}});@@ -2531,9 +2465,8 @@ describe("publish scan", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.publishScan = async () => {- throw new Error("Linear is not connected to your Codex account.");- };+ deps.publishScan = async () =>+ fail("Linear is not connected to your Codex account.");expect(await main(@@ -2549,3 +2482,20 @@ describe("publish scan", () => {expect(stdout.text()).toBe("");});});++const publishWithProgress: ReturnType<+ typeof dependencies+>["publishScan"] = async (_scanDirectory, options) => {+ options.onProgress?.({ type: "started", scanId: "scan-123", total: 1 });+ options.onProgress?.({+ type: "completed",+ created: 1,+ failed: 0,+ total: 1,+ });+ return publicationResult();+};++async function successfulPublication<Args extends unknown[]>(..._args: Args) {+ return publicationResult();+}diff --git a/sdk/typescript/tests-ts/cli-scan-import.test.ts b/sdk/typescript/tests-ts/cli-scan-import.test.tsindex 8e0260c0f..dfc631865 100644--- a/sdk/typescript/tests-ts/cli-scan-import.test.ts+++ b/sdk/typescript/tests-ts/cli-scan-import.test.ts@@ -1,5 +1,5 @@import { resolve } from "node:path";-import { describe, expect, test } from "bun:test";+import { describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import {capture,@@ -7,13 +7,12 @@ import {fakeResult,FakeSignals,} from "./cli-fixtures.js";+import { rejecting, throwing } from "./support/errors.js";function importDependencies() {const deps = dependencies({currentDirectory: resolve("workspace"),- onConfig: () => {- throw new Error("Import must not create a model client");- },+ onConfig: throwing("Import must not create a model client"),});const calls: Parameters<NonNullable<typeof deps.importScan>>[0][] = [];deps.importScan = async (options, settings) => {@@ -186,11 +185,9 @@ describe("scan import", () => {test("ordinary scan retains repository arguments and the --json output shortcut", async () => {const stdout = capture();const stderr = capture();- let scans = 0;+ const onRun = mock();const deps = dependencies({- onRun: () => {- scans += 1;- },+ onRun,});expect(await main(@@ -200,7 +197,7 @@ describe("scan import", () => {deps,),).toBe(0);- expect(scans).toBe(1);+ expect(onRun).toHaveBeenCalledTimes(1);expect(JSON.parse(stdout.text())).toMatchObject({manifest: { scan: { id: "scan" } },});@@ -231,9 +228,7 @@ describe("scan import", () => {["scan", "import", "--csv", "findings.csv", "--format", "json"],stdout.stream,{- write() {- throw new Error("closed progress stream");- },+ write: throwing("closed progress stream"),},deps,),@@ -307,9 +302,7 @@ describe("scan import", () => {test("reports import errors without leaving signal handlers", async () => {const signals = new FakeSignals();const deps = dependencies({ signals });- deps.importScan = async () => {- throw new Error("Input JSON is not a findings document.");- };+ deps.importScan = rejecting("Input JSON is not a findings document.");const stderr = capture();expect(await main(@@ -319,7 +312,9 @@ describe("scan import", () => {deps,),).toBe(2);- expect(stderr.text()).toContain("Input JSON is not a findings document.");+ expect(stderr.text()).toBe(+ "codex-security: Input JSON is not a findings document.\n",+ );expect(signals.listeners.get("SIGINT")?.size).toBe(0);expect(signals.listeners.get("SIGTERM")?.size).toBe(0);});diff --git a/sdk/typescript/tests-ts/cli-scan-prompts.test.ts b/sdk/typescript/tests-ts/cli-scan-prompts.test.tsindex 94ad8724d..09926cf29 100644--- a/sdk/typescript/tests-ts/cli-scan-prompts.test.ts+++ b/sdk/typescript/tests-ts/cli-scan-prompts.test.ts@@ -1,21 +1,14 @@import { spawnSync } from "node:child_process";-import {- mkdir,- mkdtemp,- realpath,- rm,- symlink,- writeFile,-} from "node:fs/promises";-import { tmpdir } from "node:os";+import { mkdir, realpath, rm, symlink, writeFile } from "node:fs/promises";import { join } from "node:path";-import { describe, expect, test } from "bun:test";+import { describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import { capture, dependencies } from "./cli-fixtures.js";+import { temporaryDirectory } from "./support/temporary-directories.js";describe("CLI scan prompts", () => {test("loads scan, validation, and post-scan prompt files", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-cli-prompts-"));+ const root = await temporaryDirectory("codex-security-cli-prompts-");try {await Promise.all([writeFile(join(root, "scan.md"), "Review authentication boundaries.\n"),@@ -58,7 +51,7 @@ describe("CLI scan prompts", () => {});test("rejects linked prompt files without rejecting selected external files", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-cli-prompts-"));+ const root = await temporaryDirectory("codex-security-cli-prompts-");try {const repository = join(root, "repository");const repositoryAlias = join(root, "repository-alias");@@ -105,7 +98,7 @@ describe("CLI scan prompts", () => {join("linked-directory", "external-prompt.md"),],] as const) {- let started = false;+ const onTurn = mock();const stderr = capture();expect(await main(@@ -114,15 +107,13 @@ describe("CLI scan prompts", () => {stderr.stream,dependencies({currentDirectory: directory,- onTurn: () => {- started = true;- },+ onTurn,}),),).toBe(2);expect(stderr.text()).toContain("Input files must");expect(stderr.text()).not.toContain("SYNTHETIC_EXTERNAL_PROMPT");- expect(started).toBe(false);+ expect(onTurn).not.toHaveBeenCalled();}for (const [directory, target] of [@@ -153,7 +144,7 @@ describe("CLI scan prompts", () => {});test("combines shared and repository-specific bulk scan prompts", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-cli-prompts-"));+ const root = await temporaryDirectory("codex-security-cli-prompts-");try {const repository = join(root, "repository");for (const args of [@@ -228,20 +219,16 @@ describe("CLI scan prompts", () => {});test("does not silently drop custom validation on a saved rerun", async () => {- const root = await mkdtemp(- join(tmpdir(), "codex-security-cli-validation-"),- );+ const root = await temporaryDirectory("codex-security-cli-validation-");try {await writeFile(join(root, "validation.md"),"Validate with the fixture.\n",);- let selected: unknown;+ const onTurn = mock<(repository: string, options: unknown) => void>();const deps = dependencies({currentDirectory: root,- onTurn: (_repository, value) => {- selected = value;- },+ onTurn,onWorkbench: () => ({recipe: {repository: root,@@ -262,7 +249,7 @@ describe("CLI scan prompts", () => {),).toBe(2);expect(error.text()).toContain("--validation-prompt-file");- expect(selected).toBeUndefined();+ expect(onTurn.mock.lastCall?.[1]).toBeUndefined();expect(await main([@@ -278,7 +265,7 @@ describe("CLI scan prompts", () => {deps,),).toBe(0);- expect(selected).toMatchObject({+ expect(onTurn.mock.lastCall?.[1]).toMatchObject({validationPrompt: "Validate with the fixture.\n",});await writeFile(join(root, "validation.md"), " \n");diff --git a/sdk/typescript/tests-ts/cli-serve.test.ts b/sdk/typescript/tests-ts/cli-serve.test.tsindex 36f7795d0..de6532f34 100644--- a/sdk/typescript/tests-ts/cli-serve.test.ts+++ b/sdk/typescript/tests-ts/cli-serve.test.ts@@ -90,10 +90,8 @@ for (const [name, args, port, customEmbeddings] of [timeout: 20_000,});const exited = once(child, "exit");- let stderr = "";- child.stderr.setEncoding("utf8").on("data", (chunk) => {- stderr += chunk;- });+ const stderr = capture();+ child.stderr.setEncoding("utf8").on("data", stderr.stream.write);try {let base: string | undefined;for await (const line of createInterface({ input: child.stdout })) {@@ -105,7 +103,7 @@ for (const [name, args, port, customEmbeddings] of [break;}}- expect(base, stderr).toBeDefined();+ expect(base, stderr.text()).toBeDefined();const response = await fetch(`${base}/v1/findings`);expect(response.status).toBe(200);expect(await response.json()).toMatchObject({ findings: [], total: 0 });@@ -139,7 +137,7 @@ for (const [name, args, port, customEmbeddings] of [const [code] = await exited;// Windows terminates child processes instead of delivering SIGTERM.if (process.platform !== "win32") expect(code).toBe(0);- expect(stderr).toBe("");+ expect(stderr.text()).toBe("");} finally {if (child.exitCode === null && child.signalCode === null) {child.kill("SIGKILL");diff --git a/sdk/typescript/tests-ts/cli-signals.test.ts b/sdk/typescript/tests-ts/cli-signals.test.tsindex 0bc0e054f..a81138a7b 100644--- a/sdk/typescript/tests-ts/cli-signals.test.ts+++ b/sdk/typescript/tests-ts/cli-signals.test.ts@@ -1,12 +1,13 @@-import { describe, expect, test } from "bun:test";+import { describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import {FakeSignals,capture,dependencies,- fakePreflight,fakeResult,+ fakeSecurity,} from "./cli-fixtures.js";+import { throwing } from "./support/errors.js";describe("CLI signals", () => {test("maps Ctrl-C and SIGTERM to conventional exits and preserves partial output", async () => {@@ -17,7 +18,7 @@ describe("CLI signals", () => {const stdout = capture();const stderr = capture();const signals = new FakeSignals();- let interrupted = false;+ const onInterrupt = mock();const exit = await main(["scan", "."],stdout.stream,@@ -25,16 +26,14 @@ describe("CLI signals", () => {dependencies({signals,onRun: () => signals.emit(signal),- onInterrupt: () => {- interrupted = true;- },+ onInterrupt,}),);expect(exit).toBe(expectedExit);expect(stdout.text()).toBe("");expect(stderr.text()).toContain(phrase);expect(stderr.text()).toContain("Partial output was kept at /tmp/scan.");- expect(interrupted).toBe(true);+ expect(onInterrupt).toHaveBeenCalled();expect(signals.listeners.get(signal)?.size).toBe(0);}});@@ -44,16 +43,13 @@ describe("CLI signals", () => {const stderr = capture();const signals = new FakeSignals();const deps = dependencies({ signals });- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {signals.emit("SIGINT");const signal = (options as { signal?: AbortSignal }).signal;expect(signal?.aborted).toBe(true);throw new DOMException("aborted", "AbortError");- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ });expect(await main(["scan", "."], stdout.stream, stderr.stream, deps)).toBe(130,);@@ -92,18 +88,15 @@ describe("CLI signals", () => {deps.now = () => now;deps.writeSynchronously = (_stream, value) => synchronousWrites.push(value);deps.forceExit = (signal) => forced.push(signal);- deps.createSecurity = () => ({- run: async () => {+ deps.createSecurity = () =>+ fakeSecurity(async () => {signals.emit("SIGINT");signals.emit("SIGINT");expect(forced).toEqual([]);now = 1_000;signals.emit("SIGINT");return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ });expect(await main(["scan", "."], stdout.stream, stderr.stream, deps)).toBe(130,@@ -121,16 +114,13 @@ describe("CLI signals", () => {const deps = dependencies({ signals });deps.now = () => now;deps.forceExit = (signal) => forced.push(signal);- deps.createSecurity = () => ({- run: async () => {+ deps.createSecurity = () =>+ fakeSecurity(async () => {signals.emit("SIGINT");now = 100;signals.emit("SIGTERM");return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ });await main(["scan", "."], capture().stream, capture().stream, deps);expect(forced).toEqual(["SIGTERM"]);@@ -142,20 +132,15 @@ describe("CLI signals", () => {let now = 0;const deps = dependencies({ signals });deps.now = () => now;- deps.writeSynchronously = () => {- throw new Error("terminal unavailable");- };+ deps.writeSynchronously = throwing("terminal unavailable");deps.forceExit = (signal) => forced.push(signal);- deps.createSecurity = () => ({- run: async () => {+ deps.createSecurity = () =>+ fakeSecurity(async () => {signals.emit("SIGINT");now = 1_000;signals.emit("SIGINT");return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ });await main(["scan", "."], capture().stream, capture(true).stream, deps);expect(forced).toEqual(["SIGINT"]);diff --git a/sdk/typescript/tests-ts/cli-skills.test.ts b/sdk/typescript/tests-ts/cli-skills.test.tsindex a97f85a1e..236ca71cf 100644--- a/sdk/typescript/tests-ts/cli-skills.test.ts+++ b/sdk/typescript/tests-ts/cli-skills.test.ts@@ -1,10 +1,9 @@import { execFileSync, spawn } from "node:child_process";-import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";+import { mkdir, rm, symlink, writeFile } from "node:fs/promises";import * as filesystem from "node:fs/promises";-import { tmpdir } from "node:os";import { join, posix, resolve, win32 } from "node:path";import { setTimeout as delay } from "node:timers/promises";-import { describe, expect, spyOn, test } from "bun:test";+import { describe, expect, spyOn, test, mock } from "bun:test";import { parse as parseToml } from "smol-toml";import {main,@@ -13,8 +12,10 @@ import {skillCommandFailure,} from "../src/cli.js";import type { LinearClientFactory } from "../src/linear.js";-import { capture, dependencies } from "./cli-fixtures.js";+import { capture, dependencies, type OnCodex } from "./cli-fixtures.js";import { runTestInSubprocess } from "./support/test-subprocess.js";+import { temporaryDirectory } from "./support/temporary-directories.js";+import { throwing } from "./support/errors.js";function linearIssue(identifier: string, comments: string[] = []) {const nodes = comments.map((body, index) => ({@@ -40,7 +41,7 @@ function linearIssue(identifier: string, comments: string[] = []) {describe("CLI skill commands", () => {test("runs validation and patch skills with file and literal inputs", async () => {- const directory = await mkdtemp(join(tmpdir(), "codex-security-skills-"));+ const directory = await temporaryDirectory("codex-security-skills-");try {for (const [command, skill, argument, status] of [["validate", "validation", "findings...", 0],@@ -226,7 +227,7 @@ describe("CLI skill commands", () => {});test("keeps imported Unix and Windows paths literal", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-linear-input-"));+ const root = await temporaryDirectory("codex-security-linear-input-");try {const repository = join(root, "repository");const selected = join(repository, "selected.txt");@@ -410,7 +411,7 @@ describe("CLI skill commands", () => {];for (const [args, message, environment] of cases) {- let started = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const stderr = capture();expect(await main(@@ -421,21 +422,18 @@ describe("CLI skill commands", () => {environment: environment ?? {CODEX_SECURITY_LINEAR_API_KEY: "lin_api_SYNTHETIC_SECRET",},- onCodex: () => {- started = true;- return 0;- },+ onCodex,}),),).toBe(2);expect(stderr.text()).toContain(message);expect(stderr.text()).not.toContain("lin_api_SYNTHETIC_SECRET");- expect(started).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();}});test("rejects linked findings while preserving selected external files", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-skill-inputs-"));+ const root = await temporaryDirectory("codex-security-skill-inputs-");try {const repository = join(root, "repository");const externalDirectory = join(root, "external");@@ -457,7 +455,7 @@ describe("CLI skill commands", () => {);for (const command of ["validate", "patch"] as const) {- let invocation: readonly string[] | undefined;+ const onCodex = mock<() => number>().mockReturnValue(0);let prompt: string | undefined;for (const input of ["linked-finding.txt",@@ -471,16 +469,12 @@ describe("CLI skill commands", () => {stderr.stream,dependencies({currentDirectory: repository,- onCodex: (args, output, _environment, input) => {- invocation = args;- prompt = output?.appServer?.prompt ?? input;- return 0;- },+ onCodex,}),),).toBe(2);expect(stderr.text()).not.toContain("SYNTHETIC_EXTERNAL_FINDING");- expect(invocation).toBeUndefined();+ expect(onCodex).not.toHaveBeenCalled();}for (const selected of [@@ -495,8 +489,7 @@ describe("CLI skill commands", () => {capture().stream,dependencies({currentDirectory: repository,- onCodex: (args, output, _environment, input) => {- invocation = args;+ onCodex: (_args, output, _environment, input) => {prompt = output?.appServer?.prompt ?? input;return 0;},@@ -522,7 +515,7 @@ describe("CLI skill commands", () => {) {return;}- const root = await mkdtemp(join(tmpdir(), "codex-security-file-identity-"));+ const root = await temporaryDirectory("codex-security-file-identity-");const selected = join(root, "finding.txt");const replacement = join(root, "replacement.txt");const selectedInode = 2n ** 60n;@@ -570,7 +563,7 @@ describe("CLI skill commands", () => {},);try {- let started = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const stderr = capture();const status = await main(["validate", "finding.txt"],@@ -578,16 +571,13 @@ describe("CLI skill commands", () => {stderr.stream,dependencies({currentDirectory: root,- onCodex: () => {- started = true;- return 0;- },+ onCodex,}),);expect(replaced).toBe(true);expect(status).toBe(2);expect(stderr.text()).not.toContain("SYNTHETIC_REPLACEMENT_FINDING");- expect(started).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();} finally {restoreOpenedStat?.();opening.mockRestore();@@ -609,7 +599,7 @@ describe("CLI skill commands", () => {) {return;}- const root = await mkdtemp(join(tmpdir(), "codex-security-skill-inputs-"));+ const root = await temporaryDirectory("codex-security-skill-inputs-");try {const repository = join(root, "repository");const selected = join(repository, "finding.txt");@@ -635,7 +625,7 @@ describe("CLI skill commands", () => {);try {- let started = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const stderr = capture();const status = await main(["validate", "finding.txt"],@@ -643,16 +633,13 @@ describe("CLI skill commands", () => {stderr.stream,dependencies({currentDirectory: repository,- onCodex: () => {- started = true;- return 0;- },+ onCodex,}),);expect(replaced, "the file-open replacement hook ran").toBe(true);expect(status).toBe(2);expect(stderr.text()).not.toContain("SYNTHETIC_EXTERNAL_FINDING");- expect(started).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();} finally {opening.mockRestore();}@@ -662,9 +649,7 @@ describe("CLI skill commands", () => {});test("preserves Windows network paths without probing them as finding files", async () => {- const directory = await mkdtemp(- join(tmpdir(), "codex-security-network-input-"),- );+ const directory = await temporaryDirectory("codex-security-network-input-");try {const localFile = join(directory, "local finding.txt");const localDrivePaths =@@ -727,7 +712,7 @@ describe("CLI skill commands", () => {}}- let prompt = "";+ const onCodex = mock<OnCodex>().mockReturnValue(0);const stdout = capture();const stderr = capture();expect(@@ -743,13 +728,11 @@ describe("CLI skill commands", () => {stderr.stream,dependencies({currentDirectory: directory,- onCodex: (_args, _output, _environment, input) => {- prompt = input ?? "";- return 0;- },+ onCodex,}),),).toBe(0);+ const prompt = onCodex.mock.lastCall?.[3] ?? "";expect(JSON.parse(prompt.split("\n").at(-1)!)).toEqual(["local finding contents\n",...localDrivePaths.map(@@ -767,7 +750,7 @@ describe("CLI skill commands", () => {test("applies bounded model and reasoning overrides to validation and patching", async () => {for (const command of ["validate", "patch"] as const) {- let invocation: readonly string[] = [];+ const onCodex = mock<OnCodex>().mockReturnValue(0);const stderr = capture();expect(await main(@@ -782,13 +765,11 @@ describe("CLI skill commands", () => {capture().stream,stderr.stream,dependencies({- onCodex: (args) => {- invocation = args;- return 0;- },+ onCodex,}),),).toBe(0);+ const invocation = onCodex.mock.lastCall?.[0] ?? [];expect(invocation).toContain('model="gpt-5.6-custom"');expect(invocation).toContain('model_reasoning_effort="high"');expect(stderr.text()).toBe(@@ -800,7 +781,7 @@ describe("CLI skill commands", () => {"This candidate finding has enough context to exceed a filesystem name. ".repeat(8,);- let literalPrompt = "";+ const onCodex = mock<OnCodex>().mockReturnValue(0);expect(await main(["validate", longLiteral],@@ -808,13 +789,11 @@ describe("CLI skill commands", () => {capture().stream,dependencies({currentDirectory: process.cwd(),- onCodex: (_args, _output, _environment, input) => {- literalPrompt = input ?? "";- return 0;- },+ onCodex,}),),).toBe(0);+ const literalPrompt = onCodex.mock.lastCall?.[3] ?? "";expect(JSON.parse(literalPrompt.split("\n").at(-1)!)).toEqual([longLiteral,]);@@ -824,7 +803,7 @@ describe("CLI skill commands", () => {"model_reasoning_effort=5",'model=" "',]) {- let started = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const stderr = capture();expect(await main(@@ -832,15 +811,12 @@ describe("CLI skill commands", () => {capture().stream,stderr.stream,dependencies({- onCodex: () => {- started = true;- return 0;- },+ onCodex,}),),).toBe(2);expect(stderr.text()).toContain("codex-security:");- expect(started).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();}});@@ -870,7 +846,27 @@ describe("CLI skill commands", () => {),];const stderr = capture();- let invocation: readonly string[] = [];+ const onCodex = mock<OnCodex>((_args, output, environment) => {+ expect(output?.modelProvider).toBe("synthetic");+ expect(output?.codexOverrides).toMatchObject({+ model_providers: { synthetic: providerConfiguration },+ });+ expect(environment?.["SYNTHETIC_GATEWAY_KEY"]).toBe("SYNTHETIC_VALUE");+ if (command === "verify-fix") {+ output?.stdout.write(+ JSON.stringify({+ results: [+ {+ id: "finding-1",+ status: "fixed",+ evidence: "Synthetic verification",+ },+ ],+ }),+ );+ }+ return 0;+ });expect(await main([@@ -883,34 +879,12 @@ describe("CLI skill commands", () => {stderr.stream,dependencies({environment: { SYNTHETIC_GATEWAY_KEY: "SYNTHETIC_VALUE" },- onCodex: (args, output, environment) => {- invocation = args;- expect(output?.modelProvider).toBe("synthetic");- expect(output?.codexOverrides).toMatchObject({- model_providers: { synthetic: providerConfiguration },- });- expect(environment?.["SYNTHETIC_GATEWAY_KEY"]).toBe(- "SYNTHETIC_VALUE",- );- if (command === "verify-fix") {- output?.stdout.write(- JSON.stringify({- results: [- {- id: "finding-1",- status: "fixed",- evidence: "Synthetic verification",- },- ],- }),- );- }- return 0;- },+ onCodex,}),),stderr.text(),).toBe(0);+ const invocation = onCodex.mock.lastCall?.[0] ?? [];expect(invocation).toContain('model="synthetic-model"');expect(invocation).toContain(`model_reasoning_effort="${effort}"`);expect(invocation).toContain('model_provider="synthetic"');@@ -981,7 +955,7 @@ describe("CLI skill commands", () => {"analytics.unrelated=false","analytics.enabled=false",]) {- let started = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const stderr = capture();expect(await main(@@ -997,15 +971,12 @@ describe("CLI skill commands", () => {capture().stream,stderr.stream,dependencies({- onCodex: () => {- started = true;- return 0;- },+ onCodex,}),),).toBe(2);expect(stderr.text()).toContain("codex-security:");- expect(started).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();}},);@@ -1071,7 +1042,7 @@ describe("CLI skill commands", () => {[["--model", " "], "model must be a nonempty string"],[["--model"], "Missing value for flag: --model"],] as const) {- let started = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const invalidStderr = capture();expect(await main(@@ -1079,23 +1050,18 @@ describe("CLI skill commands", () => {capture().stream,invalidStderr.stream,dependencies({- onCodex: () => {- started = true;- return 0;- },+ onCodex,}),),).toBe(2);expect(invalidStderr.text()).toContain(message);- expect(started).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();}},);test("rejects empty and non-file skill inputs before launching Codex", async () => {- const directory = await mkdtemp(- join(tmpdir(), "codex-security-skill-inputs-"),- );+ const directory = await temporaryDirectory("codex-security-skill-inputs-");try {await mkdir(join(directory, "nested"));await writeFile(join(directory, "empty.txt"), " \n\t");@@ -1105,7 +1071,7 @@ describe("CLI skill commands", () => {["empty.txt", "must not be empty"],];for (const [input, expected] of invalidInputs) {- let started = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const stderr = capture();expect(await main(@@ -1114,15 +1080,12 @@ describe("CLI skill commands", () => {stderr.stream,dependencies({currentDirectory: directory,- onCodex: () => {- started = true;- return 0;- },+ onCodex,}),),).toBe(2);expect(stderr.text()).toContain(expected!);- expect(started).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();}} finally {await rm(directory, { recursive: true, force: true });@@ -1130,8 +1093,8 @@ describe("CLI skill commands", () => {});test("accepts large skill inputs and more than 64 findings", async () => {- const directory = await mkdtemp(- join(tmpdir(), "codex-security-skill-large-inputs-"),+ const directory = await temporaryDirectory(+ "codex-security-skill-large-inputs-",);try {const largeInput = "x".repeat(1024 * 1024 + 1);@@ -1248,7 +1211,6 @@ process.stdout.write(JSON.stringify({await expect(readSkillCommandOutput(events())).resolves.toEqual({message: "Validated finding",error: "Reconnecting... 2/5",- malformed: false,});async function* failed(): AsyncGenerator<Buffer> {@@ -1259,7 +1221,6 @@ process.stdout.write(JSON.stringify({}await expect(readSkillCommandOutput(failed())).resolves.toEqual({error: "401 sk-proj-SYNTHETIC_SECRET",- malformed: true,});async function* unicode(): AsyncGenerator<Buffer> {@@ -1275,7 +1236,6 @@ process.stdout.write(JSON.stringify({}await expect(readSkillCommandOutput(unicode())).resolves.toEqual({message: "Café 🔒",- malformed: false,});});@@ -1294,7 +1254,6 @@ process.stdout.write(JSON.stringify({}await expect(readSkillCommandOutput(oversizedLine())).resolves.toEqual({message: "must still drain",- malformed: true,});expect(drained).toBe(true);@@ -1312,7 +1271,6 @@ process.stdout.write(JSON.stringify({}await expect(readSkillCommandOutput(oversizedResponse())).resolves.toEqual({message: largeResponse,- malformed: false,});const stdout = capture();@@ -1685,8 +1643,8 @@ lines.on("line", (line) => {test.skipIf(process.platform === "win32")("forces a skill child to settle when it ignores SIGTERM",async () => {- const directory = await mkdtemp(- join(tmpdir(), "codex-security-skill-signal-"),+ const directory = await temporaryDirectory(+ "codex-security-skill-signal-",);const ready = join(directory, "ready");const child = join(directory, "child.mjs");@@ -1748,9 +1706,7 @@ process.exit(status);invocation.once("error", reject);invocation.once("close", resolve);}),- delay(5_000).then(() => {- throw new Error("CLI skill cancellation did not settle.");- }),+ delay(5_000).then(throwing("CLI skill cancellation did not settle.")),]);expect(status).toBe(143);} finally {diff --git a/sdk/typescript/tests-ts/cli-suggest-owners.test.ts b/sdk/typescript/tests-ts/cli-suggest-owners.test.tsindex e6b642450..762122d8e 100644--- a/sdk/typescript/tests-ts/cli-suggest-owners.test.ts+++ b/sdk/typescript/tests-ts/cli-suggest-owners.test.ts@@ -1,25 +1,22 @@-import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";-import { tmpdir } from "node:os";+import { resolving } from "./support/promises.js";+import { mkdir, readFile, writeFile } from "node:fs/promises";import { join } from "node:path";-import { afterEach, expect, test } from "bun:test";+import { afterEach, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import type { FindingsDocument } from "../src/models.js";import type { OwnerSuggestions } from "../src/suggest-owners.js";import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";import { PLUGIN_ROOT } from "./plugin-root.js";+import { createApiTestFixtures } from "./support/temporary-directories.js";-const directories: string[] = [];-afterEach(async () => {- await Promise.all(- directories- .splice(0)- .map((directory) => rm(directory, { recursive: true, force: true })),- );-});+const { temporaryDirectory, cleanup } = createApiTestFixtures(+ "owner-cli-",+ false,+);+afterEach(cleanup);async function input() {- const directory = await mkdtemp(join(tmpdir(), "owner-cli-"));- directories.push(directory);+ const directory = await temporaryDirectory();const contents = await readFile(join(PLUGIN_ROOT, "examples", "completed-scan", "findings.json"),"utf8",@@ -124,11 +121,8 @@ test("rejects invalid input and extra arguments before model execution", async ('{"findings":[{"title":"Incomplete"}]}',);const deps = dependencies({ currentDirectory: directory });- let called = false;- deps.suggestOwners = async () => {- called = true;- return report;- };+ const suggestOwners = mock(resolving(report));+ deps.suggestOwners = suggestOwners;for (const args of [[],["invalid.json"],@@ -144,7 +138,7 @@ test("rejects invalid input and extra arguments before model execution", async (),).toBe(2);}- expect(called).toBe(false);+ expect(suggestOwners).not.toHaveBeenCalled();});test.each([@@ -173,3 +167,24 @@ test.each([expect(signals.listeners.get("SIGTERM")?.size).toBe(0);},);++test("reports owner lookup errors without changing the diagnostic or leaving listeners", async () => {+ const { directory } = await input();+ const signals = new FakeSignals();+ const deps = dependencies({ currentDirectory: directory, signals });+ deps.suggestOwners = async () => {+ throw new Error("Owner lookup failed.");+ };+ const stderr = capture();+ expect(+ await main(+ ["suggest-owners", "findings.json"],+ capture().stream,+ stderr.stream,+ deps,+ ),+ ).toBe(2);+ expect(stderr.text()).toBe("codex-security: Owner lookup failed.\n");+ expect(signals.listeners.get("SIGINT")?.size).toBe(0);+ expect(signals.listeners.get("SIGTERM")?.size).toBe(0);+});diff --git a/sdk/typescript/tests-ts/cli-verify-fix.test.ts b/sdk/typescript/tests-ts/cli-verify-fix.test.tsindex 061ffd0e6..77cba01ac 100644--- a/sdk/typescript/tests-ts/cli-verify-fix.test.ts+++ b/sdk/typescript/tests-ts/cli-verify-fix.test.ts@@ -1,4 +1,4 @@-import { describe, expect, test } from "bun:test";+import { describe, expect, test, mock } from "bun:test";import { readFile } from "node:fs/promises";import { join } from "node:path";import { stripVTControlCharacters } from "node:util";@@ -8,7 +8,7 @@ import type { LinearClientFactory } from "../src/linear.js";import { capture, dependencies, fakeResult } from "./cli-fixtures.js";import { PLUGIN_ROOT } from "./plugin-root.js";-function linearIssue(identifier: string) {+async function linearIssue(identifier: string) {return {identifier,title: `Verify ${identifier}`,@@ -26,6 +26,9 @@ function linearIssue(identifier: string) {};}+const linearClient = () =>+ ({ issue: linearIssue }) as ReturnType<LinearClientFactory>;+describe("read-only finding verification", () => {test("verifies imported Linear issues in a read-only sandbox without exposing credentials", async () => {const stdout = capture();@@ -44,10 +47,7 @@ describe("read-only finding verification", () => {LINEAR_ACCESS_TOKEN: "SYNTHETIC_OAUTH_SECRET",OPENAI_API_KEY: "sk-proj-SYNTHETIC_MODEL_KEY",},- linearClient: () =>- ({- issue: async (id: string) => linearIssue(id),- }) as ReturnType<LinearClientFactory>,+ linearClient,onCodex: (args, output, processEnvironment, input) => {expect(args[0]).toBe("app-server");expect(input).toBeUndefined();@@ -270,10 +270,7 @@ describe("read-only finding verification", () => {capture().stream,dependencies({environment: { LINEAR_ACCESS_TOKEN: "SYNTHETIC_OAUTH_TOKEN" },- linearClient: () =>- ({- issue: async (id: string) => linearIssue(id),- }) as ReturnType<LinearClientFactory>,+ linearClient,onCodex: (_args, output) => {agentCalls += 1;prompt = output!.appServer!.prompt;@@ -508,10 +505,7 @@ describe("read-only finding verification", () => {stderr.stream,dependencies({environment: { CODEX_SECURITY_LINEAR_API_KEY: "synthetic-key" },- linearClient: () =>- ({- issue: async (id: string) => linearIssue(id),- }) as ReturnType<LinearClientFactory>,+ linearClient,onCodex: (_args, output) => {output?.stdout.write(JSON.stringify(result));return 0;@@ -537,7 +531,7 @@ describe("read-only finding verification", () => {"rejects invalid verification selection %j",async (args, expected) => {const stderr = capture();- let started = false;+ const onCodex = mock<() => number>().mockReturnValue(0);expect(await main(@@ -546,15 +540,12 @@ describe("read-only finding verification", () => {stderr.stream,dependencies({environment: { CODEX_SECURITY_LINEAR_API_KEY: "synthetic-key" },- onCodex: () => {- started = true;- return 0;- },+ onCodex,}),),).toBe(2);expect(stderr.text()).toContain(expected);- expect(started).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();},);});diff --git a/sdk/typescript/tests-ts/cli-workbench.test.ts b/sdk/typescript/tests-ts/cli-workbench.test.tsindex 9c12e14a5..254392058 100644--- a/sdk/typescript/tests-ts/cli-workbench.test.ts+++ b/sdk/typescript/tests-ts/cli-workbench.test.ts@@ -1,21 +1,23 @@-import { mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";-import { tmpdir } from "node:os";+import { mkdir, rm, writeFile } from "node:fs/promises";import { join, resolve } from "node:path";-import { describe, expect, test } from "bun:test";+import { describe, expect, test, mock } from "bun:test";import type { CodexSecurityConfig, JsonObject } from "../src/index.js";-import { DiffTarget } from "../src/index.js";+import { DiffTarget, type ScanOptions } from "../src/index.js";import { main } from "../src/cli.js";import {matchScanFindings,type ScanComparisonInput,} from "../src/scan-comparison.js";import {+ savedRecipe,capture,dependencies,FakeSignals,fakeResult,SYNTHETIC_CREDENTIALS,} from "./cli-fixtures.js";+import { temporaryDirectory } from "./support/temporary-directories.js";+import { rejecting, throwing } from "./support/errors.js";describe("CLI workbench", () => {test("lists and summarizes open findings for the current repository", async () => {@@ -106,20 +108,17 @@ describe("CLI workbench", () => {],];for (const [argv, expected] of cases) {- let invocation: readonly string[] | undefined;+ const onWorkbench = mock((_args: readonly string[]) => {+ return { scans: [{ scanId: "scan-1" }] };+ });const deps = dependencies({- onWorkbench: (args) => {- invocation = args;- return { scans: [{ scanId: "scan-1" }] };- },+ onWorkbench,});- deps.createSecurity = () => {- throw new Error("history must not initialize Codex");- };+ deps.createSecurity = throwing("history must not initialize Codex");expect(await main(argv, capture().stream, capture().stream, deps)).toBe(0,);- expect(invocation).toEqual(expected);+ expect(onWorkbench.mock.lastCall?.[0]).toEqual(expected);}const stdout = capture();@@ -248,30 +247,23 @@ describe("CLI workbench", () => {],];for (const [argv, expected, response, output] of cases) {- const calls: Array<readonly string[]> = [];+ const calls = mock((_args: readonly string[]) => {+ return response;+ });const stdout = capture();const deps = dependencies({- onWorkbench: (args) => {- calls.push(args);- return response;- },+ onWorkbench: calls,});- deps.createSecurity = () => {- throw new Error("history must not initialize Codex");- };- deps.matchFindings = async () => {- throw new Error("saved matches must not initialize Codex");- };+ deps.createSecurity = throwing("history must not initialize Codex");+ deps.matchFindings = rejecting("saved matches must not initialize Codex");expect(await main(argv, stdout.stream, capture().stream, deps)).toBe(0);- expect(calls).toEqual([expected]);+ expect(calls.mock.calls.map(([value]) => value)).toEqual([expected]);expect(JSON.parse(stdout.text())).toEqual(output);}});test("shows saved scan activity without starting Codex", async () => {- const state = await realpath(- await mkdtemp(join(tmpdir(), "codex-security-cli-logs-")),- );+ const state = await temporaryDirectory("codex-security-cli-logs-", true);try {const sessions = join(state, "codex-home", "sessions", "2026", "08");const scanDirectory = join(state, "scans", "scan-1");@@ -368,9 +360,7 @@ describe("CLI workbench", () => {};},});- deps.createSecurity = () => {- throw new Error("logs must not initialize Codex");- };+ deps.createSecurity = throwing("logs must not initialize Codex");expect(await main(["scans", "logs", "scan-1", "--json"],@@ -601,9 +591,7 @@ describe("CLI workbench", () => {matchingInputs: { before: [], after: [] },};},- onMatch: async () => {- throw new Error("Root-cause matching failed.");- },+ onMatch: rejecting("Root-cause matching failed."),}),),).toBe(2);@@ -763,45 +751,41 @@ describe("CLI workbench", () => {"debounces matching %s and allows a later %s to terminate a blocked workbench",async (first, second, delay, expectedExit) => {const signals = new FakeSignals();- const { promise: started, resolve: began } =- Promise.withResolvers<void>();- const { promise: pending, resolve: finish } =- Promise.withResolvers<JsonObject>();+ const started = Promise.withResolvers<void>();+ const pending = Promise.withResolvers<JsonObject>();let observedSignal: AbortSignal | undefined;- const forced: string[] = [];+ const forced = mock((_signal: string) => {});let now = 0;const deps = dependencies({signals,onWorkbench: async (_args, _input, signal) => {observedSignal = signal;- began();- return await pending;+ started.resolve();+ return await pending.promise;},});deps.now = () => now;- deps.forceExit = (signal) => {- forced.push(signal);- };+ deps.forceExit = forced;const running = main(["scans", "match", "before", "after", "--json"],capture().stream,capture().stream,deps,);- await started;+ await started.promise;signals.emit(first);expect(observedSignal?.aborted).toBe(true);signals.emit(first);- expect(forced).toEqual([]);+ expect(forced).not.toHaveBeenCalled();now = delay;signals.emit(second);- expect(forced).toEqual([second]);+ expect(forced.mock.calls.map(([value]) => value)).toEqual([second]);expect([...signals.listeners.values()].every((listeners) => listeners.size === 0,),).toBe(true);- finish({ matchingCached: true, summary: {} });+ pending.resolve({ matchingCached: true, summary: {} });expect(await running).toBe(expectedExit);},);@@ -836,7 +820,43 @@ describe("CLI workbench", () => {];const calls: Array<readonly string[]> = [];const inputs: Array<string | undefined> = [];- let matcherCalls = 0;+ const matcherCalls = mock<typeof matchScanFindings>(async (input) => {+ return input.after[0]?.occurrenceId === "b"+ ? {+ matches: [+ {+ beforeOccurrenceIds: ["a"],+ afterOccurrenceIds: ["b"],+ confidence: "high",+ reason: "Same root cause.",+ },+ ],+ uncertain: [],+ }+ : {+ matches: [+ {+ beforeOccurrenceIds: ["a", "b"],+ afterOccurrenceIds: ["c"],+ confidence: "high",+ reason: "Same root cause.",+ },+ {+ beforeOccurrenceIds: ["a-shared"],+ afterOccurrenceIds: ["c-shared"],+ confidence: "high",+ reason: "Same root cause.",+ },+ ],+ uncertain: [+ {+ beforeOccurrenceId: "b-shared",+ afterOccurrenceId: "c-shared",+ reason: "Possibly the same root cause.",+ },+ ],+ };+ });const stdout = capture();expect(@@ -858,48 +878,11 @@ describe("CLI workbench", () => {}: {};},- onMatch: async (input) => {- matcherCalls += 1;- return input.after[0]?.occurrenceId === "b"- ? {- matches: [- {- beforeOccurrenceIds: ["a"],- afterOccurrenceIds: ["b"],- confidence: "high",- reason: "Same root cause.",- },- ],- uncertain: [],- }- : {- matches: [- {- beforeOccurrenceIds: ["a", "b"],- afterOccurrenceIds: ["c"],- confidence: "high",- reason: "Same root cause.",- },- {- beforeOccurrenceIds: ["a-shared"],- afterOccurrenceIds: ["c-shared"],- confidence: "high",- reason: "Same root cause.",- },- ],- uncertain: [- {- beforeOccurrenceId: "b-shared",- afterOccurrenceId: "c-shared",- reason: "Possibly the same root cause.",- },- ],- };- },+ onMatch: matcherCalls,}),),).toBe(0);- expect(matcherCalls).toBe(2);+ expect(matcherCalls).toHaveBeenCalledTimes(2);expect(calls[0]).toEqual(["list-unmatched-scan-pairs","--repository",@@ -1099,9 +1082,7 @@ describe("CLI workbench", () => {: {};},});- deps.matchFindings = async () => {- throw new Error("empty comparisons must not start Codex");- };+ deps.matchFindings = rejecting("empty comparisons must not start Codex");expect(await main(@@ -1225,7 +1206,11 @@ describe("CLI workbench", () => {const before = [{ occurrenceId: "old", findingId: "identity-old" }];const after = [{ occurrenceId: "new", findingId: "identity-new" }];const calls: Array<readonly string[]> = [];- let modelCalls = 0;+ const run = mock<+ () => Promise<{ finalResponse: string }>+ >().mockResolvedValue({+ finalResponse: JSON.stringify({ matches: [], uncertain: [] }),+ });let saved: unknown;expect(await main(@@ -1255,22 +1240,14 @@ describe("CLI workbench", () => {...options,codex: {startThread: () => ({- async run() {- modelCalls += 1;- return {- finalResponse: JSON.stringify({- matches: [],- uncertain: [],- }),- };- },+ run,}),},}),}),),).toBe(0);- expect(modelCalls).toBe(0);+ expect(run).toHaveBeenCalledTimes(0);expect(saved).toMatchObject({matches: [{ beforeOccurrenceIds: ["old"], afterOccurrenceIds: ["new"] },@@ -1292,21 +1269,18 @@ describe("CLI workbench", () => {["scans", "match", "before", "after", "--all"],["scans", "compare", "before", "after", "--force"],]) {- let calls = 0;+ const onWorkbench = mock<() => {}>().mockReturnValue({});expect(await main(args,capture().stream,capture().stream,dependencies({- onWorkbench: () => {- calls += 1;- return {};- },+ onWorkbench,}),),).toBe(2);- expect(calls).toBe(0);+ expect(onWorkbench).toHaveBeenCalledTimes(0);}});@@ -1315,7 +1289,7 @@ describe("CLI workbench", () => {async (scanId) => {const stdout = capture();const stderr = capture();- let workbenchCalls = 0;+ const onWorkbench = mock<() => {}>().mockReturnValue({});expect(await main(@@ -1329,14 +1303,11 @@ describe("CLI workbench", () => {stdout.stream,stderr.stream,dependencies({- onWorkbench: () => {- workbenchCalls += 1;- return {};- },+ onWorkbench,}),),).toBe(2);- expect(workbenchCalls).toBe(0);+ expect(onWorkbench).toHaveBeenCalledTimes(0);expect(stdout.text()).toBe("");expect(stderr.text()).toContain("Markdown output is not supported for scan results.",@@ -1354,7 +1325,7 @@ describe("CLI workbench", () => {capture().stream,dependencies({onTurn: (_repository, options) => {- parentScanId = (options as { parentScanId?: string }).parentScanId;+ parentScanId = options.parentScanId;},onWorkbench: (args): JsonObject =>args[0] === "list-scans"@@ -1374,9 +1345,8 @@ describe("CLI workbench", () => {});test("reruns canonical recipes with exact config, policy, plugin, and lineage", async () => {- let config: CodexSecurityConfig | undefined;- let repository: string | undefined;- let options: Record<string, unknown> | undefined;+ const onConfig = mock<(config: CodexSecurityConfig) => void>();+ const onTurn = mock<(repository: string, options: ScanOptions) => void>();const knowledgeBasePath = resolve("/original/security.md");const savedConfig = {approval_policy: "on-request",@@ -1391,13 +1361,8 @@ describe("CLI workbench", () => {capture().stream,capture().stream,dependencies({- onConfig: (value) => {- config = value;- },- onTurn: (value, runOptions) => {- repository = value;- options = runOptions as Record<string, unknown>;- },+ onConfig,+ onTurn,onWorkbench: () => ({recipe: {repository: "/original/repository",@@ -1419,9 +1384,9 @@ describe("CLI workbench", () => {}),),).toBe(0);- expect(config?.codexOverrides).toEqual(savedConfig);- expect(repository).toBe("/original/repository");- expect(options).toMatchObject({+ expect(onConfig.mock.lastCall?.[0]?.codexOverrides).toEqual(savedConfig);+ expect(onTurn.mock.lastCall?.[0]).toBe("/original/repository");+ expect(onTurn.mock.lastCall?.[1]).toMatchObject({target: ["src", "packages/core"],mode: "deep",parentScanId: "scan-original",@@ -1454,28 +1419,19 @@ describe("CLI workbench", () => {],];for (const [target, expected] of references) {- let runOptions: Record<string, unknown> | undefined;+ const onTurn = mock<(repository: string, options: ScanOptions) => void>();expect(await main(["scans", "rerun", "scan-original"],capture().stream,capture().stream,dependencies({- onTurn: (_repository, value) => {- runOptions = value as Record<string, unknown>;- },- onWorkbench: () => ({- recipe: {- repository: "/original/repository",- target,- mode: "standard",- config: {},- },- }),+ onTurn,+ onWorkbench: () => savedRecipe({}, target),}),),).toBe(0);- expect(runOptions?.["target"]).toEqual(expected);+ expect(onTurn.mock.lastCall?.[1]?.target).toEqual(expected);}});@@ -1486,7 +1442,7 @@ describe("CLI workbench", () => {] as const)("preserves %s scan approval policy when rerunning saved scans",async (_scenario, savedApprovalPolicy, expectedApprovalPolicy) => {- let config: CodexSecurityConfig | undefined;+ const onConfig = mock<(config: CodexSecurityConfig) => void>();const savedConfig = {model: "gpt-original",...(savedApprovalPolicy === undefined@@ -1500,21 +1456,12 @@ describe("CLI workbench", () => {capture().stream,capture().stream,dependencies({- onConfig: (value) => {- config = value;- },- onWorkbench: () => ({- recipe: {- repository: "/original/repository",- target: { kind: "repository", paths: [] },- mode: "standard",- config: savedConfig,- },- }),+ onConfig,+ onWorkbench: () => savedRecipe(savedConfig),}),),).toBe(0);- expect(config?.codexOverrides).toEqual({+ expect(onConfig.mock.lastCall?.[0]?.codexOverrides).toEqual({...savedConfig,approval_policy: expectedApprovalPolicy,});@@ -1523,24 +1470,20 @@ describe("CLI workbench", () => {test("preserves workbench failures and does not initialize Codex", async () => {const stderr = capture();- let started = false;+ const onRun = mock();expect(await main(["scans", "show", "missing"],capture().stream,stderr.stream,dependencies({- onRun: () => {- started = true;- },- onWorkbench: () => {- throw new Error(`Scan lookup failed ${SYNTHETIC_CREDENTIALS}`);- },+ onRun,+ onWorkbench: throwing(`Scan lookup failed ${SYNTHETIC_CREDENTIALS}`),}),),).toBe(2);expect(stderr.text()).toContain(SYNTHETIC_CREDENTIALS);expect(stderr.text()).toContain("SYNTHETIC_KEY_123");- expect(started).toBe(false);+ expect(onRun).not.toHaveBeenCalled();});});diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.tsindex 596dead71..97dc10c57 100644--- a/sdk/typescript/tests-ts/cli.test.ts+++ b/sdk/typescript/tests-ts/cli.test.ts@@ -1,18 +1,18 @@+import { resolving } from "./support/promises.js";+import { parseJsonLines } from "./support/json.js";import {mkdir,- mkdtemp,readFile,realpath,rm,stat,writeFile,} from "node:fs/promises";-import { tmpdir } from "node:os";import { delimiter, join, normalize, resolve } from "node:path";import { PassThrough, Writable } from "node:stream";import { fileURLToPath } from "node:url";import { stripVTControlCharacters } from "node:util";-import { describe, expect, test } from "bun:test";+import { describe, expect, test, mock } from "bun:test";import { parse as parseToml } from "smol-toml";import type {CodexSecurityConfig,@@ -47,15 +47,23 @@ import {scanModelConfiguration,} from "../src/config.js";import {+ warningResult,+ savedRecipe,FakeSignals,SYNTHETIC_CREDENTIALS,capture,dependencies,fakePreflight,fakeResult,+ mustNotInitializeCodex,+ fakeSecurity,+ failingSecurity,+ fakeInterval,} from "./cli-fixtures.js";import { PLUGIN_ROOT } from "./plugin-root.js";import { runCommand } from "./support/shell.js";+import { temporaryDirectory } from "./support/temporary-directories.js";+import { fail, throwing } from "./support/errors.js";const DEFAULT_SCAN_MODEL_CONFIGURATION =scanModelConfiguration(DEFAULT_CODEX_CONFIG);@@ -96,7 +104,7 @@ async function multiscanInventory(root: string): Promise<void> {describe("CLI", () => {test("passes the safety identifier as a per-scan option", async () => {- let options: unknown;+ const onTurn = mock<(repository: string, options: unknown) => void>();const stderr = capture();expect(await main(@@ -104,12 +112,11 @@ describe("CLI", () => {capture().stream,stderr.stream,dependencies({- onTurn: (_repository, value) => {- options = value;- },+ onTurn,}),),).toBe(0);+ const options = onTurn.mock.lastCall?.[1];expect(options).toMatchObject({ safetyIdentifier: "synthetic-user" });expect(stderr.text()).not.toContain("synthetic-user");});@@ -463,7 +470,7 @@ describe("CLI", () => {const python = Bun.which("python3") ?? Bun.which("python");expect(python).not.toBeNull();- const root = await mkdtemp(join(tmpdir(), "codex-security-deep-defaults-"));+ const root = await temporaryDirectory("codex-security-deep-defaults-");try {const { status, stdout, stderr } = await runCommand(@@ -534,18 +541,16 @@ describe("CLI", () => {],},};- const calls: Array<readonly string[]> = [];+ const calls = mock((_args: readonly string[]) => {+ return response;+ });const stdout = capture();const stderr = capture();const deps = dependencies({- onWorkbench: (args) => {- calls.push(args);- return response;- },+ onWorkbench: calls,});- deps.createSecurity = () => {- throw new Error("finding feedback must not initialize Codex");- };+ deps.createSecurity = () =>+ fail("finding feedback must not initialize Codex");expect(await main(@@ -562,7 +567,7 @@ describe("CLI", () => {deps,),).toBe(0);- expect(calls).toEqual([+ expect(calls.mock.calls.map(([value]) => value)).toEqual([["set-finding-triage","--occurrence-id",@@ -582,7 +587,7 @@ describe("CLI", () => {test("requires a reason before marking a finding as a false positive", async () => {const stdout = capture();const stderr = capture();- let workbenchCalled = false;+ const onWorkbench = mock<() => {}>().mockReturnValue({});expect(await main(@@ -590,21 +595,18 @@ describe("CLI", () => {stdout.stream,stderr.stream,dependencies({- onWorkbench: () => {- workbenchCalled = true;- return {};- },+ onWorkbench,}),),).toBe(2);expect(stderr.text()).toContain("reason");- expect(workbenchCalled).toBe(false);+ expect(onWorkbench).not.toHaveBeenCalled();});test("preserves false-positive workbench failures", async () => {const stdout = capture();const stderr = capture();- let started = false;+ const onRun = mock();expect(await main(@@ -619,26 +621,21 @@ describe("CLI", () => {stdout.stream,stderr.stream,dependencies({- onRun: () => {- started = true;- },- onWorkbench: () => {- throw new Error(- `Could not update finding ${SYNTHETIC_CREDENTIALS}`,- );- },+ onRun,+ onWorkbench: () =>+ fail(`Could not update finding ${SYNTHETIC_CREDENTIALS}`),}),),).toBe(2);expect(stdout.text()).toBe("");expect(stderr.text()).toContain(SYNTHETIC_CREDENTIALS);expect(stderr.text()).toContain("SYNTHETIC_KEY_123");- expect(started).toBe(false);+ expect(onRun).not.toHaveBeenCalled();});test("installs a pre-commit hook that blocks failed diff scans", async () => {const root = await realpath(- await mkdtemp(join(tmpdir(), "codex-security-cli-pre-commit-")),+ await temporaryDirectory("codex-security-cli-pre-commit-"),);try {for (const args of [@@ -800,7 +797,7 @@ describe("CLI", () => {});test("runs a bulk scan and keeps structured output on stdout", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-cli-multiscan-"));+ const root = await temporaryDirectory("codex-security-cli-multiscan-");const architecturePath = resolve(root, "/shared/architecture.pdf");const threatModelsPath = resolve(root, "/shared/threat-models");try {@@ -864,7 +861,7 @@ describe("CLI", () => {});test("expands home-relative bulk scan paths", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-cli-home-"));+ const root = await temporaryDirectory("codex-security-cli-home-");const home = join(root, "home");const currentDirectory = join(root, "current");const previousHome = process.env["HOME"];@@ -928,7 +925,7 @@ describe("CLI", () => {] as const)("routes bulk scans through %s",async (_name, provider, model, providerConfig) => {- const root = await mkdtemp(join(tmpdir(), `codex-security-${provider}-`));+ const root = await temporaryDirectory(`codex-security-${provider}-`);try {await multiscanInventory(root);let config: CodexSecurityConfig | undefined;@@ -988,7 +985,7 @@ describe("CLI", () => {);test("preserves diagnostic text in bulk-scan failures and progress", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-cli-multiscan-"));+ const root = await temporaryDirectory("codex-security-cli-multiscan-");try {await multiscanInventory(root);const stdout = capture();@@ -1129,10 +1126,7 @@ describe("CLI", () => {},);expect(child.status).toBe(0);- const responses = child.stdout- .trim()- .split("\n")- .map((line) => JSON.parse(line));+ const responses = parseJsonLines(child.stdout);const tools = responses.find((response) => response.id === 2).result.tools;expect(tools).toHaveLength(1);expect(tools[0]).toMatchObject({@@ -1310,11 +1304,8 @@ describe("CLI", () => {stderr.stream,{...dependencies({- onWorkbench: () => {- throw new Error(- "Scan ID prefixes must be at least eight characters.",- );- },+ onWorkbench: () =>+ fail("Scan ID prefixes must be at least eight characters."),}),},);@@ -1553,9 +1544,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => {- throw new Error("info must stay local and read-only");- };+ deps.createSecurity = () => fail("info must stay local and read-only");expect(await main(@@ -1590,7 +1579,7 @@ describe("CLI", () => {});test("registers the scoped package as the MCP command", async () => {- const home = await mkdtemp(join(tmpdir(), "codex-security-mcp-home-"));+ const home = await temporaryDirectory("codex-security-mcp-home-");try {const child = await runCommand(process.execPath,@@ -1625,13 +1614,10 @@ describe("CLI", () => {test("prints non-TTY progress stages once without starting a timer", () => {const stderr = capture();- let timers = 0;+ const setIntervalMock = mock(fakeInterval);const progress = new Progress(stderr.stream, {now: () => 0,- setInterval: () => {- timers += 1;- return {} as NodeJS.Timeout;- },+ setInterval: setIntervalMock,clearInterval: () => {},});@@ -1639,13 +1625,13 @@ describe("CLI", () => {progress.stopTimer();expect(stderr.text()).toBe("[00:00] Running scan\n");- expect(timers).toBe(0);+ expect(setIntervalMock).toHaveBeenCalledTimes(0);});test("keeps later progress redraw failures from stopping the scan", () => {const stderr = capture(true);const write = stderr.stream.write;- let redraw: (() => void) | undefined;+ const setIntervalMock = mock(fakeInterval);let failRedraw = false;stderr.stream.write = (chunk) => {if (failRedraw) throw new Error("Progress redraw failed.");@@ -1653,24 +1639,21 @@ describe("CLI", () => {};const progress = new Progress(stderr.stream, {now: () => 0,- setInterval: (callback) => {- redraw = callback;- return {} as NodeJS.Timeout;- },+ setInterval: setIntervalMock,clearInterval: () => {},});progress.startTimer("Running scan");failRedraw = true;- expect(() => redraw?.()).not.toThrow();+ expect(() => setIntervalMock.mock.lastCall?.[0]?.()).not.toThrow();failRedraw = false;progress.stopTimer();});test("handles asynchronous progress stream failures while a scan is active", async () => {- let redraw: (() => void) | undefined;+ const setIntervalMock = mock(fakeInterval);let failRedraw = false;const stream = Object.assign(new Writable({@@ -1689,10 +1672,7 @@ describe("CLI", () => {);const progress = new Progress(stream, {now: () => 0,- setInterval: (callback) => {- redraw = callback;- return {} as NodeJS.Timeout;- },+ setInterval: setIntervalMock,clearInterval: () => {},});@@ -1702,7 +1682,7 @@ describe("CLI", () => {stream.once("error", resolve),);failRedraw = true;- redraw?.();+ setIntervalMock.mock.lastCall?.[0]?.();progress.stopTimer();expect(stream.listenerCount("error")).toBe(2);@@ -1792,17 +1772,14 @@ describe("CLI", () => {});});const deps = dependencies();- deps.createSecurity = () => ({- async run(_repository, options) {+ deps.createSecurity = () =>+ fakeSecurity(async function (_repository, options) {if (scenario === "archive") {options?.onOutputArchived?.("/tmp/previous-results");return fakeResult();}throw new CodexSecurityError(failingMessage);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(["scan", ".", "--json"], capture().stream, stream, deps),@@ -1819,14 +1796,11 @@ describe("CLI", () => {test("passes the workflow ID to scans without changing their JSON output", async () => {const deps = dependencies();const result = fakeResult();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {expect(options?.workflowId).toBe("scan-workflow");return result;- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });const stdout = capture();expect(await main(@@ -1861,8 +1835,8 @@ describe("CLI", () => {deps.clearInterval = (timer) => {activeTimers.delete(timer);};- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {expect(activeTimers.size).toBe(1);options?.onAuthentication?.({method: "api_key",@@ -1884,10 +1858,7 @@ describe("CLI", () => {options?.onScanStarted?.();expect(activeTimers.size).toBe(1);return result;- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ });expect(await main(@@ -1930,12 +1901,9 @@ describe("CLI", () => {]) {const stdout = capture();const stderr = capture(true);- let timers = 0;+ const setIntervalMock = mock(fakeInterval);const deps = dependencies();- deps.setInterval = () => {- timers += 1;- return {} as NodeJS.Timeout;- };+ deps.setInterval = setIntervalMock;expect(await main(@@ -1950,7 +1918,7 @@ describe("CLI", () => {expect(stderr.text()).toContain("Running scan");expect(stderr.text()).not.toContain("\u001B");expect(stderr.text()).not.toContain("\r");- expect(timers).toBe(0);+ expect(setIntervalMock).toHaveBeenCalledTimes(0);}});@@ -1974,8 +1942,7 @@ describe("CLI", () => {stderr.stream,dependencies({onTurn: (_repository, options) => {- subscribed =- typeof (options as ScanOptions).onSessionEvent === "function";+ subscribed = typeof options.onSessionEvent === "function";},}),);@@ -2006,7 +1973,7 @@ describe("CLI", () => {cached_input_tokens: 200,output_tokens: 30,});- let timers = 0;+ const setIntervalMock = mock(fakeInterval);const deps = dependencies({environment,result,@@ -2018,10 +1985,7 @@ describe("CLI", () => {{ kind: "dispatch", phase: "file_review", planned: 2, started: 2 },],});- deps.setInterval = () => {- timers += 1;- return {} as NodeJS.Timeout;- };+ deps.setInterval = setIntervalMock;expect(await main(@@ -2046,7 +2010,7 @@ describe("CLI", () => {expect(stderr.text()).not.toContain("CODEX SECURITY");expect(stderr.text()).not.toContain("\u001B");expect(stderr.text()).not.toContain("\r");- expect(timers).toBe(0);+ expect(setIntervalMock).toHaveBeenCalledTimes(0);}});@@ -2061,14 +2025,7 @@ describe("CLI", () => {stderr.stream,dependencies({environment: { CI: "true" },- onWorkbench: () => ({- recipe: {- repository: "/original/repository",- target: { kind: "repository", paths: [] },- mode: "standard",- config: {},- },- }),+ onWorkbench: () => savedRecipe(),scanProgress: [{ phase: "discovery", filesCompleted: 3, filesTotal: 8 },],@@ -2084,28 +2041,21 @@ describe("CLI", () => {test("falls back to plain progress when the dashboard cannot initialize", async () => {const stdout = capture();const stderr = capture(true);- let scans = 0;- let closed = 0;- let timers = 0;+ const onRun = mock();+ const onClose = mock();+ const setIntervalMock = mock(throwing("Dashboard timer unavailable."));const deps = dependencies({- onRun: () => {- scans += 1;- },- onClose: () => {- closed += 1;- },+ onRun,+ onClose,});- deps.setInterval = () => {- timers += 1;- throw new Error("Dashboard timer unavailable.");- };+ deps.setInterval = setIntervalMock;expect(await main(["scan", "."], stdout.stream, stderr.stream, deps)).toBe(0,);- expect(scans).toBe(1);- expect(closed).toBe(1);- expect(timers).toBe(1);+ expect(onRun).toHaveBeenCalledTimes(1);+ expect(onClose).toHaveBeenCalledTimes(1);+ expect(setIntervalMock).toHaveBeenCalledTimes(1);expect(stderr.text()).toContain("Preparing scan");expect(stderr.text()).toContain("Running scan");});@@ -2115,7 +2065,7 @@ describe("CLI", () => {const stderr = capture(true);const write = stderr.stream.write;const signals = new FakeSignals();- let closed = 0;+ const onClose = mock();stderr.stream.write = (chunk) => {if (chunk.toString().includes("\u001B[?25h\u001B[?1049l")) {throw new Error("Terminal cleanup failed.");@@ -2130,13 +2080,11 @@ describe("CLI", () => {stderr.stream,dependencies({signals,- onClose: () => {- closed += 1;- },+ onClose,}),),).toBe(0);- expect(closed).toBe(1);+ expect(onClose).toHaveBeenCalledTimes(1);expect(signals.listeners.get("SIGINT")?.size).toBe(0);expect(signals.listeners.get("SIGTERM")?.size).toBe(0);});@@ -2317,7 +2265,7 @@ describe("CLI", () => {["--format", "jsonl"],["--format=jsonl"],] as const) {- let invoked = false;+ const onCodex = mock<() => number>().mockReturnValue(0);const stdout = capture();const stderr = capture(true);@@ -2327,14 +2275,11 @@ describe("CLI", () => {stdout.stream,stderr.stream,dependencies({- onCodex: () => {- invoked = true;- return 0;- },+ onCodex,}),),).toBe(2);- expect(invoked).toBe(false);+ expect(onCodex).not.toHaveBeenCalled();expect(stdout.text()).toBe("");expect(stderr.text()).toContain(`${command} does not support noninteractive JSON output; run it without --json, --format json, or --format jsonl.`,@@ -2344,14 +2289,11 @@ describe("CLI", () => {});test("rejects CSV stdout when JSON output is requested", async () => {- let exported = false;+ const exportFindings = mock(resolving(new Uint8Array()));const stdout = capture();const stderr = capture();const deps = dependencies();- deps.exportFindings = async () => {- exported = true;- return new Uint8Array();- };+ deps.exportFindings = exportFindings;expect(await main(@@ -2361,7 +2303,7 @@ describe("CLI", () => {deps,),).toBe(2);- expect(exported).toBe(false);+ expect(exportFindings).not.toHaveBeenCalled();expect(stdout.text()).toBe("");expect(stderr.text()).toContain("CSV stdout cannot be combined with JSON output",@@ -2372,9 +2314,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => {- throw new Error("must not initialize Codex");- };+ deps.createSecurity = mustNotInitializeCodex;expect(await main(["export", "--help"], stdout.stream, stderr.stream, deps),).toBe(0);@@ -2977,7 +2917,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const captured: { config?: CodexSecurityConfig } = {};- let repository = "";+ const onTurn = mock<(repository: string) => void>();const exit = await main(["scan",@@ -2996,9 +2936,7 @@ describe("CLI", () => {onConfig: (value) => {captured.config = value;},- onTurn: (value) => {- repository = value;- },+ onTurn,}),);expect(exit).toBe(0);@@ -3011,7 +2949,7 @@ describe("CLI", () => {pythonPath: "/managed/python",codexOverrides: { features: { goals: true } },});- expect(repository).toBe("repo");+ expect(onTurn.mock.lastCall?.[0] ?? "").toBe("repo");});test("emits verbose scan lifecycle diagnostics without changing JSON output", async () => {@@ -3025,8 +2963,8 @@ describe("CLI", () => {const deps = dependencies({environment: { OPENAI_API_KEY: "sk-proj-SYNTHETIC_VERBOSE_SECRET_123" },});- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onAuthentication?.({method: "api_key",source: "OPENAI_API_KEY",@@ -3051,10 +2989,7 @@ describe("CLI", () => {});options?.onCost?.(result.cost!);return result;- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(@@ -3251,17 +3186,11 @@ describe("CLI", () => {stdout.stream,stderr.stream,dependencies({- onWorkbench: () => ({- recipe: {- repository: "/original/repository",- target: { kind: "repository", paths: [] },- mode: "standard",- config: {- model: "gpt-original",- model_reasoning_effort: "high",- },- },- }),+ onWorkbench: () =>+ savedRecipe({+ model: "gpt-original",+ model_reasoning_effort: "high",+ }),}),),).toBe(0);@@ -3286,24 +3215,18 @@ describe("CLI", () => {stderr.stream,dependencies({environment: { CODEX_SECURITY_LOG_LEVEL: "debug" },- onWorkbench: () => ({- recipe: {- repository: "/original/repository",- target: { kind: "repository", paths: [] },- mode: "standard",- config: {- profile: "review",- model: "gpt-5.6-sol",- model_reasoning_effort: "low",- profiles: {- review: {- model: "gpt-5.6-terra",- model_reasoning_effort: "high",- },+ onWorkbench: () =>+ savedRecipe({+ profile: "review",+ model: "gpt-5.6-sol",+ model_reasoning_effort: "low",+ profiles: {+ review: {+ model: "gpt-5.6-terra",+ model_reasoning_effort: "high",},},- },- }),+ }),}),),).toBe(0);@@ -3360,14 +3283,7 @@ describe("CLI", () => {dependencies({environment,onConfig: (value) => (rerunConfig = value),- onWorkbench: () => ({- recipe: {- repository: "/original/repository",- target: { kind: "repository", paths: [] },- mode: "standard",- config: savedConfig,- },- }),+ onWorkbench: () => savedRecipe(savedConfig),}),),).toBe(0);@@ -3490,7 +3406,7 @@ describe("CLI", () => {test("keeps verbose dry-run authentication unverified without starting a scan", async () => {const stdout = capture();const stderr = capture();- let scanStarted = false;+ const onRun = mock();const preflight = {...fakePreflight("."),authentication: {@@ -3510,9 +3426,7 @@ describe("CLI", () => {OPENAI_API_KEY: "sk-proj-SYNTHETIC_DRY_RUN_SECRET_123",},preflight,- onRun: () => {- scanStarted = true;- },+ onRun,}),),).toBe(0);@@ -3522,7 +3436,7 @@ describe("CLI", () => {);expect(stderr.text()).not.toContain("codex-security: debug: scan.started");expect(stderr.text()).not.toContain("SYNTHETIC_DRY_RUN_SECRET");- expect(scanStarted).toBe(false);+ expect(onRun).not.toHaveBeenCalled();});test("reports selected profile configuration consistently in verbose dry runs", async () => {@@ -3605,15 +3519,10 @@ describe("CLI", () => {const deps = dependencies({environment: { OPENAI_API_KEY: "sk-proj-SYNTHETIC_VERBOSE_KEY_123" },});- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(- "401 invalid API key for org-private sk-proj-SYNTHETIC_PROVIDER_SECRET_123",- );- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () =>+ failingSecurity(+ "401 invalid API key for org-private sk-proj-SYNTHETIC_PROVIDER_SECRET_123",+ );expect(await main(@@ -3636,15 +3545,10 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(- "Provider failed for tenant=tenant-private request_id=req-internal",- );- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () =>+ failingSecurity(+ "Provider failed for tenant=tenant-private request_id=req-internal",+ );expect(await main(@@ -3748,13 +3652,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(message);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () => failingSecurity(message);expect(await main(@@ -3781,8 +3679,8 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onWarning?.('Provider warning {"organizationId":"organization private","requestId":"request private"} tenant=tenant-private',);@@ -3795,10 +3693,7 @@ describe("CLI", () => {})}`,);return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(@@ -3823,8 +3718,8 @@ describe("CLI", () => {const stderr = capture();const deps = dependencies();const separators = "\u0085\u2028\u2029";- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onOutputDirReady?.(`/tmp/scan${separators}codex-security: debug: forged`,);@@ -3832,10 +3727,7 @@ describe("CLI", () => {`Scanner warning${separators}codex-security: debug: forged`,);return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(@@ -3869,8 +3761,8 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onOutputArchived?.("/tmp/archive_sk-proj-SYNTHETIC_ARCHIVE_SECRET_123",);@@ -3882,10 +3774,7 @@ describe("CLI", () => {new Error(`observer failed ${SYNTHETIC_CREDENTIALS}`),);return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(@@ -3914,8 +3803,8 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onObserverError?.("onWorkerStatus",new Error(@@ -3923,10 +3812,7 @@ describe("CLI", () => {),);return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(@@ -3965,11 +3851,10 @@ describe("CLI", () => {stdout.stream,stderr.stream,dependencies({- onClose: () => {- throw new Error(+ onClose: () =>+ fail("Cleanup failed for tenant=tenant-private request_id=req-internal",- );- },+ ),}),),).toBe(2);@@ -3999,8 +3884,8 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {const callbacks = options as {onScanStarted?: () => void;onReconnect?: (attempt: number, maxAttempts: number) => void;@@ -4008,10 +3893,7 @@ describe("CLI", () => {callbacks.onScanStarted?.();callbacks.onReconnect?.(2, 5);return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(["scan", "--json"], stdout.stream, stderr.stream, deps),@@ -4027,8 +3909,8 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onScanStarted?.();options?.onReconnect?.(2, 5, {reason: "rate_limit",@@ -4036,10 +3918,7 @@ describe("CLI", () => {});options?.onReconnect?.(3, 5, { reason: "rate_limit" });return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(["scan", "--json"], stdout.stream, stderr.stream, deps),@@ -4055,16 +3934,13 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onReconnect?.(1, 5, { reason: "network" });options?.onReconnect?.(2, 5, { reason: "authentication" });options?.onReconnect?.(3, 5, { reason: "authorization" });return fakeResult();- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ });expect(await main(["scan", "--json"], stdout.stream, stderr.stream, deps),@@ -4084,13 +3960,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(message);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () => failingSecurity(message);expect(await main(["scan"], stdout.stream, stderr.stream, deps)).toBe(2);expect(stderr.text()).toContain(expected);@@ -4108,13 +3978,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(message);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () => failingSecurity(message);expect(await main(@@ -4143,13 +4007,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(message);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () => failingSecurity(message);expect(await main(@@ -4171,15 +4029,8 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(- `network failure ECONNRESET ${SYNTHETIC_CREDENTIALS}`,- );- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () =>+ failingSecurity(`network failure ECONNRESET ${SYNTHETIC_CREDENTIALS}`);expect(await main(@@ -4209,13 +4060,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(message);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () => failingSecurity(message);expect(await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),@@ -4279,13 +4124,8 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw failure;- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () =>+ fakeSecurity((Promise.reject<never>).bind(Promise, failure));expect(await main(@@ -4315,13 +4155,7 @@ describe("CLI", () => {] as const) {const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(detail);- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () => failingSecurity(detail);expect(await main(["scan", "."], capture().stream, stderr.stream, deps),@@ -4335,15 +4169,8 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(- `network failure ECONNRESET ${SYNTHETIC_CREDENTIALS}`,- );- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () =>+ failingSecurity(`network failure ECONNRESET ${SYNTHETIC_CREDENTIALS}`);expect(await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),@@ -4363,20 +4190,15 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError(- [- "Could not save the Codex Security scan: Traceback (most recent call last):",- " with closing(connect()) as connection:",- "sqlite3.OperationalError: unable to open database file",- "token=sk-proj-SYNTHETIC_DATABASE_SECRET_123",- ].join("\n"),- );- },- preflight: async () => fakePreflight(),- close: async () => {},- });+ deps.createSecurity = () =>+ failingSecurity(+ [+ "Could not save the Codex Security scan: Traceback (most recent call last):",+ " with closing(connect()) as connection:",+ "sqlite3.OperationalError: unable to open database file",+ "token=sk-proj-SYNTHETIC_DATABASE_SECRET_123",+ ].join("\n"),+ );expect(await main(["scan"], stdout.stream, stderr.stream, deps)).toBe(2);expect(stderr.text()).toContain("unable to open database file");@@ -4520,14 +4342,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {- options?.onWarning?.(warning, { kind: "target_changed" });- return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ deps.createSecurity = () => fakeSecurity(warningResult(warning, true));expect(await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),@@ -4551,14 +4366,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {- options?.onWarning?.(warning);- return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ deps.createSecurity = () => fakeSecurity(warningResult(warning));expect(await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),@@ -4572,16 +4380,12 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {- options?.onWarning?.(+ deps.createSecurity = () =>+ fakeSecurity(+ warningResult("Repository HEAD changed during the scan: sk-proj-SYNTHETIC_WARNING_SECRET_123",- );- return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ ),+ );expect(await main(@@ -4604,14 +4408,11 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onTrustedAccessStatus?.("granted");return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ });expect(await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),@@ -4627,16 +4428,12 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {- options?.onWarning?.(+ deps.createSecurity = () =>+ fakeSecurity(+ 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.",- );- return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ ),+ );expect(await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),@@ -4652,16 +4449,12 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {- options?.onWarning?.(+ deps.createSecurity = () =>+ fakeSecurity(+ 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.",- );- return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ ),+ );expect(await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),@@ -4683,14 +4476,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {- options?.onWarning?.(warning);- return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ deps.createSecurity = () => fakeSecurity(warningResult(warning));expect(await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),@@ -4705,17 +4491,14 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {+ deps.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onObserverError?.("onWorkerStatus",new Error(`status observer failed ${SYNTHETIC_CREDENTIALS}`),);return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ });expect(await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),@@ -4978,7 +4761,7 @@ describe("CLI", () => {...dependencies({environment,onTurn: (_repository, options) => {- budgetCallback = (options as ScanOptions).onBudgetApproaching;+ budgetCallback = options.onBudgetApproaching;},}),scanInput: input,@@ -5060,7 +4843,7 @@ describe("CLI", () => {stderr.stream,dependencies({onTurn: (_repository, options) => {- (options as ScanOptions).onOutputDirReady?.("/tmp/scan");+ options.onOutputDirReady?.("/tmp/scan");throw new ScanCostLimitExceededError(0.004, cost, "/tmp/scan");},}),@@ -5167,20 +4950,18 @@ describe("CLI", () => {test("validates a dry run without starting a scan", async () => {const stdout = capture();const stderr = capture();- let runStarted = false;+ const onRun = mock();expect(await main(["scan", "repo", "--dry-run"],stdout.stream,stderr.stream,dependencies({- onRun: () => {- runStarted = true;- },+ onRun,}),),).toBe(0);- expect(runStarted).toBe(false);+ expect(onRun).not.toHaveBeenCalled();expect(stdout.text()).toContain("dryRun: true");expect(stdout.text()).toContain("repository: repo");expect(stdout.text()).toContain("mode: standard");@@ -5406,13 +5187,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const failing = dependencies();- failing.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError("invalid scan request");- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ failing.createSecurity = () => failingSecurity("invalid scan request");expect(await main(["scan", "."], stdout.stream, stderr.stream, failing),).toBe(2);@@ -5426,13 +5201,7 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const failing = dependencies();- failing.createSecurity = () => ({- run: async () => {- throw new CodexSecurityError("invalid scan request");- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ failing.createSecurity = () => failingSecurity("invalid scan request");expect(await main(["scan", ".", "--full-output"],@@ -5445,9 +5214,8 @@ describe("CLI", () => {expect(stderr.text()).toContain("invalid scan request\n");const unavailableCwd = dependencies();- unavailableCwd.currentDirectory = () => {- throw new Error("working directory is unavailable");- };+ unavailableCwd.currentDirectory = () =>+ fail("working directory is unavailable");const cwdOutput = capture();const cwdError = capture();expect(@@ -5463,7 +5231,7 @@ describe("CLI", () => {});test("explains protected-root output failures without contaminating JSON stdout", async () => {- const root = await mkdtemp(join(tmpdir(), "codex security's output & "));+ const root = await temporaryDirectory("codex security's output & ");const worktree = join(root, "worktree");const repository = join(worktree, "packages", "service");const output = join(worktree, "scan");@@ -5515,7 +5283,7 @@ describe("CLI", () => {});test("explains when the temporary root is inside the protected scan root", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-cli-tmp-"));+ const root = await temporaryDirectory("codex-security-cli-tmp-");const worktree = join(root, "worktree");const temporary = join(worktree, "tmp");await mkdir(temporary, { recursive: true });@@ -5555,18 +5323,15 @@ describe("CLI", () => {const stderr = capture();const partial = "/tmp/codex-security-partial";const failing = dependencies();- failing.createSecurity = () => ({- run: async (_repository, options) => {+ failing.createSecurity = () =>+ fakeSecurity(async (_repository, options) => {options?.onOutputDirReady?.(partial);throw new OutputInsideProtectedRootError("/tmp/worktree/runtime","/tmp/worktree","runtime",);- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ });expect(await main(["scan", "."], stdout.stream, stderr.stream, failing),@@ -5586,13 +5351,10 @@ describe("CLI", () => {"/private/tmp/worktree_sk-proj-SYNTHETIC_ROOT_KEY_123";const output = `${protectedRoot}/results_sk-proj-SYNTHETIC_OUTPUT_KEY_123`;const failing = dependencies();- failing.createSecurity = () => ({- run: async () => {+ failing.createSecurity = () =>+ fakeSecurity(async () => {throw new OutputInsideProtectedRootError(output, protectedRoot);- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ });expect(await main(@@ -5621,13 +5383,8 @@ describe("CLI", () => {const stdout = capture();const stderr = capture();const failing = dependencies();- failing.createSecurity = () => ({- run: async () => {- throw failure;- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ failing.createSecurity = () =>+ fakeSecurity((Promise.reject<never>).bind(Promise, failure));expect(await main(["scan", "."], stdout.stream, stderr.stream, failing),@@ -5680,9 +5437,7 @@ describe("CLI", () => {stdout.stream,stderr.stream,dependencies({- onClose: () => {- throw new Error("SYNTHETIC_AUTH_HOME_CLEANUP_FAILED");- },+ onClose: () => fail("SYNTHETIC_AUTH_HOME_CLEANUP_FAILED"),}),),).toBe(2);@@ -5709,12 +5464,8 @@ describe("CLI", () => {stdout.stream,stderr.stream,dependencies({- onRun: () => {- throw new Error("SYNTHETIC_ORIGINAL_SCAN_FAILED");- },- onClose: () => {- throw new Error("SYNTHETIC_AUTH_HOME_CLEANUP_FAILED");- },+ onRun: () => fail("SYNTHETIC_ORIGINAL_SCAN_FAILED"),+ onClose: () => fail("SYNTHETIC_AUTH_HOME_CLEANUP_FAILED"),}),),).toBe(2);diff --git a/sdk/typescript/tests-ts/github.test.ts b/sdk/typescript/tests-ts/github.test.tsindex f9b377f6b..120c2b12c 100644--- a/sdk/typescript/tests-ts/github.test.ts+++ b/sdk/typescript/tests-ts/github.test.ts@@ -1,8 +1,9 @@+import { resolving } from "./support/promises.js";import { mkdtemp, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { Octokit } from "@octokit/core";-import { describe, expect, test } from "bun:test";+import { describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import {createAuthenticatedGitHub,@@ -11,6 +12,8 @@ import {type GitHubCodeScanningImportOptions,} from "../src/github.js";import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";+import type { OnCodex } from "./cli-fixtures.js";+import { throwing } from "./support/errors.js";function alert(number: number, ref = "refs/heads/main") {return {@@ -241,11 +244,7 @@ describe("GitHub code scanning import", () => {});test("validates selectors before authentication and supports cancellation", async () => {- let authenticated = false;- const createGitHub = async () => {- authenticated = true;- return github(() => Response.json([]));- };+ const createGitHub = mock(resolving(github(() => Response.json([]))));for (const options of [{ repository: "https://github.com/example/repository" },{ repository: "example/.." },@@ -268,7 +267,7 @@ describe("GitHub code scanning import", () => {{ createGitHub },),).rejects.toThrow("Synthetic cancellation");- expect(authenticated).toBe(false);+ expect(createGitHub).not.toHaveBeenCalled();const controller = new AbortController();await expect(@@ -310,9 +309,7 @@ describe("GitHub import CLI", () => {{ repository: "example/repository", alertNumbers: [42] },{ createGitHub: async () => github(() => Response.json(alert(42))) },);- const mustNotStartCodex = () => {- throw new Error("Import must not start Codex");- };+ const mustNotStartCodex = throwing("Import must not start Codex");expect(await main(["import", "github", "example/repository", "--json"],@@ -330,7 +327,7 @@ describe("GitHub import CLI", () => {const file = join(root, "github-alerts.json");await writeFile(file, stdout.text());- let input = "";+ const onCodex = mock<OnCodex>().mockReturnValue(0);expect(await main(["validate", file],@@ -338,13 +335,11 @@ describe("GitHub import CLI", () => {capture().stream,dependencies({currentDirectory: root,- onCodex: (_args, _output, _environment, prompt) => {- input = prompt!;- return 0;- },+ onCodex,}),),).toBe(0);+ const input = onCodex.mock.lastCall ? onCodex.mock.lastCall[3]! : "";expect(JSON.parse(input.split("\n").at(-1)!)).toEqual([stdout.text()]);} finally {await rm(root, { recursive: true, force: true });@@ -416,21 +411,18 @@ describe("GitHub import CLI", () => {["import", "github", "example/repository", "--github-state", "unknown"],].map((argv) => [argv] as const),)("rejects invalid arguments %j", async (argv) => {- let called = false;+ const importGitHubAlerts = mock(resolving([]));expect(await main(argv,capture().stream,capture().stream,dependencies({- importGitHubAlerts: async () => {- called = true;- return [];- },+ importGitHubAlerts,}),),).toBe(2);- expect(called).toBe(false);+ expect(importGitHubAlerts).not.toHaveBeenCalled();});test("returns cancellation and removes signal handlers", async () => {diff --git a/sdk/typescript/tests-ts/linear.test.ts b/sdk/typescript/tests-ts/linear.test.tsindex 22ee602eb..a3ee34b65 100644--- a/sdk/typescript/tests-ts/linear.test.ts+++ b/sdk/typescript/tests-ts/linear.test.ts@@ -1,3 +1,4 @@+import { emptyPage } from "./support/linear-pagination.js";import { AuthenticationLinearError, RatelimitedLinearError } from "@linear/sdk";import { describe, expect, test } from "bun:test";import {@@ -74,10 +75,7 @@ describe("Linear issue intake", () => {title: "Recheck a completed issue",description: null,url: "https://linear.app/example/issue/SEC-123",- comments: async () => ({- nodes: [],- pageInfo: { hasNextPage: false },- }),+ comments: emptyPage,},],(value) => (filter = value),@@ -112,10 +110,7 @@ describe("Linear issue intake", () => {title: "Synthetic finding",description: "Synthetic evidence",url,- comments: async () => ({- nodes: [],- pageInfo: { hasNextPage: false },- }),+ comments: emptyPage,};},}) as unknown as LinearImportClient,@@ -177,9 +172,7 @@ describe("Linear issue intake", () => {comments: async () => ({nodes: [],pageInfo: { hasNextPage: true },- fetchNext: async () => {- throw error;- },+ fetchNext: (Promise.reject<never>).bind(Promise, error),}),}),}) as unknown as LinearImportClient,diff --git a/sdk/typescript/tests-ts/mock-scan.test.ts b/sdk/typescript/tests-ts/mock-scan.test.tsindex 711ae63fd..be812918a 100644--- a/sdk/typescript/tests-ts/mock-scan.test.ts+++ b/sdk/typescript/tests-ts/mock-scan.test.ts@@ -1,15 +1,15 @@+import type { ScanOptions } from "../src/index.js";import {mkdir,mkdtemp,readFile,readdir,realpath,- rm,writeFile,} from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";-import { afterEach, expect, test } from "bun:test";+import { afterEach, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import { DiffTarget } from "../src/targets.js";import { runWorkbench } from "../src/runtime.js";@@ -19,17 +19,15 @@ import { TestClient } from "./support/api-client.js";import { capture, dependencies, fakeResult } from "./cli-fixtures.js";import { PLUGIN_ROOT } from "./plugin-root.js";import { runCommand } from "./support/shell.js";+import { createApiTestFixtures } from "./support/temporary-directories.js";+import { rejecting, throwing } from "./support/errors.js";-const roots: string[] = [];-afterEach(async () => {- await Promise.all(- roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),- );-});+const { temporaryDirectories: roots, cleanup } = createApiTestFixtures();+afterEach(cleanup);async function fixture() {const root = await mkdtemp(join(await realpath(tmpdir()), "mock-scan-test-"));- roots.push(root);+ roots.track(root);const repository = join(root, "repository");await mkdir(repository);await writeFile(@@ -49,12 +47,8 @@ async function fixture() {{environment,runWorkbench,- prepareRuntime: async () => {- throw new Error("Mock scan initialized Codex");- },- matchFindings: async () => {- throw new Error("Mock scan called model matching");- },+ prepareRuntime: rejecting("Mock scan initialized Codex"),+ matchFindings: rejecting("Mock scan called model matching"),},);return { root, repository, client, environment, python: python! };@@ -136,7 +130,7 @@ test("mock scans seal real artifacts and index shared and unique findings withou{ python, pluginRoot: PLUGIN_ROOT, environment },["get-scan-recipe", "--scan-id", first.manifest.scan.id],);- let rerunMock: boolean | undefined;+ const onTurn = mock((_repository: string, { mock }: ScanOptions) => mock);expect(await main(["scans", "rerun", first.manifest.scan.id],@@ -145,13 +139,11 @@ test("mock scans seal real artifacts and index shared and unique findings withoudependencies({currentDirectory: repository,onWorkbench: async () => recipe,- onTurn: (_repository, options) => {- rerunMock = (options as { mock?: boolean }).mock;- },+ onTurn,}),),).toBe(0);- expect(rerunMock).toBe(true);+ expect(onTurn.mock.results.at(-1)?.value).toBe(true);} finally {await client.close();}@@ -177,15 +169,14 @@ test("mock scans preserve output protection and archive existing completed resulclient.run(repository, { mock: true, outputDir }),).rejects.toThrow();expect(await readFile(first.manifestPath, "utf8")).toBe(original);- let archive = "";+ const onOutputArchived = mock((_path: string) => {});const second = await client.run(repository, {mock: true,outputDir,archiveExisting: true,- onOutputArchived: (path) => {- archive = path;- },+ onOutputArchived,});+ const archive = onOutputArchived.mock.lastCall?.[0] ?? "";expect(await readFile(join(archive, "scan-manifest.json"), "utf8")).toBe(original,);@@ -200,7 +191,7 @@ test("mock scans preserve output protection and archive existing completed resultest("mock scan CLI forwards the flag and never offers authentication or patching", async () => {const stdout = capture();const stderr = capture(true);- let mock: boolean | undefined;+ const onTurn = mock((_repository: string, { mock }: ScanOptions) => mock);const code = await main(["scan", ".", "--mock", "--fail-on-severity", "high", "--auth", "api-key"],stdout.stream,@@ -208,17 +199,13 @@ test("mock scan CLI forwards the flag and never offers authentication or patchin{...dependencies({result: fakeResult(["high"]),- onTurn: (_repository, options) => {- mock = (options as { mock?: boolean }).mock;- },+ onTurn,}),- confirmPatchReview: async () => {- throw new Error("Unexpected patch prompt");- },+ confirmPatchReview: rejecting("Unexpected patch prompt"),},);expect(code).toBe(1);- expect(mock).toBe(true);+ expect(onTurn.mock.results.at(-1)?.value).toBe(true);expect(stderr.text()).toContain("Mock scan");});@@ -310,9 +297,7 @@ test.each(["--dry-run", "--patch"])(capture().stream,stderr.stream,dependencies({- onRun: () => {- throw new Error("Unexpected scan");- },+ onRun: throwing("Unexpected scan"),}),);expect(code).toBe(2);diff --git a/sdk/typescript/tests-ts/skeleton.test.ts b/sdk/typescript/tests-ts/skeleton.test.tsindex 2c8a7e634..28115247f 100644--- a/sdk/typescript/tests-ts/skeleton.test.ts+++ b/sdk/typescript/tests-ts/skeleton.test.ts@@ -9,6 +9,7 @@ import {VERSION,} from "../src/index.js";import { main } from "../src/cli.js";+import { capture } from "./cli-fixtures.js";interface WorkflowStep {name?: string;@@ -41,22 +42,6 @@ async function workflow(name: string) {};}-function capture(): {- stream: Pick<NodeJS.WriteStream, "write">;- text: () => string;-} {- let value = "";- return {- stream: {- write(chunk: string | Uint8Array): boolean {- value += chunk.toString();- return true;- },- },- text: () => value,- };-}-describe("TypeScript package skeleton", () => {test("pins one Codex version across the CLI, MCP app, and evals", async () => {const directories = [@@ -501,20 +486,20 @@ describe("TypeScript package skeleton", () => {});test("provides executable help and version behavior", async () => {- const stdout = capture();- const stderr = capture();+ const stdout = capture(null);+ const stderr = capture(null);expect(await main([], stdout.stream, stderr.stream)).toBe(0);expect(stdout.text()).toContain("Usage: codex-security <command>");expect(stdout.text()).toContain("Integrations:");expect(stderr.text()).toBe("");- const versionOutput = capture();+ const versionOutput = capture(null);expect(await main(["--version"], versionOutput.stream, stderr.stream)).toBe(0,);expect(versionOutput.text()).toBe(`${VERSION}\n`);- const scanHelpOutput = capture();+ const scanHelpOutput = capture(null);expect(await main(["scan", "--help"], scanHelpOutput.stream, stderr.stream),).toBe(0);diff --git a/sdk/typescript/tests-ts/support/linear-pagination.ts b/sdk/typescript/tests-ts/support/linear-pagination.tsindex 0fb0d671d..973560da4 100644--- a/sdk/typescript/tests-ts/support/linear-pagination.ts+++ b/sdk/typescript/tests-ts/support/linear-pagination.ts@@ -23,3 +23,8 @@ export function paginated<Value>(values: Value[]) {);return { connection: page(0), cursors };}++export const emptyPage = async () => ({+ nodes: [],+ pageInfo: { hasNextPage: false },+});