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): reuse API and resumed-scan fixtures (#1394)

mldangelo-oai07429cf69631
diff --git a/sdk/typescript/tests-ts/api-events.test.ts b/sdk/typescript/tests-ts/api-events.test.ts
index d90e14063..fc3367719 100644
--- a/sdk/typescript/tests-ts/api-events.test.ts
+++ b/sdk/typescript/tests-ts/api-events.test.ts
@@ -23,6 +23,7 @@ import {
import {
collectObserverErrors,
completedEvents,
+ completedTurn,
runEvents,
type ScanObserverName,
} from "./support/api-events.js";
@@ -713,16 +714,7 @@ describe("one-shot scan events", () => {
text: "scan complete",
},
};
- yield {
- type: "turn.completed",
- usage: {
- input_tokens: 10,
- cached_input_tokens: 2,
- cache_write_input_tokens: 0,
- output_tokens: 3,
- reasoning_output_tokens: 1,
- },
- };
+ yield completedTurn();
} finally {
closed = true;
}
@@ -1178,16 +1170,7 @@ describe("one-shot scan events", () => {
status: "completed",
},
};
- yield {
- type: "turn.completed",
- usage: {
- input_tokens: 10,
- cached_input_tokens: 2,
- cache_write_input_tokens: 0,
- output_tokens: 3,
- reasoning_output_tokens: 1,
- },
- };
+ yield completedTurn();
}
await expect(
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index 596111608..ca34322e1 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -102,6 +102,17 @@ const EXAMPLE = join(PLUGIN_ROOT, "examples", "completed-scan");
const { cleanup, temporaryDirectory } = createApiTestFixtures();
afterEach(cleanup);
+function localClient(
+ config: ConstructorParameters<typeof TestClient>[0] = {},
+ environment: ConstructorParameters<typeof TestClient>[1]["environment"] = {},
+) {
+ const prepareRuntime = mock(rejecting("runtime should not initialize"));
+ return {
+ client: new TestClient(config, { environment, prepareRuntime }),
+ prepareRuntime,
+ };
+}
+
async function runtimeDirectories() {
const root = await temporaryDirectory();
const repository = join(root, "repository");
@@ -842,15 +853,11 @@ describe("CodexSecurity orchestration", () => {
const output = join(root, "scan");
await mkdir(repository, { mode: 0o700 });
await mkdir(source, { mode: 0o700 });
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
+ const { client, prepareRuntime } = localClient(
{ pythonPath: "/definitely/missing/python" },
{
- environment: {
- OPENAI_API_KEY: "must-not-be-used",
- CODEX_HOME: join(root, "ambient"),
- },
- prepareRuntime,
+ OPENAI_API_KEY: "must-not-be-used",
+ CODEX_HOME: join(root, "ambient"),
},
);
@@ -912,18 +919,14 @@ describe("CodexSecurity orchestration", () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
await mkdir(repository);
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
+ const { client, prepareRuntime } = localClient(
{
codexOverrides: {
model: "configured-model",
model_reasoning_effort: "high",
},
},
- {
- environment: { OPENAI_API_KEY: "must-not-be-used" },
- prepareRuntime,
- },
+ { OPENAI_API_KEY: "must-not-be-used" },
);
await expect(client.preflight(repository)).resolves.toMatchObject({
@@ -938,25 +941,19 @@ describe("CodexSecurity orchestration", () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
await mkdir(repository);
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = new TestClient(
- {
- codexOverrides: {
- profile: "review",
- model: "gpt-5.6-sol",
- model_reasoning_effort: "low",
- profiles: {
- review: {
- model: "gpt-5.6-terra",
- model_reasoning_effort: "high",
- },
+ const { client, prepareRuntime } = localClient({
+ codexOverrides: {
+ profile: "review",
+ model: "gpt-5.6-sol",
+ model_reasoning_effort: "low",
+ profiles: {
+ review: {
+ model: "gpt-5.6-terra",
+ model_reasoning_effort: "high",
},
},
},
- {
- prepareRuntime,
- },
- );
+ });
await expect(
client.preflight(repository, { maxCostUsd: 5 }),
@@ -973,10 +970,7 @@ describe("CodexSecurity orchestration", () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
await mkdir(repository);
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient();
await expect(
client.preflight(repository, { maxCostUsd: 5 }),
@@ -1021,10 +1015,7 @@ describe("CodexSecurity orchestration", () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
await mkdir(repository);
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient();
await expect(
client.preflight(repository, {
@@ -1090,11 +1081,10 @@ describe("CodexSecurity orchestration", () => {
await writeFile(knowledgeBase, '{"scope":"Public API"}');
await writeFile(invalidDocument, "not a PDF");
await writeFile(unsupportedDocument, new Uint8Array([0, 1, 2]));
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- environment: { OPENAI_API_KEY: "must-not-be-used" },
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient(
+ {},
+ { OPENAI_API_KEY: "must-not-be-used" },
+ );
await expect(
client.preflight(repository, { knowledgeBasePaths: [knowledgeBase] }),
@@ -1169,11 +1159,7 @@ describe("CodexSecurity orchestration", () => {
],
[{}, { method: "stored_credentials", verified: false }],
] as const) {
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- environment,
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient({}, environment);
const preflight = await client.preflight(repository);
const authentication: ScanAuthentication = preflight.authentication;
@@ -1189,14 +1175,13 @@ describe("CodexSecurity orchestration", () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
await mkdir(repository);
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- environment: {
+ const { client, prepareRuntime } = localClient(
+ {},
+ {
OPENAI_API_KEY: "synthetic-openai-key",
CODEX_API_KEY: "synthetic-codex-key",
},
- prepareRuntime,
- });
+ );
await expect(
client.preflight(repository, { auth: "chatgpt" }),
@@ -1238,12 +1223,10 @@ describe("CodexSecurity orchestration", () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
await mkdir(repository);
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- environment:
- field === "auth" ? { OPENAI_API_KEY: "synthetic-openai-key" } : {},
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient(
+ {},
+ field === "auth" ? { OPENAI_API_KEY: "synthetic-openai-key" } : {},
+ );
const options = { [field]: value } as unknown as ScanOptions;
await expect(client.preflight(repository, options)).rejects.toThrow(
@@ -1259,11 +1242,10 @@ describe("CodexSecurity orchestration", () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
await mkdir(repository);
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- environment: { OPENAI_API_KEY: " " },
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient(
+ {},
+ { OPENAI_API_KEY: " " },
+ );
await expect(
client.preflight(repository, { auth: "api-key" }),
@@ -1969,10 +1951,7 @@ describe("CodexSecurity orchestration", () => {
await mkdir(repository);
await mkdir(output, { mode: 0o700 });
await writeFile(join(output, "previous.txt"), "previous scan\n");
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient();
const preflight = await client.preflight(repository, {
outputDir: output,
@@ -2108,10 +2087,7 @@ describe("CodexSecurity orchestration", () => {
const repository = join(root, "repository");
await mkdir(repository);
await writeFile(join(repository, "preserved.txt"), "preserved\n");
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient();
await expect(
client.run(repository, { outputDir: join(repository, "scan") }),
@@ -2149,10 +2125,7 @@ describe("CodexSecurity orchestration", () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
await mkdir(repository);
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient();
for (const separator of ["\n", "\u0085", "\u2028", "\u2029"]) {
await expect(
@@ -2200,10 +2173,7 @@ describe("CodexSecurity orchestration", () => {
const repository = join(worktree, "packages", "service");
const output = join(worktree, "scan");
await mkdir(repository, { recursive: true });
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient();
await expect(
client.run(repository, { outputDir: output }),
@@ -2227,10 +2197,7 @@ describe("CodexSecurity orchestration", () => {
const temporaryVariable = process.platform === "win32" ? "TEMP" : "TMPDIR";
const previous = process.env[temporaryVariable];
process.env[temporaryVariable] = temporaryRoot;
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient();
try {
await expect(client.run(repository)).rejects.toMatchObject({
@@ -2260,11 +2227,10 @@ describe("CodexSecurity orchestration", () => {
"GIT_COMMON_DIR",
"GIT_REPLACE_REF_BASE",
]) {
- const prepareRuntime = mock(rejecting("runtime should not initialize"));
- const client = TestClient.withDependencies({
- environment: { [name.toLowerCase()]: join(root, "override") },
- prepareRuntime,
- });
+ const { client, prepareRuntime } = localClient(
+ {},
+ { [name.toLowerCase()]: join(root, "override") },
+ );
await expect(client.preflight(repository)).rejects.toThrow(
`${name.toLowerCase()} is not supported`,
diff --git a/sdk/typescript/tests-ts/scan-resume.test.ts b/sdk/typescript/tests-ts/scan-resume.test.ts
index 22565c3ce..57398a3a2 100644
--- a/sdk/typescript/tests-ts/scan-resume.test.ts
+++ b/sdk/typescript/tests-ts/scan-resume.test.ts
@@ -402,10 +402,9 @@ test("resumed Bedrock scans retain provider context for the account advisory", a
const f = await interruptedScan("deep", false, {}, false, "amazon-bedrock");
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(
+ const code = await runCli(
+ ["scans", "resume", f.scanId, "--json"],
+ resumeDependencies(
f,
(options) => ({
startThread() {
@@ -433,44 +432,48 @@ test("resumed Bedrock scans retain provider context for the account advisory", a
}),
{ deepScanConfigPath: join(f.codexHome, "deep-scan.toml") },
),
- });
+ );
expect(code).not.toBe(0);
expect(stderr.text()).toContain("Resumed Bedrock prompt captured");
});
-function resumeClient(
+function resumeDependencies(
f: Awaited<ReturnType<typeof interruptedScan>>,
createCodex: NonNullable<
ConstructorParameters<typeof TestClient>[1]["createCodex"]
>,
runtimeOverrides: Partial<ReturnType<typeof preparedRuntime>> = {},
) {
- return (config: ConstructorParameters<typeof TestClient>[0]) =>
- new TestClient(config, {
- environment: f.environment,
- prepareRuntime: async () => {
- const runtime = {
- ...preparedRuntime(f.codexHome),
- ...runtimeOverrides,
- };
- runtime.configPath = join(f.root, "resumed-runtime.toml");
- runtime.environment = Object.fromEntries(
- Object.entries(f.environment).filter(
- (entry): entry is [string, string] => entry[1] !== undefined,
- ),
- );
- runtime.plugin.version = JSON.parse(
- await readFile(
- join(PLUGIN_ROOT, ".codex-plugin", "plugin.json"),
- "utf8",
- ),
- ).version;
- return runtime;
- },
- resolvePluginPython: async () => f.python,
- runWorkbench,
- createCodex,
- });
+ return {
+ ...dependencies({ environment: f.environment, currentDirectory: f.root }),
+ runWorkbench: f.command,
+ createSecurity: (config: ConstructorParameters<typeof TestClient>[0]) =>
+ new TestClient(config, {
+ environment: f.environment,
+ prepareRuntime: async () => {
+ const runtime = {
+ ...preparedRuntime(f.codexHome),
+ ...runtimeOverrides,
+ };
+ runtime.configPath = join(f.root, "resumed-runtime.toml");
+ runtime.environment = Object.fromEntries(
+ Object.entries(f.environment).filter(
+ (entry): entry is [string, string] => entry[1] !== undefined,
+ ),
+ );
+ runtime.plugin.version = JSON.parse(
+ await readFile(
+ join(PLUGIN_ROOT, ".codex-plugin", "plugin.json"),
+ "utf8",
+ ),
+ ).version;
+ return runtime;
+ },
+ resolvePluginPython: async () => f.python,
+ runWorkbench,
+ createCodex,
+ }),
+ };
}
async function finishDiscovery(f: Awaited<ReturnType<typeof interruptedScan>>) {
@@ -545,26 +548,19 @@ test.each([
bulk
? ["bulk-scan", f.input, "--output-dir", f.root, "--recover", "--json"]
: ["scans", "resume", f.scanId, "--json"],
- {
- ...dependencies({
- environment: f.environment,
- currentDirectory: f.root,
- }),
- runWorkbench: f.command,
- createSecurity: resumeClient(f, () => ({
- startThread: () => fail("Unexpected new session"),
- resumeThread(threadId) {
- expect(threadId).toBe(f.threadId);
- return {
- id: threadId,
- async runStreamed() {
- if (!alreadyFinished) await finishDiscovery(f);
- return { events: completedEvents(threadId) };
- },
- };
- },
- })),
- },
+ resumeDependencies(f, () => ({
+ startThread: () => fail("Unexpected new session"),
+ resumeThread(threadId) {
+ expect(threadId).toBe(f.threadId);
+ return {
+ id: threadId,
+ async runStreamed() {
+ if (!alreadyFinished) await finishDiscovery(f);
+ return { events: completedEvents(threadId) };
+ },
+ };
+ },
+ })),
);
// Preserve the CLI's nonzero exit for a valid, sealed partial result.
expect(code, stderr.text()).toBe(2);
@@ -699,26 +695,19 @@ test.each([
scenario === "bulk"
? ["bulk-scan", f.input, "--output-dir", f.root, "--recover", "--json"]
: ["scans", "resume", f.scanId, "--json"],
- {
- ...dependencies({
- environment: f.environment,
- currentDirectory: f.root,
- }),
- runWorkbench: f.command,
- createSecurity: resumeClient(f, () => ({
- startThread: () => fail("Unexpected new session"),
- resumeThread(threadId) {
- expect(threadId).toBe(f.threadId);
- return {
- id: threadId,
- async runStreamed() {
- turns++;
- return { events: completedEvents(threadId) };
- },
- };
- },
- })),
- },
+ resumeDependencies(f, () => ({
+ startThread: () => fail("Unexpected new session"),
+ resumeThread(threadId) {
+ expect(threadId).toBe(f.threadId);
+ return {
+ id: threadId,
+ async runStreamed() {
+ turns++;
+ return { events: completedEvents(threadId) };
+ },
+ };
+ },
+ })),
);
expect(code, stderr.text()).toBe(2);
expect(
@@ -877,79 +866,72 @@ test.each([
bulk
? ["bulk-scan", f.input, "--output-dir", f.root, "--recover", "--json"]
: ["scans", "resume", f.scanId, "--json"],
- {
- ...dependencies({
- environment: f.environment,
- currentDirectory: f.root,
- }),
- runWorkbench: f.command,
- createSecurity: resumeClient(f, (options) => {
- expect(options.env?.["CODEX_SAFETY_IDENTIFIER"]).toBe(
- settings.safetyIdentifier,
- );
- expect(options.apiKey).toBe(
- auth === "chatgpt" ? undefined : "synthetic-resume-key",
- );
- expect(options.env?.["OPENAI_API_KEY"]).toBeUndefined();
- expect(options.env?.["CODEX_API_KEY"]).toBeUndefined();
- expect(options.config?.["features"]).toMatchObject({
- api_key_cyber_access_programs: true,
- });
- return {
- startThread: () => fail("Resume must use the original session."),
- resumeThread(threadId) {
- expect(threadId).toBe(f.threadId);
- return {
- id: threadId,
- async runStreamed(prompt, turnOptions) {
- expect(turnOptions?.cyberAccessProgram).toBe(
- settings.cyberAccessProgram,
+ resumeDependencies(f, (options) => {
+ expect(options.env?.["CODEX_SAFETY_IDENTIFIER"]).toBe(
+ settings.safetyIdentifier,
+ );
+ expect(options.apiKey).toBe(
+ auth === "chatgpt" ? undefined : "synthetic-resume-key",
+ );
+ expect(options.env?.["OPENAI_API_KEY"]).toBeUndefined();
+ expect(options.env?.["CODEX_API_KEY"]).toBeUndefined();
+ expect(options.config?.["features"]).toMatchObject({
+ api_key_cyber_access_programs: true,
+ });
+ return {
+ startThread: () => fail("Resume must use the original session."),
+ resumeThread(threadId) {
+ expect(threadId).toBe(f.threadId);
+ return {
+ id: threadId,
+ async runStreamed(prompt, turnOptions) {
+ expect(turnOptions?.cyberAccessProgram).toBe(
+ settings.cyberAccessProgram,
+ );
+ prompts.push(prompt as string);
+ if (prompts.length === 1) {
+ expect(prompt).toContain(
+ "Keep the original scan instructions.",
);
- prompts.push(prompt as string);
- if (prompts.length === 1) {
- expect(prompt).toContain(
- "Keep the original scan instructions.",
- );
- const deep = await readFile(
- join(f.codexHome, "codex-security", "config.toml"),
- "utf8",
- );
- expect(deep).toContain("subagents = 0");
- expect(deep).toContain("stop_after_consecutive_errors = 2");
- expect(deep).toContain("max_time_hours = 1.5");
- const workerConfigPath =
- options.env?.["CODEX_SECURITY_CONFIG_PATH"];
- expect(workerConfigPath).toBe(
- join(f.root, "resumed-runtime.toml"),
- );
- const workerConfig = parseToml(
- await readFile(workerConfigPath!, "utf8"),
- );
- expect(workerConfig).toMatchObject({
- codex_security: {
- cyber_access_program: settings.cyberAccessProgram,
- },
- features: {
- api_key_cyber_access_programs: true,
- },
- });
- const saved = await f.command([
- "get-scan-recipe",
- "--scan-id",
- f.scanId,
- ]);
- expect(saved["recipe"]).toMatchObject({
- cyberAccessProgram: settings.cyberAccessProgram,
- });
- await finishDiscovery(f);
- }
- return { events: completedEvents(threadId) };
- },
- };
- },
- };
- }),
- },
+ const deep = await readFile(
+ join(f.codexHome, "codex-security", "config.toml"),
+ "utf8",
+ );
+ expect(deep).toContain("subagents = 0");
+ expect(deep).toContain("stop_after_consecutive_errors = 2");
+ expect(deep).toContain("max_time_hours = 1.5");
+ const workerConfigPath =
+ options.env?.["CODEX_SECURITY_CONFIG_PATH"];
+ expect(workerConfigPath).toBe(
+ join(f.root, "resumed-runtime.toml"),
+ );
+ const workerConfig = parseToml(
+ await readFile(workerConfigPath!, "utf8"),
+ );
+ expect(workerConfig).toMatchObject({
+ codex_security: {
+ cyber_access_program: settings.cyberAccessProgram,
+ },
+ features: {
+ api_key_cyber_access_programs: true,
+ },
+ });
+ const saved = await f.command([
+ "get-scan-recipe",
+ "--scan-id",
+ f.scanId,
+ ]);
+ expect(saved["recipe"]).toMatchObject({
+ cyberAccessProgram: settings.cyberAccessProgram,
+ });
+ await finishDiscovery(f);
+ }
+ return { events: completedEvents(threadId) };
+ },
+ };
+ },
+ };
+ }),
);
expect(code, stderr.text()).toBe(2);
expect(prompts, stderr.text()).toHaveLength(2);
@@ -969,13 +951,12 @@ test("missing session logs do not create another session or fail the original sc
await rm(f.sessionPath);
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, () =>
+ const code = await runCli(
+ ["scans", "resume", f.scanId],
+ resumeDependencies(f, () =>
fail("Must not invoke Codex without the original session"),
),
- });
+ );
expect(code).not.toBe(0);
expect(stderr.text()).toContain("original Codex session");
expect(
diff --git a/sdk/typescript/tests-ts/support/api-events.ts b/sdk/typescript/tests-ts/support/api-events.ts
index 1a27f25a0..ceff9d12c 100644
--- a/sdk/typescript/tests-ts/support/api-events.ts
+++ b/sdk/typescript/tests-ts/support/api-events.ts
@@ -48,6 +48,22 @@ type ScanEventOptions = Omit<
"thread" | "events" | "signal" | "scanDir" | "pluginRoot" | "expectation"
> & { abortController?: AbortController };
+export function completedTurn(): Extract<
+ ThreadEvent,
+ { type: "turn.completed" }
+> {
+ return {
+ type: "turn.completed",
+ usage: {
+ input_tokens: 10,
+ cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
+ output_tokens: 3,
+ reasoning_output_tokens: 1,
+ },
+ };
+}
+
export async function* completedEvents(
threadId = "thread-1",
events?: AsyncIterable<ThreadEvent>,
@@ -61,16 +77,7 @@ export async function* completedEvents(
item: { id: "message-1", type: "agent_message", text: "scan complete" },
};
}
- yield {
- type: "turn.completed",
- usage: {
- input_tokens: 10,
- cached_input_tokens: 2,
- cache_write_input_tokens: 0,
- output_tokens: 3,
- reasoning_output_tokens: 1,
- },
- };
+ yield completedTurn();
}
export function runEvents(