Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

test(sdk): share CLI invocation fixtures (#1228)

mldangelo-oai398c4ce0edc0
diff --git a/sdk/typescript/tests-ts/cli-authentication.test.ts b/sdk/typescript/tests-ts/cli-authentication.test.ts
index 351de8567..f6c223b1c 100644
--- a/sdk/typescript/tests-ts/cli-authentication.test.ts
+++ b/sdk/typescript/tests-ts/cli-authentication.test.ts
@@ -41,6 +41,12 @@ import {
import { temporaryDirectory } from "./support/temporary-directories.js";
import { throwing } from "./support/errors.js";
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
+
let stateDirectory: string;
beforeEach(async () => {
@@ -77,15 +83,15 @@ describe("CLI authentication", () => {
["logout"],
] as const;
for (const argv of cases) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.prepareAuthenticationHome = async () =>
join(stateDirectory, "codex-home");
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(await runCli(argv, deps)).toBe(17);
expect(runCodex.mock.lastCall?.[0]).toEqual([argv[0], ...argv.slice(1)]);
expect(stdout.text()).toBe("");
expect(stderr.text()).toBe("");
@@ -100,8 +106,8 @@ describe("CLI authentication", () => {
);
for (const argv of [["login"], ["login", "status"], ["logout"]] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { runCli } = createCliTest(main);
+
const deps = dependencies({
environment: { CODEX_SECURITY_STATE_DIR: stateDirectory },
});
@@ -113,7 +119,7 @@ describe("CLI authentication", () => {
return 0;
};
- expect(await main(argv, stdout.stream, stderr.stream, deps)).toBe(0);
+ expect(await runCli(argv, deps)).toBe(0);
expect(forwarded).toEqual([...argv]);
expect(environment?.["CODEX_HOME"]).toBe(expectedHome);
expect(environment?.["CODEX_SECURITY_STATE_DIR"]).toBe(stateDirectory);
@@ -134,8 +140,8 @@ describe("CLI authentication", () => {
const expectedHome = join(actualState, "codex-home");
for (const argv of [["login", "status"], ["logout"]] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { runCli } = createCliTest(main);
+
const deps = dependencies({
environment: { CODEX_SECURITY_STATE_DIR: linkedState },
});
@@ -146,7 +152,7 @@ describe("CLI authentication", () => {
return 0;
};
- expect(await main(argv, stdout.stream, stderr.stream, deps)).toBe(0);
+ expect(await runCli(argv, deps)).toBe(0);
expect(forwardedHome).toBe(expectedHome);
}
@@ -154,16 +160,14 @@ describe("CLI authentication", () => {
await codexSecurityCredentialAllowsAmbientImport(expectedHome),
).toBe(false);
- const stdout = capture();
- const stderr = capture();
+ const { runCli } = createCliTest(main);
+
const deps = dependencies({
environment: { CODEX_SECURITY_STATE_DIR: linkedState },
});
deps.prepareAuthenticationHome = prepareCodexSecurityCredentialHome;
deps.runCodex = async () => 0;
- expect(await main(["login"], stdout.stream, stderr.stream, deps)).toBe(
- 0,
- );
+ expect(await runCli(["login"], deps)).toBe(0);
expect(
await codexSecurityCredentialAllowsAmbientImport(expectedHome),
).toBe(true);
@@ -178,15 +182,10 @@ describe("CLI authentication", () => {
[{ OPENAI_API_KEY: "sk-proj-SYNTHETIC_SECRET_123" }, "OPENAI_API_KEY"],
[{ Codex_Api_Key: "sk-proj-SYNTHETIC_SECRET_456" }, "CODEX_API_KEY"],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
expect(
- await main(
- ["login", "status"],
- stdout.stream,
- stderr.stream,
- dependencies({ environment }),
- ),
+ await runCli(["login", "status"], dependencies({ environment })),
).toBe(0);
expect(stderr.text()).toContain(
`Effective scan authentication: API key from ${expectedSource}.`,
@@ -222,17 +221,9 @@ describe("CLI authentication", () => {
"remove OPENAI_API_KEY and CODEX_API_KEY from the environment",
],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
- expect(
- await main(
- argv,
- stdout.stream,
- stderr.stream,
- dependencies({ environment }),
- ),
- ).toBe(0);
+ expect(await runCli(argv, dependencies({ environment }))).toBe(0);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain(
"ChatGPT login succeeded. Interactive scans will ask which account to use;",
@@ -268,14 +259,11 @@ describe("CLI authentication", () => {
"remove OPENAI_API_KEY and CODEX_API_KEY from the environment",
],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["login", "--with-access-token"],
- stdout.stream,
- stderr.stream,
dependencies({ environment }),
),
).toBe(0);
@@ -300,13 +288,11 @@ describe("CLI authentication", () => {
[["login", "--with-api-key"], 0],
[["login", "--with-access-token"], 2],
] as const) {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
argv,
- capture().stream,
- stderr.stream,
dependencies({ environment, onCodex: () => exitCode }),
),
).toBe(exitCode);
@@ -317,14 +303,11 @@ describe("CLI authentication", () => {
});
test("does not warn after access-token login without an overriding API key", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["login", "--with-access-token"],
- stdout.stream,
- stderr.stream,
dependencies({ environment: {} }),
),
).toBe(0);
@@ -340,13 +323,11 @@ describe("CLI authentication", () => {
[["scan"], "auto"],
] as const) {
const onTurn = mock((_repository: string, { auth }: ScanOptions) => auth);
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
argv,
- capture().stream,
- stderr.stream,
dependencies({
environment: { OPENAI_API_KEY: "synthetic-private-key" },
onTurn,
@@ -374,8 +355,8 @@ describe("CLI authentication", () => {
[{ AWS_PROFILE: "synthetic-bedrock-profile" }, "AWS_PROFILE"],
[{}, "default_credential_chain"],
] as const) {
- const stdout = capture();
- const stderr = capture(false);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: false });
+
const deps = dependencies({ environment });
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -388,7 +369,7 @@ describe("CLI authentication", () => {
});
expect(
- await main(
+ await runCli(
[
"scan",
"--provider",
@@ -398,8 +379,6 @@ describe("CLI authentication", () => {
"--json",
"--verbose",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -453,8 +432,8 @@ describe("CLI authentication", () => {
] as const)(
"preserves Bedrock failure details and recovery advice: %s",
async (detail, expected, classification) => {
- const stderr = capture(false);
- const stdout = capture();
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: false });
+
const deps = dependencies({
environment: { AWS_BEARER_TOKEN_BEDROCK: "synthetic-bedrock-bearer" },
});
@@ -469,7 +448,7 @@ describe("CLI authentication", () => {
});
expect(
- await main(
+ await runCli(
[
"scan",
"--codex",
@@ -478,8 +457,6 @@ describe("CLI authentication", () => {
"--verbose",
"--full-output",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -495,7 +472,7 @@ describe("CLI authentication", () => {
);
test("explains refreshing temporary AWS profile credentials without a stored OpenAI login", async () => {
- const stderr = capture(false);
+ const stderr = captureCli(main, "stderr", false);
const deps = dependencies({
environment: { AWS_PROFILE: "synthetic-profile" },
});
@@ -514,10 +491,8 @@ describe("CLI authentication", () => {
close: async () => {},
});
expect(
- await main(
+ await stderr.run(
["scan", "--codex", 'model_provider="amazon-bedrock"'],
- capture().stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -559,8 +534,8 @@ describe("CLI authentication", () => {
] as const)(
"preserves command-authenticated Bedrock diagnostics in stderr and JSON: %s",
async (detail, expected, classification) => {
- const stderr = capture(false);
- const stdout = capture();
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: false });
+
const deps = dependencies();
deps.createSecurity = () => ({
run: async (_repository, options) => {
@@ -571,7 +546,7 @@ describe("CLI authentication", () => {
close: async () => {},
});
expect(
- await main(
+ await runCli(
[
"scan",
"--codex",
@@ -582,8 +557,6 @@ describe("CLI authentication", () => {
"--verbose",
"--full-output",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -613,7 +586,7 @@ describe("CLI authentication", () => {
[["scans", "rerun", "scan-original", "--verbose"], "chatgpt"],
[["scans", "rerun", "scan-original", "--verbose"], "api-key"],
] as const) {
- const stderr = capture(true);
+ const stderr = captureCli(main, "stderr", true);
const onTurn = mock((_repository: string, { auth }: ScanOptions) => auth);
let question = "";
let choices: readonly { label: string; value: string }[] = [];
@@ -635,7 +608,7 @@ describe("CLI authentication", () => {
},
};
- expect(await main(argv, capture().stream, stderr.stream, deps)).toBe(0);
+ expect(await stderr.run(argv, deps)).toBe(0);
expect(onTurn.mock.results.at(-1)?.value).toBe(selection);
expect(question).toBe("How would you like to authenticate this scan?");
expect(choices).toEqual([
@@ -688,14 +661,11 @@ describe("CLI authentication", () => {
});
test("does not hide or relabel a failed ChatGPT login", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["login"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: { OPENAI_API_KEY: "sk-proj-SYNTHETIC_SECRET_123" },
onCodex: () => 17,
@@ -772,8 +742,10 @@ describe("CLI authentication", () => {
inputInteractive: false,
},
]) {
- const stdout = capture();
- const stderr = capture(scenario.terminal);
+ const { stdout, stderr, runCli } = createCliTest(main, {
+ stderr: scenario.terminal,
+ });
+
const onTurn = mock((_repository: string, { auth }: ScanOptions) => auth);
let prompts = 0;
const hasStoredChatGPTSignIn = mock(resolving(scenario.stored));
@@ -809,9 +781,7 @@ describe("CLI authentication", () => {
},
};
- expect(
- await main(scenario.argv, stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(scenario.argv, deps)).toBe(0);
expect(prompts).toBe(0);
if (scenario.argv.includes("--json") || scenario.argv.includes("jsonl")) {
expect(hasStoredChatGPTSignIn).toHaveBeenCalledTimes(0);
@@ -828,18 +798,11 @@ describe("CLI authentication", () => {
});
test("rejects explicit API-key authentication before initializing a scan when no key is set", async () => {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const deps = dependencies();
deps.createSecurity = throwing("must not initialize Codex Security");
- expect(
- await main(
- ["scan", "--auth", "api-key"],
- capture().stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ expect(await stderr.run(["scan", "--auth", "api-key"], deps)).toBe(2);
expect(stderr.text()).toContain(
"API-key authentication requires OPENAI_API_KEY or CODEX_API_KEY.",
);
@@ -848,13 +811,11 @@ describe("CLI authentication", () => {
});
test("keeps stored-login status unchanged when no environment key is set", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["login", "status"],
- stdout.stream,
- stderr.stream,
dependencies({ environment: { OPENAI_API_KEY: " " } }),
),
).toBe(0);
@@ -862,17 +823,15 @@ describe("CLI authentication", () => {
});
test("reports effective environment credentials without a stored sign-in", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
const environment: NodeJS.ProcessEnv = {
OPENAI_API_KEY: "synthetic-primary-key",
CODEX_API_KEY: "synthetic-secondary-key",
};
expect(
- await main(
+ await runCli(
["login", "status"],
- stdout.stream,
- stderr.stream,
dependencies({ environment, onCodex: () => 1 }),
),
).toBe(0);
@@ -880,31 +839,27 @@ describe("CLI authentication", () => {
expect(stderr.text()).not.toContain("synthetic");
delete environment["OPENAI_API_KEY"];
- const rotated = capture();
+ const rotated = captureCli(main, "stderr");
expect(
- await main(
+ await rotated.run(
["login", "status"],
- capture().stream,
- rotated.stream,
dependencies({ environment, onCodex: () => 1 }),
),
).toBe(0);
expect(rotated.text()).toContain("API key from CODEX_API_KEY");
expect(
- await main(
+ await runCapturedCli(
+ main,
["login", "status"],
- capture().stream,
- capture().stream,
dependencies({ environment: {}, onCodex: () => 1 }),
),
).toBe(1);
expect(
- await main(
+ await runCapturedCli(
+ main,
["login", "status"],
- capture().stream,
- capture().stream,
dependencies({
environment: { OPENAI_API_KEY: "synthetic-key" },
onCodex: () => 17,
@@ -1005,8 +960,8 @@ describe("CLI authentication", () => {
}, 60_000);
test("reports selected scan credentials without contaminating JSON output", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -1019,9 +974,7 @@ describe("CLI authentication", () => {
return fakeResult();
});
- expect(
- await main(["scan", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(
"Authentication: API key from OPENAI_API_KEY.",
@@ -1043,8 +996,10 @@ describe("CLI authentication", () => {
"cannot access the configured model",
],
] as const) {
- const stdout = capture();
- const stderr = capture(false);
+ const { stdout, stderr, runCli } = createCliTest(main, {
+ stderr: false,
+ });
+
const deps = dependencies({ environment });
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -1056,9 +1011,7 @@ describe("CLI authentication", () => {
throw new CodexSecurityError(detail);
});
- expect(await main(["scan"], stdout.stream, stderr.stream, deps)).toBe(
- 2,
- );
+ expect(await runCli(["scan"], deps)).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain(expected);
expect(stderr.text()).toContain(source);
@@ -1078,8 +1031,10 @@ describe("CLI authentication", () => {
"Your access token could not be refreshed because your refresh token was already used.",
"Your access token could not be refreshed because your refresh token was revoked.",
]) {
- const stdout = capture();
- const stderr = capture(false);
+ const { stdout, stderr, runCli } = createCliTest(main, {
+ stderr: false,
+ });
+
const deps = dependencies({
environment: { OPENAI_API_KEY: "sk-proj-SYNTHETIC_SECRET_123" },
onRun: () => {
@@ -1090,12 +1045,7 @@ describe("CLI authentication", () => {
});
expect(
- await main(
- ["scan", ".", "--auth", auth, "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
+ await runCli(["scan", ".", "--auth", auth, "--json"], deps),
).toBe(2);
expect(JSON.parse(stdout.text())).toMatchObject({
status: "failed",
@@ -1122,8 +1072,8 @@ describe("CLI authentication", () => {
"Your access token could not be refreshed because you have since logged out or signed in to another account. Please sign in again.",
"Your authentication session could not be refreshed automatically. Please log out and sign in again.",
]) {
- const stdout = capture();
- const stderr = capture(false);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: false });
+
const deps = dependencies({
onRun: () => {
throw new CodexSecurityError(
@@ -1132,9 +1082,7 @@ describe("CLI authentication", () => {
},
});
- expect(
- await main(["scan", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(2);
+ expect(await runCli(["scan", "--json"], deps)).toBe(2);
expect(JSON.parse(stdout.text())).toMatchObject({
status: "failed",
code: "SCAN_FAILED",
@@ -1147,8 +1095,8 @@ describe("CLI authentication", () => {
});
test("prints the ChatGPT recovery hint on noninteractive scan output", async () => {
- const stdout = capture();
- const stderr = capture(false);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: false });
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -1160,9 +1108,7 @@ describe("CLI authentication", () => {
return fakeResult();
});
- expect(
- await main(["scan", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain("API key from OPENAI_API_KEY");
expect(stderr.text()).toContain("retry with --auth chatgpt");
@@ -1181,13 +1127,11 @@ describe("CLI authentication", () => {
"403 model access denied for org-private",
],
] as const) {
- const stderr = capture(false);
+ const stderr = captureCli(main, "stderr", false);
const deps = dependencies({ environment });
deps.createSecurity = () => failingSecurity(message);
- expect(await main(["scan"], capture().stream, stderr.stream, deps)).toBe(
- 2,
- );
+ expect(await stderr.run(["scan"], deps)).toBe(2);
expect(stderr.text()).toContain(source);
expect(stderr.text()).toContain("--auth chatgpt");
expect(stderr.text()).not.toContain("SYNTHETIC_SECRET");
@@ -1206,8 +1150,8 @@ describe("CLI authentication", () => {
"Authentication: API key from CODEX_API_KEY.",
],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -1215,9 +1159,7 @@ describe("CLI authentication", () => {
return fakeResult();
});
- expect(
- await main(["scan", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", "--json"], deps)).toBe(0);
expect(stderr.text()).toContain(expected);
expect(stderr.text()).not.toContain("env -u");
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
@@ -1225,18 +1167,16 @@ describe("CLI authentication", () => {
});
test("keeps selected dry-run authentication metadata safe and machine readable", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const authentication = {
method: "api_key" as const,
source: "CODEX_API_KEY" as const,
verified: false as const,
};
expect(
- await main(
+ await runCli(
["scan", "repo", "--dry-run", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: { CODEX_API_KEY: "synthetic-private-key" },
preflight: { ...fakePreflight("repo"), authentication },
@@ -1259,8 +1199,8 @@ describe("CLI authentication", () => {
'{"auth_mode":"chatgpt"}\n',
);
- const stdout = capture();
- const stderr = capture();
+ const { runCli } = createCliTest(main);
+
let forwardedHome: string | undefined;
const deps = dependencies({
environment: {
@@ -1273,9 +1213,7 @@ describe("CLI authentication", () => {
return 0;
};
- expect(
- await main(["login", "status"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["login", "status"], deps)).toBe(0);
expect(forwardedHome).toBe(join(stateDirectory, "codex-home"));
expect(existsSync(join(stateDirectory, "codex-home", "auth.json"))).toBe(
true,
@@ -1302,8 +1240,8 @@ describe("CLI authentication", () => {
});
await setCodexSecurityCredentialLogout(credentialHome, true);
- const stdout = capture();
- const stderr = capture();
+ const { runCli } = createCliTest(main);
+
const deps = dependencies({
environment: {
CODEX_HOME: ambientHome,
@@ -1312,9 +1250,7 @@ describe("CLI authentication", () => {
});
deps.runCodex = async () => 0;
- expect(
- await main(["login", "status"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["login", "status"], deps)).toBe(0);
expect(existsSync(join(credentialHome, "auth.json"))).toBe(false);
} finally {
await rm(root, { recursive: true, force: true });
@@ -1356,9 +1292,9 @@ describe("skill authentication", () => {
{ mode: 0o600 },
);
}
- const stdout = capture();
- const stderr = capture();
- const status = await main(
+ const { stderr, runCli } = createCliTest(main);
+
+ const status = await runCli(
[
command,
"Synthetic issue",
@@ -1366,8 +1302,6 @@ describe("skill authentication", () => {
auth,
...overrides.flatMap((value) => ["--codex", value]),
],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: repository,
environment: {
@@ -1991,12 +1925,10 @@ describe("skill authentication", () => {
test.each(["validate", "patch", "verify-fix"])(
"%s advertises scan auth modes",
async (command) => {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
[command, "--schema", "--format", "json"],
- stdout.stream,
- capture().stream,
dependencies(),
),
).toBe(0);
@@ -2004,15 +1936,8 @@ describe("skill authentication", () => {
enum: ["auto", "chatgpt", "api-key"],
default: "auto",
});
- const help = capture();
- expect(
- await main(
- [command, "--help"],
- help.stream,
- capture().stream,
- dependencies(),
- ),
- ).toBe(0);
+ const help = captureCli(main, "stdout");
+ expect(await help.run([command, "--help"], dependencies())).toBe(0);
expect(help.text()).toContain("--auth");
},
);
@@ -2060,12 +1985,10 @@ describe("skill authentication", () => {
test.each(["validate", "patch", "verify-fix"])(
"%s rejects missing explicit API-key authentication before launch",
async (command) => {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[command, "Synthetic issue", "--auth", "api-key"],
- capture().stream,
- stderr.stream,
dependencies({
onCodex: (_args, output, environment, input) =>
runCodexSkillCommand(
@@ -2154,8 +2077,8 @@ describe("skill authentication", () => {
}
const usesSessionKey = auth !== "chatgpt" && provider === undefined;
const requestLog = join(stateDirectory, "requests.jsonl");
- const stderr = capture();
- const stdout = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const environment = {
CODEX_HOME: ambientHome,
...(provider === undefined || auth === "chatgpt"
@@ -2182,7 +2105,7 @@ describe("skill authentication", () => {
: {}),
};
expect(
- await main(
+ await runCli(
[
"patch",
"Synthetic issue",
@@ -2201,8 +2124,6 @@ describe("skill authentication", () => {
]
: []),
],
- stdout.stream,
- stderr.stream,
dependencies({
environment,
currentDirectory: repository,
@@ -2536,11 +2457,9 @@ if (basename(process.argv[1] ?? "") === "login" && process.argv[2] === "status")
join(alias, "component"),
nestedRepository,
]) {
- const stderr = capture();
- const status = await main(
+ const stderr = captureCli(main, "stderr");
+ const status = await stderr.run(
[command, "Synthetic issue", "--auth", "chatgpt"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: target,
environment: {
diff --git a/sdk/typescript/tests-ts/cli-classify-severity.test.ts b/sdk/typescript/tests-ts/cli-classify-severity.test.ts
index 76e09824a..ea2a6f070 100644
--- a/sdk/typescript/tests-ts/cli-classify-severity.test.ts
+++ b/sdk/typescript/tests-ts/cli-classify-severity.test.ts
@@ -2,7 +2,9 @@ import { rejecting } from "./support/errors.js";
import { resolve } from "node:path";
import { expect, test, mock } from "bun:test";
import { main } from "../src/cli.js";
-import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";
+import { dependencies, FakeSignals } from "./cli-fixtures.js";
+
+import { captureCli, runCapturedCli } from "./support/cli-run.js";
const result = {
schemaVersion: 1 as const,
@@ -17,7 +19,7 @@ test.each(["latest", "scan_prefix"])(
"classify-severity accepts saved scan selector %s",
async (selector) => {
const deps = dependencies();
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
deps.classifyScanSeverity = async (scanId, options, history, surface) => {
expect(scanId).toBe(selector);
expect(options!.rubricPath).toBe(
@@ -35,7 +37,7 @@ test.each(["latest", "scan_prefix"])(
return result;
};
expect(
- await main(
+ await stdout.run(
[
"classify-severity",
"--scan",
@@ -55,8 +57,6 @@ test.each(["latest", "scan_prefix"])(
"--reprocess",
"--json",
],
- stdout.stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -77,10 +77,9 @@ test("classify-severity accepts external scan directories and defaults to existi
return result;
};
expect(
- await main(
+ await runCapturedCli(
+ main,
["classify-severity", "--scan-dir", "saved scan", "--json"],
- capture().stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -93,23 +92,13 @@ test("classify-severity rejects missing or conflicting selectors and surfaces SD
deps.classifyScanSeverity = classifyScanSeverity;
for (const args of [[], ["--scan", "latest", "--scan-dir", "saved"]]) {
expect(
- await main(
- ["classify-severity", ...args],
- capture().stream,
- capture().stream,
- deps,
- ),
+ await runCapturedCli(main, ["classify-severity", ...args], deps),
).toBe(2);
}
expect(classifyScanSeverity).toHaveBeenCalledTimes(0);
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
- ["classify-severity", "--scan", "latest"],
- capture().stream,
- stderr.stream,
- deps,
- ),
+ await stderr.run(["classify-severity", "--scan", "latest"], deps),
).toBe(2);
expect(stderr.text()).toBe("codex-security: The scan is incomplete\n");
});
@@ -131,10 +120,9 @@ test.each([
return result;
};
expect(
- await main(
+ await runCapturedCli(
+ main,
["classify-severity", "--scan", "latest"],
- capture().stream,
- capture().stream,
deps,
),
).toBe(expectedCode);
@@ -159,7 +147,8 @@ test("publication forwards selected finding IDs only to Linear", async () => {
};
};
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"publish",
"scan",
@@ -176,13 +165,12 @@ test("publication forwards selected finding IDs only to Linear", async () => {
"--dry-run",
"--json",
],
- capture().stream,
- capture().stream,
deps,
),
).toBe(0);
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"publish",
"scan",
@@ -195,8 +183,6 @@ test("publication forwards selected finding IDs only to Linear", async () => {
"--finding-id",
"finding-one",
],
- capture().stream,
- capture().stream,
deps,
),
).toBe(2);
diff --git a/sdk/typescript/tests-ts/cli-cloud-publish.test.ts b/sdk/typescript/tests-ts/cli-cloud-publish.test.ts
index 1d050bb31..70659a7a3 100644
--- a/sdk/typescript/tests-ts/cli-cloud-publish.test.ts
+++ b/sdk/typescript/tests-ts/cli-cloud-publish.test.ts
@@ -6,7 +6,6 @@ import { afterEach, describe, expect, test, mock } from "bun:test";
import { main } from "../src/cli.js";
import type { JsonObject } from "../src/index.js";
import {
- capture,
dependencies,
FakeSignals,
SYNTHETIC_CREDENTIALS,
@@ -15,6 +14,12 @@ import {
import { createApiTestFixtures } from "./support/temporary-directories.js";
import { rejecting, throwing } from "./support/errors.js";
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
+
const receipt = {
scanId: "scan-1",
findingIds: ["finding-1"],
@@ -58,14 +63,9 @@ async function savedScansFixture() {
describe("publish scan to Cloud", () => {
test("documents the findings CSV option", async () => {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
- ["publish", "scan", "--help"],
- stdout.stream,
- capture().stream,
- dependencies(),
- ),
+ await stdout.run(["publish", "scan", "--help"], dependencies()),
).toBe(0);
expect(stdout.text()).toContain("--csv <file>");
expect(stdout.text()).toContain("Findings CSV");
@@ -83,10 +83,10 @@ describe("publish scan to Cloud", () => {
expect(options?.signal).toBeInstanceOf(AbortSignal);
return { ...receipt, dryRun: true };
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -97,8 +97,6 @@ describe("publish scan to Cloud", () => {
"--dry-run",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -133,15 +131,8 @@ describe("publish scan to Cloud", () => {
const deps = dependencies();
const publishFindingsCsvToCloud = mock(resolving(receipt));
deps.publishFindingsCsvToCloud = publishFindingsCsvToCloud;
- const stderr = capture();
- expect(
- await main(
- ["publish", "scan", ...args],
- capture().stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ const stderr = captureCli(main, "stderr");
+ expect(await stderr.run(["publish", "scan", ...args], deps)).toBe(2);
expect(publishFindingsCsvToCloud).toHaveBeenCalledTimes(0);
expect(stderr.text()).toContain(message);
});
@@ -159,10 +150,10 @@ describe("publish scan to Cloud", () => {
calls.push(scan.scanId);
return { ...receipt, scanId: scan.scanId, dryRun: true };
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -176,8 +167,6 @@ describe("publish scan to Cloud", () => {
"--dry-run",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -205,9 +194,9 @@ describe("publish scan to Cloud", () => {
await rm(second!.scanDir, { recursive: true });
const publishScanToCloud = mock(resolving(receipt));
deps.publishScanToCloud = publishScanToCloud;
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[
"publish",
"scan",
@@ -218,8 +207,6 @@ describe("publish scan to Cloud", () => {
"--to",
"cloud",
],
- capture().stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -236,9 +223,9 @@ describe("publish scan to Cloud", () => {
const deps = dependencies({
onWorkbench: throwing("unexpected lookup"),
});
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[
"publish",
"scan",
@@ -249,8 +236,6 @@ describe("publish scan to Cloud", () => {
"--to",
"cloud",
],
- capture().stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -290,14 +275,9 @@ describe("publish scan to Cloud", () => {
calls.push(options!.expectedScanId!);
return { ...receipt, scanId: options!.expectedScanId! };
};
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
- ["publish", "scan", "--to", "cloud", "--json"],
- stdout.stream,
- capture().stream,
- deps,
- ),
+ await stdout.run(["publish", "scan", "--to", "cloud", "--json"], deps),
).toBe(0);
expect(calls).toEqual(picks);
expect(
@@ -326,12 +306,7 @@ describe("publish scan to Cloud", () => {
const publishScanToCloud = mock(resolving(receipt));
deps.publishScanToCloud = publishScanToCloud;
expect(
- await main(
- ["publish", "scan", "--to", "cloud"],
- capture().stream,
- capture().stream,
- deps,
- ),
+ await runCapturedCli(main, ["publish", "scan", "--to", "cloud"], deps),
).toBe(130);
expect(publishScanToCloud).toHaveBeenCalledTimes(0);
expect(
@@ -355,9 +330,9 @@ describe("publish scan to Cloud", () => {
}
return { ...receipt, scanId: options!.expectedScanId! };
};
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
[
"publish",
"scan",
@@ -366,8 +341,6 @@ describe("publish scan to Cloud", () => {
"cloud",
"--json",
],
- stdout.stream,
- capture().stream,
deps,
),
).toBe(143);
@@ -393,10 +366,10 @@ describe("publish scan to Cloud", () => {
if (uploads === scans.length) signals.emit("SIGINT");
return { ...receipt, scanId: options!.expectedScanId! };
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -405,8 +378,6 @@ describe("publish scan to Cloud", () => {
"cloud",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -458,10 +429,10 @@ describe("publish scan to Cloud", () => {
publishing = false;
return result;
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -474,8 +445,6 @@ describe("publish scan to Cloud", () => {
"--json",
...(dryRun ? ["--dry-run"] : []),
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -506,10 +475,10 @@ describe("publish scan to Cloud", () => {
scanId: directory === directories[0] ? "scan-1" : "scan-3",
};
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -518,8 +487,6 @@ describe("publish scan to Cloud", () => {
"cloud",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -568,10 +535,10 @@ describe("publish scan to Cloud", () => {
}
return receipt;
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -580,8 +547,6 @@ describe("publish scan to Cloud", () => {
"cloud",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(code);
@@ -620,10 +585,10 @@ describe("publish scan to Cloud", () => {
const deps = dependencies();
const publishScan = mock(rejecting("unexpected publication"));
deps.publishScan = publishScan;
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -636,8 +601,6 @@ describe("publish scan to Cloud", () => {
"synthetic-team",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -678,10 +641,10 @@ describe("publish scan to Cloud", () => {
});
return result;
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -692,8 +655,6 @@ describe("publish scan to Cloud", () => {
"--json",
...(dryRun ? ["--dry-run"] : []),
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -724,12 +685,10 @@ describe("publish scan to Cloud", () => {
});
const calls = mock(resolving<typeof receipt, [string]>(receipt));
deps.publishScanToCloud = calls;
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["publish", "scan", ...inputs, "--to", "cloud", "--json"],
- stdout.stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -763,9 +722,9 @@ describe("publish scan to Cloud", () => {
onWorkbench: unexpectedScanHistory,
});
deps.publishScanToCloud = calls;
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
[
"publish",
"scan",
@@ -777,8 +736,6 @@ describe("publish scan to Cloud", () => {
"cloud",
"--json",
],
- stdout.stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -799,15 +756,10 @@ describe("publish scan to Cloud", () => {
const deps = dependencies();
const publishScanToCloud = mock(resolving(receipt));
deps.publishScanToCloud = publishScanToCloud;
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
- ["publish", "scan", ...inputs, "--to", "cloud"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
+ await runCli(["publish", "scan", ...inputs, "--to", "cloud"], deps),
).toBe(2);
expect(publishScanToCloud).toHaveBeenCalledTimes(0);
expect(stdout.text()).toBe("");
@@ -852,14 +804,9 @@ describe("publish scan to Cloud", () => {
expect(options?.expectedScanId).toBe("scan-1");
return receipt;
};
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
- ["publish", "scan", "--to", "cloud", "--json"],
- stdout.stream,
- capture().stream,
- deps,
- ),
+ await stdout.run(["publish", "scan", "--to", "cloud", "--json"], deps),
).toBe(0);
expect(selections).toBe(1);
expect(JSON.parse(stdout.text())).toEqual(receipt);
@@ -875,15 +822,10 @@ describe("publish scan to Cloud", () => {
select: rejecting("unexpected picker"),
};
deps.publishScanToCloud = rejecting("unexpected publication");
- const stderr = capture();
- expect(
- await main(
- ["publish", "scan", "--to", "cloud"],
- capture().stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ const stderr = captureCli(main, "stderr");
+ expect(await stderr.run(["publish", "scan", "--to", "cloud"], deps)).toBe(
+ 2,
+ );
expect(stderr.text()).toContain("--scan SCAN_ID --to cloud");
expect(stderr.text()).not.toContain("--linear-team");
});
@@ -900,10 +842,10 @@ describe("publish scan to Cloud", () => {
const deps = dependencies();
const publishScanToCloud = mock(resolving(receipt));
deps.publishScanToCloud = publishScanToCloud;
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -912,8 +854,6 @@ describe("publish scan to Cloud", () => {
"cloud",
...linearOptions,
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -925,17 +865,10 @@ describe("publish scan to Cloud", () => {
test("keeps the internal Cloud destination out of help and discovery", async () => {
for (const flag of ["--help", "--schema", "--llms", "--llms-full"]) {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
const deps = dependencies();
deps.publishScanToCloud = rejecting("unexpected publication");
- expect(
- await main(
- ["publish", "scan", flag],
- stdout.stream,
- capture().stream,
- deps,
- ),
- ).toBe(0);
+ expect(await stdout.run(["publish", "scan", flag], deps)).toBe(0);
expect(stdout.text().toLowerCase()).not.toContain("cloud");
}
});
@@ -945,13 +878,11 @@ describe("publish scan to Cloud", () => {
deps.publishScanToCloud = rejecting(
`Cloud failed: ${SYNTHETIC_CREDENTIALS}\u001b[2J\ncontinued`,
);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", "--to", "cloud"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -968,13 +899,11 @@ describe("publish scan to Cloud", () => {
signals.emit("SIGINT");
return receipt;
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", "--to", "cloud", "--json"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1009,13 +938,11 @@ describe("publish scan to Cloud", () => {
options?.signal?.throwIfAborted();
return receipt;
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", "--to", "cloud"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(code);
diff --git a/sdk/typescript/tests-ts/cli-dedupe.test.ts b/sdk/typescript/tests-ts/cli-dedupe.test.ts
index 305c6cf54..69d01f2ae 100644
--- a/sdk/typescript/tests-ts/cli-dedupe.test.ts
+++ b/sdk/typescript/tests-ts/cli-dedupe.test.ts
@@ -1,8 +1,14 @@
import { expect, test, mock } from "bun:test";
import { main } from "../src/cli.js";
-import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";
+import { dependencies, FakeSignals } from "./cli-fixtures.js";
import { throwing, rejecting } from "./support/errors.js";
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
+
const args = [
"dedupe",
"--scan",
@@ -32,10 +38,10 @@ test.each([false, true])(
],
};
deps.deduplicateScan = async () => result;
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
if (brokenLog) stderr.stream.write = throwing("Synthetic logging failure");
- expect(await main(args, stdout.stream, stderr.stream, deps)).toBe(0);
+ expect(await runCli(args, deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(result);
if (!brokenLog) {
expect(stderr.text()).toContain("pair-review refused by gpt-5.6-sol");
@@ -72,9 +78,9 @@ test("dedupe resolves a workflow's pinned scan and passes the workflow ID to the
deduplicationStatus: "completed",
};
};
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
[
"dedupe",
"--workflow-id",
@@ -83,8 +89,6 @@ test("dedupe resolves a workflow's pinned scan and passes the workflow ID to the
"http://localhost:3000",
"--json",
],
- stdout.stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -99,8 +103,8 @@ test("dedupe resolves a workflow's pinned scan and passes the workflow ID to the
test.each([false, true])(
"dedupe passes the scan selector, URL, and all-repository scope %j to the SDK",
async (allRepositories) => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
const result = {
scanId: "scan-example",
@@ -120,10 +124,8 @@ test.each([false, true])(
return result;
};
expect(
- await main(
+ await runCli(
[...args, ...(allRepositories ? ["--all-repositories"] : [])],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -149,9 +151,7 @@ test.each([
deduplicationStatus: "completed",
};
};
- expect(
- await main([...args, ...flags], capture().stream, capture().stream, deps),
- ).toBe(0);
+ expect(await runCapturedCli(main, [...args, ...flags], deps)).toBe(0);
expect(called).toBe(true);
});
@@ -163,52 +163,29 @@ test.each(["0", "-1", "1.5", "NaN", "Infinity", "9007199254740992"])(
rejecting("Invalid concurrency must not reach the SDK"),
);
deps.deduplicateScan = deduplicateScan;
- const stderr = capture();
- expect(
- await main(
- [...args, "--concurrency", value],
- capture().stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ const stderr = captureCli(main, "stderr");
+ expect(await stderr.run([...args, "--concurrency", value], deps)).toBe(2);
expect(stderr.text()).toContain("concurrency");
expect(deduplicateScan).not.toHaveBeenCalled();
},
);
test("dedupe requires a value for concurrency", async () => {
- const stderr = capture();
- expect(
- await main(
- [...args, "--concurrency"],
- capture().stream,
- stderr.stream,
- dependencies(),
- ),
- ).toBe(2);
+ const stderr = captureCli(main, "stderr");
+ expect(await stderr.run([...args, "--concurrency"], dependencies())).toBe(2);
expect(stderr.text()).toContain("Missing value for flag: --concurrency");
});
test("dedupe help and schema expose concurrency and its default", async () => {
- const help = capture();
- expect(
- await main(
- ["dedupe", "--help"],
- help.stream,
- capture().stream,
- dependencies(),
- ),
- ).toBe(0);
+ const help = captureCli(main, "stdout");
+ expect(await help.run(["dedupe", "--help"], dependencies())).toBe(0);
expect(help.text()).toContain("--concurrency");
expect(help.text()).toContain("serial execution");
- const schema = capture();
+ const schema = captureCli(main, "stdout");
expect(
- await main(
+ await schema.run(
["dedupe", "--schema", "--format", "json"],
- schema.stream,
- capture().stream,
dependencies(),
),
).toBe(0);
@@ -229,19 +206,12 @@ test("dedupe requires both explicit inputs and reports SDK failures", async () =
["--scan", "latest"],
["--findings-url", "http://127.0.0.1:3000"],
]) {
- expect(
- await main(
- ["dedupe", ...flags],
- capture().stream,
- capture().stream,
- deps,
- ),
- ).not.toBe(0);
+ expect(await runCapturedCli(main, ["dedupe", ...flags], deps)).not.toBe(0);
}
expect(deduplicateScan).not.toHaveBeenCalled();
- const stdout = capture();
- const stderr = capture();
- expect(await main(args, stdout.stream, stderr.stream, deps)).toBe(2);
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ expect(await runCli(args, deps)).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toBe("codex-security: Finding has not been indexed\n");
});
@@ -261,11 +231,9 @@ test("dedupe forwards cancellation and removes signal handlers", async () => {
options.signal!.throwIfAborted();
throw new Error("Cancellation must throw");
};
- const stdout = capture();
- const stderr = capture();
- expect(await main(args, stdout.stream, stderr.stream, deps)).toBe(
- expectedCode,
- );
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ expect(await runCli(args, deps)).toBe(expectedCode);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain("Deduplication canceled");
expect(signals.listeners.get("SIGINT")?.size).toBe(0);
diff --git a/sdk/typescript/tests-ts/cli-deep-scan-summary.test.ts b/sdk/typescript/tests-ts/cli-deep-scan-summary.test.ts
index e8df74826..4d51e35a9 100644
--- a/sdk/typescript/tests-ts/cli-deep-scan-summary.test.ts
+++ b/sdk/typescript/tests-ts/cli-deep-scan-summary.test.ts
@@ -1,9 +1,11 @@
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 { dependencies, fakeResult } from "./cli-fixtures.js";
import { throwing } from "./support/errors.js";
+import { createCliTest } from "./support/cli-run.js";
+
const cappedState: JsonObject = {
terminalReason: "capped",
dispatchedCount: 40,
@@ -18,14 +20,12 @@ async function summary(
options: Parameters<typeof dependencies>[0],
args = ["--mode", "deep"],
) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = options?.result ?? fakeResult(["high"], "partial");
expect(
- await main(
+ await runCli(
["scan", ...args, "--json"],
- stdout.stream,
- stderr.stream,
dependencies({ ...options, result }),
),
).toBe(result.coverage.completeness === "complete" ? 0 : 2);
diff --git a/sdk/typescript/tests-ts/cli-diagnostics.test.ts b/sdk/typescript/tests-ts/cli-diagnostics.test.ts
index 6d2e97e9a..b96d765f1 100644
--- a/sdk/typescript/tests-ts/cli-diagnostics.test.ts
+++ b/sdk/typescript/tests-ts/cli-diagnostics.test.ts
@@ -4,13 +4,14 @@ import { main } from "../src/cli.js";
import { CodexSecurityError, OutputDirectoryError } from "../src/errors.js";
import {
warningResult,
- capture,
dependencies,
fakeResult,
fakeSecurity,
} from "./cli-fixtures.js";
import { throwing } from "./support/errors.js";
+import { createCliTest } from "./support/cli-run.js";
+
describe("CLI diagnostics", () => {
test.each([
{
@@ -72,10 +73,9 @@ describe("CLI diagnostics", () => {
deps.importGitHubAlerts = fail;
if (command === "policy" || command === "suggest-owners")
deps.currentDirectory = fail;
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
- expect(await main(args, stdout.stream, stderr.stream, deps)).toBe(2);
+ expect(await runCli(args, deps)).toBe(2);
expect(stderr.text()).toContain(
"codex-security: Operation failed: token=SYNTHETIC_VALUE [2J continued tail\n",
);
@@ -96,20 +96,13 @@ describe("CLI diagnostics", () => {
new CodexSecurityError("request timed out token=SYNTHETIC_TIMEOUT_VALUE"),
]) {
test(`preserves scan failure details for ${failure.message}`, async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity((Promise.reject<never>).bind(Promise, failure));
- expect(
- await main(
- ["scan", ".", "--json", "--verbose"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--json", "--verbose"], deps)).toBe(2);
expect(JSON.parse(stdout.text()).message).toBe(failure.message);
expect(stderr.text()).toContain(failure.message);
});
@@ -163,8 +156,10 @@ describe("CLI diagnostics", () => {
])("preserves warning details and verbosity for $name", async (mode) => {
const warning = "recoverable warning: token=SYNTHETIC_WARNING_VALUE";
const observer = "observer failure: token=SYNTHETIC_OBSERVER_VALUE";
- const stdout = capture();
- const stderr = capture(mode.tty);
+ const { stderr, runCli } = createCliTest(main, {
+ stderr: mode.tty,
+ });
+
const result = fakeResult([], "complete", {
input_tokens: 200,
cached_input_tokens: 20,
@@ -187,14 +182,7 @@ describe("CLI diagnostics", () => {
return result;
});
- expect(
- await main(
- ["scan", ".", ...mode.args],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli(["scan", ".", ...mode.args], deps)).toBe(0);
const output = stripVTControlCharacters(stderr.text()).replace(
/\s+/gu,
" ",
@@ -218,18 +206,11 @@ describe("CLI diagnostics", () => {
test("preserves target warning details in diagnostics and result data", async () => {
const warning = "Source changed: token=SYNTHETIC_TARGET_VALUE";
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () => fakeSecurity(warningResult(warning, true));
- expect(
- await main(
- ["scan", ".", "--json", "--verbose"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--json", "--verbose"], deps)).toBe(2);
expect(JSON.parse(stdout.text()).warnings).toEqual([warning]);
expect(stderr.text()).toContain(`codex-security: warning: ${warning}`);
});
@@ -237,8 +218,8 @@ describe("CLI diagnostics", () => {
test("preserves live activity and observer messages", async () => {
const activity = "command result: token=SYNTHETIC_ACTIVITY_VALUE";
const observer = "observer result: token=SYNTHETIC_OBSERVER_VALUE";
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
const deps = dependencies({ environment: { NO_COLOR: "1" } });
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -254,9 +235,7 @@ describe("CLI diagnostics", () => {
return fakeResult();
});
- expect(await main(["scan", "."], stdout.stream, stderr.stream, deps)).toBe(
- 0,
- );
+ expect(await runCli(["scan", "."], deps)).toBe(0);
const output = stripVTControlCharacters(stderr.text()).replace(
/\s+/gu,
" ",
diff --git a/sdk/typescript/tests-ts/cli-export.test.ts b/sdk/typescript/tests-ts/cli-export.test.ts
index a7962bdf8..93403596c 100644
--- a/sdk/typescript/tests-ts/cli-export.test.ts
+++ b/sdk/typescript/tests-ts/cli-export.test.ts
@@ -34,6 +34,11 @@ import {
import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";
import { temporaryDirectory } from "./support/temporary-directories.js";
import { runCommand } from "./support/shell.js";
+import {
+ createCliTest,
+ runCapturedCli,
+ captureCli,
+} from "./support/cli-run.js";
async function copyCompletedScan(root: string): Promise<string> {
const scan = join(root, "scan");
@@ -68,15 +73,13 @@ describe("CLI", () => {
["json", '{"documentType":"codex-security.findings"}\n'],
["sarif", '{"version":"2.1.0"}\n'],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = mustNotInitializeCodex;
expect(
- await main(
+ await runCli(
["export", "scan", "--export-format", format, "--output", "-"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -99,14 +102,9 @@ describe("CLI", () => {
return new Uint8Array();
};
- expect(
- await main(
- ["export", "--output", "-"],
- capture().stream,
- capture().stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCapturedCli(main, ["export", "--output", "-"], deps)).toBe(
+ 0,
+ );
expect(exportedScanDir).toBe(scanDir);
});
@@ -123,11 +121,9 @@ describe("CLI", () => {
exports += 1;
return undefined;
};
- const stdout = capture();
- const stderr = capture();
- expect(await main(["export"], stdout.stream, stderr.stream, deps)).toBe(
- 2,
- );
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ expect(await runCli(["export"], deps)).toBe(2);
expect(stderr.text()).toBe(
lookupFails
? "codex-security: Synthetic history failure.\n"
@@ -285,8 +281,8 @@ describe("CLI", () => {
test("exports a saved provisional model by scan prefix without starting Codex", async () => {
const root = await mkdtemp(join(tmpdir(), "codex-security-export-model-"));
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const calls: string[][] = [];
const deps = dependencies({
onWorkbench: (args) => {
@@ -310,7 +306,7 @@ describe("CLI", () => {
};
try {
expect(
- await main(
+ await runCli(
[
"export",
"--scan",
@@ -320,8 +316,6 @@ describe("CLI", () => {
"--output",
"-",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -363,21 +357,13 @@ describe("CLI", () => {
};
try {
expect(
- await main(
+ await runCapturedCli(
+ main,
["export", "saved", "--artifact", "threat-model"],
- capture().stream,
- capture().stream,
- deps,
- ),
- ).toBe(0);
- expect(
- await main(
- ["export", "saved"],
- capture().stream,
- capture().stream,
deps,
),
).toBe(0);
+ expect(await runCapturedCli(main, ["export", "saved"], deps)).toBe(0);
expect(selected).toMatchObject([
{
artifact: "threat-model",
@@ -412,14 +398,12 @@ describe("CLI", () => {
"Markdown stdout cannot",
],
] as const) {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const deps = dependencies();
deps.exportFindings = async () => {
throw new Error("must not export invalid input");
};
- expect(
- await main(["export", ...args], capture().stream, stderr.stream, deps),
- ).toBe(2);
+ expect(await stderr.run(["export", ...args], deps)).toBe(2);
expect(stderr.text()).toContain(expected);
}
});
@@ -791,7 +775,8 @@ describe("CLI", () => {
const exports = mock<typeof deps.exportFindings>(resolving(undefined));
deps.exportFindings = exports;
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"export",
"~/scan",
@@ -802,8 +787,6 @@ describe("CLI", () => {
"--source-root",
"~/source",
],
- capture().stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -827,12 +810,10 @@ describe("CLI", () => {
const root = await temporaryDirectory("codex-security-export-missing-");
try {
const output = join(root, "reports", "results.sarif");
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["export", "scan", "--output", output],
- capture().stream,
- stderr.stream,
dependencies(),
),
).toBe(2);
@@ -888,10 +869,9 @@ describe("CLI", () => {
return new TextEncoder().encode('{"version":"2.1.0"}\n');
};
expect(
- await main(
+ await runCapturedCli(
+ main,
["export", join(linked, "scan"), "--output", output],
- capture().stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -970,10 +950,10 @@ describe("CLI", () => {
linked,
process.platform === "win32" ? "junction" : "dir",
);
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[
"export",
scan,
@@ -982,8 +962,6 @@ describe("CLI", () => {
"--output",
join(linked, "results.json"),
],
- capture().stream,
- stderr.stream,
dependencies(),
),
).toBe(2);
@@ -1012,7 +990,7 @@ describe("CLI", () => {
join(repository, "reports"),
process.platform === "win32" ? "junction" : "dir",
);
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const deps = dependencies();
deps.currentDirectory = () => repository;
deps.exportFindings = async () => {
@@ -1020,15 +998,13 @@ describe("CLI", () => {
};
expect(
- await main(
+ await stderr.run(
[
"export",
scan,
"--output",
join(repository, "reports", "results.sarif"),
],
- capture().stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -1041,22 +1017,15 @@ describe("CLI", () => {
});
test("reports strict export failures without a stack trace", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.exportFindings = async () => {
throw new CodexSecurityError(
"manifest.scan: SARIF projection requires a sealed scan",
);
};
- expect(
- await main(
- ["export", "scan", "--output", "-"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ expect(await runCli(["export", "scan", "--output", "-"], deps)).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toBe(
"codex-security: manifest.scan: SARIF projection requires a sealed scan\n",
@@ -1064,21 +1033,14 @@ describe("CLI", () => {
});
test("preserves caught export failures", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.exportFindings = async () => {
throw new CodexSecurityError(`export failed ${SYNTHETIC_CREDENTIALS}`);
};
- expect(
- await main(
- ["export", "scan", "--output", "-"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ expect(await runCli(["export", "scan", "--output", "-"], deps)).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toBe(
`codex-security: export failed ${SYNTHETIC_CREDENTIALS}\n`,
diff --git a/sdk/typescript/tests-ts/cli-feedback.test.ts b/sdk/typescript/tests-ts/cli-feedback.test.ts
index 71be83331..2457715ff 100644
--- a/sdk/typescript/tests-ts/cli-feedback.test.ts
+++ b/sdk/typescript/tests-ts/cli-feedback.test.ts
@@ -6,19 +6,15 @@ 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 { dependencies, FakeSignals } from "./cli-fixtures.js";
import { temporaryDirectory } from "./support/temporary-directories.js";
import { rejecting, throwing } from "./support/errors.js";
+import { createCliTest } from "./support/cli-run.js";
async function run(args: string[], deps = dependencies()) {
- const stdout = capture();
- const stderr = capture();
- const code = await main(
- ["feedback", ...args],
- stdout.stream,
- stderr.stream,
- deps,
- );
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(["feedback", ...args], deps);
return { code, stdout: stdout.text(), stderr: stderr.text() };
}
diff --git a/sdk/typescript/tests-ts/cli-help.test.ts b/sdk/typescript/tests-ts/cli-help.test.ts
index e3205bd99..b6e08334d 100644
--- a/sdk/typescript/tests-ts/cli-help.test.ts
+++ b/sdk/typescript/tests-ts/cli-help.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test";
import { main } from "../src/cli.js";
import { DEFAULT_DEEP_SCAN_SETTINGS } from "../src/deep-scan-defaults.js";
import { capture, dependencies } from "./cli-fixtures.js";
+import { captureCli } from "./support/cli-run.js";
async function help(args: readonly string[], columns?: number) {
const stdout = capture(true);
@@ -125,14 +126,9 @@ describe("CLI help", () => {
const text = await help([command, "--help"]);
expect(option(text, "--format")).not.toMatch(/\bjsonl?\b/u);
expect(option(text, "--json")).toBe("");
- const schema = capture();
+ const schema = captureCli(main, "stdout");
expect(
- await main(
- [command, "--schema", "--json"],
- schema.stream,
- capture().stream,
- dependencies(),
- ),
+ await schema.run([command, "--schema", "--json"], dependencies()),
).toBe(0);
expect(JSON.parse(schema.text())).toEqual(expect.any(Object));
},
diff --git a/sdk/typescript/tests-ts/cli-history-paths.test.ts b/sdk/typescript/tests-ts/cli-history-paths.test.ts
index aea34277f..d0dbacd98 100644
--- a/sdk/typescript/tests-ts/cli-history-paths.test.ts
+++ b/sdk/typescript/tests-ts/cli-history-paths.test.ts
@@ -3,7 +3,8 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, test, mock } from "bun:test";
import { main } from "../src/cli.js";
-import { capture, dependencies } from "./cli-fixtures.js";
+import { dependencies } from "./cli-fixtures.js";
+import { captureCli } from "./support/cli-run.js";
test.skipIf(process.platform !== "win32")(
"rejects an aliased Windows scan root before querying history",
@@ -19,13 +20,11 @@ test.skipIf(process.platform !== "win32")(
const onWorkbench = mock(() => {
return { scans: [] };
});
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scans", "list", "--scan-root", ambiguous],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: root,
onWorkbench,
diff --git a/sdk/typescript/tests-ts/cli-patch-results.test.ts b/sdk/typescript/tests-ts/cli-patch-results.test.ts
index 1b5a3a738..c54994901 100644
--- a/sdk/typescript/tests-ts/cli-patch-results.test.ts
+++ b/sdk/typescript/tests-ts/cli-patch-results.test.ts
@@ -5,7 +5,8 @@ import { join } from "node:path";
import { promisify } from "node:util";
import { describe, expect, test } from "bun:test";
import { main, runCodexSkillCommand } from "../src/cli.js";
-import { capture, dependencies } from "./cli-fixtures.js";
+import { dependencies } from "./cli-fixtures.js";
+import { createCliTest } from "./support/cli-run.js";
type FixtureOptions = NonNullable<Parameters<typeof dependencies>[0]>;
const execFileAsync = promisify(execFile);
@@ -45,12 +46,10 @@ async function repositoryFixture({ initializeGit = true } = {}) {
await rm(directory, { recursive: true, force: true });
},
async patch(args: string[], options: FixtureOptions = {}) {
- const stdout = capture();
- const stderr = capture();
- const status = await main(
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ const status = await runCli(
["patch", ...args, "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: directory,
environment: {
diff --git a/sdk/typescript/tests-ts/cli-patch.test.ts b/sdk/typescript/tests-ts/cli-patch.test.ts
index 7687f796a..37d3cb679 100644
--- a/sdk/typescript/tests-ts/cli-patch.test.ts
+++ b/sdk/typescript/tests-ts/cli-patch.test.ts
@@ -16,6 +16,7 @@ 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";
+import { createCliTest } from "./support/cli-run.js";
const CURRENT_REPOSITORY = resolve("/current/repository");
const SAVED_REPOSITORY = resolve("/saved/repository");
@@ -118,8 +119,10 @@ async function runWorkflow(
configure?: (value: ReturnType<typeof dependencies>) => void;
} = {},
) {
- const stdout = capture();
- const stderr = capture(options.interactive);
+ const { stdout, stderr, runCli } = createCliTest(main, {
+ stderr: options.interactive,
+ });
+
const current = dependencies({
currentDirectory: CURRENT_REPOSITORY,
onCodex: (args, output) => {
@@ -136,7 +139,7 @@ async function runWorkflow(
}
options.configure?.(current);
return {
- exitCode: await main(arguments_, stdout.stream, stderr.stream, current),
+ exitCode: await runCli(arguments_, current),
stdout: stdout.text(),
stderr: stderr.text(),
};
@@ -147,8 +150,8 @@ describe("scan and patch workflow", () => {
"shows progress during baseline preparation and cleans up on failure: %p",
async (failSnapshot) => {
const result = resultWithFindings(["high"]);
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
let snapshotHadProgress = false;
let resultSnapshotHadProgress = false;
let modelStarted = false;
@@ -178,10 +181,8 @@ describe("scan and patch workflow", () => {
current.setInterval = setInterval;
current.clearInterval = clearInterval;
- const status = await main(
+ const status = await runCli(
["scan", "--patch", "--patch-severity", "high"],
- stdout.stream,
- stderr.stream,
current,
);
@@ -247,8 +248,8 @@ describe("scan and patch workflow", () => {
async (title) => {
const result = resultWithFindings(["high"]);
result.findings.findings[0]!.title = title;
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
Object.assign(stderr.stream, { columns: 36 });
let now = 0;
const current = dependencies({
@@ -267,12 +268,7 @@ describe("scan and patch workflow", () => {
current.clearInterval = () => {};
expect(
- await main(
- ["patch", "--scan", "scan-1", "--json"],
- stdout.stream,
- stderr.stream,
- current,
- ),
+ await runCli(["patch", "--scan", "scan-1", "--json"], current),
).toBe(0);
const frames = stripVTControlCharacters(stderr.text())
@@ -292,8 +288,8 @@ describe("scan and patch workflow", () => {
["patch", "--scan", "scan-1", "--json"],
]) {
const result = resultWithFindings(["high", "high"]);
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
let now = 0;
let index = 0;
const timers = new Map<NodeJS.Timeout, () => void>();
@@ -342,10 +338,7 @@ describe("scan and patch workflow", () => {
timers.delete(timer);
};
- expect(
- await main(args, stdout.stream, stderr.stream, current),
- stderr.text(),
- ).toBe(0);
+ expect(await runCli(args, current), stderr.text()).toBe(0);
expect(index).toBe(2);
expect(timers.size).toBe(0);
const progress = stderr.text();
diff --git a/sdk/typescript/tests-ts/cli-policy.test.ts b/sdk/typescript/tests-ts/cli-policy.test.ts
index 708aada88..b606680f4 100644
--- a/sdk/typescript/tests-ts/cli-policy.test.ts
+++ b/sdk/typescript/tests-ts/cli-policy.test.ts
@@ -18,6 +18,11 @@ import {
stageResult,
} from "./support/security-policy.js";
import { rejecting, throwing } from "./support/errors.js";
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
const { fixture, cleanup } = createPolicyTestFixtures();
afterEach(cleanup);
@@ -113,15 +118,8 @@ function policyDependencies(
describe("policy CLI", () => {
test("documents the policy workflow in help", async () => {
- const stdout = capture();
- expect(
- await main(
- ["policy", "--help"],
- stdout.stream,
- capture().stream,
- dependencies(),
- ),
- ).toBe(0);
+ const stdout = captureCli(main, "stdout");
+ expect(await stdout.run(["policy", "--help"], dependencies())).toBe(0);
expect(stdout.text()).toContain("SECURITY.md");
expect(stdout.text()).not.toContain("--apply");
expect(stdout.text()).not.toContain("--write");
@@ -145,12 +143,12 @@ describe("policy CLI", () => {
estimatedUsd: 1,
estimatedUsdRange: { min: 1, max: 2, context: "unknown" },
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const onClose = mock();
const onConfig = mock<(config: unknown) => void>();
expect(
- await main(
+ await runCli(
[
"policy",
".",
@@ -161,8 +159,6 @@ describe("policy CLI", () => {
"high",
"--json",
],
- stdout.stream,
- stderr.stream,
policyDependencies(f, {
draft,
onClose,
@@ -334,13 +330,11 @@ describe("policy CLI", () => {
test("does not present a partial cost as the final estimate", async () => {
const f = await fixture();
const draft = await f.generate();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["policy", "--headless", "--json"],
- stdout.stream,
- stderr.stream,
policyDependencies(f, {
draft,
onGenerate: (_repository, options) =>
@@ -378,13 +372,11 @@ describe("policy CLI", () => {
test("preserves a completed draft when runtime cleanup fails", async () => {
const f = await fixture();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["policy", "--headless", "--json"],
- stdout.stream,
- stderr.stream,
policyDependencies(f, {
onClose: throwing("synthetic cleanup failure"),
}),
@@ -469,13 +461,11 @@ describe("policy CLI", () => {
"does not run a diff preview for %s output",
async (format) => {
const f = await fixture();
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
let previews = 0;
expect(
- await main(
+ await stdout.run(
["policy", "--format", format],
- stdout.stream,
- capture().stream,
policyDependencies(f, { onPreview: () => previews++ }),
),
).toBe(0);
@@ -539,18 +529,11 @@ describe("policy CLI", () => {
test("preflights without generation", async () => {
const f = await fixture();
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
const deps = policyDependencies(f, {
onGenerate: throwing("Must not generate"),
});
- expect(
- await main(
- ["policy", "--dry-run", "--json"],
- stdout.stream,
- capture().stream,
- deps,
- ),
- ).toBe(0);
+ expect(await stdout.run(["policy", "--dry-run", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text()).dryRun).toBe(true);
expect(await readdir(f.outputDir)).toEqual([]);
});
@@ -563,13 +546,11 @@ describe("policy CLI", () => {
for (const cooperative of [false, true]) {
const f = await fixture();
const signals = new FakeSignals();
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
const onClose = mock();
expect(
- await main(
+ await stdout.run(
["policy", "--dry-run", "--json"],
- stdout.stream,
- capture().stream,
policyDependencies(f, {
signals,
onPreflight: (_repository, options) => {
@@ -595,12 +576,10 @@ describe("policy CLI", () => {
const draft = await f.generate({
run: async (stage) => stageResult(stage, markdown),
});
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["policy", "--format", "md"],
- stdout.stream,
- capture().stream,
policyDependencies(f, { draft }),
),
).toBe(0);
@@ -627,26 +606,22 @@ describe("policy CLI", () => {
[["--filter-output", "status"], "draft"],
[["--format", "md", "--filter-output", "status"], "draft"],
] as const) {
- const stdout = capture(true);
- const stderr = capture(true);
- expect(
- await main(["policy", ...args], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ const { stdout, stderr, runCli } = createCliTest(main, {
+ stdout: true,
+ stderr: true,
+ });
+
+ expect(await runCli(["policy", ...args], deps)).toBe(0);
expect(stdout.text()).toContain(marker);
expect(stderr.text()).not.toContain(
draft.content.split("\n").filter(Boolean).at(-1)!,
);
expect(stderr.text()).not.toContain(draft.reviewNotes[0]!);
}
- const stdout = capture();
- expect(
- await main(
- ["policy", "--json", "--full-output"],
- stdout.stream,
- capture().stream,
- deps,
- ),
- ).toBe(0);
+ const stdout = captureCli(main, "stdout");
+ expect(await stdout.run(["policy", "--json", "--full-output"], deps)).toBe(
+ 0,
+ );
expect(JSON.parse(stdout.text())).toMatchObject({
ok: true,
data: { status: "draft", draftPath: draft.draftPath },
@@ -668,16 +643,9 @@ describe("policy CLI", () => {
["--token-offset", "1"],
["--token-offset", "1", "--token-limit", "4"],
]) {
- const stdout = capture();
- const stderr = capture();
- expect(
- await main(
- ["policy", ...format, ...transform],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ expect(await runCli(["policy", ...format, ...transform], deps)).toBe(0);
expect(stderr.text()).not.toContain(
draft.content.split("\n").filter(Boolean).at(-1)!,
);
@@ -691,14 +659,9 @@ describe("policy CLI", () => {
}
}
}
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
- ["policy", "--format", "md", "--full-output"],
- stdout.stream,
- capture().stream,
- deps,
- ),
+ await stdout.run(["policy", "--format", "md", "--full-output"], deps),
).toBe(0);
expect(stdout.text()).toContain("## data");
expect(stdout.text()).toContain(POLICY.trim());
@@ -729,30 +692,19 @@ describe("policy CLI", () => {
};
};
for (const args of [["--dry-run"], ["--format", "toon"]]) {
- const stdout = capture(true);
- expect(
- await main(["policy", ...args], stdout.stream, capture().stream, deps),
- ).toBe(0);
+ const stdout = captureCli(main, "stdout", true);
+ expect(await stdout.run(["policy", ...args], deps)).toBe(0);
expect(stdout.text()).not.toMatch(/[\u001b\p{Bidi_Control}]/u);
expect(stdout.text()).toContain("\\u202e");
}
- const json = capture();
- expect(
- await main(["policy", "--json"], json.stream, capture().stream, deps),
- ).toBe(0);
+ const json = captureCli(main, "stdout");
+ expect(await json.run(["policy", "--json"], deps)).toBe(0);
expect(JSON.parse(json.text())).toMatchObject({
scope,
reviewNotes: expect.arrayContaining([note]),
});
- const markdown = capture();
- expect(
- await main(
- ["policy", "--format", "md"],
- markdown.stream,
- capture().stream,
- deps,
- ),
- ).toBe(0);
+ const markdown = captureCli(main, "stdout");
+ expect(await markdown.run(["policy", "--format", "md"], deps)).toBe(0);
expect(markdown.text()).toBe(content);
});
@@ -764,13 +716,11 @@ describe("policy CLI", () => {
["SIGTERM", 143],
] as const) {
const signals = new FakeSignals();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["policy", "--json", "--full-output"],
- stdout.stream,
- stderr.stream,
policyDependencies(f, {
signals,
onGenerate: (_repository, options) => {
@@ -811,13 +761,11 @@ describe("policy CLI", () => {
] as const) {
for (const leadingOutputFlags of [false, true]) {
const flags = ["--json", "--full-output"];
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
leadingOutputFlags ? [...flags, ...args] : [...args, ...flags],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -864,12 +812,10 @@ describe("policy CLI", () => {
const draft = await f.generate({ path: scope });
const controlled = `${POLICY}\nLiteral \u001b[2J text.${controls}\n`;
await writeFile(draft.draftPath, controlled);
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["policy", "--path", scope],
- capture().stream,
- stderr.stream,
policyDependencies(f, { draft: { ...draft, content: controlled } }),
),
).toBe(0);
@@ -889,10 +835,9 @@ describe("policy CLI", () => {
const signals = new FakeSignals();
const onClose = mock();
expect(
- await main(
+ await runCapturedCli(
+ main,
["policy", "--headless"],
- capture().stream,
- capture().stream,
policyDependencies(f, {
signals,
onClose,
diff --git a/sdk/typescript/tests-ts/cli-project-config.test.ts b/sdk/typescript/tests-ts/cli-project-config.test.ts
index 03e135995..d74403838 100644
--- a/sdk/typescript/tests-ts/cli-project-config.test.ts
+++ b/sdk/typescript/tests-ts/cli-project-config.test.ts
@@ -7,14 +7,14 @@ import { main } from "../src/cli.js";
import type { CodexSecurityConfig, JsonObject } from "../src/config.js";
import type { ProjectConfigInput } from "../src/project-config-schema.js";
import { readProjectConfig } from "../src/project-config.js";
-import {
- capture,
- dependencies,
- fakeResult,
- fakeSecurity,
-} from "./cli-fixtures.js";
+import { dependencies, fakeResult, fakeSecurity } from "./cli-fixtures.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
import { rejecting, throwing } from "./support/errors.js";
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
const { temporaryDirectory, cleanup } = createApiTestFixtures(
"cli-project-config-",
@@ -55,12 +55,12 @@ test.each([
async (file, modules) => {
const input = await fixture({});
const args = ["init", ...(file === undefined ? [] : [file]), "--json"];
- const output = capture();
+ const output = captureCli(main, "stdout");
const deps = dependencies({
currentDirectory: input.root,
onConfig: throwing("No runtime for init"),
});
- expect(await main(args, output.stream, capture().stream, deps)).toBe(0);
+ expect(await output.run(args, deps)).toBe(0);
const path = join(input.root, file ?? "codex-security.yaml");
expect(JSON.parse(output.text())).toEqual({ path });
const selected = await readProjectConfig(path);
@@ -68,7 +68,7 @@ test.each([
$schema: `${modules}/@openai/codex-security/schemas/project-config.schema.json`,
});
const contents = await readFile(path, "utf8");
- expect(await main(args, capture().stream, capture().stream, deps)).toBe(2);
+ expect(await runCapturedCli(main, args, deps)).toBe(2);
expect(await readFile(path, "utf8")).toBe(contents);
},
);
@@ -91,12 +91,10 @@ test("info resolves a config and its sources without a target, prompt reads, or
join(home, "codex-security", "config.toml"),
"[deep_scan]\nsubagents = 1\n",
);
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["info", "-c", input.config, "--json"],
- stdout.stream,
- capture().stream,
dependencies({
currentDirectory: input.configDirectory,
environment: { CODEX_HOME: home },
@@ -127,12 +125,10 @@ test("info resolves a config and its sources without a target, prompt reads, or
test("info without a file reports default sources, including unset settings", async () => {
const input = await fixture({});
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["info", "--json", "--filter-output", "configuration"],
- stdout.stream,
- capture().stream,
dependencies({ currentDirectory: input.root }),
),
).toBe(0);
@@ -166,10 +162,9 @@ test("an explicit config flag overrides the operator-selected environment file",
] as const) {
const onConfig = mock<(config: CodexSecurityConfig) => void>();
expect(
- await main(
+ await runCapturedCli(
+ main,
["scan", ...flags, "--json"],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: input.repository,
environment: { CODEX_SECURITY_PROJECT_CONFIG: input.config },
@@ -190,7 +185,8 @@ test.each([undefined, "standard", "daybreak_red"] as const)(
});
const onTurn = mock<(_target: string, options: ScanOptions) => void>();
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"scan",
input.repository,
@@ -201,8 +197,6 @@ test.each([undefined, "standard", "daybreak_red"] as const)(
? []
: ["--cyber-access-program", override]),
],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: input.repository,
onTurn,
@@ -218,10 +212,9 @@ test("a missing operator-selected environment file fails without falling back",
const input = await fixture({});
const onConfig = mock();
expect(
- await main(
+ await runCapturedCli(
+ main,
["scan", "--json"],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: input.repository,
environment: {
@@ -241,12 +234,10 @@ test.each([123, "", " "])(
codex: { profile: "selected", profiles: { selected: { model } } },
});
const onConfig = mock();
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scan", "-c", input.config, "--provider", "amazon-bedrock", "--json"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: input.repository,
onConfig,
@@ -264,13 +255,11 @@ test("rerun rejects a blank replacement when scan instructions are required", as
const input = await fixture({});
const prompt = join(input.root, "empty.md");
await writeFile(prompt, " \n");
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const onConfig = mock();
expect(
- await main(
+ await stderr.run(
["scans", "rerun", "saved", "--scan-prompt-file", prompt, "--json"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: input.root,
onWorkbench: async () => ({
@@ -307,11 +296,9 @@ test.each(["instructions_file", "validation_file"] as const)(
`id,repository,revision\nsource,${input.repository},${"a".repeat(40)}\n`,
);
const onConfig = mock(throwing("Runtime must not start"));
- const stderr = capture();
- const exit = await main(
+ const stderr = captureCli(main, "stderr");
+ const exit = await stderr.run(
["bulk-scan", csv, "-c", input.config, "--json"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: input.repository,
onConfig,
@@ -420,8 +407,8 @@ test("bulk scans apply config and linked operator prompts, preserve CSV scope ov
"3",
"--json",
];
- const output = capture();
- expect(await main(args, output.stream, capture().stream, deps)).toBe(1);
+ const output = captureCli(main, "stdout");
+ expect(await output.run(args, deps)).toBe(1);
expect(JSON.parse(output.text())).toMatchObject({
completed: 2,
failed: 0,
@@ -445,8 +432,8 @@ test("bulk scans apply config and linked operator prompts, preserve CSV scope ov
workers: 2,
subagents: 0,
});
- const resumed = capture();
- expect(await main(args, resumed.stream, capture().stream, deps)).toBe(1);
+ const resumed = captureCli(main, "stdout");
+ expect(await resumed.run(args, deps)).toBe(1);
expect(JSON.parse(resumed.text())).toMatchObject({
skipped: 2,
policyFailed: true,
@@ -456,8 +443,8 @@ test("bulk scans apply config and linked operator prompts, preserve CSV scope ov
input.config,
JSON.stringify({ ...config, policy: { fail_on_severity: "low" } }),
);
- const stderr = capture();
- expect(await main(args, capture().stream, stderr.stream, deps)).toBe(2);
+ const stderr = captureCli(main, "stderr");
+ expect(await stderr.run(args, deps)).toBe(2);
expect(stderr.text()).toContain("manifest does not match");
expect(selected).toHaveLength(2);
});
@@ -491,15 +478,15 @@ test.each(["standard", "deep"] as const)(
"Validate synthetic boundaries.",
);
const onTurn = mock<(_target: string, options: ScanOptions) => void>();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = fakeResult(["high"], "complete", {
input_tokens: 1250,
cached_input_tokens: 200,
output_tokens: 30,
});
expect(
- await main(
+ await runCli(
[
"scan-components",
input.repository,
@@ -510,8 +497,6 @@ test.each(["standard", "deep"] as const)(
"--headless",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: input.root,
result,
@@ -559,13 +544,11 @@ test("actual CLI parsing preserves file values when flags are absent", async ()
});
const onTurn = mock<(_target: string, options: ScanOptions) => void>();
const onConfig = mock<(config: CodexSecurityConfig) => void>();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["scan", "-c", input.config, "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: input.repository,
environment: { OPENAI_API_KEY: "synthetic-test-key" },
@@ -618,7 +601,8 @@ test("CLI values override matching file values, including native objects and lis
const onTurn = mock<(_target: string, options: ScanOptions) => void>();
const onConfig = mock<(config: CodexSecurityConfig) => void>();
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"scan",
"--config",
@@ -645,8 +629,6 @@ test("CLI values override matching file values, including native objects and lis
"synthetic_setting.names=[]",
"--json",
],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: input.repository,
onConfig,
@@ -692,9 +674,9 @@ test("rerun accepts replacement scan and validation files relative to the invoca
cyberAccessProgram: "daybreak_blue",
};
const onTurn = mock<(_target: string, options: ScanOptions) => void>();
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[
"scans",
"rerun",
@@ -705,8 +687,6 @@ test("rerun accepts replacement scan and validation files relative to the invoca
"validation.md",
"--json",
],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: input.configDirectory,
onWorkbench: async () => ({ recipe }),
@@ -748,12 +728,10 @@ test.each([
scan: { scope: structuredClone(scope) },
} as ProjectConfigInput);
const onTurn = mock<(_target: string, options: ScanOptions) => void>();
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scan", "-c", input.config, ...flags, "--json"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: input.repository,
onTurn,
@@ -781,12 +759,10 @@ test.each([
async (flags, message) => {
const input = await fixture({});
const onConfig = mock();
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scan", "-c", input.config, ...flags, "--json"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: input.repository,
onConfig,
@@ -807,10 +783,9 @@ test("selecting standard mode leaves inactive file deep settings out of the acti
});
const onTurn = mock<(_target: string, options: ScanOptions) => void>();
expect(
- await main(
+ await runCapturedCli(
+ main,
["scan", "-c", input.config, "--mode", "standard", "--json"],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: input.repository,
onTurn,
@@ -841,7 +816,8 @@ test("file prompts use the config directory and CLI prompt overrides use the inv
);
const onTurn = mock<(_target: string, options: ScanOptions) => void>();
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"scan",
"-c",
@@ -850,8 +826,6 @@ test("file prompts use the config directory and CLI prompt overrides use the inv
"validate.md",
"--json",
],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: input.repository,
onTurn,
@@ -871,10 +845,9 @@ test("an explicit file can supply the model required by a provider override", as
});
const onConfig = mock<(config: CodexSecurityConfig) => void>();
expect(
- await main(
+ await runCapturedCli(
+ main,
["scan", "-c", input.config, "--provider", "amazon-bedrock", "--json"],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: input.repository,
onConfig,
@@ -903,7 +876,8 @@ test.each(["file", "CLI"])(
});
const onConfig = mock<(config: CodexSecurityConfig) => void>();
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"scan",
"-c",
@@ -913,8 +887,6 @@ test.each(["file", "CLI"])(
...(overrideProfile ? ["--codex", 'profile="review"'] : []),
"--json",
],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: input.repository,
onConfig,
@@ -936,13 +908,11 @@ test("an unselected file profile does not satisfy the provider model requirement
profiles: { review: { model: "synthetic-profile-model" } },
},
});
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const onConfig = mock();
expect(
- await main(
+ await stderr.run(
["scan", "-c", input.config, "--provider", "amazon-bedrock", "--json"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: input.repository,
onConfig,
@@ -964,10 +934,9 @@ test.each([
await writeFile(join(input.repository, "codex-security.yaml"), "scan: [");
const onConfig = mock();
expect(
- await main(
+ await runCapturedCli(
+ main,
[...argv],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: input.repository,
onConfig,
@@ -981,12 +950,10 @@ test.each(["--help", "--schema"])(
"%s does not load even an explicitly selected invalid file",
async (flag) => {
const input = await fixture("scan: [");
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["scan", "-c", input.config, flag, "--json"],
- stdout.stream,
- capture().stream,
dependencies({ currentDirectory: input.repository }),
),
).toBe(0);
@@ -1000,13 +967,11 @@ test.each(["--help", "--schema"])(
test("a malformed selected file fails before constructing a client", async () => {
const input = await fixture("scan: [");
const onConfig = mock();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["scan", "-c", input.config, "--dry-run", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: input.repository,
onConfig,
@@ -1048,8 +1013,8 @@ test("dry-run uses the real SDK without initializing its runtime and reports pro
codex_home: ambient,
CODEX_SECURITY_STATE_DIR: join(input.root, "state"),
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main);
+
const createCodex = mock(throwing("No inference in dry-run"));
const prepareRuntime = mock(rejecting("No runtime in dry-run"));
const deps = dependencies({
@@ -1062,7 +1027,7 @@ test("dry-run uses the real SDK without initializing its runtime and reports pro
prepareRuntime,
});
expect(
- await main(
+ await runCli(
[
"scan",
"-c",
@@ -1074,8 +1039,6 @@ test("dry-run uses the real SDK without initializing its runtime and reports pro
"--dry-run",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1125,7 +1088,7 @@ test.each([
CODEX_HOME: join(input.root, "ambient"),
CODEX_SECURITY_STATE_DIR: join(input.root, "state"),
};
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const createCodex = mock(throwing("No inference"));
const prepareRuntime = mock(rejecting("No runtime"));
const deps = dependencies({
@@ -1138,10 +1101,8 @@ test.each([
prepareRuntime,
});
expect(
- await main(
+ await stderr.run(
["scan", "-c", input.config, "--dry-run", "--json"],
- capture().stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -1172,10 +1133,9 @@ test("rerun restores all saved deep settings and authentication without loading
};
const onTurn = mock<(_target: string, options: ScanOptions) => void>();
expect(
- await main(
+ await runCapturedCli(
+ main,
["scans", "rerun", "saved", "--json"],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: input.repository,
environment: {
@@ -1212,13 +1172,11 @@ test.each([
config: {},
...extra,
};
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const onRun = mock();
expect(
- await main(
+ await stderr.run(
["scans", "rerun", "saved", "--json"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: input.repository,
onWorkbench: async () => ({ recipe }),
diff --git a/sdk/typescript/tests-ts/cli-publish.test.ts b/sdk/typescript/tests-ts/cli-publish.test.ts
index 07070a2e8..57594d169 100644
--- a/sdk/typescript/tests-ts/cli-publish.test.ts
+++ b/sdk/typescript/tests-ts/cli-publish.test.ts
@@ -8,6 +8,11 @@ import type { CheckScanPublicationResult } from "../src/publish.js";
import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
import { fail } from "./support/errors.js";
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
const DESTINATION_OPTIONS = [
"--to",
@@ -89,9 +94,9 @@ describe("publish scan to custom", () => {
expect(options.expectedScanId).toBe("exact-scan");
return receipt;
};
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
[
"publish",
"scan",
@@ -103,8 +108,6 @@ describe("publish scan to custom", () => {
"http://localhost:3000",
"--json",
],
- stdout.stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -114,8 +117,8 @@ describe("publish scan to custom", () => {
"publishes a selected saved scan with dry-run=%j",
async (dryRun) => {
const [scanDir] = await publicationScanDirectories(1);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies({
onWorkbench: () => ({
scan: {
@@ -144,7 +147,7 @@ describe("publish scan to custom", () => {
return receipt;
};
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -157,8 +160,6 @@ describe("publish scan to custom", () => {
"--json",
...(dryRun ? ["--dry-run"] : []),
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -189,15 +190,13 @@ describe("publish scan to custom", () => {
])(
"rejects incompatible custom publication inputs %j",
async (flags, message) => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.publishScanToCustom = async () => fail("must not publish");
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...flags, "--json"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -207,8 +206,8 @@ describe("publish scan to custom", () => {
);
test("forwards cancellation for an external scan and does not report success", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const signals = new FakeSignals();
const deps = dependencies({ signals });
deps.publishScanToCustom = async (directory, options) => {
@@ -219,7 +218,7 @@ describe("publish scan to custom", () => {
throw new Error("unreachable");
};
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -231,8 +230,6 @@ describe("publish scan to custom", () => {
"http://localhost:3000",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(130);
@@ -245,8 +242,8 @@ describe("publish scan to custom", () => {
describe("publish check", () => {
test("resolves the shared destination options without invoking publication", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const currentDirectory = join(tmpdir(), "codex-security-check-current");
const result: CheckScanPublicationResult = {
scanId: "scan-example",
@@ -287,7 +284,7 @@ describe("publish check", () => {
return result;
};
expect(
- await main(
+ await runCli(
[
"publish",
"check",
@@ -299,8 +296,6 @@ describe("publish check", () => {
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -311,14 +306,14 @@ describe("publish check", () => {
});
test("reports a failed check without publishing or returning a successful result", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.checkScanPublication = async () =>
fail("The selected project is unavailable.");
deps.publishScan = async () => fail("Check must not publish.");
expect(
- await main(
+ await runCli(
[
"publish",
"check",
@@ -326,8 +321,6 @@ describe("publish check", () => {
...DESTINATION_OPTIONS,
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -344,8 +337,8 @@ describe("publish check", () => {
["SIGINT", 130],
["SIGTERM", 143],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const signals = new FakeSignals();
const deps = dependencies({
signals,
@@ -373,7 +366,7 @@ describe("publish check", () => {
const observeFinished = mock((status: number) => {
return status;
});
- const running = main(
+ const running = runCli(
[
"publish",
command[0]!,
@@ -382,8 +375,6 @@ describe("publish check", () => {
...DESTINATION_OPTIONS,
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
).then(observeFinished);
await operationStarted.promise;
@@ -408,8 +399,8 @@ describe("publish check", () => {
describe("publish scan", () => {
test("forwards opt-in retry and reports skipped issues separately", async () => {
for (const dryRun of [false, true]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.publishScan = async (_directory, options) => {
expect(options.skipExisting).toBe(true);
@@ -424,7 +415,7 @@ describe("publish scan", () => {
};
};
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -433,8 +424,6 @@ describe("publish scan", () => {
"--skip-existing",
...(dryRun ? ["--dry-run", "--json"] : []),
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -477,7 +466,8 @@ describe("publish scan", () => {
return publicationResult();
};
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"publish",
"scan",
@@ -487,8 +477,6 @@ describe("publish scan", () => {
...(skipExisting ? ["--skip-existing"] : []),
"--json",
],
- capture().stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -505,7 +493,8 @@ describe("publish scan", () => {
return publicationResult();
};
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"publish",
"scan",
@@ -518,17 +507,15 @@ describe("publish scan", () => {
"selected-project",
"--json",
],
- capture().stream,
- capture().stream,
deps,
),
).toBe(0);
expect(projectId).toBe("selected-project");
}
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[
"publish",
"scan",
@@ -537,8 +524,6 @@ describe("publish scan", () => {
"--project",
"different-project",
],
- capture().stream,
- stderr.stream,
dependencies(),
),
).toBe(2);
@@ -551,8 +536,8 @@ describe("publish scan", () => {
"publishes an explicit %s scan directory without inspecting scan history",
async (syntax) => {
const currentDirectory = join(tmpdir(), "codex-security-publish-current");
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
let invocation:
{ scanDirectory: string; options: Record<string, unknown> } | undefined;
const deps = dependencies({
@@ -567,7 +552,7 @@ describe("publish scan", () => {
};
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -577,8 +562,6 @@ describe("publish scan", () => {
...DESTINATION_OPTIONS,
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -617,8 +600,8 @@ describe("publish scan", () => {
assignee: "user-123",
},
]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main);
+
const deps = dependencies({
environment: { CODEX_SECURITY_LINEAR_API_KEY: "environment-key" },
});
@@ -629,7 +612,7 @@ describe("publish scan", () => {
};
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -638,8 +621,6 @@ describe("publish scan", () => {
...scenario.flags,
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -657,10 +638,10 @@ describe("publish scan", () => {
});
test("rejects a Linear assignee without direct API authentication", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -669,8 +650,6 @@ describe("publish scan", () => {
"--linear-assignee",
],
- stdout.stream,
- stderr.stream,
dependencies(),
),
).toBe(2);
@@ -681,18 +660,16 @@ describe("publish scan", () => {
test("preserves Linear API error details", async () => {
const key = "lin_api_SYNTHETIC_ERROR_DETAILS";
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
const deps = dependencies({
environment: { CODEX_SECURITY_LINEAR_API_KEY: key },
});
deps.publishScan = async () => fail(`Linear rejected ${key}.`);
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -700,8 +677,8 @@ describe("publish scan", () => {
});
test("publishes directly to a Linear team when no project is selected", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = {
...publicationResult(),
destination: { type: "linear" as const, teamId: "team-from-flags" },
@@ -714,7 +691,7 @@ describe("publish scan", () => {
};
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -725,8 +702,6 @@ describe("publish scan", () => {
"team-from-flags",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -749,8 +724,8 @@ describe("publish scan", () => {
["SIGINT", 130, "Publication canceled by Ctrl-C."],
["SIGTERM", 143, "Publication terminated by SIGTERM."],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const signals = new FakeSignals();
const events: string[] = [];
let now = 0;
@@ -775,10 +750,8 @@ describe("publish scan", () => {
);
};
- const publishing = main(
+ const publishing = runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS, "--json"],
- stdout.stream,
- stderr.stream,
deps,
);
await publicationStarted.promise;
@@ -806,8 +779,8 @@ describe("publish scan", () => {
});
test("prints the first five persisted Linear issues and database-backed totals", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = publicationResult();
result.created = Array.from({ length: 7 }, (_, index) => ({
findingId: `private-finding-${index + 1}`,
@@ -822,10 +795,8 @@ describe("publish scan", () => {
deps.publishScan = async () => result;
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -872,8 +843,8 @@ describe("publish scan", () => {
{ json: true, tty: true, failed: false },
{ json: false, tty: true, failed: true },
]) {
- const stdout = capture();
- const stderr = capture(tty);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: tty });
+
const result = {
...publicationResult(
failed ? [{ findingId: "finding-2", error: "Private details" }] : [],
@@ -884,7 +855,7 @@ describe("publish scan", () => {
deps.publishScan = async () => result;
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -892,8 +863,6 @@ describe("publish scan", () => {
...DESTINATION_OPTIONS,
...(json ? ["--json"] : []),
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(failed ? 2 : 0);
@@ -922,8 +891,8 @@ describe("publish scan", () => {
test("omits the issue-list ellipsis when zero to five issues were created", async () => {
for (const total of [0, 1, 5]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main);
+
const result = publicationResult();
result.created = Array.from({ length: total }, (_, index) => ({
findingId: `finding-${index + 1}`,
@@ -937,10 +906,8 @@ describe("publish scan", () => {
deps.publishScan = async () => result;
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -961,8 +928,8 @@ describe("publish scan", () => {
});
test("renders partial failures without exposing finding errors or changing exit codes", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const failures = [
{
findingId: "private-finding-2",
@@ -976,10 +943,8 @@ describe("publish scan", () => {
deps.publishScan = async () => result;
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -1002,8 +967,8 @@ describe("publish scan", () => {
});
test("omits missing or unsafe issue links and sanitizes issue identifiers", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main);
+
const initial = publicationResult();
const base = initial.created[0]!;
const { url: _unusedUrl, ...withoutUrl } = base;
@@ -1039,10 +1004,8 @@ describe("publish scan", () => {
deps.publishScan = async () => result;
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1069,16 +1032,16 @@ describe("publish scan", () => {
] as const;
for (const scenario of scenarios) {
- const stdout = capture(scenario.interactive);
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main, {
+ stdout: scenario.interactive,
+ });
+
const deps = dependencies({ environment: scenario.environment });
deps.publishScan = successfulPublication<[]>;
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1102,14 +1065,14 @@ describe("publish scan", () => {
{ flags: ["--format", "toon"], json: false },
{ flags: ["--full-output"], json: false },
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main);
+
const result = publicationResult();
const deps = dependencies();
deps.publishScan = async () => result;
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -1117,8 +1080,6 @@ describe("publish scan", () => {
...DESTINATION_OPTIONS,
...format.flags,
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1131,8 +1092,8 @@ describe("publish scan", () => {
expect(stdout.text()).not.toContain("Linear publication complete");
}
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
const signals = new FakeSignals();
const deps = dependencies({ signals });
deps.publishScan = async (_scanDirectory, options) => {
@@ -1142,7 +1103,7 @@ describe("publish scan", () => {
};
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -1150,8 +1111,6 @@ describe("publish scan", () => {
...DESTINATION_OPTIONS,
"--dry-run",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1163,8 +1122,8 @@ describe("publish scan", () => {
});
test("reports every created Linear issue with a successful exit code", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = publicationResult();
result.created.push({
findingId: "finding-2",
@@ -1210,10 +1169,8 @@ describe("publish scan", () => {
};
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS, "--json"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1237,8 +1194,8 @@ describe("publish scan", () => {
linkedDirectory,
process.platform === "win32" ? "junction" : "dir",
);
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
let question = "";
let choices: readonly { label: string; short?: string; value: string }[] =
[];
@@ -1311,12 +1268,7 @@ describe("publish scan", () => {
deps.publishScan = publishScan;
expect(
- await main(
- ["publish", "scan", ...DESTINATION_OPTIONS, "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
+ await runCli(["publish", "scan", ...DESTINATION_OPTIONS, "--json"], deps),
).toBe(0);
expect(workbenchArguments).toEqual(["list-scans", "--status", "complete"]);
expect(question).toBe("Which completed scan would you like to publish?");
@@ -1526,8 +1478,8 @@ describe("publish scan", () => {
});
test("shows actual Codex reasoning and Linear activity in a full-screen publication dashboard", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
const deps = dependencies();
deps.publishScan = async (_scanDirectory, options) => {
options.onProgress?.({ type: "started", scanId: "scan-123", total: 2 });
@@ -1572,10 +1524,8 @@ describe("publish scan", () => {
};
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS, "--json"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1653,8 +1603,8 @@ describe("publish scan", () => {
});
test("streams sanitized Codex progress to noninteractive stderr without terminal controls", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.publishScan = async (_scanDirectory, options) => {
options.onProgress?.({ type: "started", scanId: "scan-123", total: 1 });
@@ -1700,10 +1650,8 @@ describe("publish scan", () => {
};
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS, "--json"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1723,8 +1671,10 @@ describe("publish scan", () => {
test("hides source-bearing handoff commands in plain and full-screen progress", async () => {
for (const interactive of [false, true]) {
- const stdout = capture();
- const stderr = capture(interactive);
+ const { stdout, stderr, runCli } = createCliTest(main, {
+ stderr: interactive,
+ });
+
const deps = dependencies();
deps.publishScan = async (_scanDirectory, options) => {
options.onProgress?.({ type: "started", scanId: "scan-123", total: 1 });
@@ -1798,7 +1748,7 @@ describe("publish scan", () => {
};
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -1806,8 +1756,6 @@ describe("publish scan", () => {
...DESTINATION_OPTIONS,
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1826,8 +1774,8 @@ describe("publish scan", () => {
});
test("restores the publication screen before reporting publisher failures", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
const deps = dependencies();
deps.publishScan = async (_scanDirectory, options) => {
options.onProgress?.({ type: "started", scanId: "scan-123", total: 1 });
@@ -1835,10 +1783,8 @@ describe("publish scan", () => {
};
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -1854,13 +1800,13 @@ describe("publish scan", () => {
test("uses plain progress in CI and dumb terminals even when stderr is a TTY", async () => {
for (const environment of [{ CI: "1" }, { TERM: "dumb" }]) {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
const deps = dependencies({ environment });
deps.publishScan = publishWithProgress;
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -1868,8 +1814,6 @@ describe("publish scan", () => {
...DESTINATION_OPTIONS,
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -2045,8 +1989,8 @@ describe("publish scan", () => {
{ id: "linked-scan", directory: linkedDirectory },
{ id: "selected-scan", directory: "selected-completed-scan" },
];
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
let offered: readonly { label: string; value: string }[] = [];
const publishScan = mock(successfulPublication<[string]>);
const deps = dependencies({
@@ -2075,12 +2019,7 @@ describe("publish scan", () => {
deps.publishScan = publishScan;
expect(
- await main(
- ["publish", "scan", ...DESTINATION_OPTIONS, "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
+ await runCli(["publish", "scan", ...DESTINATION_OPTIONS, "--json"], deps),
).toBe(0);
expect(offered.map(({ value }) => value)).toEqual([
firstDirectory,
@@ -2099,8 +2038,8 @@ describe("publish scan", () => {
});
test("requires an interactive terminal when no scan directory is supplied", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const onWorkbench = mock(() => {
return { scans: [] };
});
@@ -2118,12 +2057,7 @@ describe("publish scan", () => {
deps.publishScan = publishScan;
expect(
- await main(
- ["publish", "scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
- deps,
- ),
+ await runCli(["publish", "scan", ...DESTINATION_OPTIONS], deps),
).toBe(2);
expect(stderr.text()).toContain(
"Interactive scan selection requires a terminal.",
@@ -2135,8 +2069,8 @@ describe("publish scan", () => {
});
test("fails clearly when no completed scans are available", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
let prompted = false;
const publishScan = mock(successfulPublication<[]>);
const deps = dependencies({ onWorkbench: () => ({ scans: [] }) });
@@ -2153,12 +2087,7 @@ describe("publish scan", () => {
deps.publishScan = publishScan;
expect(
- await main(
- ["publish", "scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
- deps,
- ),
+ await runCli(["publish", "scan", ...DESTINATION_OPTIONS], deps),
).toBe(2);
expect(stderr.text()).toContain(
"No completed Codex Security scans are available to publish.",
@@ -2179,8 +2108,8 @@ describe("publish scan", () => {
]);
await symlink(validDirectory, linkedDirectory, "junction");
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
let prompted = false;
const publishScan = mock(successfulPublication<[]>);
const deps = dependencies({
@@ -2209,12 +2138,7 @@ describe("publish scan", () => {
deps.publishScan = publishScan;
expect(
- await main(
- ["publish", "scan", ...DESTINATION_OPTIONS, "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
+ await runCli(["publish", "scan", ...DESTINATION_OPTIONS, "--json"], deps),
).toBe(2);
expect(stderr.text()).toContain(
"No completed Codex Security scans are available to publish.",
@@ -2277,13 +2201,13 @@ describe("publish scan", () => {
];
for (const [argv, expected] of cases) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const publishScan = mock(successfulPublication<[]>);
const deps = dependencies();
deps.publishScan = publishScan;
- expect(await main(argv, stdout.stream, stderr.stream, deps)).toBe(2);
+ expect(await runCli(argv, deps)).toBe(2);
expect(stderr.text()).toContain(expected);
expect(stdout.text()).toBe("");
expect(publishScan).not.toHaveBeenCalled();
@@ -2319,9 +2243,7 @@ describe("publish scan", () => {
return publicationResult();
};
- expect(
- await main(scenario.argv, capture().stream, capture().stream, deps),
- ).toBe(0);
+ expect(await runCapturedCli(main, scenario.argv, deps)).toBe(0);
expect(destination).toEqual({
teamId: scenario.expectedTeam,
projectId: scenario.expectedProject,
@@ -2330,8 +2252,8 @@ describe("publish scan", () => {
});
test("passes dry-run mode through and preserves machine-readable output", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
let dryRun: boolean | undefined;
const deps = dependencies();
deps.publishScan = async (_scanDirectory, options) => {
@@ -2340,7 +2262,7 @@ describe("publish scan", () => {
};
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -2349,8 +2271,6 @@ describe("publish scan", () => {
"--dry-run",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -2368,16 +2288,14 @@ describe("publish scan", () => {
const warning =
"Could not save the publication receipt: token=SYNTHETIC_RECEIPT_VALUE. Linear issues were already created; do not retry publication.";
const result = { ...publicationResult(), warnings: [warning] };
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.publishScan = async () => result;
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS, "--json"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -2388,8 +2306,8 @@ describe("publish scan", () => {
test("surfaces receipt warnings for default human-readable publication output", async () => {
const warning =
"Could not save the publication receipt: Disk is unavailable. Linear issues were already created; do not retry publication.";
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.publishScan = async () => ({
...publicationResult(),
@@ -2397,10 +2315,8 @@ describe("publish scan", () => {
});
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -2419,16 +2335,14 @@ describe("publish scan", () => {
]),
warnings,
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.publishScan = async () => result;
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS, "--json"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -2441,8 +2355,8 @@ describe("publish scan", () => {
});
test("returns a nonzero exit code while preserving partial publication results", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const failed = [
{ findingId: "finding-2", error: "Linear issue creation failed." },
];
@@ -2450,10 +2364,8 @@ describe("publish scan", () => {
deps.publishScan = async () => publicationResult(failed);
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS, "--json"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -2462,17 +2374,15 @@ describe("publish scan", () => {
});
test("reports publisher failures without claiming a successful upload", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.publishScan = async () =>
fail("Linear is not connected to your Codex account.");
expect(
- await main(
+ await runCli(
["publish", "scan", "completed-scan", ...DESTINATION_OPTIONS],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
diff --git a/sdk/typescript/tests-ts/cli-scan-import.test.ts b/sdk/typescript/tests-ts/cli-scan-import.test.ts
index dfc631865..6fcefd86d 100644
--- a/sdk/typescript/tests-ts/cli-scan-import.test.ts
+++ b/sdk/typescript/tests-ts/cli-scan-import.test.ts
@@ -8,6 +8,7 @@ import {
FakeSignals,
} from "./cli-fixtures.js";
import { rejecting, throwing } from "./support/errors.js";
+import { createCliTest, captureCli } from "./support/cli-run.js";
function importDependencies() {
const deps = dependencies({
@@ -35,10 +36,10 @@ describe("scan import", () => {
"imports --%s while --format json controls output",
async (format) => {
const { deps, calls } = importDependencies();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"scan",
"import",
@@ -50,8 +51,6 @@ describe("scan import", () => {
"--format",
"json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -79,10 +78,10 @@ describe("scan import", () => {
test("accepts equals input syntax and validates without saving", async () => {
const { deps, calls } = importDependencies();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"scan",
"import",
@@ -90,8 +89,6 @@ describe("scan import", () => {
"--dry-run",
"--format=json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -107,15 +104,10 @@ describe("scan import", () => {
test("uses a concise summary for default output", async () => {
const { deps } = importDependencies();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
- ["scan", "import", "--json", "findings.json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
+ await runCli(["scan", "import", "--json", "findings.json"], deps),
).toBe(0);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain("Imported 2 findings as scan scan.");
@@ -138,39 +130,23 @@ describe("scan import", () => {
],
])("rejects invalid import arguments %j", async (args, message) => {
const { deps, calls } = importDependencies();
- const stderr = capture();
- expect(
- await main(
- ["scan", "import", ...args],
- capture().stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ const stderr = captureCli(main, "stderr");
+ expect(await stderr.run(["scan", "import", ...args], deps)).toBe(2);
expect(calls).toHaveLength(0);
expect(stderr.text()).toContain(message);
});
test("exposes both source flags in help and schemas", async () => {
const { deps, calls } = importDependencies();
- const help = capture();
- expect(
- await main(
- ["scan", "import", "--help"],
- help.stream,
- capture().stream,
- deps,
- ),
- ).toBe(0);
+ const help = captureCli(main, "stdout");
+ expect(await help.run(["scan", "import", "--help"], deps)).toBe(0);
expect(help.text()).toContain("--csv <file>");
expect(help.text()).toContain("--json <file>");
expect(help.text()).toContain("--format json");
- const schema = capture();
+ const schema = captureCli(main, "stdout");
expect(
- await main(
+ await schema.run(
["scan", "import", "--schema", "--format", "json"],
- schema.stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -183,20 +159,13 @@ describe("scan import", () => {
});
test("ordinary scan retains repository arguments and the --json output shortcut", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main);
+
const onRun = mock();
const deps = dependencies({
onRun,
});
- expect(
- await main(
- ["scan", "./import", "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli(["scan", "./import", "--json"], deps)).toBe(0);
expect(onRun).toHaveBeenCalledTimes(1);
expect(JSON.parse(stdout.text())).toMatchObject({
manifest: { scan: { id: "scan" } },
@@ -205,12 +174,10 @@ describe("scan import", () => {
test("preserves the global JSON shortcut before the scan command", async () => {
const { deps, calls } = importDependencies();
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["--json", "scan", "import", "--csv", "findings.csv"],
- stdout.stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -252,14 +219,9 @@ describe("scan import", () => {
},
};
};
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
- ["scans", "rerun", "previous-scan", "--json"],
- stdout.stream,
- capture().stream,
- deps,
- ),
+ await stdout.run(["scans", "rerun", "previous-scan", "--json"], deps),
).toBe(0);
expect(calls[0]).toMatchObject({
sourcePath: resolve("retained", "source.csv"),
@@ -283,12 +245,10 @@ describe("scan import", () => {
},
},
});
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scans", "rerun", "previous-scan", option, "workflow.md"],
- capture().stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -303,14 +263,9 @@ describe("scan import", () => {
const signals = new FakeSignals();
const deps = dependencies({ signals });
deps.importScan = rejecting("Input JSON is not a findings document.");
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
- ["scan", "import", "--json", "findings.json"],
- capture().stream,
- stderr.stream,
- deps,
- ),
+ await stderr.run(["scan", "import", "--json", "findings.json"], deps),
).toBe(2);
expect(stderr.text()).toBe(
"codex-security: Input JSON is not a findings document.\n",
@@ -330,14 +285,9 @@ describe("scan import", () => {
options.signal!.throwIfAborted();
throw new Error("unreachable");
};
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
- ["scan", "import", "--csv", "findings.csv"],
- capture().stream,
- stderr.stream,
- deps,
- ),
+ await stderr.run(["scan", "import", "--csv", "findings.csv"], deps),
).toBe(status);
expect(stderr.text()).toContain("Scan import canceled.");
expect(signals.listeners.get(signal)?.size).toBe(0);
diff --git a/sdk/typescript/tests-ts/cli-scan-logs.test.ts b/sdk/typescript/tests-ts/cli-scan-logs.test.ts
index 3d25fbbb2..4c458ec4d 100644
--- a/sdk/typescript/tests-ts/cli-scan-logs.test.ts
+++ b/sdk/typescript/tests-ts/cli-scan-logs.test.ts
@@ -14,6 +14,7 @@ import { readSavedScanLogs } from "../src/scan-logs.js";
import { VERSION } from "../src/version.js";
import { capture, dependencies } from "./cli-fixtures.js";
import { throwing } from "./support/errors.js";
+import { createCliTest } from "./support/cli-run.js";
async function fixture() {
const state = await realpath(await mkdtemp(join(tmpdir(), "saved-logs-")));
@@ -102,16 +103,11 @@ describe("saved logs JSON output", () => {
["--format", "json"],
["--format=json"],
]) {
- const stdout = capture();
- const stderr = capture();
- expect(
- await main(
- ["scans", "logs", "scan-1", ...args],
- stdout.stream,
- stderr.stream,
- f.deps,
- ),
- ).toBe(0);
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ expect(await runCli(["scans", "logs", "scan-1", ...args], f.deps)).toBe(
+ 0,
+ );
const expected = await referenceOutput(["--json"], f.logs);
expect(Object.keys(JSON.parse(expected))).toEqual([
"scanId",
@@ -157,16 +153,11 @@ describe("saved logs JSON output", () => {
)("preserves Incur output for %j", async (args) => {
const f = await fixture();
try {
- const stdout = capture();
- const stderr = capture();
- expect(
- await main(
- ["scans", "logs", "scan-1", ...args],
- stdout.stream,
- stderr.stream,
- f.deps,
- ),
- ).toBe(0);
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ expect(await runCli(["scans", "logs", "scan-1", ...args], f.deps)).toBe(
+ 0,
+ );
const expected = await referenceOutput(
args.flatMap((arg) =>
arg === "--format=json" ? ["--format", "json"] : [arg],
@@ -195,15 +186,10 @@ describe("saved logs JSON output", () => {
["--json", "--filter-output"],
].map((args) => [args]),
)("preserves invalid-option failure for %j", async (args) => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
- ["scans", "logs", "scan-1", ...args],
- stdout.stream,
- stderr.stream,
- dependencies(),
- ),
+ await runCli(["scans", "logs", "scan-1", ...args], dependencies()),
).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).not.toBe("");
diff --git a/sdk/typescript/tests-ts/cli-scan-prompts.test.ts b/sdk/typescript/tests-ts/cli-scan-prompts.test.ts
index 09926cf29..61beb481d 100644
--- a/sdk/typescript/tests-ts/cli-scan-prompts.test.ts
+++ b/sdk/typescript/tests-ts/cli-scan-prompts.test.ts
@@ -3,8 +3,9 @@ import { mkdir, realpath, rm, symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, test, mock } from "bun:test";
import { main } from "../src/cli.js";
-import { capture, dependencies } from "./cli-fixtures.js";
+import { dependencies } from "./cli-fixtures.js";
import { temporaryDirectory } from "./support/temporary-directories.js";
+import { runCapturedCli, captureCli } from "./support/cli-run.js";
describe("CLI scan prompts", () => {
test("loads scan, validation, and post-scan prompt files", async () => {
@@ -20,7 +21,8 @@ describe("CLI scan prompts", () => {
]);
let options: unknown;
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"scan",
".",
@@ -32,8 +34,6 @@ describe("CLI scan prompts", () => {
"follow-up.md",
"--json",
],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: root,
onTurn: (_repository, value) => (options = value),
@@ -99,12 +99,10 @@ describe("CLI scan prompts", () => {
],
] as const) {
const onTurn = mock();
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scan", target, option, input, "--json"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: directory,
onTurn,
@@ -123,10 +121,9 @@ describe("CLI scan prompts", () => {
] as const) {
let selected: unknown;
expect(
- await main(
+ await runCapturedCli(
+ main,
["scan", target, option, external, "--json"],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: directory,
onTurn: (_repository, value) => (selected = value),
@@ -185,7 +182,8 @@ describe("CLI scan prompts", () => {
]);
let options: unknown;
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"bulk-scan",
"repositories.csv",
@@ -199,8 +197,6 @@ describe("CLI scan prompts", () => {
"follow-up.md",
"--json",
],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: root,
onTurn: (_repository, value) => (options = value),
@@ -239,19 +235,15 @@ describe("CLI scan prompts", () => {
},
}),
});
- const error = capture();
- expect(
- await main(
- ["scans", "rerun", "saved", "--json"],
- capture().stream,
- error.stream,
- deps,
- ),
- ).toBe(2);
+ const error = captureCli(main, "stderr");
+ expect(await error.run(["scans", "rerun", "saved", "--json"], deps)).toBe(
+ 2,
+ );
expect(error.text()).toContain("--validation-prompt-file");
expect(onTurn.mock.lastCall?.[1]).toBeUndefined();
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"scans",
"rerun",
@@ -260,8 +252,6 @@ describe("CLI scan prompts", () => {
"validation.md",
"--json",
],
- capture().stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -270,10 +260,9 @@ describe("CLI scan prompts", () => {
});
await writeFile(join(root, "validation.md"), " \n");
expect(
- await main(
+ await runCapturedCli(
+ main,
["scan", ".", "--validation-prompt-file", "validation.md", "--json"],
- capture().stream,
- capture().stream,
deps,
),
).toBe(2);
diff --git a/sdk/typescript/tests-ts/cli-serve.test.ts b/sdk/typescript/tests-ts/cli-serve.test.ts
index de6532f34..2670e5b5e 100644
--- a/sdk/typescript/tests-ts/cli-serve.test.ts
+++ b/sdk/typescript/tests-ts/cli-serve.test.ts
@@ -15,6 +15,7 @@ import {
import { capture, dependencies } from "./cli-fixtures.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
import { runCommand } from "./support/shell.js";
+import { createCliTest } from "./support/cli-run.js";
const packageRoot = join(import.meta.dir, "..");
const cli = join(packageRoot, "src", "cli.ts");
@@ -179,11 +180,9 @@ test("serve exposes help and schema without starting the service", async () => {
["serve", "--help"],
["serve", "--schema", "--json"],
]) {
- const stdout = capture();
- const stderr = capture();
- expect(await main(args, stdout.stream, stderr.stream, dependencies())).toBe(
- 0,
- );
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ expect(await runCli(args, dependencies())).toBe(0);
if (args.includes("--schema")) {
expect(JSON.parse(stdout.text())).toMatchObject({
options: {
@@ -204,11 +203,9 @@ test("serve rejects positional arguments and JSON output", async () => {
["serve", "repository"],
["serve", "--json"],
]) {
- const stdout = capture();
- const stderr = capture();
- expect(await main(args, stdout.stream, stderr.stream, dependencies())).toBe(
- 2,
- );
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ expect(await runCli(args, dependencies())).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain("serve");
}
@@ -216,12 +213,10 @@ test("serve rejects positional arguments and JSON output", async () => {
test("serve rejects missing or invalid ports", async () => {
for (const value of [undefined, "invalid", "-1", "65536", "1.5"]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const args = ["serve", "--port", ...(value === undefined ? [] : [value])];
- expect(await main(args, stdout.stream, stderr.stream, dependencies())).toBe(
- 2,
- );
+ expect(await runCli(args, dependencies())).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain("port");
}
diff --git a/sdk/typescript/tests-ts/cli-show-cost.test.ts b/sdk/typescript/tests-ts/cli-show-cost.test.ts
index ca9cb0d0e..4ce8bdb46 100644
--- a/sdk/typescript/tests-ts/cli-show-cost.test.ts
+++ b/sdk/typescript/tests-ts/cli-show-cost.test.ts
@@ -1,6 +1,7 @@
import { expect, test } from "bun:test";
import { main } from "../src/cli.js";
-import { capture, dependencies, fakeResult } from "./cli-fixtures.js";
+import { dependencies, fakeResult } from "./cli-fixtures.js";
+import { createCliTest, captureCli } from "./support/cli-run.js";
const result = fakeResult([], "complete", {
input_tokens: 1250,
@@ -14,13 +15,11 @@ test.each([false, true])("scan cost visibility with TTY %p", async (tty) => {
["--show-cost"],
["--show-cost=false", "--max-cost", "20"],
]) {
- const stdout = capture();
- const stderr = capture(tty);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: tty });
+
expect(
- await main(
+ await runCli(
["scan", ".", ...costFlags, ...(tty ? [] : ["--json"])],
- stdout.stream,
- stderr.stream,
dependencies({
result,
costUpdates: [result.cost!],
@@ -45,12 +44,10 @@ test.each(["resume", "rerun"])(
[["--show-cost"], undefined, true],
[[], 20, true],
] as const) {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scans", command, "scan-original", ...flags],
- capture().stream,
- stderr.stream,
dependencies({
result,
costUpdates: [result.cost!],
@@ -76,12 +73,10 @@ test.each(["resume", "rerun"])(
test.each([false, true])(
"missing pricing follows cost visibility: %j",
async (showCost) => {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scan", ...(showCost ? ["--show-cost"] : [])],
- capture().stream,
- stderr.stream,
dependencies(),
),
).toBe(0);
diff --git a/sdk/typescript/tests-ts/cli-signals.test.ts b/sdk/typescript/tests-ts/cli-signals.test.ts
index a81138a7b..1f9d3171d 100644
--- a/sdk/typescript/tests-ts/cli-signals.test.ts
+++ b/sdk/typescript/tests-ts/cli-signals.test.ts
@@ -8,6 +8,7 @@ import {
fakeSecurity,
} from "./cli-fixtures.js";
import { throwing } from "./support/errors.js";
+import { createCliTest, runCapturedCli } from "./support/cli-run.js";
describe("CLI signals", () => {
test("maps Ctrl-C and SIGTERM to conventional exits and preserves partial output", async () => {
@@ -15,14 +16,12 @@ describe("CLI signals", () => {
["SIGINT", 130, "Scan canceled by Ctrl-C."],
["SIGTERM", 143, "Scan terminated by SIGTERM."],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const signals = new FakeSignals();
const onInterrupt = mock();
- const exit = await main(
+ const exit = await runCli(
["scan", "."],
- stdout.stream,
- stderr.stream,
dependencies({
signals,
onRun: () => signals.emit(signal),
@@ -39,8 +38,8 @@ describe("CLI signals", () => {
});
test("cancels runtime preparation when a signal arrives", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const signals = new FakeSignals();
const deps = dependencies({ signals });
deps.createSecurity = () =>
@@ -50,22 +49,18 @@ describe("CLI signals", () => {
expect(signal?.aborted).toBe(true);
throw new DOMException("aborted", "AbortError");
});
- expect(await main(["scan", "."], stdout.stream, stderr.stream, deps)).toBe(
- 130,
- );
+ expect(await runCli(["scan", "."], deps)).toBe(130);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain("Scan canceled by Ctrl-C.");
expect(stderr.text()).toContain("No partial output was kept.");
});
test("preserves signals received during client cleanup", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const signals = new FakeSignals();
- const exit = await main(
+ const exit = await runCli(
["scan", "."],
- stdout.stream,
- stderr.stream,
dependencies({
signals,
onClose: () => signals.emit("SIGTERM"),
@@ -78,8 +73,8 @@ describe("CLI signals", () => {
});
test("lets a later repeated signal escape cleanup while suppressing delivery duplicates", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
const signals = new FakeSignals();
const forced: string[] = [];
const synchronousWrites: string[] = [];
@@ -98,9 +93,7 @@ describe("CLI signals", () => {
return fakeResult();
});
- expect(await main(["scan", "."], stdout.stream, stderr.stream, deps)).toBe(
- 130,
- );
+ expect(await runCli(["scan", "."], deps)).toBe(130);
expect(forced).toEqual(["SIGINT"]);
expect(synchronousWrites).toEqual(["\u001B[?25h"]);
expect(stderr.text()).toContain("\u001B[?25h");
@@ -122,7 +115,7 @@ describe("CLI signals", () => {
return fakeResult();
});
- await main(["scan", "."], capture().stream, capture().stream, deps);
+ await runCapturedCli(main, ["scan", "."], deps);
expect(forced).toEqual(["SIGTERM"]);
});
diff --git a/sdk/typescript/tests-ts/cli-skills.test.ts b/sdk/typescript/tests-ts/cli-skills.test.ts
index 236ca71cf..15ef41963 100644
--- a/sdk/typescript/tests-ts/cli-skills.test.ts
+++ b/sdk/typescript/tests-ts/cli-skills.test.ts
@@ -16,6 +16,11 @@ 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";
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
function linearIssue(identifier: string, comments: string[] = []) {
const nodes = comments.map((body, index) => ({
@@ -51,10 +56,10 @@ describe("CLI skill commands", () => {
await writeFile(file, `${command} file contents\n`);
let invocation: readonly string[] = [];
let prompt = "";
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
command,
`${command}.txt`,
@@ -62,8 +67,6 @@ describe("CLI skill commands", () => {
"C:\\tmp\\finding one.txt",
"\\\\server\\share\\issue.txt",
],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: directory,
onCodex: (args, output, _environment, input) => {
@@ -128,15 +131,8 @@ describe("CLI skill commands", () => {
: "",
);
- const help = capture();
- expect(
- await main(
- [command, "--help"],
- help.stream,
- capture().stream,
- dependencies(),
- ),
- ).toBe(0);
+ const help = captureCli(main, "stdout");
+ expect(await help.run([command, "--help"], dependencies())).toBe(0);
expect(help.text()).toContain(
`Usage: codex-security ${command} ${command === "patch" ? `[${argument}]` : `<${argument}>`}`,
);
@@ -163,7 +159,8 @@ describe("CLI skill commands", () => {
let environment: NodeJS.ProcessEnv | undefined;
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"patch",
"--linear-issue",
@@ -173,8 +170,6 @@ describe("CLI skill commands", () => {
"--linear-api-key",
"lin_api_SYNTHETIC_EXPLICIT",
],
- capture().stream,
- capture().stream,
dependencies({
environment: {
CODEX_SECURITY_LINEAR_API_KEY: "lin_api_SYNTHETIC_SECRET",
@@ -260,10 +255,9 @@ describe("CLI skill commands", () => {
}) as typeof filesystem.lstat);
try {
expect(
- await main(
+ await runCapturedCli(
+ main,
["patch", selected, "--linear-issue", "SEC-123"],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: repository,
environment: { CODEX_SECURITY_LINEAR_API_KEY: "synthetic-key" },
@@ -299,7 +293,8 @@ describe("CLI skill commands", () => {
let inputs: string[] = [];
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"patch",
"--linear-project",
@@ -307,8 +302,6 @@ describe("CLI skill commands", () => {
"--linear-filter",
'{"labels":{"name":{"eq":"security"}}}',
],
- capture().stream,
- capture().stream,
dependencies({
environment: { LINEAR_ACCESS_TOKEN: "SYNTHETIC_OAUTH_TOKEN" },
linearClient: ({ accessToken }) => {
@@ -412,12 +405,10 @@ describe("CLI skill commands", () => {
for (const [args, message, environment] of cases) {
const onCodex = mock<() => number>().mockReturnValue(0);
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
args,
- capture().stream,
- stderr.stream,
dependencies({
environment: environment ?? {
CODEX_SECURITY_LINEAR_API_KEY: "lin_api_SYNTHETIC_SECRET",
@@ -461,12 +452,10 @@ describe("CLI skill commands", () => {
"linked-finding.txt",
join("linked-directory", "finding.txt"),
]) {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[command, input],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: repository,
onCodex,
@@ -483,10 +472,9 @@ describe("CLI skill commands", () => {
join(linkedDirectory, "finding.txt"),
]) {
expect(
- await main(
+ await runCapturedCli(
+ main,
[command, selected],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: repository,
onCodex: (_args, output, _environment, input) => {
@@ -564,11 +552,9 @@ describe("CLI skill commands", () => {
);
try {
const onCodex = mock<() => number>().mockReturnValue(0);
- const stderr = capture();
- const status = await main(
+ const stderr = captureCli(main, "stderr");
+ const status = await stderr.run(
["validate", "finding.txt"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: root,
onCodex,
@@ -626,11 +612,9 @@ describe("CLI skill commands", () => {
try {
const onCodex = mock<() => number>().mockReturnValue(0);
- const stderr = capture();
- const status = await main(
+ const stderr = captureCli(main, "stderr");
+ const status = await stderr.run(
["validate", "finding.txt"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: repository,
onCodex,
@@ -713,10 +697,10 @@ describe("CLI skill commands", () => {
}
const onCodex = mock<OnCodex>().mockReturnValue(0);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"validate",
localFile,
@@ -724,8 +708,6 @@ describe("CLI skill commands", () => {
...posixDoubleSlashPaths,
...networkPaths,
],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: directory,
onCodex,
@@ -751,9 +733,9 @@ describe("CLI skill commands", () => {
test("applies bounded model and reasoning overrides to validation and patching", async () => {
for (const command of ["validate", "patch"] as const) {
const onCodex = mock<OnCodex>().mockReturnValue(0);
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[
command,
"a candidate finding",
@@ -762,8 +744,6 @@ describe("CLI skill commands", () => {
"--codex",
'model_reasoning_effort="high"',
],
- capture().stream,
- stderr.stream,
dependencies({
onCodex,
}),
@@ -783,10 +763,9 @@ describe("CLI skill commands", () => {
);
const onCodex = mock<OnCodex>().mockReturnValue(0);
expect(
- await main(
+ await runCapturedCli(
+ main,
["validate", longLiteral],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: process.cwd(),
onCodex,
@@ -804,12 +783,10 @@ describe("CLI skill commands", () => {
'model=" "',
]) {
const onCodex = mock<() => number>().mockReturnValue(0);
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["validate", "finding", "--codex", override],
- capture().stream,
- stderr.stream,
dependencies({
onCodex,
}),
@@ -845,7 +822,7 @@ describe("CLI skill commands", () => {
`model_providers.synthetic.${key}=${JSON.stringify(value)}`,
),
];
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const onCodex = mock<OnCodex>((_args, output, environment) => {
expect(output?.modelProvider).toBe("synthetic");
expect(output?.codexOverrides).toMatchObject({
@@ -868,15 +845,13 @@ describe("CLI skill commands", () => {
return 0;
});
expect(
- await main(
+ await stderr.run(
[
command,
"Synthetic finding",
...selection,
...overrides.flatMap((override) => ["--codex", override]),
],
- capture().stream,
- stderr.stream,
dependencies({
environment: { SYNTHETIC_GATEWAY_KEY: "SYNTHETIC_VALUE" },
onCodex,
@@ -910,9 +885,9 @@ describe("CLI skill commands", () => {
"analytics={enabled=false}",
]) {
let invocation: readonly string[] = [];
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[
command,
"Synthetic finding",
@@ -921,8 +896,6 @@ describe("CLI skill commands", () => {
"--codex",
override,
],
- capture().stream,
- stderr.stream,
dependencies({
onCodex: (args, output) => {
invocation = args;
@@ -956,9 +929,9 @@ describe("CLI skill commands", () => {
"analytics.enabled=false",
]) {
const onCodex = mock<() => number>().mockReturnValue(0);
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[
command,
"Synthetic finding",
@@ -968,8 +941,6 @@ describe("CLI skill commands", () => {
? ["--codex", "analytics.enabled=true"]
: []),
],
- capture().stream,
- stderr.stream,
dependencies({
onCodex,
}),
@@ -986,9 +957,9 @@ describe("CLI skill commands", () => {
async (command) => {
for (const model of ["gpt-6-astra", "gpt-6.1-sol"]) {
let invocation: readonly string[] = [];
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
[
command,
"a candidate finding",
@@ -998,8 +969,6 @@ describe("CLI skill commands", () => {
"--effort",
"max",
],
- capture().stream,
- stderr.stream,
dependencies({
onCodex: (args, output) => {
invocation = args;
@@ -1043,12 +1012,10 @@ describe("CLI skill commands", () => {
[["--model"], "Missing value for flag: --model"],
] as const) {
const onCodex = mock<() => number>().mockReturnValue(0);
- const invalidStderr = capture();
+ const invalidStderr = captureCli(main, "stderr");
expect(
- await main(
+ await invalidStderr.run(
[command, "a candidate finding", ...options],
- capture().stream,
- invalidStderr.stream,
dependencies({
onCodex,
}),
@@ -1072,12 +1039,10 @@ describe("CLI skill commands", () => {
];
for (const [input, expected] of invalidInputs) {
const onCodex = mock<() => number>().mockReturnValue(0);
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["validate", input!],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: directory,
onCodex,
@@ -1107,10 +1072,9 @@ describe("CLI skill commands", () => {
]) {
let received: string[] = [];
expect(
- await main(
+ await runCapturedCli(
+ main,
["validate", ...inputs],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: directory,
onCodex: (args, _output, _environment, input) => {
diff --git a/sdk/typescript/tests-ts/cli-suggest-owners.test.ts b/sdk/typescript/tests-ts/cli-suggest-owners.test.ts
index 762122d8e..cbdb2e254 100644
--- a/sdk/typescript/tests-ts/cli-suggest-owners.test.ts
+++ b/sdk/typescript/tests-ts/cli-suggest-owners.test.ts
@@ -5,9 +5,14 @@ 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 { dependencies, FakeSignals } from "./cli-fixtures.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
const { temporaryDirectory, cleanup } = createApiTestFixtures(
"owner-cli-",
@@ -41,8 +46,8 @@ test("accepts exported findings, forwards model settings, and leaves the input u
const { directory, contents, document } = await input();
await mkdir(join(directory, "source repo"));
const deps = dependencies({ currentDirectory: directory });
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
deps.suggestOwners = async (repository, findings, options, surface) => {
expect(repository).toBe(join(directory, "source repo"));
expect(findings).toEqual(document.findings);
@@ -56,7 +61,7 @@ test("accepts exported findings, forwards model settings, and leaves the input u
return report;
};
expect(
- await main(
+ await runCli(
[
"suggest-owners",
"findings.json",
@@ -68,8 +73,6 @@ test("accepts exported findings, forwards model settings, and leaves the input u
"high",
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
stderr.text(),
@@ -83,7 +86,7 @@ test("accepts exported findings, forwards model settings, and leaves the input u
test("uses the current repository by default and emits a partial report with exit code 2", async () => {
const { directory } = await input();
const deps = dependencies({ currentDirectory: directory });
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
deps.suggestOwners = async (repository, findings, options) => {
expect(repository).toBe(directory);
expect(options!.model).toBeUndefined();
@@ -104,12 +107,7 @@ test("uses the current repository by default and emits a partial report with exi
};
};
expect(
- await main(
- ["suggest-owners", "findings.json", "--json"],
- stdout.stream,
- capture().stream,
- deps,
- ),
+ await stdout.run(["suggest-owners", "findings.json", "--json"], deps),
).toBe(2);
expect(JSON.parse(stdout.text()).results[0].status).toBe("error");
});
@@ -129,14 +127,9 @@ test("rejects invalid input and extra arguments before model execution", async (
["findings.json", "extra"],
["findings.json", "--source-root"],
]) {
- expect(
- await main(
- ["suggest-owners", ...args],
- capture().stream,
- capture().stream,
- deps,
- ),
- ).toBe(2);
+ expect(await runCapturedCli(main, ["suggest-owners", ...args], deps)).toBe(
+ 2,
+ );
}
expect(suggestOwners).not.toHaveBeenCalled();
});
@@ -156,12 +149,7 @@ test.each([
return report;
};
expect(
- await main(
- ["suggest-owners", "findings.json"],
- capture().stream,
- capture().stream,
- deps,
- ),
+ await runCapturedCli(main, ["suggest-owners", "findings.json"], deps),
).toBe(code);
expect(signals.listeners.get("SIGINT")?.size).toBe(0);
expect(signals.listeners.get("SIGTERM")?.size).toBe(0);
@@ -175,15 +163,8 @@ test("reports owner lookup errors without changing the diagnostic or leaving lis
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);
+ const stderr = captureCli(main, "stderr");
+ expect(await stderr.run(["suggest-owners", "findings.json"], 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.ts
index 77cba01ac..59b81bcbc 100644
--- a/sdk/typescript/tests-ts/cli-verify-fix.test.ts
+++ b/sdk/typescript/tests-ts/cli-verify-fix.test.ts
@@ -5,8 +5,9 @@ import { stripVTControlCharacters } from "node:util";
import { main } from "../src/cli.js";
import type { Finding, JsonObject } from "../src/index.js";
import type { LinearClientFactory } from "../src/linear.js";
-import { capture, dependencies, fakeResult } from "./cli-fixtures.js";
+import { dependencies, fakeResult } from "./cli-fixtures.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
+import { createCliTest, captureCli } from "./support/cli-run.js";
async function linearIssue(identifier: string) {
return {
@@ -31,16 +32,14 @@ const linearClient = () =>
describe("read-only finding verification", () => {
test("verifies imported Linear issues in a read-only sandbox without exposing credentials", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
let prompt = "";
let environment: NodeJS.ProcessEnv | undefined;
expect(
- await main(
+ await runCli(
["verify-fix", "--linear-issue", "SEC-123", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: {
CODEX_SECURITY_LINEAR_API_KEY: "lin_api_SYNTHETIC_SECRET",
@@ -113,14 +112,11 @@ describe("read-only finding verification", () => {
});
test("shows live agent progress in the verification dashboard while keeping JSON clean", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
expect(
- await main(
+ await runCli(
["verify-fix", "A previously reported authorization bypass", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
onCodex: (_args, output) => {
const emit = output?.appServer?.onEvent;
@@ -197,18 +193,15 @@ describe("read-only finding verification", () => {
test("uses plain verification progress in CI and dumb terminals", async () => {
for (const environment of [{ CI: "1" }, { TERM: "dumb" }]) {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
expect(
- await main(
+ await runCli(
[
"verify-fix",
"A previously reported authorization bypass",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies({
environment,
onCodex: (_args, output) => {
@@ -252,12 +245,12 @@ describe("read-only finding verification", () => {
});
test("verifies repeated Linear issues together in one agent invocation", async () => {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
let agentCalls = 0;
let prompt = "";
expect(
- await main(
+ await stdout.run(
[
"verify-fix",
"--linear-issue",
@@ -266,8 +259,6 @@ describe("read-only finding verification", () => {
"SEC-456",
"--json",
],
- stdout.stream,
- capture().stream,
dependencies({
environment: { LINEAR_ACCESS_TOKEN: "SYNTHETIC_OAUTH_TOKEN" },
linearClient,
@@ -319,10 +310,10 @@ describe("read-only finding verification", () => {
title: `Finding ${index + 1}`,
});
});
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
[
"verify-fix",
"--scan",
@@ -335,8 +326,6 @@ describe("read-only finding verification", () => {
"max",
"--json",
],
- stdout.stream,
- capture().stream,
dependencies({
onWorkbench: (args) => {
expect(args).toEqual(["get-scan", "--scan-id", "scan-1"]);
@@ -385,12 +374,10 @@ describe("read-only finding verification", () => {
});
test("reports inconclusive findings without treating them as fixed", async () => {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["verify-fix", "A previously reported authorization bypass"],
- stdout.stream,
- capture().stream,
dependencies({
onCodex: (_args, output) => {
output?.stdout.write(
@@ -423,17 +410,15 @@ describe("read-only finding verification", () => {
] as const)(
"maps Codex exit %i to verification exit %i",
async (codexStatus, expectedStatus) => {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
[
"verify-fix",
"A previously reported authorization bypass",
"--json",
],
- stdout.stream,
- capture().stream,
dependencies({ onCodex: () => codexStatus }),
),
).toBe(expectedStatus);
@@ -442,13 +427,11 @@ describe("read-only finding verification", () => {
);
test("sanitizes model-controlled evidence in human-readable output", async () => {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["verify-fix", "A previously reported authorization bypass"],
- stdout.stream,
- capture().stream,
dependencies({
onCodex: (_args, output) => {
output?.stdout.write(
@@ -495,14 +478,11 @@ describe("read-only finding verification", () => {
] as const)(
"rejects %s instead of reporting an unverified fix",
async (_name, result) => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["verify-fix", "--linear-issue", "SEC-123", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: { CODEX_SECURITY_LINEAR_API_KEY: "synthetic-key" },
linearClient,
@@ -530,14 +510,12 @@ describe("read-only finding verification", () => {
] as const)(
"rejects invalid verification selection %j",
async (args, expected) => {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const onCodex = mock<() => number>().mockReturnValue(0);
expect(
- await main(
+ await stderr.run(
args,
- capture().stream,
- stderr.stream,
dependencies({
environment: { CODEX_SECURITY_LINEAR_API_KEY: "synthetic-key" },
onCodex,
diff --git a/sdk/typescript/tests-ts/cli-workbench.test.ts b/sdk/typescript/tests-ts/cli-workbench.test.ts
index 254392058..63a183ac8 100644
--- a/sdk/typescript/tests-ts/cli-workbench.test.ts
+++ b/sdk/typescript/tests-ts/cli-workbench.test.ts
@@ -10,7 +10,6 @@ import {
} from "../src/scan-comparison.js";
import {
savedRecipe,
- capture,
dependencies,
FakeSignals,
fakeResult,
@@ -18,11 +17,16 @@ import {
} from "./cli-fixtures.js";
import { temporaryDirectory } from "./support/temporary-directories.js";
import { rejecting, throwing } from "./support/errors.js";
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
describe("CLI workbench", () => {
test("lists and summarizes open findings for the current repository", async () => {
const repository = resolve("/current/repository");
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
const calls: Array<readonly string[]> = [];
const responses: JsonObject[] = [
{
@@ -35,10 +39,8 @@ describe("CLI workbench", () => {
{ findings: [{ title: "Finding 2" }], nextOffset: null },
];
expect(
- await main(
+ await stdout.run(
["findings", "list", "--json"],
- stdout.stream,
- capture().stream,
dependencies({
onWorkbench: (args) => responses[calls.push(args) - 1]!,
}),
@@ -58,10 +60,9 @@ describe("CLI workbench", () => {
findings: [{ title: "Finding 1" }, { title: "Finding 2" }],
});
expect(
- await main(
+ await runCapturedCli(
+ main,
["findings", "--json"],
- capture().stream,
- capture().stream,
dependencies({ onWorkbench: () => ({ repositories: [] }) }),
),
).toBe(0);
@@ -73,15 +74,8 @@ describe("CLI workbench", () => {
confirmedInLatestScan,
})),
});
- const stderr = capture();
- expect(
- await main(
- ["scan"],
- capture().stream,
- stderr.stream,
- dependencies({ result }),
- ),
- ).toBe(0);
+ const stderr = captureCli(main, "stderr");
+ expect(await stderr.run(["scan"], dependencies({ result }))).toBe(0);
expect(stderr.text()).toContain(
confirmed.length
? "FINDINGS 2 (1 confirmed this scan; 1 previously found; 2 high)"
@@ -115,18 +109,14 @@ describe("CLI workbench", () => {
onWorkbench,
});
deps.createSecurity = throwing("history must not initialize Codex");
- expect(await main(argv, capture().stream, capture().stream, deps)).toBe(
- 0,
- );
+ expect(await runCapturedCli(main, argv, deps)).toBe(0);
expect(onWorkbench.mock.lastCall?.[0]).toEqual(expected);
}
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["scan", "scans", "--dry-run", "--json"],
- stdout.stream,
- capture().stream,
dependencies(),
),
).toBe(0);
@@ -250,13 +240,13 @@ describe("CLI workbench", () => {
const calls = mock((_args: readonly string[]) => {
return response;
});
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
const deps = dependencies({
onWorkbench: calls,
});
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(await stdout.run(argv, deps)).toBe(0);
expect(calls.mock.calls.map(([value]) => value)).toEqual([expected]);
expect(JSON.parse(stdout.text())).toEqual(output);
}
@@ -336,7 +326,7 @@ describe("CLI workbench", () => {
);
const calls: Array<readonly string[]> = [];
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
const deps = dependencies({
environment: { CODEX_SECURITY_STATE_DIR: state },
onWorkbench: (args): JsonObject => {
@@ -362,12 +352,7 @@ describe("CLI workbench", () => {
});
deps.createSecurity = throwing("logs must not initialize Codex");
expect(
- await main(
- ["scans", "logs", "scan-1", "--json"],
- stdout.stream,
- capture().stream,
- deps,
- ),
+ await stdout.run(["scans", "logs", "scan-1", "--json"], deps),
).toBe(0);
expect(calls).toEqual([["get-scan", "--scan-id", "scan-1"]]);
expect(stdout.text()).toContain("SYNTHETIC_KEY");
@@ -375,15 +360,8 @@ describe("CLI workbench", () => {
expect(stdout.text()).not.toContain("PRIVATE LATER SESSION");
calls.length = 0;
- const latest = capture();
- expect(
- await main(
- ["scans", "logs", "--json"],
- latest.stream,
- capture().stream,
- deps,
- ),
- ).toBe(0);
+ const latest = captureCli(main, "stdout");
+ expect(await latest.run(["scans", "logs", "--json"], deps)).toBe(0);
expect(calls).toEqual([
["list-scans", "--repository", "/current/repository", "--limit", "1"],
["get-scan", "--scan-id", "scan-1"],
@@ -395,12 +373,10 @@ describe("CLI workbench", () => {
});
test("explains when a saved scan has no associated session", async () => {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scans", "logs", "scan-1"],
- capture().stream,
- stderr.stream,
dependencies({
onWorkbench: () => ({
scan: { scanId: "scan-1", targetPath: "/repo" },
@@ -436,13 +412,11 @@ describe("CLI workbench", () => {
] as const) {
const calls: Array<readonly string[]> = [];
let comparisonInput: string | undefined;
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["scans", command, ...scanIds, "--json"],
- stdout.stream,
- capture().stream,
dependencies({
onWorkbench: (args, input): JsonObject => {
calls.push(args);
@@ -507,10 +481,9 @@ describe("CLI workbench", () => {
const before = [{ occurrenceId: "before" }];
const after = [{ occurrenceId: "after" }];
expect(
- await main(
+ await runCapturedCli(
+ main,
["scans", command, ...scanArgs, ...selection, "--json"],
- capture().stream,
- capture().stream,
dependencies({
onWorkbench: (args): JsonObject => {
if (args[0] === "compare-scans") {
@@ -556,12 +529,10 @@ describe("CLI workbench", () => {
);
test("requires two completed scans for a default comparison", async () => {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scans", "compare"],
- capture().stream,
- stderr.stream,
dependencies({
onWorkbench: () => ({
scans: [{ scanId: "scan-1" }],
@@ -576,13 +547,11 @@ describe("CLI workbench", () => {
test("reports automatic matching failures without saving a comparison", async () => {
const calls: string[] = [];
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scans", "compare", "before", "after"],
- capture().stream,
- stderr.stream,
dependencies({
onWorkbench: (args) => {
calls.push(args[0]!);
@@ -602,13 +571,11 @@ describe("CLI workbench", () => {
test.each([false, true])(
"keeps matching progress on stderr with TTY=%p",
async (isTTY) => {
- const stdout = capture();
- const stderr = capture(isTTY);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: isTTY });
+
expect(
- await main(
+ await runCli(
["scans", "match", "before", "after", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
onWorkbench: (args): JsonObject =>
args[0] === "compare-scans"
@@ -651,12 +618,10 @@ describe("CLI workbench", () => {
async (args, signal, expectedExit) => {
const signals = new FakeSignals();
const commands: string[] = [];
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scans", "match", ...args, "--json"],
- capture().stream,
- stderr.stream,
dependencies({
signals,
environment: { CODEX_SECURITY_STATE_DIR: "/synthetic/state" },
@@ -702,8 +667,8 @@ describe("CLI workbench", () => {
"reports cancellation during a %s instead of success",
async (stage) => {
const signals = new FakeSignals();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
let observedSignal: AbortSignal | undefined;
const target =
stage === "cached comparison"
@@ -713,10 +678,8 @@ describe("CLI workbench", () => {
: "save-scan-comparison";
const args = stage === "matching plan" ? ["--all"] : ["before", "after"];
expect(
- await main(
+ await runCli(
["scans", "match", ...args, "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
signals,
onWorkbench: (command, _input, signal): JsonObject => {
@@ -766,10 +729,9 @@ describe("CLI workbench", () => {
});
deps.now = () => now;
deps.forceExit = forced;
- const running = main(
+ const running = runCapturedCli(
+ main,
["scans", "match", "before", "after", "--json"],
- capture().stream,
- capture().stream,
deps,
);
await started.promise;
@@ -857,13 +819,11 @@ describe("CLI workbench", () => {
],
};
});
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["scans", "match", "--all", "--force", "--json"],
- stdout.stream,
- capture().stream,
dependencies({
onWorkbench: (args, input): JsonObject => {
calls.push(args);
@@ -944,10 +904,9 @@ describe("CLI workbench", () => {
let saved: string | undefined;
expect(
- await main(
+ await runCapturedCli(
+ main,
["scans", "match", "--all", "--json"],
- capture().stream,
- capture().stream,
dependencies({
onWorkbench: (args, input): JsonObject => {
if (args[0] === "save-scan-comparison") saved = input;
@@ -996,10 +955,9 @@ describe("CLI workbench", () => {
const matchedInputs: ScanComparisonInput[] = [];
expect(
- await main(
+ await runCapturedCli(
+ main,
["scans", "match", "--all", "--json"],
- capture().stream,
- capture().stream,
dependencies({
onWorkbench: (args): JsonObject =>
args[0] === "list-unmatched-scan-pairs"
@@ -1084,14 +1042,9 @@ describe("CLI workbench", () => {
});
deps.matchFindings = rejecting("empty comparisons must not start Codex");
- expect(
- await main(
- ["scans", "match", "--all"],
- capture().stream,
- capture().stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCapturedCli(main, ["scans", "match", "--all"], deps)).toBe(
+ 0,
+ );
expect(calls[1]!.at(-1)).toBe("--matches-json-stdin");
expect(JSON.parse(comparisonInput!)).toEqual({
matches: [],
@@ -1102,13 +1055,11 @@ describe("CLI workbench", () => {
test("projects historical uncertainty per scan without losing a known match", async () => {
const calls: Array<readonly string[]> = [];
const inputs: Array<string | undefined> = [];
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["scans", "match", "--all", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
onWorkbench: (args, input): JsonObject => {
calls.push(args);
@@ -1213,10 +1164,9 @@ describe("CLI workbench", () => {
});
let saved: unknown;
expect(
- await main(
+ await runCapturedCli(
+ main,
["scans", "match", "before", "after", ...(force ? ["--force"] : [])],
- capture().stream,
- capture().stream,
dependencies({
onWorkbench: (args, input): JsonObject => {
calls.push(args);
@@ -1271,10 +1221,9 @@ describe("CLI workbench", () => {
]) {
const onWorkbench = mock<() => {}>().mockReturnValue({});
expect(
- await main(
+ await runCapturedCli(
+ main,
args,
- capture().stream,
- capture().stream,
dependencies({
onWorkbench,
}),
@@ -1287,12 +1236,12 @@ describe("CLI workbench", () => {
test.each(["scan-original", undefined])(
"rejects Markdown rerun output before loading scan %p",
async (scanId) => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const onWorkbench = mock<() => {}>().mockReturnValue({});
expect(
- await main(
+ await runCli(
[
"scans",
"rerun",
@@ -1300,8 +1249,6 @@ describe("CLI workbench", () => {
"--format",
"md",
],
- stdout.stream,
- stderr.stream,
dependencies({
onWorkbench,
}),
@@ -1319,10 +1266,9 @@ describe("CLI workbench", () => {
let parentScanId: unknown;
expect(
- await main(
+ await runCapturedCli(
+ main,
["scans", "rerun"],
- capture().stream,
- capture().stream,
dependencies({
onTurn: (_repository, options) => {
parentScanId = options.parentScanId;
@@ -1356,10 +1302,9 @@ describe("CLI workbench", () => {
agents: { max_threads: 6 },
};
expect(
- await main(
+ await runCapturedCli(
+ main,
["scans", "rerun", "scan-original"],
- capture().stream,
- capture().stream,
dependencies({
onConfig,
onTurn,
@@ -1421,10 +1366,9 @@ describe("CLI workbench", () => {
for (const [target, expected] of references) {
const onTurn = mock<(repository: string, options: ScanOptions) => void>();
expect(
- await main(
+ await runCapturedCli(
+ main,
["scans", "rerun", "scan-original"],
- capture().stream,
- capture().stream,
dependencies({
onTurn,
onWorkbench: () => savedRecipe({}, target),
@@ -1451,10 +1395,9 @@ describe("CLI workbench", () => {
};
expect(
- await main(
+ await runCapturedCli(
+ main,
["scans", "rerun", "scan-original"],
- capture().stream,
- capture().stream,
dependencies({
onConfig,
onWorkbench: () => savedRecipe(savedConfig),
@@ -1469,13 +1412,11 @@ describe("CLI workbench", () => {
);
test("preserves workbench failures and does not initialize Codex", async () => {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const onRun = mock();
expect(
- await main(
+ await stderr.run(
["scans", "show", "missing"],
- capture().stream,
- stderr.stream,
dependencies({
onRun,
onWorkbench: throwing(`Scan lookup failed ${SYNTHETIC_CREDENTIALS}`),
diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.ts
index 97dc10c57..e14079936 100644
--- a/sdk/typescript/tests-ts/cli.test.ts
+++ b/sdk/typescript/tests-ts/cli.test.ts
@@ -64,6 +64,11 @@ 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";
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
const DEFAULT_SCAN_MODEL_CONFIGURATION =
scanModelConfiguration(DEFAULT_CODEX_CONFIG);
@@ -105,12 +110,10 @@ async function multiscanInventory(root: string): Promise<void> {
describe("CLI", () => {
test("passes the safety identifier as a per-scan option", async () => {
const onTurn = mock<(repository: string, options: unknown) => void>();
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["scan", "--safety-identifier", "synthetic-user", ".", "--json"],
- capture().stream,
- stderr.stream,
dependencies({
onTurn,
}),
@@ -134,12 +137,10 @@ describe("CLI", () => {
expect(root.text()).toContain("--llms-full");
expect(stderr.text()).toBe("");
- const schema = capture();
+ const schema = captureCli(main, "stdout");
expect(
- await main(
+ await schema.run(
["scan", "--schema", "--format", "json"],
- schema.stream,
- capture().stream,
dependencies(),
),
).toBe(0);
@@ -195,12 +196,10 @@ describe("CLI", () => {
additionalProperties: false,
});
- const rerunSchema = capture();
+ const rerunSchema = captureCli(main, "stdout");
expect(
- await main(
+ await rerunSchema.run(
["scans", "rerun", "--schema", "--format", "json"],
- rerunSchema.stream,
- capture().stream,
dependencies(),
),
).toBe(0);
@@ -209,12 +208,10 @@ describe("CLI", () => {
options: { properties: { verbose: { type: "boolean" } } },
});
- const matchSchema = capture();
+ const matchSchema = captureCli(main, "stdout");
expect(
- await main(
+ await matchSchema.run(
["scans", "match", "--schema", "--format", "json"],
- matchSchema.stream,
- capture().stream,
dependencies(),
),
).toBe(0);
@@ -223,12 +220,10 @@ describe("CLI", () => {
options: { properties: { all: { type: "boolean" } } },
});
- const falsePositiveSchema = capture();
+ const falsePositiveSchema = captureCli(main, "stdout");
expect(
- await main(
+ await falsePositiveSchema.run(
["findings", "false-positive", "--schema", "--format", "json"],
- falsePositiveSchema.stream,
- capture().stream,
dependencies(),
),
).toBe(0);
@@ -246,10 +241,8 @@ describe("CLI", () => {
},
});
- const manifest = capture();
- expect(
- await main(["--llms"], manifest.stream, capture().stream, dependencies()),
- ).toBe(0);
+ const manifest = captureCli(main, "stdout");
+ expect(await manifest.run(["--llms"], dependencies())).toBe(0);
expect(manifest.text()).toContain("codex-security scan [repository]");
expect(manifest.text()).toContain(
"codex-security install-hook [repository]",
@@ -278,15 +271,10 @@ describe("CLI", () => {
);
expect(manifest.text()).toContain("codex-security info");
- const completions = capture();
- expect(
- await main(
- ["completions", "bash"],
- completions.stream,
- capture().stream,
- dependencies(),
- ),
- ).toBe(0);
+ const completions = captureCli(main, "stdout");
+ expect(await completions.run(["completions", "bash"], dependencies())).toBe(
+ 0,
+ );
expect(completions.text()).toContain('export COMPLETE="bash"');
});
@@ -326,22 +314,13 @@ describe("CLI", () => {
] as const;
for (const command of commands) {
- const help = capture();
- const schema = capture();
- expect(
- await main(
- [...command, "--help"],
- help.stream,
- capture().stream,
- dependencies(),
- ),
- ).toBe(0);
+ const help = captureCli(main, "stdout");
+ const schema = captureCli(main, "stdout");
+ expect(await help.run([...command, "--help"], dependencies())).toBe(0);
expect(help.text()).not.toMatch(/--[a-z][a-z0-9-]*[A-Z][A-Za-z0-9-]*/u);
expect(
- await main(
+ await schema.run(
[...command, "--schema", "--format", "json"],
- schema.stream,
- capture().stream,
dependencies(),
),
).toBe(0);
@@ -544,8 +523,8 @@ describe("CLI", () => {
const calls = mock((_args: readonly string[]) => {
return response;
});
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies({
onWorkbench: calls,
});
@@ -553,7 +532,7 @@ describe("CLI", () => {
fail("finding feedback must not initialize Codex");
expect(
- await main(
+ await runCli(
[
"findings",
"false-positive",
@@ -562,8 +541,6 @@ describe("CLI", () => {
reason,
"--json",
],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -585,15 +562,13 @@ describe("CLI", () => {
});
test("requires a reason before marking a finding as a false positive", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
const onWorkbench = mock<() => {}>().mockReturnValue({});
expect(
- await main(
+ await runCli(
["findings", "false-positive", "occurrence-1", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
onWorkbench,
}),
@@ -604,12 +579,12 @@ describe("CLI", () => {
});
test("preserves false-positive workbench failures", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const onRun = mock();
expect(
- await main(
+ await runCli(
[
"findings",
"false-positive",
@@ -618,8 +593,6 @@ describe("CLI", () => {
"Not reachable from untrusted input.",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies({
onRun,
onWorkbench: () =>
@@ -652,12 +625,10 @@ describe("CLI", () => {
onRun: () => (started = true),
});
for (let attempt = 0; attempt < 2; attempt += 1) {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["install-hook", ".", "--fail-on-severity", "medium", "--json"],
- stdout.stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -678,12 +649,10 @@ describe("CLI", () => {
hook,
"#!/bin/sh\nset -eu\nexec npx --no-install codex-security scan . --working-tree --fail-on-severity medium\n",
);
- const migratedHook = capture();
+ const migratedHook = captureCli(main, "stdout");
expect(
- await main(
+ await migratedHook.run(
["install-hook", ".", "--fail-on-severity", "medium", "--json"],
- migratedHook.stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -695,15 +664,8 @@ describe("CLI", () => {
expect(migrated.failOnSeverity).toBe("medium");
expect(await readFile(hook, "utf8")).toBe(trustedHook);
- const existingHook = capture();
- expect(
- await main(
- ["install-hook", "."],
- capture().stream,
- existingHook.stream,
- deps,
- ),
- ).toBe(2);
+ const existingHook = captureCli(main, "stderr");
+ expect(await existingHook.run(["install-hook", "."], deps)).toBe(2);
expect(existingHook.text()).toContain("A pre-commit hook already exists");
expect(await readFile(hook, "utf8")).toContain(
"--fail-on-severity medium",
@@ -711,12 +673,10 @@ describe("CLI", () => {
const customHook = "#!/bin/sh\nexit 0\n";
await writeFile(hook, customHook);
- const customHookError = capture();
+ const customHookError = captureCli(main, "stderr");
expect(
- await main(
+ await customHookError.run(
["install-hook", ".", "--fail-on-severity", "medium"],
- capture().stream,
- customHookError.stream,
deps,
),
).toBe(2);
@@ -802,12 +762,12 @@ describe("CLI", () => {
const threatModelsPath = resolve(root, "/shared/threat-models");
try {
await multiscanInventory(root);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
let config: CodexSecurityConfig | undefined;
let scanOptions: unknown;
expect(
- await main(
+ await runCli(
[
"bulk-scan",
"repositories.csv",
@@ -826,8 +786,6 @@ describe("CLI", () => {
"features.goals=true",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: root,
onConfig: (value) => (config = value),
@@ -880,9 +838,9 @@ describe("CLI", () => {
resolveCliPath(currentDirectory, "~person/repositories.csv"),
).toBe(join(currentDirectory, "~person", "repositories.csv"));
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
[
"bulk-scan",
"~/repositories.csv",
@@ -890,8 +848,6 @@ describe("CLI", () => {
"~/results",
"--json",
],
- stdout.stream,
- capture().stream,
dependencies({ currentDirectory }),
),
).toBe(0);
@@ -929,9 +885,9 @@ describe("CLI", () => {
try {
await multiscanInventory(root);
let config: CodexSecurityConfig | undefined;
- const missingModelError = capture();
+ const missingModelError = captureCli(main, "stderr");
expect(
- await main(
+ await missingModelError.run(
[
"bulk-scan",
"repositories.csv",
@@ -940,8 +896,6 @@ describe("CLI", () => {
"--provider",
provider,
],
- capture().stream,
- missingModelError.stream,
dependencies({
currentDirectory: root,
onConfig: (value) => (config = value),
@@ -953,7 +907,8 @@ describe("CLI", () => {
);
expect(config).toBeUndefined();
expect(
- await main(
+ await runCapturedCli(
+ main,
[
"bulk-scan",
"repositories.csv",
@@ -965,8 +920,6 @@ describe("CLI", () => {
model,
"--json",
],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: root,
onConfig: (value) => (config = value),
@@ -988,10 +941,10 @@ describe("CLI", () => {
const root = await temporaryDirectory("codex-security-cli-multiscan-");
try {
await multiscanInventory(root);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"bulk-scan",
"repositories.csv",
@@ -999,8 +952,6 @@ describe("CLI", () => {
"results",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: root,
onRun: () => {
@@ -1061,17 +1012,12 @@ describe("CLI", () => {
"accounts/fireworks/models/qwen3-235b-a22b",
],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
let started = false;
expect(
- await main(
- argv,
- stdout.stream,
- stderr.stream,
- dependencies({ onRun: () => (started = true) }),
- ),
+ await runCli(argv, dependencies({ onRun: () => (started = true) })),
).toBe(2);
expect(started).toBe(false);
expect(stdout.text()).toBe("");
@@ -1080,28 +1026,20 @@ describe("CLI", () => {
});
test("requires an output directory for a supplied bulk scan CSV", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
- ["bulk-scan", "repositories.csv"],
- stdout.stream,
- stderr.stream,
- dependencies(),
- ),
+ await runCli(["bulk-scan", "repositories.csv"], dependencies()),
).toBe(2);
expect(stderr.text()).toContain("--output-dir is required");
expect(stdout.text()).toBe("");
});
test("rejects an output directory without a repository CSV", async () => {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["bulk-scan", "--output-dir", "results"],
- capture().stream,
- stderr.stream,
dependencies(),
),
).toBe(2);
@@ -1161,7 +1099,7 @@ describe("CLI", () => {
}, 30_000);
test("presents interactive scan history and hides abandoned running scans", async () => {
- const stdout = capture(true);
+ const stdout = captureCli(main, "stdout", true);
const scan = {
mode: "standard",
targetPath: "/demo/juice-shop",
@@ -1210,14 +1148,9 @@ describe("CLI", () => {
});
deps.now = () => Date.parse("2026-07-24T12:00:00Z");
- expect(
- await main(
- ["scans", "list", "/demo/juice-shop"],
- stdout.stream,
- capture().stream,
- deps,
- ),
- ).toBe(0);
+ expect(await stdout.run(["scans", "list", "/demo/juice-shop"], deps)).toBe(
+ 0,
+ );
const text = stdout.text();
expect(text).toContain("CODEX SECURITY");
expect(text).toContain("SCAN HISTORY");
@@ -1238,7 +1171,7 @@ describe("CLI", () => {
});
test("shows the latest completed scan for the current repository by default", async () => {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
const calls: Array<readonly string[]> = [];
const responses: JsonObject[] = [
{ scans: [{ scanId: "latest-scan" }] },
@@ -1246,10 +1179,8 @@ describe("CLI", () => {
];
expect(
- await main(
+ await stdout.run(
["scans", "show", "--json"],
- stdout.stream,
- capture().stream,
dependencies({
onWorkbench: (args) => responses[calls.push(args) - 1]!,
}),
@@ -1271,16 +1202,9 @@ describe("CLI", () => {
});
test("reports when the current repository has no saved scans", async () => {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
- expect(
- await main(
- ["scans", "show"],
- capture().stream,
- stderr.stream,
- dependencies(),
- ),
- ).toBe(2);
+ expect(await stderr.run(["scans", "show"], dependencies())).toBe(2);
expect(stderr.text()).toContain(
"No completed scans found for the current repository.",
);
@@ -1296,12 +1220,10 @@ describe("CLI", () => {
["scans", "match", "before", "after", "--json"],
["scans", "match", "--all", "--json"],
]) {
- const stdout = capture();
- const stderr = capture();
- const result = await main(
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ const result = await runCli(
[...argv, ...(fullOutput ? ["--full-output"] : [])],
- stdout.stream,
- stderr.stream,
{
...dependencies({
onWorkbench: () =>
@@ -1353,17 +1275,15 @@ describe("CLI", () => {
];
for (const showLinkedFindings of [false, true]) {
- const stdout = capture(true);
+ const stdout = captureCli(main, "stdout", true);
expect(
- await main(
+ await stdout.run(
[
"scans",
"show",
"scan-1",
...(showLinkedFindings ? ["--show-linked-findings"] : []),
],
- stdout.stream,
- capture().stream,
dependencies({
onWorkbench: () => ({
scan: {
@@ -1412,12 +1332,10 @@ describe("CLI", () => {
});
test("sanitizes interactive finding text and respects NO_COLOR", async () => {
- const stdout = capture(true);
+ const stdout = captureCli(main, "stdout", true);
expect(
- await main(
+ await stdout.run(
["scans", "show", "scan-1"],
- stdout.stream,
- capture().stream,
dependencies({
environment: { NO_COLOR: "1" },
onWorkbench: () => ({
@@ -1475,15 +1393,8 @@ describe("CLI", () => {
["scans", "compare", "before", "after", "--json"],
["scans", "compare", "before", "after", "--format", "yaml"],
]) {
- const stdout = capture(true);
- expect(
- await main(
- argv,
- stdout.stream,
- capture().stream,
- dependencies({ onWorkbench }),
- ),
- ).toBe(0);
+ const stdout = captureCli(main, "stdout", true);
+ expect(await stdout.run(argv, dependencies({ onWorkbench }))).toBe(0);
expect(stdout.text()).toContain("internal-finding-id");
expect(stdout.text()).toContain("internal-occurrence-id");
if (argv.includes("--json")) {
@@ -1491,12 +1402,10 @@ describe("CLI", () => {
}
}
- const redirected = capture();
+ const redirected = captureCli(main, "stdout");
expect(
- await main(
+ await redirected.run(
["scans", "compare", "before", "after"],
- redirected.stream,
- capture().stream,
dependencies({ onWorkbench }),
),
).toBe(0);
@@ -1504,12 +1413,10 @@ describe("CLI", () => {
expect(redirected.text()).toContain("status: unknown");
expect(redirected.text()).not.toContain("CODEX SECURITY");
- const filtered = capture(true);
+ const filtered = captureCli(main, "stdout", true);
expect(
- await main(
+ await filtered.run(
["scans", "compare", "before", "after", "--filter-output", "summary"],
- filtered.stream,
- capture().stream,
dependencies({ onWorkbench }),
),
).toBe(0);
@@ -1519,15 +1426,13 @@ describe("CLI", () => {
});
test("prints SDK metadata without starting a scan", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
let started = false;
expect(
- await main(
+ await runCli(
["info", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({ onRun: () => (started = true) }),
),
).toBe(0);
@@ -1541,16 +1446,14 @@ describe("CLI", () => {
});
test("filters useful first-run metadata without starting Codex", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () => fail("info must stay local and read-only");
expect(
- await main(
+ await runCli(
["info", "--json", "--filter-output", "model,reasoningEffort,nextStep"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -1563,14 +1466,11 @@ describe("CLI", () => {
});
test("rejects scan-only filters before running the info command", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["info", "--json", "--filter-output", "manifest"],
- stdout.stream,
- stderr.stream,
dependencies(),
),
).toBe(2);
@@ -1801,12 +1701,10 @@ describe("CLI", () => {
expect(options?.workflowId).toBe("scan-workflow");
return result;
});
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["scan", ".", "--workflow-id", "scan-workflow", "--json"],
- stdout.stream,
- capture().stream,
deps,
),
).toBe(0);
@@ -1816,8 +1714,8 @@ describe("CLI", () => {
});
test("keeps verbose diagnostics separate from interactive progress", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
const result = fakeResult([], "complete", {
input_tokens: 200,
cache_write_input_tokens: 100,
@@ -1860,14 +1758,7 @@ describe("CLI", () => {
return result;
});
- expect(
- await main(
- ["scan", ".", "--verbose"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--verbose"], deps)).toBe(0);
const output = stripVTControlCharacters(stderr.text());
const diagnosticLines = output
.split(/[\r\n]+/u)
@@ -1899,20 +1790,13 @@ describe("CLI", () => {
["--headless", "--format", "json"],
["--headless", "--format", "jsonl"],
]) {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
const setIntervalMock = mock(fakeInterval);
const deps = dependencies();
deps.setInterval = setIntervalMock;
- expect(
- await main(
- ["scan", ".", ...options],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli(["scan", ".", ...options], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain("Preparing scan");
expect(stderr.text()).toContain("Running scan");
@@ -1929,17 +1813,15 @@ describe("CLI", () => {
process.stdin,
"isTTY",
);
- const stderr = capture(true);
+ const stderr = captureCli(main, "stderr", true);
let subscribed = false;
try {
Object.defineProperty(process.stdin, "isTTY", {
configurable: true,
value: stdinTTY,
});
- const code = await main(
+ const code = await stderr.run(
["scan", "."],
- capture().stream,
- stderr.stream,
dependencies({
onTurn: (_repository, options) => {
subscribed = typeof options.onSessionEvent === "function";
@@ -1966,8 +1848,8 @@ describe("CLI", () => {
{ options: [], environment: { TERM: "dumb" }, isTTY: true },
{ options: [], environment: {}, isTTY: false },
]) {
- const stdout = capture();
- const stderr = capture(isTTY);
+ const { stderr, runCli } = createCliTest(main, { stderr: isTTY });
+
const result = fakeResult([], "complete", {
input_tokens: 1_250,
cached_input_tokens: 200,
@@ -1987,14 +1869,7 @@ describe("CLI", () => {
});
deps.setInterval = setIntervalMock;
- expect(
- await main(
- ["scan", ".", ...options],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli(["scan", ".", ...options], deps)).toBe(0);
expect(stderr.text()).toContain("[00:00] Preparing scan");
expect(stderr.text()).toContain(
"Scan phase: reviewing files (3/8 files).",
@@ -2015,14 +1890,11 @@ describe("CLI", () => {
});
test("uses plain progress when rerunning scans in CI", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
expect(
- await main(
+ await runCli(
["scans", "rerun", "scan-original"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: { CI: "true" },
onWorkbench: () => savedRecipe(),
@@ -2039,8 +1911,8 @@ describe("CLI", () => {
});
test("falls back to plain progress when the dashboard cannot initialize", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
const onRun = mock();
const onClose = mock();
const setIntervalMock = mock(throwing("Dashboard timer unavailable."));
@@ -2050,9 +1922,7 @@ describe("CLI", () => {
});
deps.setInterval = setIntervalMock;
- expect(await main(["scan", "."], stdout.stream, stderr.stream, deps)).toBe(
- 0,
- );
+ expect(await runCli(["scan", "."], deps)).toBe(0);
expect(onRun).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledTimes(1);
expect(setIntervalMock).toHaveBeenCalledTimes(1);
@@ -2061,8 +1931,8 @@ describe("CLI", () => {
});
test("closes the client when dashboard cleanup cannot restore the terminal", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
const write = stderr.stream.write;
const signals = new FakeSignals();
const onClose = mock();
@@ -2074,10 +1944,8 @@ describe("CLI", () => {
};
expect(
- await main(
+ await runCli(
["scan", "."],
- stdout.stream,
- stderr.stream,
dependencies({
signals,
onClose,
@@ -2090,8 +1958,8 @@ describe("CLI", () => {
});
test("keeps terminal scans in one live dashboard", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
const result = fakeResult([], "complete", {
input_tokens: 17_985,
cached_input_tokens: 10_496,
@@ -2099,7 +1967,7 @@ describe("CLI", () => {
});
expect(
- await main(
+ await runCli(
[
"scan",
"/code/juice-shop",
@@ -2110,8 +1978,6 @@ describe("CLI", () => {
"--max-cost",
"2",
],
- stdout.stream,
- stderr.stream,
dependencies({
environment: { NO_COLOR: "1" },
result,
@@ -2207,8 +2073,8 @@ describe("CLI", () => {
});
test("omits stage and file counts from interactive Deep scan dashboards", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
const result = fakeResult([], "complete", {
input_tokens: 1_250,
cached_input_tokens: 200,
@@ -2216,10 +2082,8 @@ describe("CLI", () => {
});
expect(
- await main(
+ await runCli(
["scan", "/code/juice-shop", "--mode", "deep"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: { NO_COLOR: "1" },
result,
@@ -2266,14 +2130,13 @@ describe("CLI", () => {
["--format=jsonl"],
] as const) {
const onCodex = mock<() => number>().mockReturnValue(0);
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, {
+ stderr: true,
+ });
expect(
- await main(
+ await runCli(
[command, ...arguments_, ...format],
- stdout.stream,
- stderr.stream,
dependencies({
onCodex,
}),
@@ -2290,16 +2153,14 @@ describe("CLI", () => {
test("rejects CSV stdout when JSON output is requested", async () => {
const exportFindings = mock(resolving(new Uint8Array()));
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.exportFindings = exportFindings;
expect(
- await main(
+ await runCli(
["export", "scan", "--export-format", "csv", "--output", "-", "--json"],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -2311,13 +2172,11 @@ describe("CLI", () => {
});
test("prints export help without initializing Codex", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = mustNotInitializeCodex;
- expect(
- await main(["export", "--help"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["export", "--help"], deps)).toBe(0);
expect(stdout.text()).toContain("Usage: codex-security export [scan-dir]");
expect(stdout.text()).toContain("--artifact <artifact>");
expect(stdout.text()).toContain("--scan <scan-id>");
@@ -2445,10 +2304,9 @@ describe("CLI", () => {
] as const) {
let config: CodexSecurityConfig | undefined;
expect(
- await main(
+ await runCapturedCli(
+ main,
["scan", ".", ...options],
- capture().stream,
- capture().stream,
dependencies({ onConfig: (value) => (config = value) }),
),
).toBe(0);
@@ -2483,10 +2341,9 @@ describe("CLI", () => {
] as const) {
let config: CodexSecurityConfig | undefined;
expect(
- await main(
+ await runCapturedCli(
+ main,
["scan", ".", ...options],
- capture().stream,
- capture().stream,
dependencies({ onConfig: (value) => (config = value) }),
),
).toBe(0);
@@ -2506,10 +2363,9 @@ describe("CLI", () => {
] as const) {
let config: CodexSecurityConfig | undefined;
expect(
- await main(
+ await runCapturedCli(
+ main,
["scan", ".", ...options],
- capture().stream,
- capture().stream,
dependencies({
environment: { AWS_BEARER_TOKEN_BEDROCK: "synthetic-bedrock" },
onConfig: (value) => (config = value),
@@ -2525,11 +2381,11 @@ describe("CLI", () => {
});
test("parses repeatable options and every scan target through Incur", async () => {
- const pathOutput = capture();
+ const pathOutput = captureCli(main, "stdout");
let pathOptions: unknown;
let pathConfig: CodexSecurityConfig | undefined;
expect(
- await main(
+ await pathOutput.run(
[
"scan",
"repo",
@@ -2560,8 +2416,6 @@ describe("CLI", () => {
"/tmp/results",
"--archive-existing",
],
- pathOutput.stream,
- capture().stream,
dependencies({
onConfig: (config) => (pathConfig = config),
onTurn: (_repository, options) => (pathOptions = options),
@@ -2598,10 +2452,9 @@ describe("CLI", () => {
] as const) {
let target: unknown;
expect(
- await main(
+ await runCapturedCli(
+ main,
argv,
- capture().stream,
- capture().stream,
dependencies({
onTurn: (_repository, options) => {
target = (options as { target?: unknown }).target;
@@ -2865,11 +2718,11 @@ describe("CLI", () => {
],
];
for (const [argv, message] of cases) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
let started = false;
expect(
- await main(argv, stdout.stream, stderr.stream, {
+ await runCli(argv, {
...dependencies({ onRun: () => (started = true) }),
exportFindings: async () => {
started = true;
@@ -2888,37 +2741,30 @@ describe("CLI", () => {
["scan", "--fail-on-severity", SYNTHETIC_CREDENTIALS],
["export", "scan", "--export-format", SYNTHETIC_CREDENTIALS],
] as const) {
- const stdout = capture();
- const stderr = capture();
- expect(
- await main(argv, stdout.stream, stderr.stream, dependencies()),
- ).toBe(2);
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ expect(await runCli(argv, dependencies())).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).not.toContain("SYNTHETIC");
}
});
test("honors Incur help before command validation", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
- ["scan", "--mode", "bogus", "--help"],
- stdout.stream,
- stderr.stream,
- dependencies(),
- ),
+ await runCli(["scan", "--mode", "bogus", "--help"], dependencies()),
).toBe(0);
expect(stdout.text()).toContain("Usage: codex-security scan [repository]");
expect(stderr.text()).toBe("");
});
test("maps configuration and emits JSON only on stdout", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const captured: { config?: CodexSecurityConfig } = {};
const onTurn = mock<(repository: string) => void>();
- const exit = await main(
+ const exit = await runCli(
[
"scan",
"repo",
@@ -2930,8 +2776,6 @@ describe("CLI", () => {
"features.goals=true",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies({
onConfig: (value) => {
captured.config = value;
@@ -2953,8 +2797,8 @@ describe("CLI", () => {
});
test("emits verbose scan lifecycle diagnostics without changing JSON output", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = fakeResult(["high"], "complete", {
input_tokens: 1_250,
cached_input_tokens: 200,
@@ -2991,14 +2835,7 @@ describe("CLI", () => {
return result;
});
- expect(
- await main(
- ["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--verbose", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(result.toJSON());
expect(stderr.text()).toContain(
`codex-security: debug: scan.configuration cli_version=${JSON.stringify(VERSION)}`,
@@ -3038,16 +2875,10 @@ describe("CLI", () => {
});
test("reports the effective default model and reasoning effort in verbose scan diagnostics", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
- ["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
- dependencies(),
- ),
+ await runCli(["scan", ".", "--verbose", "--json"], dependencies()),
).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
const configuration = stderr
@@ -3066,14 +2897,11 @@ describe("CLI", () => {
});
test("includes selected reasoning effort in verbose scan diagnostics", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["scan", ".", "--effort", "high", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
dependencies(),
),
).toBe(0);
@@ -3085,11 +2913,10 @@ describe("CLI", () => {
});
test("reports reasoning effort supplied through legacy --codex overrides", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
[
"scan",
".",
@@ -3098,8 +2925,6 @@ describe("CLI", () => {
"--verbose",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies(),
),
).toBe(0);
@@ -3141,11 +2966,10 @@ describe("CLI", () => {
] as const;
for (const scenario of scenarios) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
[
"scan",
".",
@@ -3153,8 +2977,6 @@ describe("CLI", () => {
"--verbose",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies(),
),
).toBe(0);
@@ -3177,14 +2999,11 @@ describe("CLI", () => {
});
test("reports saved model and reasoning effort for verbose scan reruns", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["scans", "rerun", "scan-original", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
onWorkbench: () =>
savedRecipe({
@@ -3205,14 +3024,11 @@ describe("CLI", () => {
});
test("reports selected profile settings for verbose scan reruns", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["scans", "rerun", "scan-original"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: { CODEX_SECURITY_LOG_LEVEL: "debug" },
onWorkbench: () =>
@@ -3276,10 +3092,9 @@ describe("CLI", () => {
let rerunConfig: CodexSecurityConfig | undefined;
expect(
- await main(
+ await runCapturedCli(
+ main,
["scans", "rerun", "scan-original", "--json"],
- capture().stream,
- capture().stream,
dependencies({
environment,
onConfig: (value) => (rerunConfig = value),
@@ -3297,14 +3112,11 @@ describe("CLI", () => {
);
test("enables verbose diagnostics through CODEX_SECURITY_LOG_LEVEL", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["scan", ".", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: { CODEX_SECURITY_LOG_LEVEL: " DeBuG " },
}),
@@ -3319,14 +3131,11 @@ describe("CLI", () => {
});
test("accepts Promptfoo-compatible LOG_LEVEL as a verbose fallback", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["scan", ".", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: {
CODEX_SECURITY_LOG_LEVEL: " ",
@@ -3343,14 +3152,11 @@ describe("CLI", () => {
});
test("prefers CODEX_SECURITY_LOG_LEVEL over a shared LOG_LEVEL", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["scan", ".", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: {
CODEX_SECURITY_LOG_LEVEL: "info",
@@ -3364,14 +3170,11 @@ describe("CLI", () => {
});
test("lets --verbose override non-debug environment log levels", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: {
CODEX_SECURITY_LOG_LEVEL: "error",
@@ -3388,24 +3191,16 @@ describe("CLI", () => {
});
test("does not emit verbose diagnostics unless explicitly requested", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
- expect(
- await main(
- ["scan", ".", "--json"],
- stdout.stream,
- stderr.stream,
- dependencies(),
- ),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--json"], dependencies())).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).not.toContain("codex-security: debug:");
});
test("keeps verbose dry-run authentication unverified without starting a scan", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const onRun = mock();
const preflight = {
...fakePreflight("."),
@@ -3417,10 +3212,8 @@ describe("CLI", () => {
};
expect(
- await main(
+ await runCli(
["scan", ".", "--dry-run", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
environment: {
OPENAI_API_KEY: "sk-proj-SYNTHETIC_DRY_RUN_SECRET_123",
@@ -3473,11 +3266,10 @@ describe("CLI", () => {
] as const;
for (const scenario of scenarios) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
[
"scan",
".",
@@ -3486,8 +3278,6 @@ describe("CLI", () => {
"--json",
...scenario.overrides.flatMap((override) => ["--codex", override]),
],
- stdout.stream,
- stderr.stream,
dependencies(),
),
).toBe(0);
@@ -3514,8 +3304,8 @@ describe("CLI", () => {
});
test("classifies provider failures without including upstream context", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
const deps = dependencies({
environment: { OPENAI_API_KEY: "sk-proj-SYNTHETIC_VERBOSE_KEY_123" },
});
@@ -3524,14 +3314,7 @@ describe("CLI", () => {
"401 invalid API key for org-private sk-proj-SYNTHETIC_PROVIDER_SECRET_123",
);
- expect(
- await main(
- ["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--verbose", "--json"], deps)).toBe(2);
expect(stderr.text()).toContain(
'codex-security: debug: scan.failed classification="unauthorized"',
);
@@ -3542,22 +3325,15 @@ describe("CLI", () => {
});
test("keeps unclassified provider context out of structured failure diagnostics", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
failingSecurity(
"Provider failed for tenant=tenant-private request_id=req-internal",
);
- expect(
- await main(
- ["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--verbose", "--json"], deps)).toBe(2);
const failureDiagnostic = stderr
.text()
@@ -3649,16 +3425,14 @@ describe("CLI", () => {
for (const { message, identifiers } of cases) {
for (const verbose of [false, true]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () => failingSecurity(message);
expect(
- await main(
+ await runCli(
["scan", ".", "--json", ...(verbose ? ["--verbose"] : [])],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(2);
@@ -3676,8 +3450,8 @@ describe("CLI", () => {
test("preserves provider identifiers in scanner warnings", async () => {
for (const verbose of [false, true]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -3696,10 +3470,8 @@ describe("CLI", () => {
});
expect(
- await main(
+ await runCli(
["scan", ".", "--json", ...(verbose ? ["--verbose"] : [])],
- stdout.stream,
- stderr.stream,
deps,
),
).toBe(0);
@@ -3714,8 +3486,8 @@ describe("CLI", () => {
});
test("prevents Unicode line separators from forging verbose diagnostics", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
const separators = "\u0085\u2028\u2029";
deps.createSecurity = () =>
@@ -3729,14 +3501,7 @@ describe("CLI", () => {
return fakeResult();
});
- expect(
- await main(
- ["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--verbose", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).not.toMatch(/[\u0085\u2028\u2029]/u);
@@ -3758,8 +3523,8 @@ describe("CLI", () => {
});
test("preserves verbose output paths and observer errors", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -3776,14 +3541,7 @@ describe("CLI", () => {
return fakeResult();
});
- expect(
- await main(
- ["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--verbose", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(
'codex-security: debug: scan.output_archived archive_dir="/tmp/archive_sk-proj-SYNTHETIC_ARCHIVE_SECRET_123"',
@@ -3800,8 +3558,8 @@ describe("CLI", () => {
});
test("excludes observer failure context from verbose diagnostics", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -3814,14 +3572,7 @@ describe("CLI", () => {
return fakeResult();
});
- expect(
- await main(
- ["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--verbose", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
const observerDiagnostic = stderr
@@ -3842,14 +3593,11 @@ describe("CLI", () => {
});
test("excludes cleanup failure context from verbose diagnostics", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
onClose: () =>
fail(
@@ -3881,8 +3629,8 @@ describe("CLI", () => {
});
test("reports reconnect progress on stderr and keeps JSON output clean", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -3895,9 +3643,7 @@ describe("CLI", () => {
return fakeResult();
});
- expect(
- await main(["scan", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(
"Codex connection interrupted; retrying (2/5)",
@@ -3906,8 +3652,8 @@ describe("CLI", () => {
});
test("renders bounded rate-limit retry details without leaking provider context", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -3920,9 +3666,7 @@ describe("CLI", () => {
return fakeResult();
});
- expect(
- await main(["scan", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(
"Rate limit reached; retrying in 1.2s (2/5).",
@@ -3931,8 +3675,8 @@ describe("CLI", () => {
});
test("renders safe reconnect causes without forwarding provider messages", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -3942,9 +3686,7 @@ describe("CLI", () => {
return fakeResult();
});
- expect(
- await main(["scan", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", "--json"], deps)).toBe(0);
expect(stderr.text()).toContain("Network connection interrupted; retrying");
expect(stderr.text()).toContain("Authentication interrupted; retrying");
expect(stderr.text()).toContain("Model access interrupted; retrying");
@@ -3957,12 +3699,12 @@ describe("CLI", () => {
["403 model access denied for org-private", "model access"],
["429 rate limit reached for org-private", "rate limit"],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () => failingSecurity(message);
- expect(await main(["scan"], stdout.stream, stderr.stream, deps)).toBe(2);
+ expect(await runCli(["scan"], deps)).toBe(2);
expect(stderr.text()).toContain(expected);
expect(stderr.text()).not.toContain("org-private");
}
@@ -3975,19 +3717,12 @@ describe("CLI", () => {
["--format", "json"],
["--format", "jsonl"],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () => failingSecurity(message);
- expect(
- await main(
- ["scan", ".", ...formatArgs],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ expect(await runCli(["scan", ".", ...formatArgs], deps)).toBe(2);
expect(JSON.parse(stdout.text().trim())).toEqual({
status: "failed",
code: "SCAN_FAILED",
@@ -4004,18 +3739,13 @@ describe("CLI", () => {
["--format", "json"],
["--format", "jsonl"],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () => failingSecurity(message);
expect(
- await main(
- ["scan", ".", ...formatArgs, "--full-output"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
+ await runCli(["scan", ".", ...formatArgs, "--full-output"], deps),
).toBe(2);
const envelope = JSON.parse(stdout.text().trim());
expect(envelope.ok).toBe(false);
@@ -4026,20 +3756,15 @@ describe("CLI", () => {
});
test("preserves diagnostic text in full-output scan failures", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
failingSecurity(`network failure ECONNRESET ${SYNTHETIC_CREDENTIALS}`);
- expect(
- await main(
- ["scan", ".", "--json", "--full-output"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--json", "--full-output"], deps)).toBe(
+ 2,
+ );
expect(JSON.parse(stdout.text()).error).toEqual({
code: "SCAN_FAILED",
message: `network failure ECONNRESET ${SYNTHETIC_CREDENTIALS}`,
@@ -4057,14 +3782,12 @@ describe("CLI", () => {
"request timed out while reading the scanner response.",
"Local scan failed: project_directory=/tmp/project tenant_count=2 request_index=3.",
]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () => failingSecurity(message);
- expect(
- await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--json"], deps)).toBe(2);
expect(JSON.parse(stdout.text())).toEqual({
status: "failed",
code: "SCAN_FAILED",
@@ -4121,20 +3844,13 @@ describe("CLI", () => {
];
for (const [, failure] of failures) {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity((Promise.reject<never>).bind(Promise, failure));
- expect(
- await main(
- ["scan", ".", "--verbose"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--verbose"], deps)).toBe(2);
expect(stderr.text()).toContain((failure as Error).message);
expect(stderr.text()).toContain('scan.failed classification="local"');
expect(stderr.text()).not.toContain("cannot access the configured model");
@@ -4153,28 +3869,24 @@ describe("CLI", () => {
"cannot access the configured model",
],
] as const) {
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
const deps = dependencies();
deps.createSecurity = () => failingSecurity(detail);
- expect(
- await main(["scan", "."], capture().stream, stderr.stream, deps),
- ).toBe(2);
+ expect(await stderr.run(["scan", "."], deps)).toBe(2);
expect(stderr.text()).toContain(expected);
expect(stderr.text()).not.toContain("org-private");
}
});
test("preserves diagnostic text in underlying network errors", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
failingSecurity(`network failure ECONNRESET ${SYNTHETIC_CREDENTIALS}`);
- expect(
- await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--json"], deps)).toBe(2);
expect(JSON.parse(stdout.text())).toEqual({
status: "failed",
code: "SCAN_FAILED",
@@ -4187,8 +3899,8 @@ describe("CLI", () => {
});
test("reports database connection failures without claiming the model network failed", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
failingSecurity(
@@ -4200,7 +3912,7 @@ describe("CLI", () => {
].join("\n"),
);
- expect(await main(["scan"], stdout.stream, stderr.stream, deps)).toBe(2);
+ expect(await runCli(["scan"], deps)).toBe(2);
expect(stderr.text()).toContain("unable to open database file");
expect(stderr.text()).not.toContain("model service could not be reached");
expect(stderr.text()).not.toContain("Check your network connection");
@@ -4210,8 +3922,8 @@ describe("CLI", () => {
});
test("prints only the completion summary for default scans", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = fakeResult(["high"], "complete", {
input_tokens: 1_250,
cached_input_tokens: 200,
@@ -4220,14 +3932,7 @@ describe("CLI", () => {
result.manifest.scan.id = "12345678-abcd-4567-abcd-1234567890ab";
result.manifest.scan.completedAt = "2026-01-01T00:06:37Z";
- expect(
- await main(
- ["scan"],
- stdout.stream,
- stderr.stream,
- dependencies({ result }),
- ),
- ).toBe(0);
+ expect(await runCli(["scan"], dependencies({ result }))).toBe(0);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain("Scan complete · 12345678");
expect(stderr.text()).not.toContain(result.manifest.scan.id);
@@ -4252,17 +3957,12 @@ describe("CLI", () => {
[{ NO_COLOR: "1" }, false],
[{ TERM: "dumb" }, false],
] as const) {
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
const result = fakeResult(["medium"]);
expect(
- await main(
- ["scan"],
- stdout.stream,
- stderr.stream,
- dependencies({ environment, result }),
- ),
+ await runCli(["scan"], dependencies({ environment, result })),
).toBe(0);
if (color) {
@@ -4291,15 +3991,8 @@ describe("CLI", () => {
[["--format=yaml"], "manifest:"],
[["--full-output"], "manifest:"],
] as const) {
- const stdout = capture();
- expect(
- await main(
- ["scan", ...arguments_],
- stdout.stream,
- capture().stream,
- dependencies(),
- ),
- ).toBe(0);
+ const stdout = captureCli(main, "stdout");
+ expect(await stdout.run(["scan", ...arguments_], dependencies())).toBe(0);
expect(stdout.text()).toContain(marker);
}
});
@@ -4311,15 +4004,8 @@ describe("CLI", () => {
["--token-offset", "1"],
["--token-offset", "1", "--token-limit", "4"],
] as const) {
- const stdout = capture();
- expect(
- await main(
- ["scan", ...arguments_],
- stdout.stream,
- capture().stream,
- dependencies(),
- ),
- ).toBe(0);
+ const stdout = captureCli(main, "stdout");
+ expect(await stdout.run(["scan", ...arguments_], dependencies())).toBe(0);
if (arguments_[0] === "--token-count") {
expect(stdout.text().trim()).toMatch(/^\d+$/u);
expect(Number(stdout.text().trim())).toBeGreaterThan(0);
@@ -4339,14 +4025,12 @@ describe("CLI", () => {
"Repository HEAD changed while the scan was running; findings belong to the previous checkout.",
"Completed findings no longer describe the selected source tree.",
]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () => fakeSecurity(warningResult(warning, true));
- expect(
- await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--json"], deps)).toBe(2);
expect(JSON.parse(stdout.text())).toEqual({
...fakeResult().toJSON(),
warnings: [warning],
@@ -4363,22 +4047,20 @@ describe("CLI", () => {
"Recovered finding: normalized its semantic anchor.",
"Repository HEAD changed while the scan was running; informational retry recovered.",
]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () => fakeSecurity(warningResult(warning));
- expect(
- await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(`codex-security: warning: ${warning}`);
}
});
test("preserves scan warnings in verbose diagnostics", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(
@@ -4387,14 +4069,7 @@ describe("CLI", () => {
),
);
- expect(
- await main(
- ["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--verbose", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(
'codex-security: debug: scan.warning message="Repository HEAD changed during the scan: sk-proj-SYNTHETIC_WARNING_SECRET_123"',
@@ -4405,8 +4080,8 @@ describe("CLI", () => {
});
test("prints granted trusted cyber access without warning or corrupting JSON scans", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -4414,9 +4089,7 @@ describe("CLI", () => {
return fakeResult();
});
- expect(
- await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(
"codex-security: ✓ Your account has Trusted Access for Cyber.\n",
@@ -4425,8 +4098,8 @@ describe("CLI", () => {
});
test("prints trusted cyber access guidance without failing or corrupting JSON scans", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(
@@ -4435,9 +4108,7 @@ describe("CLI", () => {
),
);
- expect(
- await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(
"codex-security: warning: Some cybersecurity requests or findings may be refused because your account does not have Trusted Access for Cyber.",
@@ -4446,8 +4117,8 @@ describe("CLI", () => {
});
test("prints unverified trusted cyber access guidance without corrupting JSON scans", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(
@@ -4456,9 +4127,7 @@ describe("CLI", () => {
),
);
- expect(
- await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(
"codex-security: warning: Some cybersecurity requests or findings may be refused because your Trusted Access for Cyber status could not be verified.",
@@ -4473,14 +4142,12 @@ describe("CLI", () => {
"Some cybersecurity requests or findings may be refused because your API organization does not have Trusted Access for Cyber. Apply at https://openai.com/form/enterprise-trusted-access-for-cyber/.",
"Some cybersecurity requests or findings may be refused because Trusted Access for Cyber for your API organization could not be verified. Check your organization's access or apply at https://openai.com/form/enterprise-trusted-access-for-cyber/.",
]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () => fakeSecurity(warningResult(warning));
- expect(
- await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(`codex-security: warning: ${warning}\n`);
expect(stderr.text()).not.toContain("chatgpt.com/cyber");
@@ -4488,8 +4155,8 @@ describe("CLI", () => {
});
test("reports isolated observer failures without failing the scan", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
deps.createSecurity = () =>
fakeSecurity(async (_repository, options) => {
@@ -4500,9 +4167,7 @@ describe("CLI", () => {
return fakeResult();
});
- expect(
- await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),
- ).toBe(0);
+ expect(await runCli(["scan", ".", "--json"], deps)).toBe(0);
expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());
expect(stderr.text()).toContain(
`codex-security: warning: onWorkerStatus observer failed: status observer failed ${SYNTHETIC_CREDENTIALS}`,
@@ -4543,13 +4208,11 @@ describe("CLI", () => {
});
test("reports partial worker capacity on stderr without changing JSON stdout", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["scan", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
workerStatuses: [
{
@@ -4572,8 +4235,8 @@ describe("CLI", () => {
});
test("reports scoped scan phases and deduplicates repeated worker updates", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const status = {
kind: "dispatch",
phase: "file_review",
@@ -4582,10 +4245,8 @@ describe("CLI", () => {
} as const;
expect(
- await main(
+ await runCli(
["scan", ".", "--path", "src", "--path", "tests", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({ workerStatuses: [status, status] }),
),
).toBe(0);
@@ -4600,11 +4261,10 @@ describe("CLI", () => {
});
test("preserves directory names for absolute scan paths with trailing separators", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
expect(
- await main(
+ await runCli(
[
"scan",
".",
@@ -4612,8 +4272,6 @@ describe("CLI", () => {
"/synthetic-parent/trailing-directory/",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies(),
),
).toBe(0);
@@ -4623,8 +4281,8 @@ describe("CLI", () => {
});
test("shows live stage, files, workers, tokens, and opt-in cost without a budget", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = fakeResult([], "complete", {
input_tokens: 1_250,
cached_input_tokens: 200,
@@ -4632,10 +4290,8 @@ describe("CLI", () => {
});
expect(
- await main(
+ await runCli(
["scan", ".", "--show-cost", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
result,
costUpdates: [result.cost!],
@@ -4661,8 +4317,8 @@ describe("CLI", () => {
});
test("deduplicates live file progress and reports later scan phases", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const discovery = {
phase: "discovery",
filesCompleted: 8,
@@ -4670,10 +4326,8 @@ describe("CLI", () => {
} as const;
expect(
- await main(
+ await runCli(
["scan", ".", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
scanProgress: [
discovery,
@@ -4695,8 +4349,8 @@ describe("CLI", () => {
});
test("prints a truthful completion summary without changing JSON results", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = fakeResult(
["critical", "high", "high", "informational"],
"complete",
@@ -4708,10 +4362,8 @@ describe("CLI", () => {
);
expect(
- await main(
+ await runCli(
["scan", ".", "--show-cost", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
result,
workerStatuses: [
@@ -4775,8 +4427,8 @@ describe("CLI", () => {
);
test("reports a cost range while preserving the short-context scan budget", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = fakeResult([], "complete", {
input_tokens: 1_250,
cached_input_tokens: 200,
@@ -4784,10 +4436,8 @@ describe("CLI", () => {
});
expect(
- await main(
+ await runCli(
["scan", ".", "--verbose", "--json", "--max-cost", "0.01"],
- stdout.stream,
- stderr.stream,
dependencies({ result, costUpdates: [result.cost!] }),
),
).toBe(0);
@@ -4802,8 +4452,8 @@ describe("CLI", () => {
});
test("includes cache-write tokens in verbose cost diagnostics", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const result = fakeResult([], "complete", {
input_tokens: 200,
cache_write_input_tokens: 200,
@@ -4813,10 +4463,8 @@ describe("CLI", () => {
expect(result.cost?.cacheWriteInputTokens).toBe(200);
expect(result.cost?.estimatedUsd).toBeGreaterThan(0);
expect(
- await main(
+ await runCli(
["scan", ".", "--verbose", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({ result, costUpdates: [result.cost!] }),
),
).toBe(0);
@@ -4828,8 +4476,8 @@ describe("CLI", () => {
});
test("reports and classifies a scan stopped when its live cost exceeds the limit", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const cost = fakeResult([], "complete", {
input_tokens: 1_250,
cached_input_tokens: 200,
@@ -4837,10 +4485,8 @@ describe("CLI", () => {
}).cost!;
expect(
- await main(
+ await runCli(
["scan", ".", "--verbose", "--json", "--max-cost", "0.004"],
- stdout.stream,
- stderr.stream,
dependencies({
onTurn: (_repository, options) => {
options.onOutputDirReady?.("/tmp/scan");
@@ -4866,7 +4512,7 @@ describe("CLI", () => {
});
test("accepts a scan at its estimated cost limit", async () => {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
const result = fakeResult([], "complete", {
input_tokens: 1_250,
cached_input_tokens: 200,
@@ -4874,10 +4520,8 @@ describe("CLI", () => {
});
expect(
- await main(
+ await stdout.run(
["scan", ".", "--json", "--max-cost", "0.00488"],
- stdout.stream,
- capture().stream,
dependencies({ result }),
),
).toBe(0);
@@ -4885,15 +4529,15 @@ describe("CLI", () => {
});
test("preserves scan progress scope and completion paths", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stderr, runCli } = createCliTest(main);
+
const result = fakeResult();
Object.defineProperty(result, "scanDir", {
value: "/tmp/scan_sk-proj-SYNTHETIC_OUTPUT_KEY_123",
});
expect(
- await main(
+ await runCli(
[
"scan",
".",
@@ -4901,8 +4545,6 @@ describe("CLI", () => {
"src/sk-proj-SYNTHETIC_SCOPE_KEY_123",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies({ result }),
),
).toBe(0);
@@ -4913,13 +4555,11 @@ describe("CLI", () => {
});
test("reports parent fallback when delegated workers cannot start", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["scan"],
- stdout.stream,
- stderr.stream,
dependencies({
workerStatuses: [
{
@@ -4948,14 +4588,12 @@ describe("CLI", () => {
});
test("validates a dry run without starting a scan", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const onRun = mock();
expect(
- await main(
+ await runCli(
["scan", "repo", "--dry-run"],
- stdout.stream,
- stderr.stream,
dependencies({
onRun,
}),
@@ -4971,15 +4609,10 @@ describe("CLI", () => {
});
test("emits a machine-readable dry-run plan", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
- ["scan", "repo", "--dry-run", "--json"],
- stdout.stream,
- stderr.stream,
- dependencies(),
- ),
+ await runCli(["scan", "repo", "--dry-run", "--json"], dependencies()),
).toBe(0);
expect(JSON.parse(stdout.text())).toEqual({
dryRun: true,
@@ -4994,13 +4627,11 @@ describe("CLI", () => {
["yaml", "dryRun: true"],
["jsonl", '"dryRun":true'],
] as const) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["scan", "repo", "--dry-run", "--format", format],
- stdout.stream,
- stderr.stream,
dependencies(),
),
).toBe(0);
@@ -5008,12 +4639,10 @@ describe("CLI", () => {
expect(stderr.text()).not.toContain("Running scan");
}
- const full = capture();
+ const full = captureCli(main, "stdout");
expect(
- await main(
+ await full.run(
["scan", "repo", "--dry-run", "--full-output"],
- full.stream,
- capture().stream,
dependencies(),
),
).toBe(0);
@@ -5022,15 +4651,15 @@ describe("CLI", () => {
});
test("previews an existing output archive during a dry run", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const preflight: ScanPreflight = {
...fakePreflight("repo"),
outputDir: "/tmp/results",
archiveDir: "/tmp/results.previous-20260721T031422-1234abcd",
};
expect(
- await main(
+ await runCli(
[
"scan",
"repo",
@@ -5039,8 +4668,6 @@ describe("CLI", () => {
"--archive-existing",
"--dry-run",
],
- stdout.stream,
- stderr.stream,
dependencies({ preflight }),
),
).toBe(0);
@@ -5051,10 +4678,10 @@ describe("CLI", () => {
});
test("keeps original archive notices on stderr for JSON scans", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"scan",
"repo",
@@ -5063,8 +4690,6 @@ describe("CLI", () => {
"--archive-existing",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies({
onTurn: (_repository, options) => {
expect(options).toMatchObject({
@@ -5096,13 +4721,11 @@ describe("CLI", () => {
"low",
"informational",
]);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["scan", "--json", "--fail-on-severity", "high"],
- stdout.stream,
- stderr.stream,
dependencies({ result }),
),
).toBe(1);
@@ -5114,12 +4737,10 @@ describe("CLI", () => {
["scan", "--json"],
["scan", "--json", "--fail-on-severity", "high"],
]) {
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
arguments_,
- stdout.stream,
- capture().stream,
dependencies({ result: fakeResult(["medium", "low"]) }),
),
).toBe(0);
@@ -5131,12 +4752,10 @@ describe("CLI", () => {
test("keeps JSON output complete when findings block", async () => {
const result = fakeResult(["high"]);
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
["scan", "--json", "--fail-on-severity", "high"],
- stdout.stream,
- capture().stream,
dependencies({ result }),
),
).toBe(1);
@@ -5146,13 +4765,11 @@ describe("CLI", () => {
test("does not pass a policy when coverage is incomplete", async () => {
for (const completeness of ["partial", "unknown"] as const) {
const result = fakeResult(["high"], completeness);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["scan", "--json", "--fail-on-severity", "critical"],
- stdout.stream,
- stderr.stream,
dependencies({ result }),
),
).toBe(2);
@@ -5166,16 +4783,11 @@ describe("CLI", () => {
test("does not report an incomplete scan as successful without a policy", async () => {
for (const completeness of ["partial", "unknown"] as const) {
const result = fakeResult(["high"], completeness);
- const stdout = capture();
- const stderr = capture();
- expect(
- await main(
- ["scan", "--json"],
- stdout.stream,
- stderr.stream,
- dependencies({ result }),
- ),
- ).toBe(2);
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ expect(await runCli(["scan", "--json"], dependencies({ result }))).toBe(
+ 2,
+ );
expect(JSON.parse(stdout.text())).toEqual(result.toJSON());
expect(stderr.text()).toContain(
`Scan coverage is ${completeness}; results may be incomplete.`,
@@ -5184,13 +4796,11 @@ describe("CLI", () => {
});
test("reports SDK errors without a stack trace", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const failing = dependencies();
failing.createSecurity = () => failingSecurity("invalid scan request");
- expect(
- await main(["scan", "."], stdout.stream, stderr.stream, failing),
- ).toBe(2);
+ expect(await runCli(["scan", "."], failing)).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain("invalid scan request\n");
expect(stderr.text()).not.toContain("Running scan");
@@ -5198,18 +4808,11 @@ describe("CLI", () => {
});
test("does not emit a successful full-output envelope for a failed scan", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const failing = dependencies();
failing.createSecurity = () => failingSecurity("invalid scan request");
- expect(
- await main(
- ["scan", ".", "--full-output"],
- stdout.stream,
- stderr.stream,
- failing,
- ),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--full-output"], failing)).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain("invalid scan request\n");
@@ -5237,8 +4840,8 @@ describe("CLI", () => {
const output = join(worktree, "scan");
const suggestion = join(root, "worktree-codex-security-scan");
await mkdir(repository, { recursive: true });
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const failing = dependencies();
failing.createSecurity = () => ({
run: async () => {
@@ -5250,10 +4853,8 @@ describe("CLI", () => {
try {
expect(
- await main(
+ await runCli(
["scan", repository, "--output-dir", output, "--json"],
- stdout.stream,
- stderr.stream,
failing,
),
).toBe(2);
@@ -5287,8 +4888,8 @@ describe("CLI", () => {
const worktree = join(root, "worktree");
const temporary = join(worktree, "tmp");
await mkdir(temporary, { recursive: true });
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const failing = dependencies();
failing.createSecurity = () => ({
run: async () => {
@@ -5303,9 +4904,7 @@ describe("CLI", () => {
});
try {
- expect(
- await main(["scan", worktree], stdout.stream, stderr.stream, failing),
- ).toBe(2);
+ expect(await runCli(["scan", worktree], failing)).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain(
"Temporary directory must be outside the scanned directory and any enclosing Git worktree.",
@@ -5319,8 +4918,8 @@ describe("CLI", () => {
});
test("preserves partial-output guidance for a late protected-root failure", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const partial = "/tmp/codex-security-partial";
const failing = dependencies();
failing.createSecurity = () =>
@@ -5333,9 +4932,7 @@ describe("CLI", () => {
);
});
- expect(
- await main(["scan", "."], stdout.stream, stderr.stream, failing),
- ).toBe(2);
+ expect(await runCli(["scan", "."], failing)).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain(
"Isolated Codex runtime directory must be outside the scanned directory and any enclosing Git worktree.",
@@ -5345,8 +4942,8 @@ describe("CLI", () => {
});
test("preserves complete protected-root diagnostics", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const protectedRoot =
"/private/tmp/worktree_sk-proj-SYNTHETIC_ROOT_KEY_123";
const output = `${protectedRoot}/results_sk-proj-SYNTHETIC_OUTPUT_KEY_123`;
@@ -5356,14 +4953,7 @@ describe("CLI", () => {
throw new OutputInsideProtectedRootError(output, protectedRoot);
});
- expect(
- await main(
- ["scan", ".", "--json"],
- stdout.stream,
- stderr.stream,
- failing,
- ),
- ).toBe(2);
+ expect(await runCli(["scan", ".", "--json"], failing)).toBe(2);
expect(JSON.parse(stdout.text())).toMatchObject({
status: "failed",
code: "SCAN_FAILED",
@@ -5380,15 +4970,13 @@ describe("CLI", () => {
"/tmp/scan",
),
]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const failing = dependencies();
failing.createSecurity = () =>
fakeSecurity((Promise.reject<never>).bind(Promise, failure));
- expect(
- await main(["scan", "."], stdout.stream, stderr.stream, failing),
- ).toBe(2);
+ expect(await runCli(["scan", "."], failing)).toBe(2);
expect(stdout.text()).toBe("");
expect(stderr.text()).toBe(
`[00:00] Preparing scan\nscan failed ${SYNTHETIC_CREDENTIALS}\n`,
@@ -5404,8 +4992,8 @@ describe("CLI", () => {
["SIGTERM", 143],
] as const) {
const signals = new FakeSignals();
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies({
signals,
onTurn: (_repository, options) => {
@@ -5419,9 +5007,7 @@ describe("CLI", () => {
},
});
- expect(
- await main(["scan", "."], stdout.stream, stderr.stream, deps),
- ).toBe(expectedExit);
+ expect(await runCli(["scan", "."], deps)).toBe(expectedExit);
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain(`Partial output was kept at ${path}.`);
}
@@ -5429,13 +5015,11 @@ describe("CLI", () => {
test("does not report success when SDK cleanup fails", async () => {
for (const json of [false, true]) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
json ? ["scan", ".", "--json"] : ["scan", "."],
- stdout.stream,
- stderr.stream,
dependencies({
onClose: () => fail("SYNTHETIC_AUTH_HOME_CLEANUP_FAILED"),
}),
@@ -5456,13 +5040,11 @@ describe("CLI", () => {
});
test("preserves the original scan failure when SDK cleanup also fails", async () => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
["scan", "."],
- stdout.stream,
- stderr.stream,
dependencies({
onRun: () => fail("SYNTHETIC_ORIGINAL_SCAN_FAILED"),
onClose: () => fail("SYNTHETIC_AUTH_HOME_CLEANUP_FAILED"),
diff --git a/sdk/typescript/tests-ts/component-scan.test.ts b/sdk/typescript/tests-ts/component-scan.test.ts
index a6419d777..597b689b1 100644
--- a/sdk/typescript/tests-ts/component-scan.test.ts
+++ b/sdk/typescript/tests-ts/component-scan.test.ts
@@ -1,3 +1,4 @@
+import { createCliTest } from "./support/cli-run.js";
import { gitText } from "./support/shell.js";
import { resolving } from "./support/promises.js";
import { execFileSync } from "node:child_process";
@@ -39,7 +40,6 @@ import {
type ScanComparisonResult,
} from "../src/scan-comparison.js";
import {
- capture,
dependencies,
fakePreflight,
fakeResult,
@@ -183,9 +183,9 @@ async function cli(
args: string[],
overrides: Partial<ReturnType<typeof dependencies>> = {},
) {
- const stdout = capture();
- const stderr = capture();
- const code = await main(
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(
[
"scan-components",
paths.repository,
@@ -194,8 +194,6 @@ async function cli(
...args,
"--json",
],
- stdout.stream,
- stderr.stream,
{ ...dependencies({ currentDirectory: paths.root }), ...overrides },
);
return { code, stdout: stdout.text(), stderr: stderr.text() };
@@ -545,10 +543,10 @@ test.each([
"CLI component presentation: %s, flags: %j",
async (presentation, costFlags) => {
const paths = await fixture();
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
const signals = new FakeSignals();
- const code = await main(
+ const code = await runCli(
[
"scan-components",
paths.repository,
@@ -560,8 +558,6 @@ test.each([
...costFlags,
"--json",
],
- stdout.stream,
- stderr.stream,
{
...dependencies({
currentDirectory: paths.root,
@@ -625,10 +621,10 @@ test.each([
test("CLI escapes component failure controls while preserving the saved error", async () => {
const paths = await fixture();
const failure = "Component failed: token=SYNTHETIC_VALUE\u001b[2J\ncontinued";
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[
"scan-components",
paths.repository,
@@ -638,8 +634,6 @@ test("CLI escapes component failure controls while preserving the saved error",
paths.outputDir,
"--json",
],
- stdout.stream,
- stderr.stream,
{
...dependencies({ currentDirectory: paths.root }),
createSecurity: client(rejecting(failure)),
@@ -658,10 +652,10 @@ test("CLI escapes component failure controls while preserving the saved error",
test("CLI restores the dashboard and reports saved partial results on cancellation", async () => {
const paths = await fixture();
- const stdout = capture();
- const stderr = capture(true);
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+
const signals = new FakeSignals();
- const code = await main(
+ const code = await runCli(
[
"scan-components",
paths.repository,
@@ -675,8 +669,6 @@ test("CLI restores the dashboard and reports saved partial results on cancellati
paths.outputDir,
"--json",
],
- stdout.stream,
- stderr.stream,
{
...dependencies({ currentDirectory: paths.root, signals }),
createSecurity: client(async (_repository, options) => {
diff --git a/sdk/typescript/tests-ts/github.test.ts b/sdk/typescript/tests-ts/github.test.ts
index 120c2b12c..1695e354e 100644
--- a/sdk/typescript/tests-ts/github.test.ts
+++ b/sdk/typescript/tests-ts/github.test.ts
@@ -1,3 +1,8 @@
+import {
+ createCliTest,
+ runCapturedCli,
+ captureCli,
+} from "./support/cli-run.js";
import { resolving } from "./support/promises.js";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
@@ -11,7 +16,7 @@ import {
importGitHubCodeScanningAlerts,
type GitHubCodeScanningImportOptions,
} from "../src/github.js";
-import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";
+import { dependencies, FakeSignals } from "./cli-fixtures.js";
import type { OnCodex } from "./cli-fixtures.js";
import { throwing } from "./support/errors.js";
@@ -303,18 +308,16 @@ describe("GitHub import CLI", () => {
test("emits complete JSON for file validation without starting Codex during import", async () => {
const root = await mkdtemp(join(tmpdir(), "codex-security-github-import-"));
try {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const imported = await importGitHubCodeScanningAlerts(
{ repository: "example/repository", alertNumbers: [42] },
{ createGitHub: async () => github(() => Response.json(alert(42))) },
);
const mustNotStartCodex = throwing("Import must not start Codex");
expect(
- await main(
+ await runCli(
["import", "github", "example/repository", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
importGitHubAlerts: async () => imported,
onCodex: mustNotStartCodex,
@@ -329,10 +332,9 @@ describe("GitHub import CLI", () => {
await writeFile(file, stdout.text());
const onCodex = mock<OnCodex>().mockReturnValue(0);
expect(
- await main(
+ await runCapturedCli(
+ main,
["validate", file],
- capture().stream,
- capture().stream,
dependencies({
currentDirectory: root,
onCodex,
@@ -348,9 +350,9 @@ describe("GitHub import CLI", () => {
test("routes repeated selectors, state, and ref and documents their schema", async () => {
let options: GitHubCodeScanningImportOptions | undefined;
- const stdout = capture();
+ const stdout = captureCli(main, "stdout");
expect(
- await main(
+ await stdout.run(
[
"import",
"github",
@@ -364,8 +366,6 @@ describe("GitHub import CLI", () => {
"--format",
"json",
],
- stdout.stream,
- capture().stream,
dependencies({
importGitHubAlerts: async (selected) => {
options = selected;
@@ -380,12 +380,10 @@ describe("GitHub import CLI", () => {
state: "open",
});
expect(JSON.parse(stdout.text())).toEqual([]);
- const schema = capture();
+ const schema = captureCli(main, "stdout");
expect(
- await main(
+ await schema.run(
["import", "github", "--schema", "--format", "json"],
- schema.stream,
- capture().stream,
dependencies(),
),
).toBe(0);
@@ -413,10 +411,9 @@ describe("GitHub import CLI", () => {
)("rejects invalid arguments %j", async (argv) => {
const importGitHubAlerts = mock(resolving([]));
expect(
- await main(
+ await runCapturedCli(
+ main,
argv,
- capture().stream,
- capture().stream,
dependencies({
importGitHubAlerts,
}),
@@ -427,12 +424,10 @@ describe("GitHub import CLI", () => {
test("returns cancellation and removes signal handlers", async () => {
const signals = new FakeSignals();
- const stderr = capture();
+ const stderr = captureCli(main, "stderr");
expect(
- await main(
+ await stderr.run(
["import", "github", "example/repository"],
- capture().stream,
- stderr.stream,
dependencies({
signals,
importGitHubAlerts: async ({ signal }) => {
diff --git a/sdk/typescript/tests-ts/multiscan.test.ts b/sdk/typescript/tests-ts/multiscan.test.ts
index 02e08a402..43f83c6a4 100644
--- a/sdk/typescript/tests-ts/multiscan.test.ts
+++ b/sdk/typescript/tests-ts/multiscan.test.ts
@@ -1,3 +1,4 @@
+import { createCliTest, captureCli } from "./support/cli-run.js";
import { gitText } from "./support/shell.js";
import { parseJsonLines, readJsonLines } from "./support/json.js";
import { resolving } from "./support/promises.js";
@@ -400,11 +401,11 @@ describe("multiscan", () => {
`id,repository,revision\nrepo,${source.path},${source.revision}\n`,
);
const failure = "Bulk failed: token=SYNTHETIC_VALUE\u001b[2J\ncontinued";
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies();
expect(
- await main(
+ await runCli(
[
"bulk-scan",
paths.input,
@@ -414,8 +415,6 @@ describe("multiscan", () => {
"1",
"--json",
],
- stdout.stream,
- stderr.stream,
{
...deps,
createSecurity: (config) => ({
@@ -677,13 +676,8 @@ describe("multiscan", () => {
["--recover"],
[paths.input, "--output-dir", paths.output, "--recover"],
]) {
- const error = capture();
- const code = await main(
- ["bulk-scan", ...args],
- capture().stream,
- error.stream,
- dependencies(),
- );
+ const error = captureCli(main, "stderr");
+ const code = await error.run(["bulk-scan", ...args], dependencies());
expect(code).toBe(2);
expect(error.text()).toMatch(/recovery requires/i);
}
@@ -860,12 +854,12 @@ describe("multiscan", () => {
paths.input,
`id,repository,revision\nsample,${source.path},${source.revision}\n`,
);
- const stdout = capture();
- const stderr = capture();
+ const { runCli } = createCliTest(main);
+
let scanOptions: unknown;
expect(
- await main(
+ await runCli(
[
"bulk-scan",
"repositories.csv",
@@ -875,8 +869,6 @@ describe("multiscan", () => {
"12.5",
"--json",
],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: paths.root,
onTurn: (_repository, options) => (scanOptions = options),
@@ -885,12 +877,10 @@ describe("multiscan", () => {
).toBe(0);
expect(scanOptions).toMatchObject({ maxCostUsd: 12.5 });
- const invalid = capture();
+ const invalid = captureCli(main, "stderr");
expect(
- await main(
+ await invalid.run(
["bulk-scan", "--max-cost=0"],
- capture().stream,
- invalid.stream,
dependencies({ currentDirectory: paths.root }),
),
).toBe(2);
@@ -1222,8 +1212,8 @@ describe("multiscan", () => {
`id,repository,revision\nsample,${source.path},${source.revision}\n`,
);
const outputDir = join(paths.output, "artifacts", "sample", "attempt-1");
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const onRun = mock();
const arguments_ = [
"bulk-scan",
@@ -1251,14 +1241,7 @@ describe("multiscan", () => {
};
};
- expect(
- await main(
- arguments_,
- stdout.stream,
- stderr.stream,
- clientDependencies,
- ),
- ).toBe(2);
+ expect(await runCli(arguments_, clientDependencies)).toBe(2);
expect(onRun).toHaveBeenCalledTimes(1);
expect(JSON.parse(stdout.text())).toMatchObject({
total: 1,
diff --git a/sdk/typescript/tests-ts/publication-integration.test.ts b/sdk/typescript/tests-ts/publication-integration.test.ts
index 45f743cfd..61e496389 100644
--- a/sdk/typescript/tests-ts/publication-integration.test.ts
+++ b/sdk/typescript/tests-ts/publication-integration.test.ts
@@ -1,3 +1,8 @@
+import {
+ createCliTest,
+ captureCli,
+ runCapturedCli,
+} from "./support/cli-run.js";
import { setFindingIdentity, sha256 } from "./support/finding-identity.js";
import { parseJsonLines, jsonLines } from "./support/json.js";
import { nodeCommand } from "./support/shell.js";
@@ -31,7 +36,7 @@ import {
type PublishedScanIssue,
} from "../src/publish.js";
import { runWorkbench } from "../src/runtime.js";
-import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";
+import { dependencies, FakeSignals } from "./cli-fixtures.js";
import { execNodePython } from "./support/python-probe.js";
import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
@@ -403,14 +408,9 @@ describe("database-backed Linear publication integration", () => {
"--json",
];
const run = async (flags: string[] = []) => {
- const stdout = capture();
- const stderr = capture();
- const code = await main(
- [...command, ...flags],
- stdout.stream,
- stderr.stream,
- cli,
- );
+ const { stdout, runCli } = createCliTest(main);
+
+ const code = await runCli([...command, ...flags], cli);
return { code, result: JSON.parse(stdout.text()) as PublishScanResult };
};
@@ -432,11 +432,11 @@ describe("database-backed Linear publication integration", () => {
checkScanPublicationInternal(directory, options, {
environment: localEnvironment,
});
- const checkOutput = capture();
+ const checkOutput = captureCli(main, "stdout");
const database = join(completed.stateDirectory, "workbench.sqlite3");
const before = sha256(await readFile(database));
expect(
- await main(
+ await checkOutput.run(
[
"publish",
"check",
@@ -449,8 +449,6 @@ describe("database-backed Linear publication integration", () => {
OPTIONS.projectId,
"--json",
],
- checkOutput.stream,
- capture().stream,
checkCli,
),
).toBe(0);
@@ -505,8 +503,8 @@ describe("database-backed Linear publication integration", () => {
...completed.environment,
CODEX_SECURITY_LINEAR_API_KEY: key,
};
- const stdout = capture();
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main);
+
const created: string[] = [];
const cli = dependencies({ environment });
type LinearClient = ReturnType<
@@ -550,7 +548,7 @@ describe("database-backed Linear publication integration", () => {
});
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -561,8 +559,6 @@ describe("database-backed Linear publication integration", () => {
OPTIONS.teamId,
"--json",
],
- stdout.stream,
- stderr.stream,
cli,
),
).toBe(0);
@@ -595,8 +591,8 @@ describe("database-backed Linear publication integration", () => {
test("publishes 23 sealed findings through a durable handoff without Codex JSON", async () => {
const completed = await fixture(23);
const sealed = await artifactDigests(completed.scanDirectory);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const progress: PublishScanProgress[] = [];
const cli = dependencies({ environment: completed.environment });
let sdkResult: PublishScanResult | undefined;
@@ -685,7 +681,7 @@ describe("database-backed Linear publication integration", () => {
};
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -698,8 +694,6 @@ describe("database-backed Linear publication integration", () => {
OPTIONS.projectId,
"--json",
],
- stdout.stream,
- stderr.stream,
cli,
),
).toBe(0);
@@ -755,8 +749,8 @@ describe("database-backed Linear publication integration", () => {
test("retains team-only database-backed partial successes when a later batch fails", async () => {
const completed = await fixture(22);
const sealed = await artifactDigests(completed.scanDirectory);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const cli = dependencies({ environment: completed.environment });
cli.publishScan = async (directory, options) =>
await publishScanInternal(directory, options, {
@@ -791,7 +785,7 @@ describe("database-backed Linear publication integration", () => {
});
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -802,8 +796,6 @@ describe("database-backed Linear publication integration", () => {
OPTIONS.teamId,
"--json",
],
- stdout.stream,
- stderr.stream,
cli,
),
).toBe(2);
@@ -841,8 +833,8 @@ describe("database-backed Linear publication integration", () => {
test("keeps SQLite-backed Linear issues successful when their optional receipt cannot be saved", async () => {
const completed = await fixture(2);
const sealed = await artifactDigests(completed.scanDirectory);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const cli = dependencies({ environment: completed.environment });
let publicationAttempts = 0;
@@ -873,7 +865,7 @@ describe("database-backed Linear publication integration", () => {
});
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -886,8 +878,6 @@ describe("database-backed Linear publication integration", () => {
OPTIONS.projectId,
"--json",
],
- stdout.stream,
- stderr.stream,
cli,
),
).toBe(0);
@@ -933,8 +923,8 @@ describe("database-backed Linear publication integration", () => {
}
const storedBefore = storedPublications(completed);
const pending = completed.findings[Number(skipExisting)]!;
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const cli = dependencies({ environment: completed.environment });
let handoffFile = "";
let handoffLine = "";
@@ -984,7 +974,7 @@ describe("database-backed Linear publication integration", () => {
});
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -998,8 +988,6 @@ describe("database-backed Linear publication integration", () => {
...(skipExisting ? ["--skip-existing"] : []),
"--json",
],
- stdout.stream,
- stderr.stream,
cli,
),
).toBe(2);
@@ -1121,10 +1109,9 @@ describe("database-backed Linear publication integration", () => {
};
expect(
- await main(
+ await runCapturedCli(
+ main,
["publish", "scan", "completed-scan", "--to", "linear"],
- capture().stream,
- capture().stream,
cli,
),
).toBe(130);
@@ -1191,10 +1178,9 @@ for (;;) Atomics.wait(waiter, 0, 0, 1000);`,
try {
expect(
await Promise.race([
- main(
+ runCapturedCli(
+ main,
["publish", "scan", completed.scanDirectory, "--to", "linear"],
- capture().stream,
- capture().stream,
cli,
),
Bun.sleep(20_000).then(() => -1),
@@ -1228,8 +1214,8 @@ for (;;) Atomics.wait(waiter, 0, 0, 1000);`,
test("recovers verified SQLite publications before an interrupted CLI exits", async () => {
const completed = await fixture(3);
const sealed = await artifactDigests(completed.scanDirectory);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const signals = new FakeSignals();
const cli = dependencies({ environment: completed.environment, signals });
let handoffFile = "";
@@ -1292,7 +1278,7 @@ for (;;) Atomics.wait(waiter, 0, 0, 1000);`,
});
expect(
- await main(
+ await runCli(
[
"publish",
"scan",
@@ -1305,8 +1291,6 @@ for (;;) Atomics.wait(waiter, 0, 0, 1000);`,
OPTIONS.projectId,
"--json",
],
- stdout.stream,
- stderr.stream,
cli,
),
).toBe(130);
diff --git a/sdk/typescript/tests-ts/scan-matching-e2e.test.ts b/sdk/typescript/tests-ts/scan-matching-e2e.test.ts
index ed58b7847..5a089adf0 100644
--- a/sdk/typescript/tests-ts/scan-matching-e2e.test.ts
+++ b/sdk/typescript/tests-ts/scan-matching-e2e.test.ts
@@ -1,3 +1,4 @@
+import { createCliTest } from "./support/cli-run.js";
import { hash } from "node:crypto";
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
@@ -16,7 +17,7 @@ import {
type ScanComparisonResult,
type ScanMatchingBatch,
} from "../src/scan-comparison.js";
-import { capture, dependencies } from "./cli-fixtures.js";
+import { dependencies } from "./cli-fixtures.js";
import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";
import { temporaryDirectory } from "./support/temporary-directories.js";
import { readJson } from "./support/json.js";
@@ -350,13 +351,11 @@ test("matches sealed scan history end to end without merging related findings",
});
};
const cli = async (args: string[], matcher = onMatch) => {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
expect(
- await main(
+ await runCli(
[...args, "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
currentDirectory: repository,
environment,
diff --git a/sdk/typescript/tests-ts/scan-recipe.test.ts b/sdk/typescript/tests-ts/scan-recipe.test.ts
index cbb0ff69a..a250afcc8 100644
--- a/sdk/typescript/tests-ts/scan-recipe.test.ts
+++ b/sdk/typescript/tests-ts/scan-recipe.test.ts
@@ -1,10 +1,11 @@
+import { createCliTest } from "./support/cli-run.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
import { mkdir, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, expect, test } from "bun:test";
import { main } from "../src/cli.js";
import { runWorkbench } from "../src/runtime.js";
-import { capture, dependencies } from "./cli-fixtures.js";
+import { dependencies } from "./cli-fixtures.js";
import { TestClient } from "./support/api-client.js";
import { preparedRuntime } from "./support/api-events.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
@@ -44,9 +45,9 @@ test.each(["standard", "deep"])(
args,
input,
);
- const stdout = capture();
- const stderr = capture();
- const code = await main(
+ const { stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(
[
"scan",
repository,
@@ -58,8 +59,6 @@ test.each(["standard", "deep"])(
promptFile,
"--json",
],
- stdout.stream,
- stderr.stream,
{
...dependencies({ environment, currentDirectory: root }),
runWorkbench: command,
diff --git a/sdk/typescript/tests-ts/scan-resume.test.ts b/sdk/typescript/tests-ts/scan-resume.test.ts
index b28af9c23..c3a71693c 100644
--- a/sdk/typescript/tests-ts/scan-resume.test.ts
+++ b/sdk/typescript/tests-ts/scan-resume.test.ts
@@ -1,3 +1,4 @@
+import { createCliTest } from "./support/cli-run.js";
import { gitText } from "./support/shell.js";
import { readJsonLines } from "./support/json.js";
import { randomUUID } from "node:crypto";
@@ -333,57 +334,52 @@ test("CLI resumes the owning Codex thread and preserves running state on a trans
f.threadId,
]);
let resumedThread: string | undefined;
- const stdout = capture();
- const stderr = capture();
- const code = await main(
- ["scans", "resume", f.scanId, "--json"],
- stdout.stream,
- stderr.stream,
- {
- ...dependencies({ environment: f.environment, currentDirectory: f.root }),
- runWorkbench: f.command,
- createSecurity: (config) =>
- new TestClient(config, {
- environment: f.environment,
- prepareRuntime: async () => preparedRuntime(f.codexHome),
- resolvePluginPython: async () => f.python,
- runWorkbench,
- createCodex: (options) => ({
- startThread: () => fail("Resume must not create a new thread."),
- resumeThread(threadId, threadOptions) {
- resumedThread = threadId;
- expect(threadOptions.workingDirectory).toBe(f.scanDir);
- expect(options.env).toMatchObject({
- CODEX_SECURITY_SCAN_ID: f.scanId,
- CODEX_SECURITY_SCAN_DIR: f.scanDir,
- });
- return {
- id: threadId,
- async runStreamed(prompt) {
- expect(prompt).toContain(f.scanId);
- expect(prompt).toContain(
- "Keep the original scan instructions.",
- );
- const joined = await f.command([
- "begin-deep-scan",
- "--scan-id",
- f.scanId,
- "--thread-id",
- threadId,
- "--available-parallelism",
- "4",
- ]);
- expect(joined["deepScan"]).toMatchObject({
- scanId: f.scanId,
- });
- throw new Error("Synthetic transport disconnected");
- },
- };
- },
- }),
+ const { stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(["scans", "resume", f.scanId, "--json"], {
+ ...dependencies({ environment: f.environment, currentDirectory: f.root }),
+ runWorkbench: f.command,
+ createSecurity: (config) =>
+ new TestClient(config, {
+ environment: f.environment,
+ prepareRuntime: async () => preparedRuntime(f.codexHome),
+ resolvePluginPython: async () => f.python,
+ runWorkbench,
+ createCodex: (options) => ({
+ startThread: () => fail("Resume must not create a new thread."),
+ resumeThread(threadId, threadOptions) {
+ resumedThread = threadId;
+ expect(threadOptions.workingDirectory).toBe(f.scanDir);
+ expect(options.env).toMatchObject({
+ CODEX_SECURITY_SCAN_ID: f.scanId,
+ CODEX_SECURITY_SCAN_DIR: f.scanDir,
+ });
+ return {
+ id: threadId,
+ async runStreamed(prompt) {
+ expect(prompt).toContain(f.scanId);
+ expect(prompt).toContain(
+ "Keep the original scan instructions.",
+ );
+ const joined = await f.command([
+ "begin-deep-scan",
+ "--scan-id",
+ f.scanId,
+ "--thread-id",
+ threadId,
+ "--available-parallelism",
+ "4",
+ ]);
+ expect(joined["deepScan"]).toMatchObject({
+ scanId: f.scanId,
+ });
+ throw new Error("Synthetic transport disconnected");
+ },
+ };
+ },
}),
- },
- );
+ }),
+ });
expect(stderr.text()).toContain("Synthetic transport disconnected");
expect(code).not.toBe(0);
expect(resumedThread).toBe(f.threadId);
@@ -404,41 +400,34 @@ test("CLI resumes the owning Codex thread and preserves running state on a trans
test("resumed Bedrock scans retain provider context for the account advisory", async () => {
const f = await interruptedScan("deep", false, {}, false, "amazon-bedrock");
- const stdout = capture();
- const stderr = capture();
- const code = await main(
- ["scans", "resume", f.scanId, "--json"],
- stdout.stream,
- stderr.stream,
- {
- ...dependencies({ environment: f.environment, currentDirectory: f.root }),
- runWorkbench: f.command,
- createSecurity: resumeClient(f, (options) => ({
- startThread() {
- throw new Error("Resume must not create a new thread.");
- },
- resumeThread(threadId) {
- expect(threadId).toBe(f.threadId);
- expect(options.env).toMatchObject({
- AWS_PROFILE: "synthetic-bedrock",
- });
- expect(options.apiKey).toBeUndefined();
- return {
- id: threadId,
- async runStreamed(prompt) {
- expect(prompt).toContain(
- "Amazon Bedrock with AWS authentication",
- );
- expect(prompt).toContain(
- "Skip the ChatGPT account Daybreak access advisory",
- );
- throw new Error("Resumed Bedrock prompt captured");
- },
- };
- },
- })),
- },
- );
+ const { stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(["scans", "resume", f.scanId, "--json"], {
+ ...dependencies({ environment: f.environment, currentDirectory: f.root }),
+ runWorkbench: f.command,
+ createSecurity: resumeClient(f, (options) => ({
+ startThread() {
+ throw new Error("Resume must not create a new thread.");
+ },
+ resumeThread(threadId) {
+ expect(threadId).toBe(f.threadId);
+ expect(options.env).toMatchObject({
+ AWS_PROFILE: "synthetic-bedrock",
+ });
+ expect(options.apiKey).toBeUndefined();
+ return {
+ id: threadId,
+ async runStreamed(prompt) {
+ expect(prompt).toContain("Amazon Bedrock with AWS authentication");
+ expect(prompt).toContain(
+ "Skip the ChatGPT account Daybreak access advisory",
+ );
+ throw new Error("Resumed Bedrock prompt captured");
+ },
+ };
+ },
+ })),
+ });
expect(code).not.toBe(0);
expect(stderr.text()).toContain("Resumed Bedrock prompt captured");
});
@@ -540,14 +529,12 @@ test.each([
},
}) + "\n",
);
- const stdout = capture();
- const stderr = capture();
- const code = await main(
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(
bulk
? ["bulk-scan", f.input, "--output-dir", f.root, "--recover", "--json"]
: ["scans", "resume", f.scanId, "--json"],
- stdout.stream,
- stderr.stream,
{
...dependencies({
environment: f.environment,
@@ -696,14 +683,12 @@ test.each([
const before = await f.command(["get-scan", "--scan-id", f.scanId]);
const rejected = !["single", "bulk"].includes(scenario);
let turns = 0;
- const stdout = capture();
- const stderr = capture();
- const code = await main(
+ const { stdout, stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(
scenario === "bulk"
? ["bulk-scan", f.input, "--output-dir", f.root, "--recover", "--json"]
: ["scans", "resume", f.scanId, "--json"],
- stdout.stream,
- stderr.stream,
{
...dependencies({
environment: f.environment,
@@ -789,9 +774,9 @@ test.each(["chatgpt", "api-key"] as const)(
args,
input,
);
- const stdout = capture();
- const stderr = capture();
- const code = await main(
+ const { stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(
[
"scan",
repository,
@@ -808,8 +793,6 @@ test.each(["chatgpt", "api-key"] as const)(
promptFile,
"--json",
],
- stdout.stream,
- stderr.stream,
{
...dependencies({ environment, currentDirectory: root }),
runWorkbench: command,
@@ -878,14 +861,12 @@ test.each([
await mkdir(join(ambientHome, "codex-security"), { recursive: true });
await writeFile(ambientDeepConfig, "invalid ambient TOML [");
const prompts: string[] = [];
- const stdout = capture();
- const stderr = capture();
- const code = await main(
+ const { stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(
bulk
? ["bulk-scan", f.input, "--output-dir", f.root, "--recover", "--json"]
: ["scans", "resume", f.scanId, "--json"],
- stdout.stream,
- stderr.stream,
{
...dependencies({
environment: f.environment,
@@ -976,20 +957,15 @@ test.each([
test("missing session logs do not create another session or fail the original scan", async () => {
const f = await interruptedScan();
await rm(f.sessionPath);
- const stdout = capture();
- const stderr = capture();
- const code = await main(
- ["scans", "resume", f.scanId],
- stdout.stream,
- stderr.stream,
- {
- ...dependencies({ environment: f.environment, currentDirectory: f.root }),
- runWorkbench: f.command,
- createSecurity: resumeClient(f, () =>
- fail("Must not invoke Codex without the original session"),
- ),
- },
- );
+ const { stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(["scans", "resume", f.scanId], {
+ ...dependencies({ environment: f.environment, currentDirectory: f.root }),
+ runWorkbench: f.command,
+ createSecurity: resumeClient(f, () =>
+ fail("Must not invoke Codex without the original session"),
+ ),
+ });
expect(code).not.toBe(0);
expect(stderr.text()).toContain("original Codex session");
expect(
@@ -1016,18 +992,16 @@ test.each(["failed", "missing-checkout", "missing-session", "standard"])(
await rm(f.repository, { recursive: true });
if (scenario === "missing-session") await rm(f.sessionPath);
const before = await f.command(["get-scan", "--scan-id", f.scanId]);
- const stdout = capture();
- const stderr = capture();
+ const { stdout, stderr, runCli } = createCliTest(main);
+
const deps = dependencies({
environment: f.environment,
currentDirectory: f.root,
});
let attempts = 0;
expect(
- await main(
+ await runCli(
["bulk-scan", f.input, "--output-dir", f.root, "--recover", "--json"],
- stdout.stream,
- stderr.stream,
{
...deps,
runWorkbench: f.command,
@@ -1073,9 +1047,9 @@ test.each(["failed", "missing-checkout", "missing-session", "standard"])(
);
test("resume requires an explicit scan ID", async () => {
- const stdout = capture();
- const stderr = capture();
- const code = await main(["scans", "resume"], stdout.stream, stderr.stream, {
+ const { stderr, runCli } = createCliTest(main);
+
+ const code = await runCli(["scans", "resume"], {
...dependencies(),
runWorkbench: async () => fail("Must select a scan explicitly"),
});
diff --git a/sdk/typescript/tests-ts/sdk-project-config.test.ts b/sdk/typescript/tests-ts/sdk-project-config.test.ts
index 50bcef0bf..6b45723e2 100644
--- a/sdk/typescript/tests-ts/sdk-project-config.test.ts
+++ b/sdk/typescript/tests-ts/sdk-project-config.test.ts
@@ -1,3 +1,4 @@
+import { createCliTest } from "./support/cli-run.js";
import { mkdir, stat, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, expect, test } from "bun:test";
@@ -11,7 +12,7 @@ import {
type ScanSettings,
} from "../src/index.js";
import { main } from "../src/cli.js";
-import { capture, dependencies } from "./cli-fixtures.js";
+import { dependencies } from "./cli-fixtures.js";
import { TestClient } from "./support/api-client.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
import { rejecting } from "./support/errors.js";
@@ -158,8 +159,8 @@ test.each(["standard", "deep"] as const)(
: []),
]);
for (const command of commands) {
- const stdout = capture();
- const stderr = capture();
+ const { stdout, runCli } = createCliTest(main);
+
const deps = dependencies({ currentDirectory: root, environment });
let selected: ScanOptions | undefined;
deps.createSecurity = (config) => {
@@ -173,14 +174,7 @@ test.each(["standard", "deep"] as const)(
},
};
};
- expect(
- await main(
- [...command, "--dry-run", "--json"],
- stdout.stream,
- stderr.stream,
- deps,
- ),
- ).toBe(0);
+ expect(await runCli([...command, "--dry-run", "--json"], deps)).toBe(0);
expect(selected).toMatchObject({
failureSeverity: "high",
scanPrompt: "Synthetic instructions.",
diff --git a/sdk/typescript/tests-ts/sdk-scan-prompts.test.ts b/sdk/typescript/tests-ts/sdk-scan-prompts.test.ts
index b0c2ec1a9..867417ba8 100644
--- a/sdk/typescript/tests-ts/sdk-scan-prompts.test.ts
+++ b/sdk/typescript/tests-ts/sdk-scan-prompts.test.ts
@@ -1,9 +1,10 @@
+import { captureCli } from "./support/cli-run.js";
import { mkdir, symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, expect, test, mock } from "bun:test";
import type { JsonObject } from "../src/index.js";
import { main } from "../src/cli.js";
-import { capture, dependencies } from "./cli-fixtures.js";
+import { dependencies } from "./cli-fixtures.js";
import { mockWorkbench, TestClient } from "./support/api-client.js";
import { completedCodex, preparedRuntime } from "./support/api-events.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
@@ -55,11 +56,9 @@ test.each([
expect(recipe).toBeDefined();
const requiresInstructions = scenario === "instructions";
const onRun = mock();
- const stderr = capture();
- const exit = await main(
+ const stderr = captureCli(main, "stderr");
+ const exit = await stderr.run(
["scans", "rerun", "saved", "--json"],
- capture().stream,
- stderr.stream,
dependencies({
currentDirectory: repository,
onWorkbench: async () => ({ recipe: recipe! }),
diff --git a/sdk/typescript/tests-ts/support/cli-run.ts b/sdk/typescript/tests-ts/support/cli-run.ts
new file mode 100644
index 000000000..9dc61bf49
--- /dev/null
+++ b/sdk/typescript/tests-ts/support/cli-run.ts
@@ -0,0 +1,43 @@
+import type { main as cliMain } from "../../src/cli.js";
+import { capture } from "../cli-fixtures.js";
+
+type Main = typeof cliMain;
+
+export function createCliTest(
+ main: Main,
+ {
+ stdout = false,
+ stderr = false,
+ }: { stdout?: boolean | null; stderr?: boolean | null } = {},
+) {
+ const output = capture(stdout);
+ const error = capture(stderr);
+ return {
+ stdout: output,
+ stderr: error,
+ runCli: (args: Parameters<Main>[0], dependencies: Parameters<Main>[3]) =>
+ main(args, output.stream, error.stream, dependencies),
+ };
+}
+
+export function captureCli(
+ main: Main,
+ channel: "stdout" | "stderr",
+ isTTY: boolean | null = false,
+) {
+ const output = capture(isTTY);
+ return Object.assign(output, {
+ run: (args: Parameters<Main>[0], dependencies: Parameters<Main>[3]) =>
+ channel === "stdout"
+ ? main(args, output.stream, capture().stream, dependencies)
+ : main(args, capture().stream, output.stream, dependencies),
+ });
+}
+
+export function runCapturedCli(
+ main: Main,
+ args: Parameters<Main>[0],
+ dependencies: Parameters<Main>[3],
+) {
+ return main(args, capture().stream, capture().stream, dependencies);
+}
diff --git a/sdk/typescript/tests-ts/update-notice.test.ts b/sdk/typescript/tests-ts/update-notice.test.ts
index 66e665b2b..e61508775 100644
--- a/sdk/typescript/tests-ts/update-notice.test.ts
+++ b/sdk/typescript/tests-ts/update-notice.test.ts
@@ -1,3 +1,4 @@
+import { createCliTest, runCapturedCli } from "./support/cli-run.js";
import { once } from "node:events";
import { resolving } from "./support/promises.js";
import { describe, expect, test, mock } from "bun:test";
@@ -213,8 +214,8 @@ describe("CLI update notice", () => {
});
test("prints the update banner to interactive stderr without changing JSON", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
const notice = {
currentVersion: "0.1.0",
latestVersion: "0.2.0",
@@ -222,10 +223,8 @@ describe("CLI update notice", () => {
};
expect(
- await main(
+ await runCli(
["info", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({ onUpdateCheck: async () => notice }),
),
).toBe(0);
@@ -237,15 +236,13 @@ describe("CLI update notice", () => {
});
test("finishes the command and aborts an unfinished update check", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+
const onUpdateCheck = mock(async (_signal: AbortSignal) => {
return await new Promise<undefined>(() => {});
});
- const result = await main(
+ const result = await runCli(
["info", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
onUpdateCheck,
}),
@@ -262,10 +259,9 @@ describe("CLI update notice", () => {
test("skips checks for noninteractive output, help, dry runs, and disabled notices", async () => {
const onUpdateCheck = mock(resolving(undefined));
- await main(
+ await runCapturedCli(
+ main,
["info", "--json"],
- capture().stream,
- capture().stream,
dependencies({ onUpdateCheck }),
);
for (const argv of [["--help"], ["--version"], ["scan", "--dry-run"]]) {
@@ -290,14 +286,11 @@ describe("CLI update notice", () => {
});
test("keeps commands successful when the update check fails", async () => {
- const stdout = capture();
- const stderr = capture(true);
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
expect(
- await main(
+ await runCli(
["info", "--json"],
- stdout.stream,
- stderr.stream,
dependencies({
onUpdateCheck: rejecting("registry unavailable"),
}),