Skip to content

Commit 3afb0c3

Browse files
test(runtime): share bundled runtime source loader (#407)
1 parent 2db2f81 commit 3afb0c3

5 files changed

Lines changed: 26 additions & 45 deletions

File tree

sdk/typescript/tests-ts/completed-scan-handoff.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
import { readFile } from "node:fs/promises";
2-
import { join } from "node:path";
3-
import { brotliDecompressSync } from "node:zlib";
41
import { expect, test } from "bun:test";
5-
import { PLUGIN_ROOT } from "./plugin-root.js";
2+
import { loadBundledRuntime } from "./plugin-root.js";
63

74
test("does not request completed findings after a prompt-only scan", async () => {
8-
const parts = await Promise.all(
9-
["000", "001"].map((part) =>
10-
readFile(join(PLUGIN_ROOT, "mcp", `server.mjs.br.part-${part}`)),
11-
),
12-
);
13-
const runtime = brotliDecompressSync(Buffer.concat(parts)).toString("utf8");
5+
const runtime = await loadBundledRuntime();
146
const source = /function promptOnlyScanResult\([^\n]*\) \{[\s\S]*?\n\}/u.exec(
157
runtime,
168
)?.[0];

sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { spawnSync } from "node:child_process";
22
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
3-
import { readFile } from "node:fs/promises";
43
import { tmpdir } from "node:os";
54
import { join } from "node:path";
6-
import { brotliDecompressSync } from "node:zlib";
75
import { expect, test } from "bun:test";
8-
import { PLUGIN_ROOT } from "./plugin-root.js";
9-
10-
const bundledRuntime = Promise.all(
11-
["000", "001"].map((part) =>
12-
readFile(join(PLUGIN_ROOT, "mcp", `server.mjs.br.part-${part}`)),
13-
),
14-
).then((parts) => brotliDecompressSync(Buffer.concat(parts)).toString("utf8"));
6+
import { loadBundledRuntime, PLUGIN_ROOT } from "./plugin-root.js";
157

168
function bundledFunction(runtime: string, name: string): string {
179
const source = new RegExp(
@@ -23,7 +15,7 @@ function bundledFunction(runtime: string, name: string): string {
2315
}
2416

2517
test("keeps every advertised Deep worker tool within Codex's name limit", async () => {
26-
const runtime = await bundledRuntime;
18+
const runtime = await loadBundledRuntime();
2719
const method = / compactArtifactServer\(request\) \{[\s\S]*?\n \}/u.exec(
2820
runtime,
2921
)?.[0];
@@ -112,7 +104,7 @@ test("keeps every advertised Deep worker tool within Codex's name limit", async
112104
});
113105

114106
test("classifies owned worker tool failures without exposing their contents", async () => {
115-
const runtime = await bundledRuntime;
107+
const runtime = await loadBundledRuntime();
116108
const diagnosticSource = bundledFunction(runtime, "appendSafeItemDiagnostic");
117109
const recordHelper = /\b(isRecord\d*)\(item\)/u.exec(diagnosticSource)?.[1];
118110
expect(recordHelper).toBeDefined();
@@ -182,7 +174,7 @@ test("classifies owned worker tool failures without exposing their contents", as
182174
});
183175

184176
test("resumes only when the exact reducer result is missing", async () => {
185-
const runtime = await bundledRuntime;
177+
const runtime = await loadBundledRuntime();
186178
const source = bundledFunction(runtime, "isMissingReducerResult");
187179
const pathImport = /\(0, (import_node_path\d+)\.join\)/u.exec(source)?.[1];
188180
expect(pathImport).toBeDefined();

sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { readFile } from "node:fs/promises";
2-
import { join } from "node:path";
3-
import { brotliDecompressSync } from "node:zlib";
41
import { expect, test } from "bun:test";
5-
import { PLUGIN_ROOT } from "./plugin-root.js";
2+
import { loadBundledRuntime } from "./plugin-root.js";
63

74
type WorkerEvent =
85
| { type: "thread.started"; thread_id: string }
@@ -26,12 +23,7 @@ type WorkerExecutorConstructor = new (settings: {
2623
async function bundledWorkerExecutor(
2724
events: (signal: AbortSignal) => AsyncGenerator<WorkerEvent>,
2825
): Promise<WorkerExecutorConstructor> {
29-
const chunks = await Promise.all(
30-
["000", "001"].map((part) =>
31-
readFile(join(PLUGIN_ROOT, "mcp", `server.mjs.br.part-${part}`)),
32-
),
33-
);
34-
const runtime = brotliDecompressSync(Buffer.concat(chunks)).toString("utf8");
26+
const runtime = await loadBundledRuntime();
3527
const source = /var CodexSdkWorkerExecutor = class \{[\s\S]*?\n\};/u.exec(
3628
runtime,
3729
)?.[0];
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { existsSync } from "node:fs";
2+
import { readFile } from "node:fs/promises";
23
import { fileURLToPath } from "node:url";
4+
import { brotliDecompressSync } from "node:zlib";
35

46
const sourcePlugin = new URL(
57
"../../../plugins/codex-security/",
@@ -9,11 +11,22 @@ const bundledPlugin = new URL("../_bundled_plugin/", import.meta.url);
911
const hasSourcePlugin = existsSync(
1012
new URL(".codex-plugin/plugin.json", sourcePlugin),
1113
);
14+
const plugin = hasSourcePlugin ? sourcePlugin : bundledPlugin;
1215

13-
export const PLUGIN_ROOT = fileURLToPath(
14-
hasSourcePlugin ? sourcePlugin : bundledPlugin,
15-
);
16+
export const PLUGIN_ROOT = fileURLToPath(plugin);
1617

1718
export const INTEGRATION_TARGET = hasSourcePlugin
1819
? "project/codex-security-sdk/src"
1920
: "sdk/typescript/src";
21+
22+
let bundledRuntime: Promise<string> | undefined;
23+
24+
export function loadBundledRuntime(): Promise<string> {
25+
return (bundledRuntime ??= Promise.all(
26+
["000", "001"].map((part) =>
27+
readFile(new URL(`mcp/server.mjs.br.part-${part}`, plugin)),
28+
),
29+
).then((parts) =>
30+
brotliDecompressSync(Buffer.concat(parts)).toString("utf8"),
31+
));
32+
}

sdk/typescript/tests-ts/prompt-only-start-timeout.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
import { readFile } from "node:fs/promises";
2-
import { join } from "node:path";
3-
import { brotliDecompressSync } from "node:zlib";
41
import { expect, test } from "bun:test";
5-
import { PLUGIN_ROOT } from "./plugin-root.js";
2+
import { loadBundledRuntime, PLUGIN_ROOT } from "./plugin-root.js";
63

74
test("gives prompt-only scan startup the five-minute scan timeout", async () => {
8-
const parts = await Promise.all(
9-
["000", "001"].map((part) =>
10-
readFile(join(PLUGIN_ROOT, "mcp", `server.mjs.br.part-${part}`)),
11-
),
12-
);
13-
const runtime = brotliDecompressSync(Buffer.concat(parts)).toString("utf8");
5+
const runtime = await loadBundledRuntime();
146
const source =
157
/async function executeWorkbench\([^\n]*\) \{[\s\S]*?\n\}/u.exec(
168
runtime,

0 commit comments

Comments
 (0)