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 client setup and usage fixtures (#1475)

mldangelo-oaid521e1a3f29f
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index 3d6e6191c..0b6f410e1 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -3968,37 +3968,34 @@ describe("CodexSecurity orchestration", () => {
const observed = Promise.withResolvers<void>();
const workers: ScanWorkerEvent[] = [];
const errors: ScanObserverName[] = [];
- const client = new TestClient(
- {},
- {
- environment: {},
- ...scanRuntimeDependencies(codexHome, scanDir),
- createCodex: () => ({
- startThread: () => ({
- id: "thread-1",
- async runStreamed() {
- await copyCompletedScan(root);
- await writeUsageSession(codexHome, "thread-1", {});
- async function* events(): AsyncGenerator<ThreadEvent> {
- for await (const event of completedEvents()) {
- yield event;
- if (event.type === "turn.started") {
- await writeUsageSession(
- codexHome,
- "worker-thread",
- {},
- { parent: "thread-1" },
- );
- await observed.promise;
- }
+ const client = TestClient.withDependencies({
+ environment: {},
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ createCodex: () => ({
+ startThread: () => ({
+ id: "thread-1",
+ async runStreamed() {
+ await copyCompletedScan(root);
+ await writeUsageSession(codexHome, "thread-1", {});
+ async function* events(): AsyncGenerator<ThreadEvent> {
+ for await (const event of completedEvents()) {
+ yield event;
+ if (event.type === "turn.started") {
+ await writeUsageSession(
+ codexHome,
+ "worker-thread",
+ {},
+ { parent: "thread-1" },
+ );
+ await observed.promise;
}
}
- return { events: events() };
- },
- }),
+ }
+ return { events: events() };
+ },
}),
- },
- );
+ }),
+ });
try {
const result = await client.run(repository, {
onWorkerEvent: (event) => {
@@ -6750,45 +6747,42 @@ describe("CodexSecurity orchestration", () => {
"platform",
)!;
let childPath: string | undefined;
- const client = new TestClient(
- {},
- {
+ const client = TestClient.withDependencies({
+ environment: {
+ CODEX_CLI_PATH: join(root, "codex.cmd"),
+ OPENAI_API_KEY: "synthetic-key",
+ },
+ prepareRuntime: runtimePreparer(codexHome, () => ({
environment: {
- CODEX_CLI_PATH: join(root, "codex.cmd"),
- OPENAI_API_KEY: "synthetic-key",
+ CODEX_HOME: codexHome,
+ CODEX_CLI_PATH: executable,
+ PATH: inheritedTools,
},
- prepareRuntime: runtimePreparer(codexHome, () => ({
- environment: {
- CODEX_HOME: codexHome,
- CODEX_CLI_PATH: executable,
- PATH: inheritedTools,
+ })),
+ resolveCodexCommand: () => ({ command: executable }),
+ resolvePluginPython: async () => {
+ Object.defineProperty(process, "platform", {
+ value: "win32",
+ configurable: true,
+ });
+ return "/managed/python";
+ },
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ createCodex: (options) => ({
+ startThread: () => ({
+ id: null,
+ async runStreamed() {
+ childPath = execFileSync(
+ process.execPath,
+ ["-e", "console.log(process.env.PATH)"],
+ { env: options.env, encoding: "utf8" },
+ ).trim();
+ throw new Error("child environment captured");
},
- })),
- resolveCodexCommand: () => ({ command: executable }),
- resolvePluginPython: async () => {
- Object.defineProperty(process, "platform", {
- value: "win32",
- configurable: true,
- });
- return "/managed/python";
- },
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
- createCodex: (options) => ({
- startThread: () => ({
- id: null,
- async runStreamed() {
- childPath = execFileSync(
- process.execPath,
- ["-e", "console.log(process.env.PATH)"],
- { env: options.env, encoding: "utf8" },
- ).trim();
- throw new Error("child environment captured");
- },
- }),
}),
- },
- );
+ }),
+ });
try {
await expect(client.run(repository)).rejects.toThrow(
"child environment captured",
@@ -7241,22 +7235,19 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
);
const fake = nodeCodex(fakeCodex);
const createCodex = mock(throwing("scan reached"));
- const client = new TestClient(
- {},
- {
- environment: {
- CODEX_HOME: ambientHome,
- OPENAI_API_KEY: "synthetic-key",
- },
- ...scanRuntimeDependencies(codexHome, scanDir),
- prepareRuntime: unauthenticatedRuntime(codexHome, () => ({
- CODEX_HOME: codexHome,
- ...fake.environment,
- })),
- resolveCodexCommand: () => fake.command,
- createCodex,
+ const client = TestClient.withDependencies({
+ environment: {
+ CODEX_HOME: ambientHome,
+ OPENAI_API_KEY: "synthetic-key",
},
- );
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ prepareRuntime: unauthenticatedRuntime(codexHome, () => ({
+ CODEX_HOME: codexHome,
+ ...fake.environment,
+ })),
+ resolveCodexCommand: () => fake.command,
+ createCodex,
+ });
try {
await expect(client.run(repository)).rejects.toThrow("scan reached");
await runtime.setCodexSecurityCredentialLogout(codexHome, true);
@@ -7598,19 +7589,16 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
bootstrapWorkspace,
deepScanConfigDirectory,
};
- const client = new TestClient(
- {},
- {
- environment: {
- CODEX_SECURITY_STATE_DIR: join(root, "state"),
- OPENAI_API_KEY: "ambient-key",
- },
- prepareRuntime: async () => prepared,
- resolvePluginPython: async () => "/managed/python",
- repositoryRevision: async () => null,
- createCodex: () => fail("scan reached"),
+ const client = TestClient.withDependencies({
+ environment: {
+ CODEX_SECURITY_STATE_DIR: join(root, "state"),
+ OPENAI_API_KEY: "ambient-key",
},
- );
+ prepareRuntime: async () => prepared,
+ resolvePluginPython: async () => "/managed/python",
+ repositoryRevision: async () => null,
+ createCodex: () => fail("scan reached"),
+ });
await expect(client.run(repository)).rejects.toThrow("scan reached");
const profile = await createProviderProfile(codexHome, {
model_providers: {
diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.ts
index 2d4cd157c..0d705b7ad 100644
--- a/sdk/typescript/tests-ts/cli.test.ts
+++ b/sdk/typescript/tests-ts/cli.test.ts
@@ -109,6 +109,14 @@ const profileScenarios = [
const DEFAULT_SCAN_MODEL_CONFIGURATION =
scanModelConfiguration(DEFAULT_CODEX_CONFIG);
+function sampleUsage() {
+ return {
+ input_tokens: 1_250,
+ cached_input_tokens: 200,
+ output_tokens: 30,
+ };
+}
+
async function multiscanInventory(root: string): Promise<void> {
const repository = join(root, "repository");
for (const args of [
@@ -1873,11 +1881,7 @@ describe("CLI", () => {
]) {
const { stderr, runCli } = createCliTest(main, { stderr: isTTY });
- const result = fakeResult([], "complete", {
- input_tokens: 1_250,
- cached_input_tokens: 200,
- output_tokens: 30,
- });
+ const result = fakeResult([], "complete", sampleUsage());
const setIntervalMock = mock(fakeInterval);
const deps = dependencies({
environment,
@@ -2203,11 +2207,7 @@ describe("CLI", () => {
test("omits stage and file counts from interactive Deep scan dashboards", async () => {
const { stderr, runCli } = createCliTest(main, { stderr: true });
- const result = fakeResult([], "complete", {
- input_tokens: 1_250,
- cached_input_tokens: 200,
- output_tokens: 30,
- });
+ const result = fakeResult([], "complete", sampleUsage());
expect(
await runCli(
@@ -3003,11 +3003,7 @@ describe("CLI", () => {
test("emits verbose scan lifecycle diagnostics without changing JSON output", async () => {
const { stdout, stderr, runCli } = createCliTest(main);
- const result = fakeResult(["high"], "complete", {
- input_tokens: 1_250,
- cached_input_tokens: 200,
- output_tokens: 30,
- });
+ const result = fakeResult(["high"], "complete", sampleUsage());
const deps = dependencies({
environment: { OPENAI_API_KEY: "sk-proj-SYNTHETIC_VERBOSE_SECRET_123" },
});
@@ -4030,11 +4026,7 @@ describe("CLI", () => {
test("prints only the completion summary for default scans", async () => {
const { stdout, stderr, runCli } = createCliTest(main);
- const result = fakeResult(["high"], "complete", {
- input_tokens: 1_250,
- cached_input_tokens: 200,
- output_tokens: 30,
- });
+ const result = fakeResult(["high"], "complete", sampleUsage());
result.manifest.scan.id = "12345678-abcd-4567-abcd-1234567890ab";
result.manifest.scan.completedAt = "2026-01-01T00:06:37Z";
@@ -4428,11 +4420,7 @@ describe("CLI", () => {
test("shows live stage, files, workers, tokens, and opt-in cost without a budget", async () => {
const { stdout, stderr, runCli } = createCliTest(main);
- const result = fakeResult([], "complete", {
- input_tokens: 1_250,
- cached_input_tokens: 200,
- output_tokens: 30,
- });
+ const result = fakeResult([], "complete", sampleUsage());
expect(
await runCli(
@@ -4499,11 +4487,7 @@ describe("CLI", () => {
const result = fakeResult(
["critical", "high", "high", "informational"],
"complete",
- {
- input_tokens: 1250,
- cached_input_tokens: 200,
- output_tokens: 30,
- },
+ sampleUsage(),
);
expect(
@@ -4574,11 +4558,7 @@ describe("CLI", () => {
test("reports a cost range while preserving the short-context scan budget", async () => {
const { stdout, stderr, runCli } = createCliTest(main);
- const result = fakeResult([], "complete", {
- input_tokens: 1_250,
- cached_input_tokens: 200,
- output_tokens: 30,
- });
+ const result = fakeResult([], "complete", sampleUsage());
expect(
await runCli(
@@ -4623,11 +4603,7 @@ describe("CLI", () => {
test("reports and classifies a scan stopped when its live cost exceeds the limit", async () => {
const { stdout, stderr, runCli } = createCliTest(main);
- const cost = fakeResult([], "complete", {
- input_tokens: 1_250,
- cached_input_tokens: 200,
- output_tokens: 30,
- }).cost!;
+ const cost = fakeResult([], "complete", sampleUsage()).cost!;
expect(
await runCli(
@@ -4658,11 +4634,7 @@ describe("CLI", () => {
test("accepts a scan at its estimated cost limit", async () => {
const stdout = captureCli(main, "stdout");
- const result = fakeResult([], "complete", {
- input_tokens: 1_250,
- cached_input_tokens: 200,
- output_tokens: 30,
- });
+ const result = fakeResult([], "complete", sampleUsage());
expect(
await stdout.run(