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

refactor(sdk): simplify persisted finding workflows (#1210)

mldangelo-oai08fb64341451
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.md
index 0a8a17e10..517fc5782 100644
--- a/sdk/typescript/README.md
+++ b/sdk/typescript/README.md
@@ -2756,8 +2756,8 @@ Local defaults are `HOST=127.0.0.1` and `PORT=3000`. The existing
without a state override, the service uses the same default state directory as
the CLI. These settings also work on Windows.
-HTTP routing, orchestration, embedding generation, and the SQLite adapter live
-separately under `src/server/`. `FindingsService` receives a `FindingEmbedder`
+HTTP routing, embedding generation, and the SQLite adapter live
+separately under `src/server/`. `startFindingsServer` receives a `FindingEmbedder`
whose `embed(findings)` method returns one `{ model, vector }` per finding in
input order. `OpenAiFindingEmbedder` handles tokenization, batching, API calls,
and vector normalization; it does not access storage. The `FindingsStore`
diff --git a/sdk/typescript/scripts/check-package.mjs b/sdk/typescript/scripts/check-package.mjs
index e1546bd7c..e2dad2db3 100644
--- a/sdk/typescript/scripts/check-package.mjs
+++ b/sdk/typescript/scripts/check-package.mjs
@@ -226,7 +226,6 @@ const allowedFiles = new Set([
"server/dashboard",
"server/dashboard-types",
"server/errors",
- "server/findings-service",
"server/routes",
"server/server",
"server/serve",
diff --git a/sdk/typescript/scripts/smoke-findings-service.ts b/sdk/typescript/scripts/smoke-findings-service.ts
index e7354748e..ca3638745 100644
--- a/sdk/typescript/scripts/smoke-findings-service.ts
+++ b/sdk/typescript/scripts/smoke-findings-service.ts
@@ -1,3 +1,4 @@
+import { parseJsonLines } from "../tests-ts/support/json.js";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
@@ -298,21 +299,15 @@ async function checkStoredGroups(): Promise<FindingDedupeGroup[]> {
}
async function checkReviews(): Promise<void> {
- const calls = (
+ const calls = parseJsonLines<{
+ stage: string;
+ findingIds: string[];
+ }>(
await readFile(
join(runnerRoot, "results/.codex-security-state/review-calls.jsonl"),
"utf8",
- )
- )
- .trim()
- .split("\n")
- .map(
- (line) =>
- JSON.parse(line) as {
- stage: string;
- findingIds: string[];
- },
- );
+ ),
+ );
for (const stage of ["screen", "pair"]) {
assert.ok(
calls.some((call) => call.stage === stage),
diff --git a/sdk/typescript/src/classify-scan-severity.ts b/sdk/typescript/src/classify-scan-severity.ts
index 9a5da5b05..ada2a6bb9 100644
--- a/sdk/typescript/src/classify-scan-severity.ts
+++ b/sdk/typescript/src/classify-scan-severity.ts
@@ -12,7 +12,7 @@ import { CodexSecurityError } from "./errors.js";
import type { Finding } from "./models.js";
import {
bundledPluginRoot,
- codexSecurityStateDirectory,
+ workbenchEnvironment,
resolvePluginPython,
runWorkbench,
} from "./runtime.js";
@@ -58,10 +58,7 @@ export async function classifyScanSeverityInternal(
runWorkbench:
dependencies.runWorkbench ??
(async (args) => {
- const stateEnvironment = {
- ...environment,
- CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(environment),
- };
+ const stateEnvironment = workbenchEnvironment(environment);
return runWorkbench(
{
environment: stateEnvironment,
diff --git a/sdk/typescript/src/classify-severity.ts b/sdk/typescript/src/classify-severity.ts
index 57af60c20..2761df62f 100644
--- a/sdk/typescript/src/classify-severity.ts
+++ b/sdk/typescript/src/classify-severity.ts
@@ -1,3 +1,4 @@
+import { findingEntry } from "./value.js";
import { readFile, readdir } from "node:fs/promises";
import { join } from "node:path";
import { z } from "incur";
@@ -146,12 +147,7 @@ export async function classifySeverityInternal(
knowledgeBaseSha256: knowledge === null ? null : workflowDigest(knowledge),
assessments: [],
};
- const cached = new Map(
- (await checkpoint?.load(result))?.map((assessment) => [
- assessment.findingId,
- assessment,
- ]),
- );
+ const cached = new Map((await checkpoint?.load(result))?.map(findingEntry));
for (const finding of findings) {
options.signal?.throwIfAborted();
const inputSha256 = workflowDigest(finding);
@@ -263,7 +259,7 @@ export function validateSeverityClassification(
result: SeverityClassification,
findings: readonly SeverityClassificationFinding[],
): SeverityClassification {
- const byId = new Map(findings.map((finding) => [finding.findingId, finding]));
+ const byId = new Map(findings.map(findingEntry));
for (const assessment of result.assessments) {
const finding = byId.get(assessment.findingId);
if (
diff --git a/sdk/typescript/src/contract.ts b/sdk/typescript/src/contract.ts
index d44136641..72f139e02 100644
--- a/sdk/typescript/src/contract.ts
+++ b/sdk/typescript/src/contract.ts
@@ -1,4 +1,5 @@
import { createHash, hash } from "node:crypto";
+import { isNonEmptyString } from "./value.js";
import { constants, type BigIntStats, type Stats } from "node:fs";
import {
lstat,
@@ -7,11 +8,12 @@ import {
realpath,
type FileHandle,
} from "node:fs/promises";
-import { isAbsolute, join, posix, relative, resolve, sep } from "node:path";
+import { isAbsolute, join, posix, resolve } from "node:path";
import Ajv2020, { type ErrorObject } from "ajv/dist/2020.js";
import { ContractValidationError, abortReason } from "./errors.js";
-import { isRecord as isJsonRecord } from "./record.js";
+import { isRecord } from "./record.js";
import type {
+ ContractObject as JsonRecord,
CoverageDocument,
FindingsDocument,
ScanManifest,
@@ -21,6 +23,7 @@ import {
requireSecureOutputAncestry,
} from "./runtime.js";
import type { NormalizedTarget, ScanMode } from "./targets.js";
+import { isWithin as isContained } from "./trusted-executable.js";
import { isWindowsUnsafePathComponent } from "./windows-path.js";
const DOCUMENTS = {
@@ -120,29 +123,25 @@ export async function loadContractWithScanDirectory(
options.signal,
);
let validate: ReturnType<typeof ajv.compile>;
- try {
- validate = ajv.compile(schema);
- } catch {
- throw new ContractValidationError(`${schemaName}: invalid JSON Schema.`);
- }
- let payload: unknown =
- filename === "findings.json"
- ? findingsPayload
- : payloads[filename as keyof typeof payloads];
+ let payload: unknown;
let valid: boolean;
try {
- const result = validate(payload);
- if (typeof result !== "boolean") {
- throw new Error("asynchronous JSON Schema validation is unsupported");
- }
- valid = result;
- if (!valid && filename === "findings.json") {
- payload = normalizePersistedFindings(payload);
- const compatibleResult = validate(payload);
- if (typeof compatibleResult !== "boolean") {
+ validate = ajv.compile(schema);
+ payload =
+ filename === "findings.json"
+ ? findingsPayload
+ : payloads[filename as keyof typeof payloads];
+ const validatePayload = (payload: unknown) => {
+ const result = validate(payload);
+ if (typeof result !== "boolean") {
throw new Error("asynchronous JSON Schema validation is unsupported");
}
- valid = compatibleResult;
+ return result;
+ };
+ valid = validatePayload(payload);
+ if (!valid && filename === "findings.json") {
+ payload = normalizePersistedFindings(payload);
+ valid = validatePayload(payload);
}
} catch {
throw new ContractValidationError(`${schemaName}: invalid JSON Schema.`);
@@ -209,29 +208,22 @@ export async function loadContractWithScanDirectory(
};
}
-type JsonRecord = Record<string, unknown>;
-
/** Normalize optional legacy details on a copy, without changing saved artifacts. */
export function normalizePersistedFindings(payload: unknown): unknown {
const compatible = structuredClone(payload);
- if (!isJsonRecord(compatible) || !Array.isArray(compatible["findings"])) {
+ if (!isRecord(compatible) || !Array.isArray(compatible["findings"])) {
return compatible;
}
for (const finding of compatible["findings"]) {
- if (!isJsonRecord(finding)) continue;
+ if (!isRecord(finding)) continue;
const legacyEvidence = finding["code_evidence"];
if (Array.isArray(legacyEvidence)) {
const compatibleEvidence: JsonRecord[] = [];
for (const evidence of legacyEvidence) {
- if (!isJsonRecord(evidence)) continue;
+ if (!isRecord(evidence)) continue;
const id = evidence["id"];
const code = evidence["code"];
- if (
- typeof id !== "string" ||
- id.length === 0 ||
- typeof code !== "string" ||
- code.length === 0
- ) {
+ if (!isNonEmptyString(id) || !isNonEmptyString(code)) {
continue;
}
compatibleEvidence.push(evidence);
@@ -268,12 +260,12 @@ export function normalizePersistedFindings(payload: unknown): unknown {
],
] satisfies Array<[string, string[]]>) {
const section = finding[sectionName];
- if (!isJsonRecord(section)) continue;
+ if (!isRecord(section)) continue;
normalizeLegacyStringLists(section, listFields);
}
const legacyRootCause = finding["root_cause"];
- if (isJsonRecord(legacyRootCause)) {
+ if (isRecord(legacyRootCause)) {
removeUnsupportedLegacyStrings(legacyRootCause, [
"summary",
"code",
@@ -288,7 +280,7 @@ export function normalizePersistedFindings(payload: unknown): unknown {
}
const validation = finding["validation"];
- if (isJsonRecord(validation)) {
+ if (isRecord(validation)) {
if (
typeof validation["evidence"] !== "string" ||
validation["evidence"].length === 0
@@ -304,7 +296,7 @@ export function normalizePersistedFindings(payload: unknown): unknown {
}
const attackPath = finding["attackPath"];
- if (!isJsonRecord(attackPath)) continue;
+ if (!isRecord(attackPath)) continue;
removeUnsupportedLegacyStrings(attackPath, ["summary"]);
for (const field of ["dataFlow", "data_flow", "dataflow", "reachability"]) {
const detail = attackPath[field];
@@ -316,7 +308,7 @@ export function normalizePersistedFindings(payload: unknown): unknown {
if (detail.length === 0) delete attackPath[field];
continue;
}
- if (!isJsonRecord(detail)) {
+ if (!isRecord(detail)) {
if (field in attackPath) delete attackPath[field];
continue;
}
@@ -336,12 +328,12 @@ export function normalizePersistedFindings(payload: unknown): unknown {
}
for (const field of ["impact", "likelihood"]) {
const detail = attackPath[field];
- if (isJsonRecord(detail)) {
+ if (isRecord(detail)) {
removeUnsupportedLegacyStrings(detail, ["level", "rationale", "why"]);
} else if (
detail !== undefined &&
detail !== null &&
- (typeof detail !== "string" || detail.length === 0)
+ !isNonEmptyString(detail)
) {
delete attackPath[field];
}
@@ -358,10 +350,8 @@ function normalizeLegacyStringLists(
if (!(field in section)) continue;
const value = section[field];
if (Array.isArray(value)) {
- section[field] = value.filter(
- (item): item is string => typeof item === "string" && item.length > 0,
- );
- } else if (typeof value === "string" && value.length > 0) {
+ section[field] = value.filter(isNonEmptyString);
+ } else if (isNonEmptyString(value)) {
section[field] = [value];
} else {
delete section[field];
@@ -558,17 +548,11 @@ async function requireCheckedScanFile(
}
const parents = [{ path: scanDir, metadata: rootMetadata }];
throwIfAborted(signal);
- if (
- !parents[0]!.metadata.isDirectory() ||
- parents[0]!.metadata.isSymbolicLink()
- ) {
- throw new Error("unsafe scan directory");
- }
for (const part of parts.slice(0, -1)) {
current = join(current, part);
const metadata = await lstat(current);
throwIfAborted(signal);
- if (metadata.isSymbolicLink() || !metadata.isDirectory()) {
+ if (!metadata.isDirectory()) {
throw new Error("unsafe parent");
}
parents.push({ path: current, metadata });
@@ -576,7 +560,7 @@ async function requireCheckedScanFile(
const path = join(scanDir, ...parts);
const metadata = await lstat(path);
throwIfAborted(signal);
- if (metadata.isSymbolicLink() || !metadata.isFile()) {
+ if (!metadata.isFile()) {
throw new ContractValidationError(
`${context}: expected a regular non-symlink file.`,
);
@@ -752,15 +736,15 @@ function validateExpectation(
}
if (requested.kind === "paths") {
- const actual = scan.scope.includePaths.map(safeScopePath);
- if (
- actual.length !== new Set(actual).size ||
- !sameSet(new Set(actual), new Set(requested.paths))
- ) {
- throw new ContractValidationError(
- "Manifest include paths do not match the requested path target.",
- );
+ const actualPaths = scan.scope.includePaths.map(safeScopePath);
+ const actual = new Set(actualPaths);
+ if (actualPaths.length === actual.size) {
+ const expected = new Set(requested.paths);
+ if (actual.size === expected.size && actual.isSubsetOf(expected)) return;
}
+ throw new ContractValidationError(
+ "Manifest include paths do not match the requested path target.",
+ );
}
}
@@ -791,13 +775,10 @@ async function requireScanRoot(
throwIfAborted(signal);
if (
!metadata.isDirectory() ||
- metadata.isSymbolicLink() ||
!current.isDirectory() ||
- current.isSymbolicLink() ||
metadata.dev !== current.dev ||
metadata.ino !== current.ino ||
!returned.isDirectory() ||
- returned.isSymbolicLink() ||
metadata.dev !== returned.dev ||
metadata.ino !== returned.ino
) {
@@ -834,7 +815,6 @@ async function verifyScanRoot(
throwIfAborted(signal);
if (
!current.isDirectory() ||
- current.isSymbolicLink() ||
current.dev !== root.metadata.dev ||
current.ino !== root.metadata.ino
) {
@@ -855,7 +835,7 @@ function safeRelativePath(value: string, context: string): string {
const parts = value.split("/");
if (
value.trim().length === 0 ||
- !isWellFormedUnicode(value) ||
+ Buffer.from(value, "utf8").toString("utf8") !== value ||
value === "." ||
value.startsWith("/") ||
/^[A-Za-z]:/.test(value) ||
@@ -964,7 +944,7 @@ function parseJson(path: string, bytes: Uint8Array): Record<string, unknown> {
{ cause: error },
);
}
- if (!isJsonRecord(payload)) {
+ if (!isRecord(payload)) {
throw new ContractValidationError(`${path}: expected a JSON object.`);
}
validateParsedJson(payload, path);
@@ -986,7 +966,7 @@ function validateParsedJson(value: unknown, context: string): void {
return;
}
if (typeof value === "string") {
- if (!isWellFormedUnicode(value)) {
+ if (!value.isWellFormed()) {
throw new ContractValidationError(
`${context}: expected well-formed Unicode JSON strings.`,
);
@@ -999,9 +979,9 @@ function validateParsedJson(value: unknown, context: string): void {
}
return;
}
- if (isJsonRecord(value)) {
+ if (isRecord(value)) {
for (const [key, item] of Object.entries(value)) {
- if (!isWellFormedUnicode(key)) {
+ if (!key.isWellFormed()) {
throw new ContractValidationError(
`${context}: expected well-formed Unicode JSON keys.`,
);
@@ -1011,10 +991,6 @@ function validateParsedJson(value: unknown, context: string): void {
}
}
-function isWellFormedUnicode(value: string): boolean {
- return Buffer.from(value, "utf8").toString("utf8") === value;
-}
-
function createValidator(): Ajv2020 {
// The plugin schemas are the immutable v0 contract. They are valid Draft
// 2020-12 but intentionally omit redundant local `type` keywords that Ajv's
@@ -1094,7 +1070,6 @@ async function openCheckedScanFile(
throwIfAborted(signal);
if (
!current.isDirectory() ||
- current.isSymbolicLink() ||
current.dev !== parent.metadata.dev ||
current.ino !== parent.metadata.ino
) {
@@ -1107,7 +1082,6 @@ async function openCheckedScanFile(
throwIfAborted(signal);
if (
!current.isFile() ||
- current.isSymbolicLink() ||
current.dev !== checked.metadata.dev ||
current.ino !== checked.metadata.ino
) {
@@ -1145,7 +1119,6 @@ export async function sameCheckedFileDevice(
if (
!openedIdentity.isFile() ||
!checkedIdentity.isFile() ||
- checkedIdentity.isSymbolicLink() ||
openedIdentity.ino !== checkedIdentity.ino
) {
return false;
@@ -1236,11 +1209,8 @@ function schemaError(
.join(".");
const keyword = first?.keyword ?? "unknown";
const count = errors.length;
- const format = first?.params["format"];
- const detail =
- keyword === "format" && format === "date-time" ? "; date-time" : "";
return new ContractValidationError(
- `${filename}:${location}: schema validation failed (${keyword}${detail}; ${count} ${count === 1 ? "error" : "errors"}).`,
+ `${filename}:${location}: schema validation failed (${keyword}${keyword === "format" && first?.params["format"] === "date-time" ? "; date-time" : ""}; ${count} ${count === 1 ? "error" : "errors"}).`,
);
}
@@ -1251,23 +1221,6 @@ function sameArray(left: readonly string[], right: readonly string[]): boolean {
);
}
-function sameSet(
- left: ReadonlySet<string>,
- right: ReadonlySet<string>,
-): boolean {
- return (
- left.size === right.size && [...left].every((value) => right.has(value))
- );
-}
-
-function isContained(root: string, candidate: string): boolean {
- const child = relative(root, candidate);
- return (
- child === "" ||
- (child !== ".." && !child.startsWith(`..${sep}`) && !isAbsolute(child))
- );
-}
-
function nodeErrorCode(error: unknown): string | undefined {
return typeof error === "object" && error !== null && "code" in error
? String(error.code)
diff --git a/sdk/typescript/src/deduplication/checkpointed-review.ts b/sdk/typescript/src/deduplication/checkpointed-review.ts
index 6639b2d97..d6cedb27d 100644
--- a/sdk/typescript/src/deduplication/checkpointed-review.ts
+++ b/sdk/typescript/src/deduplication/checkpointed-review.ts
@@ -30,18 +30,17 @@ export async function reviewSettingsDigest(
),
codexSecurityCredentialHome(environment),
]);
- const configs = await Promise.all(
- [...homes].map(async (home) => {
- try {
- return await readFile(join(home, "config.toml"), "utf8");
- } catch (error) {
- if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
- throw error;
- }
- }),
- );
return workflowDigest({
- configs,
+ configs: await Promise.all(
+ [...homes].map(async (home) => {
+ try {
+ return await readFile(join(home, "config.toml"), "utf8");
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
+ throw error;
+ }
+ }),
+ ),
command: resolveCodexCommand(environment),
baseUrl: environment["OPENAI_BASE_URL"],
});
diff --git a/sdk/typescript/src/deduplication/records.ts b/sdk/typescript/src/deduplication/records.ts
index cd66d7c38..860126cd5 100644
--- a/sdk/typescript/src/deduplication/records.ts
+++ b/sdk/typescript/src/deduplication/records.ts
@@ -1,4 +1,4 @@
-import { randomUUID, hash } from "node:crypto";
+import { hash, randomUUID } from "node:crypto";
import { z } from "incur";
import { DeduplicationReviewError } from "../errors.js";
import type { Finding } from "../models.js";
@@ -34,17 +34,15 @@ export function recordsReviewAttribution(
return observationId;
}),
);
- const beneficiaryObservationIds = [...participants]
- .filter((id) => anchors.has(id))
- .sort();
+ const beneficiaryObservationIds = [
+ ...participants.intersection(anchors),
+ ].sort();
if (beneficiaryObservationIds.length === 0)
throw new Error("Records review has no incoming observation participants.");
return {
version: 1,
beneficiaryObservationIds,
- contextObservationIds: [...participants]
- .filter((id) => !anchors.has(id))
- .sort(),
+ contextObservationIds: [...participants.difference(anchors)].sort(),
};
}
diff --git a/sdk/typescript/src/deduplication/scan.ts b/sdk/typescript/src/deduplication/scan.ts
index f93828efc..db9f39b47 100644
--- a/sdk/typescript/src/deduplication/scan.ts
+++ b/sdk/typescript/src/deduplication/scan.ts
@@ -1,7 +1,7 @@
import { loadContractWithScanDirectory } from "../contract.js";
import {
bundledPluginRoot,
- codexSecurityStateDirectory,
+ workbenchEnvironment,
resolvePluginPython,
runWorkbench,
} from "../runtime.js";
@@ -87,13 +87,9 @@ export async function deduplicateScanDirectoryInternal(
): Promise<DeduplicateScanResult> {
options.signal?.throwIfAborted();
deduplicationConcurrency(options.concurrency);
- const repository = await normalizeRepository(
- options.repository,
- options.signal,
- );
return await deduplicateResolvedScan(
scanDirectory,
- repository,
+ await normalizeRepository(options.repository, options.signal),
options.expectedScanId,
options,
dependencies,
@@ -117,10 +113,7 @@ export async function deduplicateScanInternal(
runWorkbench:
dependencies.runWorkbench ??
(async (args) => {
- const stateEnvironment = {
- ...environment,
- CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(environment),
- };
+ const stateEnvironment = workbenchEnvironment(environment);
return await runWorkbench(
{
environment: stateEnvironment,
diff --git a/sdk/typescript/src/finding-workflow.ts b/sdk/typescript/src/finding-workflow.ts
index 998d73772..2d0be15c7 100644
--- a/sdk/typescript/src/finding-workflow.ts
+++ b/sdk/typescript/src/finding-workflow.ts
@@ -7,6 +7,7 @@ import {
bundledPluginRoot,
canonicalizeModelSafePath,
codexSecurityStateDirectory,
+ workbenchEnvironment,
resolvePluginPython,
runWorkbench,
type WorkbenchCommandOptions,
@@ -183,10 +184,7 @@ export class FindingWorkflow {
environment: this.environment,
configuredPath: this.pythonPath,
}),
- environment: {
- ...this.environment,
- CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(this.environment),
- },
+ environment: workbenchEnvironment(this.environment),
failureMessage: "Could not save or resume the findings workflow",
}))();
return await this.workbench(await this.options, args, input);
diff --git a/sdk/typescript/src/findings-import.ts b/sdk/typescript/src/findings-import.ts
index 1f2b70f21..4dcfb6fce 100644
--- a/sdk/typescript/src/findings-import.ts
+++ b/sdk/typescript/src/findings-import.ts
@@ -1,4 +1,4 @@
-import { hash } from "node:crypto";
+import { sha256Text as sha256 } from "./contract.js";
import { readFile } from "node:fs/promises";
import { isAbsolute, join, posix } from "node:path";
import Ajv2020 from "ajv/dist/2020.js";
@@ -177,8 +177,7 @@ export function bindImportedFindings(
return findings.map((finding) => {
const ruleId = `import.${format}`;
const anchor = finding.occurrenceId;
- const fingerprint = `codex-security/v1:sha256:${hash(
- "sha256",
+ const fingerprint = `codex-security/v1:sha256:${sha256(
["codex-security/v1", targetId, ruleId, anchor, ""].join("\0"),
)}`;
// A source report path names another scan's artifact. Preserve it as
@@ -186,8 +185,8 @@ export function bindImportedFindings(
const { writeup, ...content } = finding;
return {
...content,
- findingId: `csf_${hash("sha256", fingerprint).slice(0, 24)}`,
- occurrenceId: `occ_${hash("sha256", [scanId, fingerprint].join("\0")).slice(0, 24)}`,
+ findingId: `csf_${sha256(fingerprint).slice(0, 24)}`,
+ occurrenceId: `occ_${sha256([scanId, fingerprint].join("\0")).slice(0, 24)}`,
ruleId,
identity: { anchor },
fingerprints: {
@@ -283,18 +282,15 @@ function decodeExportedCsvCell(value: string): string {
export function csvRowFinding(row: CsvFindingRow, scanId: string): Finding {
const ruleId = "import.csv";
const anchor = row.finding_id;
- const fingerprint = `codex-security/v1:sha256:${hash(
- "sha256",
+ const fingerprint = `codex-security/v1:sha256:${sha256(
["codex-security/v1", CSV_TARGET_ID, ruleId, anchor, ""].join("\0"),
)}`;
- const findingId = `csf_${hash("sha256", fingerprint).slice(0, 24)}`;
- const occurrenceId = `occ_${hash(
- "sha256",
- [scanId, fingerprint].join("\0"),
- ).slice(0, 24)}`;
return {
- findingId,
- occurrenceId,
+ findingId: `csf_${sha256(fingerprint).slice(0, 24)}`,
+ occurrenceId: `occ_${sha256([scanId, fingerprint].join("\0")).slice(
+ 0,
+ 24,
+ )}`,
ruleId,
identity: { anchor },
fingerprints: {
@@ -347,8 +343,7 @@ function requiredCsvText(column: string) {
function validCsvLine(value: string): boolean {
if (!/^[1-9]\d*$/u.test(value)) return false;
- const line = Number(value);
- return Number.isSafeInteger(line);
+ return Number.isSafeInteger(Number(value));
}
function safeFindingPath(value: string): boolean {
diff --git a/sdk/typescript/src/knowledge-base.ts b/sdk/typescript/src/knowledge-base.ts
index 79b0c8ca9..183ca8f7b 100644
--- a/sdk/typescript/src/knowledge-base.ts
+++ b/sdk/typescript/src/knowledge-base.ts
@@ -125,7 +125,6 @@ async function discover(
for (const entry of entries) {
signal?.throwIfAborted();
const path = join(directory, entry.name);
- if (entry.isSymbolicLink()) continue;
if (entry.isDirectory()) {
for (const document of await discover(path, signal)) {
documents.push(document);
diff --git a/sdk/typescript/src/multiscan.ts b/sdk/typescript/src/multiscan.ts
index 275a5dc9d..f1c39d809 100644
--- a/sdk/typescript/src/multiscan.ts
+++ b/sdk/typescript/src/multiscan.ts
@@ -186,10 +186,7 @@ export async function runMultiscan(
const requestedOutput = resolve(options.outputDir);
if (options.recoverScan !== undefined) {
const manifest = await lstat(join(requestedOutput, "manifest.json")).catch(
- (error: NodeJS.ErrnoException) => {
- if (error.code !== "ENOENT") throw error;
- return undefined;
- },
+ undefinedIfMissingFile,
);
if (!manifest?.isFile())
throw new Error("Bulk recovery requires an existing campaign manifest.");
@@ -316,9 +313,8 @@ async function runCampaign(
if (options.recoverScan !== undefined) {
await ensureOutputDirectory(artifactRoot);
for (const name of await readdir(artifactRoot)) {
- const match = /^attempt-([1-9][0-9]*)$/u.exec(name);
- if (match && Number.isSafeInteger(Number(match[1])))
- attempt = Math.max(attempt, Number(match[1]));
+ const prior = Number(/^attempt-([1-9][0-9]*)$/u.exec(name)?.[1]);
+ if (Number.isSafeInteger(prior)) attempt = Math.max(attempt, prior);
}
}
for (let retry = 0; retry < options.maxAttempts; retry += 1) {
@@ -343,12 +339,7 @@ async function runCampaign(
Partial<Pick<ScanResult, "threatModelPath">>)
| undefined;
if (options.recoverScan !== undefined && retry === 0 && attempt > 0) {
- const existing = await lstat(scanDir).catch(
- (error: NodeJS.ErrnoException) => {
- if (error.code !== "ENOENT") throw error;
- return undefined;
- },
- );
+ const existing = await lstat(scanDir).catch(undefinedIfMissingFile);
if (existing !== undefined) {
await ensureOutputDirectory(scanDir);
const retainedCheckout = join(
@@ -577,10 +568,7 @@ function notifyProgress(
async function ensureOutputDirectory(path: string): Promise<string> {
const metadata = await lstat(path, { bigint: true }).catch(
- (error: NodeJS.ErrnoException) => {
- if (error.code !== "ENOENT") throw error;
- return undefined;
- },
+ undefinedIfMissingFile,
);
if (metadata?.isSymbolicLink()) {
throw new Error("Multiscan output directories must not be symbolic links.");
@@ -673,10 +661,7 @@ async function acquireLock(output: string): Promise<() => Promise<void>> {
await writeFile(ownerPath, owner, { flag: "wx", mode: 0o600 });
} catch (error) {
const currentLock = await lstat(path, { bigint: true }).catch(
- (cleanup: NodeJS.ErrnoException) => {
- if (cleanup.code !== "ENOENT") throw cleanup;
- return undefined;
- },
+ undefinedIfMissingFile,
);
if (
currentLock?.dev === createdLock.dev &&
@@ -711,10 +696,7 @@ async function acquireLock(output: string): Promise<() => Promise<void>> {
clearInterval(timer);
await heartbeat;
const current = await readFile(ownerPath, "utf8").catch(
- (error: NodeJS.ErrnoException) => {
- if (error.code !== "ENOENT") throw error;
- return undefined;
- },
+ undefinedIfMissingFile,
);
if (current === owner) await rm(path, { recursive: true });
};
@@ -1141,3 +1123,8 @@ export function buildGitHubCredentialArgs(host: string | undefined): string[] {
const key = `credential.${url.origin}.helper`;
return ["-c", `${key}=`, "-c", `${key}=!gh auth git-credential`];
}
+
+function undefinedIfMissingFile(error: NodeJS.ErrnoException): undefined {
+ if (error.code !== "ENOENT") throw error;
+ return undefined;
+}
diff --git a/sdk/typescript/src/owner-evidence.ts b/sdk/typescript/src/owner-evidence.ts
index 4e8167c28..df4a63333 100644
--- a/sdk/typescript/src/owner-evidence.ts
+++ b/sdk/typescript/src/owner-evidence.ts
@@ -5,6 +5,7 @@ import { promisify } from "node:util";
import { CodexSecurityError, InvalidTargetError } from "./errors.js";
import type { OwnerFinding } from "./suggest-owners.js";
import {
+ nullIfMissingFile,
enclosingGitWorktreeRoot,
gitMetadataDirectories,
normalizeRepository,
@@ -124,10 +125,7 @@ async function requireBoundMetadata(
const path = pending.pop()!;
if (visited.has(path)) continue;
visited.add(path);
- const entry = await lstat(path).catch((error: NodeJS.ErrnoException) => {
- if (error.code === "ENOENT") return null;
- throw error;
- });
+ const entry = await lstat(path).catch(nullIfMissingFile);
if (entry?.isSymbolicLink()) {
const destination = await realpath(path);
if (
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 408b8eaa4..19cd3f8a9 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -3156,3 +3156,15 @@ function nullIfMissingFileError(error: unknown): null {
if (nodeErrorCode(error) === "ENOENT") return null;
throw error;
}
+
+/** @internal */
+export function workbenchEnvironment(environment: ProcessEnvironment) {
+ return {
+ ...environment,
+ CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(environment),
+ };
+}
+
+/** @internal */
+export const resolveWorkbenchRuntime = (options: PluginPythonOptions) =>
+ Promise.all([resolvePluginPython(options), bundledPluginRoot()]);
diff --git a/sdk/typescript/src/saved-scan.ts b/sdk/typescript/src/saved-scan.ts
index dab9e8ad6..7d25cb85b 100644
--- a/sdk/typescript/src/saved-scan.ts
+++ b/sdk/typescript/src/saved-scan.ts
@@ -1,3 +1,4 @@
+import { isNonEmptyString } from "./value.js";
import { lstat } from "node:fs/promises";
import { resolve } from "node:path";
import type { JsonObject } from "./config.js";
@@ -70,15 +71,13 @@ export async function resolveCompletedScan(
throw new CodexSecurityError(`Scan ${scanId} is not complete.`);
}
const storedDirectory = scan["scanDir"];
- const scanDir =
- typeof storedDirectory === "string" && storedDirectory.length > 0
- ? resolve(dependencies.currentDirectory(), expandHome(storedDirectory))
- : undefined;
- const metadata =
- scanDir === undefined
- ? undefined
- : await lstat(scanDir).catch(() => undefined);
- if (scanDir === undefined || metadata?.isDirectory() !== true) {
+ const scanDir = isNonEmptyString(storedDirectory)
+ ? resolve(dependencies.currentDirectory(), expandHome(storedDirectory))
+ : undefined;
+ if (
+ scanDir === undefined ||
+ (await lstat(scanDir).catch(() => undefined))?.isDirectory() !== true
+ ) {
throw new CodexSecurityError(
`Artifacts for scan ${scanId} are unavailable. Restore the saved scan artifacts or run a new scan.`,
);
diff --git a/sdk/typescript/src/scan-comparison.ts b/sdk/typescript/src/scan-comparison.ts
index 473941c3b..b092be417 100644
--- a/sdk/typescript/src/scan-comparison.ts
+++ b/sdk/typescript/src/scan-comparison.ts
@@ -36,7 +36,7 @@ import {
compactFinding,
findingCatalogue,
groupFindings,
- type ComparisonFinding,
+ type ComparisonFinding as Finding,
} from "./finding-catalogue.js";
import {
codexSecurityCredentialHome,
@@ -55,7 +55,6 @@ import {
/** @internal */
export { environmentEntry } from "./auth.js";
-type Finding = ComparisonFinding;
type ReadOnlyCodexThreadSource = Extract<
CodexSecurityThreadSource,
| typeof CODEX_SECURITY_THREAD_SOURCES.scan
@@ -353,10 +352,8 @@ export async function matchScanFindingsInternal(
outputSchema: z.toJSONSchema(matchingTurnSchema.required(), {
target: "draft-7",
}),
- ...(options.cyberAccessProgram === undefined
- ? {}
- : { cyberAccessProgram: options.cyberAccessProgram }),
- ...(options.signal === undefined ? {} : { signal: options.signal }),
+ cyberAccessProgram: options.cyberAccessProgram,
+ signal: options.signal,
};
let prompt = comparisonPrompt(pages[0]!, 0, pages.length);
progress("catalogue", 1);
@@ -534,10 +531,7 @@ export async function matchScanFindingsInternal(
async function startReadOnlyCodexThread(
options: ReadOnlyCodexOptions,
- runtimeOptions: {
- surface: CodexSecuritySurface;
- threadSource: ReadOnlyCodexThreadSource;
- },
+ runtimeOptions: Parameters<typeof runReadOnlyCodex>[3],
): Promise<ReturnType<ReadOnlyCodex["startThread"]>> {
const config =
options.config === undefined
@@ -579,15 +573,6 @@ async function startReadOnlyCodexThread(
const effectiveFeatures = resolveCodexProfile(
scanCyberAccessConfig(providerConfig, options.cyberAccessProgram),
)["features"] as JsonObject | undefined;
- const cyberFeatures: JsonObject = {};
- for (const feature of [
- "api_key_cyber_access_programs",
- "api_key_model_discovery",
- ]) {
- if (effectiveFeatures?.[feature] !== undefined) {
- cyberFeatures[feature] = effectiveFeatures[feature];
- }
- }
const environment =
options.codex === undefined
? await comparisonEnvironment(
@@ -627,7 +612,10 @@ async function startReadOnlyCodexThread(
codex_security_surface: runtimeOptions.surface,
},
features: {
- ...cyberFeatures,
+ api_key_cyber_access_programs:
+ effectiveFeatures?.["api_key_cyber_access_programs"],
+ api_key_model_discovery:
+ effectiveFeatures?.["api_key_model_discovery"],
apps: false,
code_mode: false,
code_mode_only: false,
@@ -670,10 +658,8 @@ export async function runReadOnlyCodex(
const thread = await startReadOnlyCodexThread(options, runtimeOptions);
const turn = await thread.run(prompt, {
outputSchema,
- ...(options.cyberAccessProgram === undefined
- ? {}
- : { cyberAccessProgram: options.cyberAccessProgram }),
- ...(options.signal === undefined ? {} : { signal: options.signal }),
+ cyberAccessProgram: options.cyberAccessProgram,
+ signal: options.signal,
});
return turn.finalResponse;
}
@@ -856,21 +842,19 @@ function reconcileComparison(
},
];
});
+ const isSeparateGroup = ({
+ beforeOccurrenceId,
+ afterOccurrenceId,
+ }: ScanComparisonPair) =>
+ groupByOccurrence.get(beforeOccurrenceId) !==
+ groupByOccurrence.get(afterOccurrenceId);
const comparison = {
matches,
- uncertain: response.uncertain.filter(
- ({ beforeOccurrenceId, afterOccurrenceId }) =>
- groupByOccurrence.get(beforeOccurrenceId) !==
- groupByOccurrence.get(afterOccurrenceId),
- ),
+ uncertain: response.uncertain.filter(isSeparateGroup),
...(response.related === undefined
? {}
: {
- related: response.related.filter(
- ({ beforeOccurrenceId, afterOccurrenceId }) =>
- groupByOccurrence.get(beforeOccurrenceId) !==
- groupByOccurrence.get(afterOccurrenceId),
- ),
+ related: response.related.filter(isSeparateGroup),
}),
};
validateComparison(input, comparison, allowHistoricalUncertainty, true);
@@ -923,12 +907,7 @@ export function comparisonFindingGroups(
comparison: ScanComparisonResult,
): string[][] {
const findingIds = new Map(
- [...input.before, ...input.after].flatMap((finding) =>
- typeof finding["findingId"] === "string" &&
- finding["findingId"].trim().length > 0
- ? [[finding.occurrenceId, finding["findingId"]] as const]
- : [],
- ),
+ [...input.before, ...input.after].flatMap(findingIdEntry),
);
return comparison.matches.flatMap((match) => {
const ids = [
@@ -1018,18 +997,11 @@ function requiredEvidenceRequest(
omitted: Record<"before" | "after", ReadonlySet<string>>,
requested: Record<"before" | "after", ReadonlyMap<string, EvidenceCursor>>,
): EvidenceRequest | undefined {
- const required = {
- before: new Set([
- ...omitted.before,
- ...matches.flatMap((match) => match.beforeOccurrenceIds),
- ]),
- after: new Set([
- ...omitted.after,
- ...matches.flatMap((match) => match.afterOccurrenceIds),
- ]),
- };
for (const side of ["before", "after"] as const) {
- for (const id of required[side]) {
+ const required = new Set(omitted[side]);
+ for (const match of matches)
+ for (const id of match[`${side}OccurrenceIds`]) required.add(id);
+ for (const id of required) {
const cursor = requested[side].get(id);
if (cursor !== undefined && cursor.nextOffset !== null) {
return {
@@ -1061,8 +1033,8 @@ function requiredEvidenceRequest(
].join("\n"),
);
for (const side of ["before", "after"] as const) {
- for (const id of required[side]) {
- if (requested[side].has(id) || !omitted[side].has(id)) continue;
+ for (const id of omitted[side]) {
+ if (requested[side].has(id)) continue;
const identities = missing[`${side}OccurrenceIds`];
const length = characterCount(JSON.stringify(id));
const separator = identities.length === 0 ? 0 : 1;
@@ -1232,12 +1204,7 @@ function validateComparison(
);
const afterIds = new Set(input.after.map(({ occurrenceId }) => occurrenceId));
const findingIds = new Map(
- [...input.before, ...input.after].flatMap((finding) =>
- typeof finding["findingId"] === "string" &&
- finding["findingId"].trim().length > 0
- ? [[finding.occurrenceId, finding["findingId"]] as const]
- : [],
- ),
+ [...input.before, ...input.after].flatMap(findingIdEntry),
);
const matchedBefore = new Map<string, number>();
const matchedAfter = new Map<string, number>();
@@ -1372,3 +1339,10 @@ function validateComparison(
relatedPairs.add(pair);
}
}
+
+function findingIdEntry(finding: Finding) {
+ return typeof finding["findingId"] === "string" &&
+ finding["findingId"].trim().length > 0
+ ? [[finding.occurrenceId, finding["findingId"]] as const]
+ : [];
+}
diff --git a/sdk/typescript/src/server/findings-service.ts b/sdk/typescript/src/server/findings-service.ts
deleted file mode 100644
index a271acd18..000000000
--- a/sdk/typescript/src/server/findings-service.ts
+++ /dev/null
@@ -1,46 +0,0 @@
-import type { Finding } from "../models.js";
-import type { DashboardQuery } from "./dashboard-types.js";
-import type { FindingSearchScope } from "../finding-retrieval.js";
-import type { FindingEmbedder } from "./embeddings.js";
-import type { FindingsPage, FindingsStore } from "./storage.js";
-
-export class FindingsService {
- constructor(
- private readonly store: FindingsStore,
- private readonly embeddings: FindingEmbedder,
- ) {}
-
- async insert(
- findings: readonly Finding[],
- repositoryId?: string,
- ): Promise<string[]> {
- const embeddings = await this.embeddings.embed(findings);
- return await this.store.insert(
- findings.map((finding, index) => ({
- finding,
- embedding: embeddings[index]!,
- })),
- repositoryId,
- );
- }
-
- async potentialDuplicates(findingId: string, scope: FindingSearchScope) {
- return await this.store.findPotentialDuplicates(findingId, scope);
- }
-
- async storeDedupeGroups(groups: readonly string[][]) {
- return await this.store.storeDedupeGroups(groups);
- }
-
- async listDedupeGroups(findingId: string) {
- return await this.store.listDedupeGroups(findingId);
- }
-
- async list(page: { limit: number; offset: number }): Promise<FindingsPage> {
- return await this.store.list(page);
- }
-
- async dashboard(query: DashboardQuery) {
- return await this.store.dashboard(query);
- }
-}
diff --git a/sdk/typescript/src/server/routes.ts b/sdk/typescript/src/server/routes.ts
index d94e11142..3dfa3d072 100644
--- a/sdk/typescript/src/server/routes.ts
+++ b/sdk/typescript/src/server/routes.ts
@@ -2,7 +2,8 @@ import type { IncomingMessage, ServerResponse } from "node:http";
import type { ValidateFunction } from "ajv";
import { FindingsError } from "./errors.js";
import { dashboardQuery, serveDashboard } from "./dashboard.js";
-import type { FindingsService } from "./findings-service.js";
+import type { FindingEmbedder } from "./embeddings.js";
+import type { FindingsStore } from "./storage.js";
import {
findingSearchScope,
pagination,
@@ -13,7 +14,8 @@ import {
export async function handleFindingsRequest(
request: IncomingMessage,
response: ServerResponse,
- service: FindingsService,
+ store: FindingsStore,
+ embedder: FindingEmbedder,
validate: ValidateFunction<FindingsRequest>,
): Promise<void> {
try {
@@ -29,13 +31,13 @@ export async function handleFindingsRequest(
json(
response,
200,
- await service.dashboard(dashboardQuery(url.searchParams)),
+ await store.dashboard(dashboardQuery(url.searchParams)),
);
return;
}
if (route === "GET /v1/findings") {
console.log(route);
- json(response, 200, await service.list(pagination(url.searchParams)));
+ json(response, 200, await store.list(pagination(url.searchParams)));
return;
}
const candidates = /^\/v1\/finding\/([^/]+)\/potential-duplicates$/.exec(
@@ -46,7 +48,7 @@ export async function handleFindingsRequest(
json(
response,
200,
- await service.potentialDuplicates(
+ await store.findPotentialDuplicates(
candidates[1]!,
findingSearchScope(url.searchParams),
),
@@ -58,7 +60,7 @@ export async function handleFindingsRequest(
);
if (request.method === "GET" && dedupeGroups) {
console.log("GET /v1/finding/:id/dedupe-groups");
- json(response, 200, await service.listDedupeGroups(dedupeGroups[1]!));
+ json(response, 200, await store.listDedupeGroups(dedupeGroups[1]!));
return;
}
if (route === "POST /v1/dedupe-groups") {
@@ -70,7 +72,7 @@ export async function handleFindingsRequest(
"Expected {groups: [[findingId, ...], ...]} with at least two distinct finding IDs per group.",
);
}
- json(response, 201, await service.storeDedupeGroups(input.groups));
+ json(response, 201, await store.storeDedupeGroups(input.groups));
return;
}
if (route === "POST /v1/bulk/findings") {
@@ -82,10 +84,17 @@ export async function handleFindingsRequest(
"Expected {findings: [...]} with an optional nonempty repositoryId, using the existing Finding schema.",
);
}
+ const embeddings = await embedder.embed(input.findings);
json(
response,
201,
- await service.insert(input.findings, input.repositoryId),
+ await store.insert(
+ input.findings.map((finding, index) => ({
+ finding,
+ embedding: embeddings[index]!,
+ })),
+ input.repositoryId,
+ ),
);
return;
}
diff --git a/sdk/typescript/src/server/server.ts b/sdk/typescript/src/server/server.ts
index 633e5731b..4444ca87a 100644
--- a/sdk/typescript/src/server/server.ts
+++ b/sdk/typescript/src/server/server.ts
@@ -1,7 +1,6 @@
import { once } from "node:events";
import { createServer, type Server } from "node:http";
import type { FindingEmbedder } from "./embeddings.js";
-import { FindingsService } from "./findings-service.js";
import { handleFindingsRequest } from "./routes.js";
import type { FindingsStore } from "./storage.js";
import { findingsRequestValidator } from "./validation.js";
@@ -14,9 +13,9 @@ export async function startFindingsServer(options: {
}): Promise<Server> {
await options.store.initialize();
const validate = await findingsRequestValidator();
- const service = new FindingsService(options.store, options.embeddings);
+ const { store, embeddings } = options;
const server = createServer((request, response) => {
- void handleFindingsRequest(request, response, service, validate);
+ void handleFindingsRequest(request, response, store, embeddings, validate);
});
server.listen(options.port, options.host);
await once(server, "listening");
diff --git a/sdk/typescript/src/server/sqlite-store.ts b/sdk/typescript/src/server/sqlite-store.ts
index 4794fa928..c05052dab 100644
--- a/sdk/typescript/src/server/sqlite-store.ts
+++ b/sdk/typescript/src/server/sqlite-store.ts
@@ -1,7 +1,6 @@
import {
- bundledPluginRoot,
- codexSecurityStateDirectory,
- resolvePluginPython,
+ workbenchEnvironment,
+ resolveWorkbenchRuntime,
runWorkbench,
type WorkbenchCommandOptions,
} from "../runtime.js";
@@ -104,11 +103,9 @@ export class SqliteFindingsStore implements FindingsStore {
}
async listDedupeGroups(findingId: string): Promise<FindingDedupeGroup[]> {
- const result = await this.run([
- "list-dedupe-groups",
- `--finding-id=${findingId}`,
- ]);
- return result["groups"] as unknown as FindingDedupeGroup[];
+ return (
+ await this.run(["list-dedupe-groups", `--finding-id=${findingId}`])
+ )["groups"] as unknown as FindingDedupeGroup[];
}
private async run(args: string[], input?: string) {
@@ -117,14 +114,8 @@ export class SqliteFindingsStore implements FindingsStore {
}
private async resolveOptions(): Promise<WorkbenchCommandOptions> {
- const environment = {
- ...this.environment,
- CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(this.environment),
- };
- const [python, pluginRoot] = await Promise.all([
- resolvePluginPython({ environment }),
- bundledPluginRoot(),
- ]);
+ const environment = workbenchEnvironment(this.environment);
+ const [python, pluginRoot] = await resolveWorkbenchRuntime({ environment });
return {
python,
pluginRoot,
diff --git a/sdk/typescript/src/severity-store.ts b/sdk/typescript/src/severity-store.ts
index 06280914a..a5ce39395 100644
--- a/sdk/typescript/src/severity-store.ts
+++ b/sdk/typescript/src/severity-store.ts
@@ -9,11 +9,11 @@ import {
import type { JsonObject } from "./config.js";
import { CodexSecurityError } from "./errors.js";
import {
- bundledPluginRoot,
canonicalizeModelSafePath,
codexSecurityStateDirectory,
+ workbenchEnvironment,
requireOutputOutsideRepository,
- resolvePluginPython,
+ resolveWorkbenchRuntime,
runWorkbench,
type WorkbenchCommandOptions,
} from "./runtime.js";
@@ -103,32 +103,25 @@ export class SeverityStore {
}
private async run(args: string[], input?: object) {
- const options = await (this.options ??= this.resolveOptions());
return runWorkbench(
- options,
+ await (this.options ??= this.resolveOptions()),
args,
input === undefined ? undefined : JSON.stringify(input),
);
}
private async resolveOptions(): Promise<WorkbenchCommandOptions> {
- const environment = {
- ...this.environment,
- CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(this.environment),
- };
+ const environment = workbenchEnvironment(this.environment);
requireOutputOutsideRepository(
this.scanDirectory,
await canonicalizeModelSafePath(environment.CODEX_SECURITY_STATE_DIR),
"runtime",
);
- const [python, pluginRoot] = await Promise.all([
- resolvePluginPython({
- environment,
- protectedRoot: this.scanDirectory,
- signal: this.signal,
- }),
- bundledPluginRoot(),
- ]);
+ const [python, pluginRoot] = await resolveWorkbenchRuntime({
+ environment,
+ protectedRoot: this.scanDirectory,
+ signal: this.signal,
+ });
return {
python,
pluginRoot,
diff --git a/sdk/typescript/src/value.ts b/sdk/typescript/src/value.ts
index 61d5c3b6f..35ecc5497 100644
--- a/sdk/typescript/src/value.ts
+++ b/sdk/typescript/src/value.ts
@@ -7,3 +7,8 @@ export function isSafeNonNegativeInteger(value: unknown): value is number {
export function isNonEmptyString(value: unknown): value is string {
return typeof value === "string" && value.length > 0;
}
+
+/** @internal */
+export const findingEntry = <T extends { findingId: string }>(
+ finding: T,
+): [string, T] => [finding.findingId, finding];
diff --git a/sdk/typescript/tests-ts/classify-scan-severity.test.ts b/sdk/typescript/tests-ts/classify-scan-severity.test.ts
index c4d754d9b..7f7e42d95 100644
--- a/sdk/typescript/tests-ts/classify-scan-severity.test.ts
+++ b/sdk/typescript/tests-ts/classify-scan-severity.test.ts
@@ -1,6 +1,6 @@
import { findingFingerprint, sha256 } from "./support/finding-identity.js";
import { spawnSync } from "node:child_process";
-import { chmod, cp, readFile, rm, symlink, writeFile } from "node:fs/promises";
+import { chmod, readFile, rm, symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, expect, test } from "bun:test";
import {
@@ -18,20 +18,20 @@ import type { Finding, FindingsDocument, ScanManifest } from "../src/models.js";
import { prepareScanPublication } from "../src/publication.js";
import { publishScanInternal } from "../src/publish.js";
import { resolvePluginPython } from "../src/runtime.js";
-import { PLUGIN_ROOT } from "./plugin-root.js";
+import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
-import { createTemporaryDirectories } from "./support/temporary-directories.js";
-
-const directories = createTemporaryDirectories({ canonical: false });
+const { temporaryDirectory, cleanup } = createApiTestFixtures(
+ "classify-scan-",
+ false,
+);
const destination = { destination: "linear", teamId: "team-example" } as const;
-afterEach(directories.cleanup);
+afterEach(cleanup);
async function fixture() {
- const root = await directories.create("classify-scan-");
+ const root = await temporaryDirectory();
const scanDirectory = join(root, "scan");
- await cp(join(PLUGIN_ROOT, "examples", "completed-scan"), scanDirectory, {
- recursive: true,
- });
+ await copyCompletedScanFixture(scanDirectory);
if (process.platform !== "win32") await chmod(scanDirectory, 0o700);
const manifest = JSON.parse(
await readFile(join(scanDirectory, "scan-manifest.json"), "utf8"),
diff --git a/sdk/typescript/tests-ts/classify-severity.test.ts b/sdk/typescript/tests-ts/classify-severity.test.ts
index d138911d4..4fbbada64 100644
--- a/sdk/typescript/tests-ts/classify-severity.test.ts
+++ b/sdk/typescript/tests-ts/classify-severity.test.ts
@@ -1,5 +1,4 @@
-import { mkdtemp, rm, writeFile } from "node:fs/promises";
-import { tmpdir } from "node:os";
+import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import type { ThreadOptions, TurnOptions } from "@openai/codex-sdk";
import Ajv2020 from "ajv/dist/2020.js";
@@ -10,19 +9,16 @@ import {
type ClassifySeverityOptions,
type SeverityClassificationFinding,
} from "../src/classify-severity.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
-const directories: string[] = [];
-afterEach(async () => {
- await Promise.all(
- directories
- .splice(0)
- .map((path) => rm(path, { recursive: true, force: true })),
- );
-});
+const { temporaryDirectory, cleanup } = createApiTestFixtures(
+ "severity-policy-",
+ false,
+);
+afterEach(cleanup);
async function document(contents: string): Promise<string> {
- const directory = await mkdtemp(join(tmpdir(), "severity-policy-"));
- directories.push(directory);
+ const directory = await temporaryDirectory();
const path = join(directory, "policy.md");
await writeFile(path, contents);
return path;
diff --git a/sdk/typescript/tests-ts/codex-review.test.ts b/sdk/typescript/tests-ts/codex-review.test.ts
index 11512567a..8e7c1b321 100644
--- a/sdk/typescript/tests-ts/codex-review.test.ts
+++ b/sdk/typescript/tests-ts/codex-review.test.ts
@@ -1,13 +1,7 @@
+import { parseJsonLines } from "./support/json.js";
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
import { existsSync } from "node:fs";
-import {
- mkdir,
- mkdtemp,
- readFile,
- realpath,
- rm,
- writeFile,
-} from "node:fs/promises";
+import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { homedir, tmpdir } from "node:os";
import { join, relative, resolve, win32 } from "node:path";
import { parse, stringify } from "smol-toml";
@@ -22,6 +16,7 @@ import { environmentEntry } from "../src/scan-comparison.js";
import { runTestInSubprocess } from "./support/test-subprocess.js";
import { retryDelay, waitForRetry } from "../src/deduplication/retry.js";
import { isReviewRefusal } from "../src/deduplication/refusal.js";
+import { temporaryDirectory } from "./support/temporary-directories.js";
const fixture = fileURLToPath(
new URL("fixtures/codex-review.mjs", import.meta.url),
@@ -154,9 +149,7 @@ for (const {
const runCase = test.skipIf(windowsOnly && process.platform !== "win32");
runCase(`Codex review transport: ${name}`, async () => {
const modelHome = await mkdtemp(join(tmpdir(), "codex-review-test-"));
- const checkout = await realpath(
- await mkdtemp(join(tmpdir(), "codex-review-source-")),
- );
+ const checkout = await temporaryDirectory("codex-review-source-", true);
const ghConfig = await mkdtemp(join(tmpdir(), "codex-review-gh-"));
const transcript = join(modelHome, "messages.jsonl");
let child: ChildProcessWithoutNullStreams | undefined;
@@ -261,7 +254,17 @@ for (const {
},
},
);
- let validations = 0;
+ const validate = mock((value: unknown) => {
+ if (
+ typeof value !== "object" ||
+ value === null ||
+ !("decision" in value) ||
+ value.decision !==
+ (scenario === "incomplete-content" ? "DISTINCT" : "SAME")
+ )
+ throw new Error("Invalid decision");
+ return { decision: value.decision };
+ });
const checkpoints = checkpointWorkbench("blocked-review", {
repository: checkout,
});
@@ -295,18 +298,7 @@ for (const {
required: ["decision"],
additionalProperties: false,
},
- validate(value: unknown) {
- validations++;
- if (
- typeof value !== "object" ||
- value === null ||
- !("decision" in value) ||
- value.decision !==
- (scenario === "incomplete-content" ? "DISTINCT" : "SAME")
- )
- throw new Error("Invalid decision");
- return { decision: value.decision };
- },
+ validate,
});
if (
recovery ||
@@ -318,7 +310,7 @@ for (const {
].includes(scenario)
) {
expect(await result).toEqual({ decision: "SAME" });
- expect(validations).toBe(
+ expect(validate).toHaveBeenCalledTimes(
recovery
? recovery === "invalid-submission"
? 3
@@ -335,7 +327,7 @@ for (const {
expect(await result).toEqual({
decision: scenario === "incomplete-content" ? "DISTINCT" : "SAME",
});
- expect(validations).toBe(1);
+ expect(validate).toHaveBeenCalledTimes(1);
} else if (
["cancel", "cancel-continuation", "cancel-backoff"].includes(scenario)
) {
@@ -395,7 +387,7 @@ for (const {
expect(supportBundle).not.toContain("synthetic-review-key");
expect(supportBundle).not.toContain(checkout);
expect(supportBundle).not.toContain("review-thread");
- expect(validations).toBe(
+ expect(validate).toHaveBeenCalledTimes(
scenario === "invalid-submission"
? 2 * sessions
: modelFailures.has(scenario) ||
@@ -440,16 +432,10 @@ for (const {
`${JSON.stringify(resolve(ghConfig))}="deny"`,
);
if (scenario !== "cancel") {
- const messages = (await readFile(transcript, "utf8"))
- .trim()
- .split("\n")
- .map(
- (line) =>
- JSON.parse(line) as {
- method?: string;
- params?: { apiKey?: string };
- },
- );
+ const messages = parseJsonLines<{
+ method?: string;
+ params?: { apiKey?: string };
+ }>(await readFile(transcript, "utf8"));
const loginRequest = messages.find(
(message) => message.method === "account/login/start",
);
@@ -631,7 +617,7 @@ test("retry backoff grows exponentially with jitter and preserves cancellation",
test("a missing Codex executable is not retried", async () => {
const root = await mkdtemp(join(tmpdir(), "codex-review-missing-command-"));
let starts = 0;
- const delays: number[] = [];
+ const delays = mock(async (_delay: number) => {});
try {
await writeFile(join(root, "config.toml"), "");
const runner = new CodexReviewRunner(
@@ -643,9 +629,7 @@ test("a missing Codex executable is not retried", async () => {
undefined,
root,
{
- wait: async (delay) => {
- delays.push(delay);
- },
+ wait: delays,
},
);
await expect(
@@ -659,7 +643,7 @@ test("a missing Codex executable is not retried", async () => {
}),
).rejects.toThrow("ENOENT");
expect(starts).toBe(1);
- expect(delays).toEqual([]);
+ expect(delays).not.toHaveBeenCalled();
} finally {
await rm(root, { recursive: true, force: true });
}
diff --git a/sdk/typescript/tests-ts/contract.test.ts b/sdk/typescript/tests-ts/contract.test.ts
index 08dd026d8..49bda1ef4 100644
--- a/sdk/typescript/tests-ts/contract.test.ts
+++ b/sdk/typescript/tests-ts/contract.test.ts
@@ -12,24 +12,27 @@ import {
writeFile,
} from "node:fs/promises";
import { dirname, join } from "node:path";
-import { afterEach, describe, expect, test } from "bun:test";
+import { afterEach, describe, expect, test, mock } from "bun:test";
import fc from "fast-check";
import { ContractValidationError, loadContract } from "../src/index.js";
import { sameCheckedFileDevice } from "../src/contract.js";
import type { NormalizedTarget, ScanExpectation } from "../src/index.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
import { runPython } from "./support/python-probe.js";
-import { readJson as readJsonFile, writeJson } from "./support/json.js";
import { propertyOptions } from "./support/property.js";
-import { createTemporaryDirectories } from "./support/temporary-directories.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
+import { readJson as readJsonFile, writeJson } from "./support/json.js";
const EXAMPLE = join(PLUGIN_ROOT, "examples", "completed-scan");
-const temporaryDirectories = createTemporaryDirectories({ canonical: false });
+const { temporaryDirectory, cleanup } = createApiTestFixtures(
+ "codex-security-contract-",
+ false,
+);
-afterEach(temporaryDirectories.cleanup);
+afterEach(cleanup);
async function copyExample(): Promise<string> {
- const root = await temporaryDirectories.create("codex-security-contract-");
+ const root = await temporaryDirectory();
const scanDir = join(root, "scan");
await cp(EXAMPLE, scanDir, { recursive: true });
if (process.platform !== "win32") await chmod(scanDir, 0o700);
@@ -180,7 +183,7 @@ describe("canonical scan contract", () => {
let referenceDevice = highDevice;
let referenceInode = identity.ino;
let referenceRegular = true;
- let referenceClosed = 0;
+ const closeMock = mock(async () => {});
const file = {
stat: async () => {
inspected += 1;
@@ -197,9 +200,7 @@ describe("canonical scan contract", () => {
ino: referenceInode,
isFile: () => referenceRegular,
}),
- close: async () => {
- referenceClosed += 1;
- },
+ close: closeMock,
} as unknown as FileHandle;
const openReference = async () => reference;
const checked = { path, metadata, parents: [] };
@@ -208,7 +209,7 @@ describe("canonical scan contract", () => {
await expect(
sameCheckedFileDevice(file, checked, opened, "win32", openReference),
).resolves.toBe(true);
- expect(referenceClosed).toBe(1);
+ expect(closeMock).toHaveBeenCalledTimes(1);
const inconsistentNumberInode = {
dev: metadata.dev,
@@ -228,13 +229,13 @@ describe("canonical scan contract", () => {
await expect(
sameCheckedFileDevice(file, checked, opened, "win32", openReference),
).resolves.toBe(false);
- expect(referenceClosed).toBe(2);
+ expect(closeMock).toHaveBeenCalledTimes(2);
referenceDevice = device;
await expect(
sameCheckedFileDevice(file, checked, opened, "win32", openReference),
).resolves.toBe(true);
- expect(referenceClosed).toBe(3);
+ expect(closeMock).toHaveBeenCalledTimes(3);
device = highDevice;
referenceDevice = highDevice;
@@ -494,9 +495,7 @@ describe("canonical scan contract", () => {
test.skipIf(process.platform === "win32")(
"accepts a scan directory beneath a symlinked parent",
async () => {
- const root = await temporaryDirectories.create(
- "codex-security-contract-link-",
- );
+ const root = await temporaryDirectory("codex-security-contract-link-");
const parent = join(root, "actual-parent");
const linkedParent = join(root, "linked-parent");
await mkdir(parent, { mode: 0o700 });
@@ -524,9 +523,7 @@ describe("canonical scan contract", () => {
});
test("loads contract schemas larger than the previous size limit", async () => {
- const pluginRoot = await temporaryDirectories.create(
- "codex-security-schema-large-",
- );
+ const pluginRoot = await temporaryDirectory("codex-security-schema-large-");
await cp(join(PLUGIN_ROOT, "schemas"), join(pluginRoot, "schemas"), {
recursive: true,
});
@@ -576,7 +573,7 @@ describe("canonical scan contract", () => {
});
test("accepts valid schemas beyond the previous complexity limit", async () => {
- const pluginRoot = await temporaryDirectories.create(
+ const pluginRoot = await temporaryDirectory(
"codex-security-schema-complex-",
);
await cp(join(PLUGIN_ROOT, "schemas"), join(pluginRoot, "schemas"), {
@@ -593,7 +590,7 @@ describe("canonical scan contract", () => {
});
test("does not expose attacker-controlled schema compilation errors", async () => {
- const pluginRoot = await temporaryDirectories.create(
+ const pluginRoot = await temporaryDirectory(
"codex-security-schema-compile-",
);
await mkdir(join(pluginRoot, "schemas"));
diff --git a/sdk/typescript/tests-ts/dedupe-records.test.ts b/sdk/typescript/tests-ts/dedupe-records.test.ts
index 87b966e3c..2bc289e39 100644
--- a/sdk/typescript/tests-ts/dedupe-records.test.ts
+++ b/sdk/typescript/tests-ts/dedupe-records.test.ts
@@ -1,4 +1,4 @@
-import { expect, test } from "bun:test";
+import { expect, test, mock } from "bun:test";
import { DeduplicationReviewError } from "../src/errors.js";
import { mkdtemp, readFile, readdir, rm } from "node:fs/promises";
import { spawn } from "node:child_process";
@@ -19,6 +19,7 @@ import { main } from "../src/cli.js";
import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
import attributionFixtures from "./fixtures/records-review-attribution.json";
+import { rejecting, throwing } from "./support/errors.js";
test("attribution maps exact structured participants to sorted host identities", () => {
const references = new Map(
@@ -182,9 +183,7 @@ test("records honors explicit observation neighborhoods and handles empty/isolat
},
{
reviewRunner: {
- async run() {
- throw new Error("No model call expected");
- },
+ run: rejecting("No model call expected"),
},
},
);
@@ -281,34 +280,33 @@ test.each([
])(
"%s review never becomes a unique disposition or retries",
async (scenario) => {
- let calls = 0;
+ const runMock = mock(async (review: DeduplicationReviewRequest) => {
+ if (scenario === "pair-failed" && review.stage !== "pair-review")
+ return answer(review);
+ if (scenario === "failed" || scenario === "pair-failed")
+ throw new Error("Remote execution may already have been accepted");
+ if (scenario === "malformed") return "not an object";
+ if (scenario === "inconclusive") return { decision: "INCONCLUSIVE" };
+ if (scenario === "unknown-canonical") {
+ if (review.stage === "screening") return answer(review);
+ return {
+ ...decision(assigned(review)),
+ canonicalFindingId: "unknown",
+ };
+ }
+ const valid = answer(review) as {
+ decisions: Record<string, unknown>;
+ };
+ if (scenario === "missing-pair") delete valid.decisions["pair-1"];
+ if (scenario === "unknown-slot") {
+ valid.decisions["unknown"] = valid.decisions["pair-1"];
+ delete valid.decisions["pair-1"];
+ }
+ return valid;
+ });
const result = await deduplicateRecords(input(), {
reviewRunner: {
- async run(review) {
- calls++;
- if (scenario === "pair-failed" && review.stage !== "pair-review")
- return answer(review);
- if (scenario === "failed" || scenario === "pair-failed")
- throw new Error("Remote execution may already have been accepted");
- if (scenario === "malformed") return "not an object";
- if (scenario === "inconclusive") return { decision: "INCONCLUSIVE" };
- if (scenario === "unknown-canonical") {
- if (review.stage === "screening") return answer(review);
- return {
- ...decision(assigned(review)),
- canonicalFindingId: "unknown",
- };
- }
- const valid = answer(review) as {
- decisions: Record<string, unknown>;
- };
- if (scenario === "missing-pair") delete valid.decisions["pair-1"];
- if (scenario === "unknown-slot") {
- valid.decisions["unknown"] = valid.decisions["pair-1"];
- delete valid.decisions["pair-1"];
- }
- return valid;
- },
+ run: runMock,
},
});
expect(result.status).toBe("unresolved");
@@ -316,7 +314,7 @@ test.each([
expect(result.unresolved.map(({ observationId }) => observationId)).toEqual(
["a", "b", "c", "d"],
);
- expect(calls).toBe(
+ expect(runMock).toHaveBeenCalledTimes(
["pair-failed", "unknown-canonical"].includes(scenario) ? 3 : 1,
);
},
@@ -368,9 +366,7 @@ test("rejects invalid inputs before calling the host", async () => {
await expect(
deduplicateRecords(data as DeduplicateRecordsInput, {
reviewRunner: {
- async run() {
- throw new Error("Host must not be called");
- },
+ run: rejecting("Host must not be called"),
},
}),
).rejects.toThrow();
@@ -448,9 +444,7 @@ test("CLI records mode uses only the fake host, bypassing saved scans, persisten
for (const key of Object.keys(deps)) {
if (typeof deps[key as keyof typeof deps] === "function")
Object.assign(deps, {
- [key]: () => {
- throw new Error(`Unexpected dependency: ${key}`);
- },
+ [key]: throwing(`Unexpected dependency: ${key}`),
});
}
deps.addSignalListener = () => {};
@@ -588,10 +582,8 @@ test("real CLI pipes exit after a fake-host run without local Codex or state wri
child.on("error", reject);
child.on("close", resolve);
});
- let stderr = "";
- child.stderr.setEncoding("utf8").on("data", (value) => {
- stderr += value;
- });
+ const stderr = capture();
+ child.stderr.setEncoding("utf8").on("data", stderr.stream.write);
child.stdin.on("error", () => {});
try {
child.stdin.write(`${JSON.stringify(run)}\n`);
@@ -627,7 +619,7 @@ test("real CLI pipes exit after a fake-host run without local Codex or state wri
reviews.filter((review) => review.stage === "pair-review"),
).toHaveLength(2);
expect(await closed).toBe(0);
- expect(stderr).toBe("");
+ expect(stderr.text()).toBe("");
expect(final).toMatchObject({
id: "run-1",
result: {
@@ -702,13 +694,12 @@ test.each(["result", "error"] as const)(
const inputStream = new PassThrough();
const messages: Message[] = [];
let release!: () => void;
- const { promise: writing, resolve: started } =
- Promise.withResolvers<void>();
+ const writing = Promise.withResolvers<void>();
const output = new Writable({
write(chunk, _encoding, callback) {
messages.push(JSON.parse(chunk.toString()));
release = callback;
- started();
+ writing.resolve();
},
});
const controller = new AbortController();
@@ -727,7 +718,7 @@ test.each(["result", "error"] as const)(
},
})}\n`,
);
- await writing;
+ await writing.promise;
if (notification)
inputStream.write(
`${JSON.stringify({ jsonrpc: "2.0", method: "cancel", params: { id: run.id } })}\n`,
diff --git a/sdk/typescript/tests-ts/feedback.test.ts b/sdk/typescript/tests-ts/feedback.test.ts
index 333d66a2e..fe5cd2abd 100644
--- a/sdk/typescript/tests-ts/feedback.test.ts
+++ b/sdk/typescript/tests-ts/feedback.test.ts
@@ -8,6 +8,7 @@ import { afterEach, expect, test } from "bun:test";
import { sendFeedback } from "../src/feedback.js";
import { codexSecurityCredentialHome } from "../src/runtime.js";
import { VERSION, BUNDLED_PLUGIN_VERSION } from "../src/version.js";
+import { throwing } from "./support/errors.js";
const fixture = fileURLToPath(
new URL("fixtures/feedback.mjs", import.meta.url),
@@ -270,9 +271,7 @@ test("respects disabled feedback without starting Codex", async () => {
"[feedback]\nenabled = false\n",
);
await expect(
- sendFeedback(context.options, () => {
- throw new Error("Must not start Codex");
- }),
+ sendFeedback(context.options, throwing("Must not start Codex")),
).rejects.toThrow("disabled by configuration");
});
diff --git a/sdk/typescript/tests-ts/finding-catalogue.test.ts b/sdk/typescript/tests-ts/finding-catalogue.test.ts
index 7c4648b64..deb27fd68 100644
--- a/sdk/typescript/tests-ts/finding-catalogue.test.ts
+++ b/sdk/typescript/tests-ts/finding-catalogue.test.ts
@@ -1,4 +1,4 @@
-import { createHash } from "node:crypto";
+import { hash as cryptoHash } from "node:crypto";
import type { TurnOptions } from "@openai/codex-sdk";
import { describe, expect, test } from "bun:test";
import {
@@ -15,6 +15,17 @@ import {
} from "../src/scan-comparison.js";
const empty = { matches: [], uncertain: [] } satisfies ScanComparisonResult;
+const evidenceRequest = (
+ beforeOccurrenceIds: string[],
+ afterOccurrenceIds: string[] = [],
+ offset = 0,
+) => ({ kind: "evidence", beforeOccurrenceIds, afterOccurrenceIds, offset });
+
+const evidenceResponse = (...args: Parameters<typeof evidenceRequest>) => ({
+ ...empty,
+ request: evidenceRequest(...args),
+});
+
const confirmedPair = (
before: string,
after: string,
@@ -422,15 +433,7 @@ describe("finding catalogue", () => {
if (index === 0) {
expect(data<CatalogueData>(prompt).findings.before).toHaveLength(2);
expect(prompt).not.toContain("EARLIER_EVIDENCE");
- return {
- ...empty,
- request: {
- kind: "evidence",
- beforeOccurrenceIds: ["old-b"],
- afterOccurrenceIds: ["new"],
- offset: 0,
- },
- };
+ return evidenceResponse(["old-b"], ["new"]);
}
const evidence = JSON.parse(
data<EvidenceData>(prompt).content,
@@ -506,15 +509,7 @@ describe("finding catalogue", () => {
).toEqual(response);
expect(direct.prompts).toHaveLength(1);
- const evidence = conversation(() => ({
- ...empty,
- request: {
- kind: "evidence",
- beforeOccurrenceIds: ["old"],
- afterOccurrenceIds: ["new"],
- offset: 0,
- },
- }));
+ const evidence = conversation(() => evidenceResponse(["old"], ["new"]));
await expect(
matchScanFindingsInternal(
input,
@@ -694,16 +689,7 @@ describe("finding catalogue", () => {
const pieces: string[] = [];
let offset = 0;
const observed = conversation((prompt, index) => {
- if (index === 0)
- return {
- ...empty,
- request: {
- kind: "evidence",
- beforeOccurrenceIds: ["old"],
- afterOccurrenceIds: [],
- offset: 0,
- },
- };
+ if (index === 0) return evidenceResponse(["old"]);
const page = data<EvidenceData>(prompt);
expect(page.offset).toBe(offset);
pieces.push(page.content);
@@ -726,16 +712,7 @@ describe("finding catalogue", () => {
test("does not finish unrelated evidence when confirming another match", async () => {
const proposed = confirmedPair("old", "new");
const observed = conversation((prompt, index) => {
- if (index === 0)
- return {
- ...empty,
- request: {
- kind: "evidence",
- beforeOccurrenceIds: ["other"],
- afterOccurrenceIds: [],
- offset: 0,
- },
- };
+ if (index === 0) return evidenceResponse(["other"]);
expect(data<EvidenceData>(prompt).nextOffset).not.toBeNull();
return proposed;
});
@@ -786,8 +763,7 @@ describe("finding catalogue", () => {
{ before: [original], after: [finding("new")] },
{ codex: observed.codex },
);
- const hash = (value: string) =>
- createHash("sha256").update(value).digest("hex");
+ const hash = (value: string) => cryptoHash("sha256", value);
expect(pieces.length).toBeGreaterThan(1);
expect(hash(pieces.join(""))).toBe(
hash(JSON.stringify({ before: [original], after: [] })),
@@ -804,15 +780,7 @@ describe("finding catalogue", () => {
const reads = { a: 0, b: 0 };
const pieces: Record<Id, string[]> = { a: [], b: [] };
const offsets = new Map<Id, number | null>();
- const request = (id: Id, offset = 0) => ({
- ...empty,
- request: {
- kind: "evidence",
- beforeOccurrenceIds: [id],
- afterOccurrenceIds: [],
- offset,
- },
- });
+ const request = (id: Id, offset = 0) => evidenceResponse([id], [], offset);
const before = ids.map((id) =>
finding(id, {
codeEvidence: [
@@ -878,44 +846,16 @@ describe("finding catalogue", () => {
);
test.each([
- [
- "no findings",
- {
- kind: "evidence",
- beforeOccurrenceIds: [],
- afterOccurrenceIds: [],
- offset: 0,
- },
- "outside its findings",
- ],
- [
- "another finding",
- {
- kind: "evidence",
- beforeOccurrenceIds: ["outside"],
- afterOccurrenceIds: [],
- offset: 0,
- },
- "outside its findings",
- ],
+ ["no findings", evidenceRequest([]), "outside its findings"],
+ ["another finding", evidenceRequest(["outside"]), "outside its findings"],
[
"a nonzero first offset",
- {
- kind: "evidence",
- beforeOccurrenceIds: ["old"],
- afterOccurrenceIds: [],
- offset: 1,
- },
+ evidenceRequest(["old"], [], 1),
"invalid evidence offset",
],
[
"an invalid offset",
- {
- kind: "evidence",
- beforeOccurrenceIds: ["old"],
- afterOccurrenceIds: [],
- offset: 999,
- },
+ evidenceRequest(["old"], [], 999),
"invalid evidence offset",
],
[
@@ -935,12 +875,7 @@ describe("finding catalogue", () => {
});
test("stops a repeated request and honors cancellation between turns", async () => {
- const request = {
- kind: "evidence",
- beforeOccurrenceIds: ["old"],
- afterOccurrenceIds: [],
- offset: 0,
- };
+ const request = evidenceRequest(["old"]);
const repeated = conversation(() => ({ ...empty, request }));
const input = { before: [finding("old")], after: [finding("new")] };
await expect(
@@ -969,15 +904,7 @@ describe("finding catalogue", () => {
const request = (
beforeOccurrenceIds: string[],
afterOccurrenceIds: string[] = [],
- ) => ({
- ...empty,
- request: {
- kind: "evidence",
- beforeOccurrenceIds,
- afterOccurrenceIds,
- offset: 0,
- },
- });
+ ) => evidenceResponse(beforeOccurrenceIds, afterOccurrenceIds);
const requests =
scenario === "alternating"
? [request(["a"]), request([], ["new"]), request(["a"])]
@@ -999,15 +926,8 @@ describe("finding catalogue", () => {
const ids = ["a", "b", "c", "d"];
const sentBefore: string[] = [];
const sentAfter: string[] = [];
- const request = (beforeOccurrenceIds: string[]) => ({
- ...empty,
- request: {
- kind: "evidence",
- beforeOccurrenceIds,
- afterOccurrenceIds: ["new"],
- offset: 0,
- },
- });
+ const request = (beforeOccurrenceIds: string[]) =>
+ evidenceResponse(beforeOccurrenceIds, ["new"]);
const observed = conversation((prompt, index) => {
if (index > 0) {
const payload = data<EvidenceData>(prompt);
@@ -1036,15 +956,8 @@ describe("finding catalogue", () => {
codeEvidence: "x".repeat(2 * (1 << 20)) + "🙂",
});
const pieces: string[] = [];
- const request = (beforeOccurrenceIds: string[], offset = 0) => ({
- ...empty,
- request: {
- kind: "evidence",
- beforeOccurrenceIds,
- afterOccurrenceIds: [],
- offset,
- },
- });
+ const request = (beforeOccurrenceIds: string[], offset = 0) =>
+ evidenceResponse(beforeOccurrenceIds, [], offset);
const observed = conversation((prompt, index) => {
if (index === 0) return request(["small"]);
const payload = data<EvidenceData>(prompt);
diff --git a/sdk/typescript/tests-ts/finding-deduplication.test.ts b/sdk/typescript/tests-ts/finding-deduplication.test.ts
index 4cce6f3ab..4d2aecb55 100644
--- a/sdk/typescript/tests-ts/finding-deduplication.test.ts
+++ b/sdk/typescript/tests-ts/finding-deduplication.test.ts
@@ -1,7 +1,8 @@
-import { chmod, cp, mkdtemp, readFile, rm } from "node:fs/promises";
+import { emptyNeighborhoodReviewer } from "./support/deduplication.js";
+import { chmod, mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { expect, test } from "bun:test";
+import { expect, test, mock } from "bun:test";
import Ajv2020 from "ajv/dist/2020.js";
import type { Finding, FindingsDocument } from "../src/models.js";
import type { CodexReview } from "../src/deduplication/codex-review.js";
@@ -26,8 +27,9 @@ import {
deduplicateScanDirectoryInternal,
deduplicateScanInternal,
} from "../src/deduplication/scan.js";
-import { PLUGIN_ROOT } from "./plugin-root.js";
+import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";
import type { JsonObject } from "../src/config.js";
+import { fail } from "./support/errors.js";
const document: FindingsDocument = JSON.parse(
await readFile(
@@ -49,11 +51,11 @@ function entry(index: number): Finding {
},
};
}
-function candidates(findings: Finding[]) {
+function candidates(findings: Finding[], neighbors = findings) {
return {
potentialDuplicates: async (id: string) => ({
finding: findings.find((finding) => finding.findingId === id)!,
- potentialDuplicates: findings.filter(
+ potentialDuplicates: neighbors.filter(
(finding) => finding.findingId !== id,
),
}),
@@ -101,6 +103,12 @@ function screening(
};
}
+const reviewSame = async (findings: readonly Finding[]) => same(findings);
+const screenNominations =
+ (nominate: (findings: readonly Finding[]) => ReadonlySet<string>) =>
+ async (findings: readonly Finding[]) =>
+ screening(findings, nominate(findings));
+
test.each(["screening", "pair-review"])(
"%s fills each free slot immediately and preserves serial grouping",
async (stage) => {
@@ -110,14 +118,7 @@ test.each(["screening", "pair-review"])(
const nominations = new Set(
ids.map((id) => pairKey([id, neighbor.findingId])),
);
- const candidates = {
- async potentialDuplicates(id: string) {
- return {
- finding: entries[ids.indexOf(id)]!,
- potentialDuplicates: [neighbor],
- };
- },
- };
+ const candidateStore = candidates(entries, [neighbor]);
const gates = entries.map(() => Promise.withResolvers<void>());
const started = entries.map(() => Promise.withResolvers<void>());
const starts: number[] = [];
@@ -145,7 +146,7 @@ test.each(["screening", "pair-review"])(
},
};
const result = new FindingDeduplicator(
- candidates,
+ candidateStore,
reviewer,
undefined,
2,
@@ -164,7 +165,7 @@ test.each(["screening", "pair-review"])(
expect(phases.filter((phase) => phase === "screening")).toHaveLength(4);
expect(phases.filter((phase) => phase === "pair-review")).toHaveLength(4);
expect(parallel).toEqual(
- await new FindingDeduplicator(candidates, reviewer, undefined, 1).run(
+ await new FindingDeduplicator(candidateStore, reviewer, undefined, 1).run(
ids,
),
);
@@ -193,14 +194,7 @@ test("ready pairs take free slots before pending screenings and share the concur
active--;
};
const result = new FindingDeduplicator(
- {
- async potentialDuplicates(id) {
- return {
- finding: entries[ids.indexOf(id)]!,
- potentialDuplicates: [neighbor],
- };
- },
- },
+ candidates(entries, [neighbor]),
{
async screen(findings) {
await hold(ids.indexOf(findings[0]!.findingId), "screen");
@@ -385,14 +379,7 @@ test.each(["screen", "pair", "cancel"])(
const controller = new AbortController();
const failure = new Error("Synthetic mixed-stage failure");
const result = new FindingDeduplicator(
- {
- async potentialDuplicates(id) {
- return {
- finding: entries[ids.indexOf(id)]!,
- potentialDuplicates: neighbors,
- };
- },
- },
+ candidates(entries, neighbors),
{
async screen(findings) {
const index = ids.indexOf(findings[0]!.findingId);
@@ -419,11 +406,10 @@ test.each(["screen", "pair", "cancel"])(
controller.signal,
2,
).run(ids);
- let settled = false;
- const observed = result.catch((error: unknown) => {
- settled = true;
+ const observeSettled = mock((error: unknown) => {
return error;
});
+ const observed = result.catch(observeSettled);
try {
await pairStarted.promise;
expect(starts).toEqual(["screen:0", "screen:1", "pair"]);
@@ -431,7 +417,7 @@ test.each(["screen", "pair", "cancel"])(
else if (failureStage === "pair") pairGate.reject(failure);
else controller.abort(failure);
await new Promise<void>((resolve) => setImmediate(resolve));
- expect(settled).toBe(false);
+ expect(observeSettled).not.toHaveBeenCalled();
screenGate.resolve();
pairGate.resolve();
expect(await observed).toBe(failure);
@@ -465,22 +451,20 @@ test("terminal failure drains started reviews without starting queued jobs", asy
await gates[index]!.promise;
return screening(findings, new Set());
},
- async reviewPair() {
- throw new Error("Pair review must not start after screening failure");
- },
+ reviewPair: async () =>
+ fail("Pair review must not start after screening failure"),
},
undefined,
2,
).run(entries.map((entry) => entry.findingId));
- let settled = false;
- const observed = result.catch((error: unknown) => {
- settled = true;
+ const observeSettled = mock((error: unknown) => {
return error;
});
+ const observed = result.catch(observeSettled);
await Promise.all([started[0]!.promise, started[1]!.promise]);
gates[0]!.reject(failure);
await new Promise<void>((resolve) => setImmediate(resolve));
- expect(settled).toBe(false);
+ expect(observeSettled).not.toHaveBeenCalled();
gates[1]!.resolve();
expect(await observed).toBe(failure);
expect(calls).toEqual(entries.slice(0, 2).map((entry) => entry.findingId));
@@ -562,9 +546,7 @@ test("groups accepted neighbors transitively without screening them as anchors",
new Set([pairKey([ids[0]!, ids[1]!]), pairKey([ids[1]!, ids[2]!])]),
);
},
- async reviewPair(findings) {
- return same(findings);
- },
+ reviewPair: reviewSame,
});
expect(await service.run([ids[1]!])).toEqual({
uniqueFindingIds: [ids[0]!],
@@ -578,27 +560,19 @@ test("an explicit DISTINCT screening vetoes the same unordered pair", async () =
const entries = [entry(1), entry(2)];
const ids = entries.map((finding) => finding.findingId);
for (const selected of [ids, [...ids].reverse()]) {
- let pairReviews = 0;
+ const reviewPair = mock(reviewSame.bind(undefined, entries));
const service = new FindingDeduplicator(candidates(entries), {
- async screen(findings) {
- return screening(
- findings,
- findings[0]!.findingId === ids[0]
- ? new Set([pairKey(ids)])
- : new Set(),
- );
- },
- async reviewPair() {
- pairReviews += 1;
- return same(entries);
- },
+ screen: screenNominations((findings) =>
+ findings[0]!.findingId === ids[0] ? new Set([pairKey(ids)]) : new Set(),
+ ),
+ reviewPair,
});
expect(await service.run(selected)).toEqual({
uniqueFindingIds: selected,
duplicateGroups: [],
deduplicationStatus: "completed",
});
- expect(pairReviews).toBe(0);
+ expect(reviewPair).toHaveBeenCalledTimes(0);
}
});
@@ -611,9 +585,7 @@ test("does not connect DISTINCT findings through accepted transitive pairs", asy
]);
const reviewedPairs: string[] = [];
const service = new FindingDeduplicator(candidates(entries), {
- async screen(findings) {
- return screening(findings, nominations);
- },
+ screen: screenNominations(() => nominations),
async reviewPair(findings) {
reviewedPairs.push(pairKey(findings.map((finding) => finding.findingId)));
return same(findings);
@@ -638,9 +610,7 @@ test("does not connect findings through a pair rejected by Sol", async () => {
]);
const reviewedPairs: string[] = [];
const service = new FindingDeduplicator(candidates(entries), {
- async screen(findings) {
- return screening(findings, nominations);
- },
+ screen: screenNominations(() => nominations),
async reviewPair(findings) {
const key = pairKey(findings.map((finding) => finding.findingId));
reviewedPairs.push(key);
@@ -679,12 +649,8 @@ test("prefers the better-supported legal subgroup in a conflicted star", async (
},
},
{
- async screen(findings) {
- return screening(findings, nominations);
- },
- async reviewPair(findings) {
- return same(findings);
- },
+ screen: screenNominations(() => nominations),
+ reviewPair: reviewSame,
},
);
expect(await service.run(ids)).toEqual({
@@ -759,12 +725,8 @@ test("keeps disconnected groups ordered and conflict ties in finding insertion o
},
},
{
- async screen(findings) {
- return screening(findings, nominations);
- },
- async reviewPair(findings) {
- return same(findings);
- },
+ screen: screenNominations(() => nominations),
+ reviewPair: reviewSame,
},
);
expect(
@@ -820,12 +782,8 @@ test("matches an import to an existing canonical", async () => {
imported.severity.level = "low";
const ids = [existing.findingId, imported.findingId];
const service = new FindingDeduplicator(candidates([existing, imported]), {
- async screen(findings) {
- return screening(findings, new Set([pairKey(ids)]));
- },
- async reviewPair(findings) {
- return same(findings);
- },
+ screen: screenNominations(() => new Set([pairKey(ids)])),
+ reviewPair: reviewSame,
});
expect(await service.run([imported.findingId])).toEqual({
uniqueFindingIds: [existing.findingId],
@@ -840,12 +798,8 @@ test("empty and isolated imports avoid models, while review failures propagate",
const findings = [first];
const failure = new CodexSecurityError("Synthetic review failed");
const reviewer: DeduplicationReviewer = {
- async screen() {
- throw failure;
- },
- async reviewPair() {
- throw failure;
- },
+ screen: (Promise.reject<never>).bind(Promise, failure),
+ reviewPair: (Promise.reject<never>).bind(Promise, failure),
};
const service = new FindingDeduplicator(candidates(findings), reviewer);
expect(await service.run([])).toEqual({
@@ -1160,9 +1114,7 @@ test("accepts complete canonical and merged reviews and rejects invalid assignme
test("resolves a saved scan and retrieves its IDs without uploading or modifying artifacts", async () => {
const directory = await mkdtemp(join(tmpdir(), "dedupe-scan-"));
try {
- await cp(join(PLUGIN_ROOT, "examples/completed-scan"), directory, {
- recursive: true,
- });
+ await copyCompletedScanFixture(directory);
if (process.platform !== "win32") await chmod(directory, 0o700);
const original = await readFile(join(directory, "findings.json"), "utf8");
for (const [requestedId, allRepositories] of [
@@ -1199,14 +1151,7 @@ test("resolves a saved scan and retrieves its IDs without uploading or modifying
potentialDuplicates: [],
});
},
- reviewer: {
- async screen() {
- throw new Error("No review for an empty neighborhood");
- },
- async reviewPair() {
- throw new Error("No pair to review");
- },
- },
+ reviewer: emptyNeighborhoodReviewer(),
},
);
expect(result).toEqual({
@@ -1247,11 +1192,8 @@ test("resolves a saved scan and retrieves its IDs without uploading or modifying
progress: { status: "complete" },
},
}),
- fetch: async () => {
- throw new Error(
- "Must not retrieve candidates for a mismatched scan",
- );
- },
+ fetch: async () =>
+ fail("Must not retrieve candidates for a mismatched scan"),
},
),
).rejects.toThrow("do not match selected scan");
@@ -1264,9 +1206,7 @@ test("deduplicates an explicit sealed scan directory without reading scan histor
const directory = await mkdtemp(join(tmpdir(), "dedupe-directory-"));
const repository = await mkdtemp(join(tmpdir(), "dedupe-repository-"));
try {
- await cp(join(PLUGIN_ROOT, "examples/completed-scan"), directory, {
- recursive: true,
- });
+ await copyCompletedScanFixture(directory);
if (process.platform !== "win32") await chmod(directory, 0o700);
const original = await readFile(join(directory, "findings.json"), "utf8");
const commands: string[][] = [];
@@ -1291,14 +1231,7 @@ test("deduplicates an explicit sealed scan directory without reading scan histor
potentialDuplicates: [],
});
},
- reviewer: {
- async screen() {
- throw new Error("No review for an empty neighborhood");
- },
- async reviewPair() {
- throw new Error("No pair to review");
- },
- },
+ reviewer: emptyNeighborhoodReviewer(),
},
);
expect(result).toEqual({
@@ -1360,9 +1293,7 @@ test("lookup failures and cancellation never produce a completed uniqueness resu
test("writes accepted groups only after all reviews and fails on review or write-back errors", async () => {
const directory = await mkdtemp(join(tmpdir(), "dedupe-writeback-"));
try {
- await cp(join(PLUGIN_ROOT, "examples/completed-scan"), directory, {
- recursive: true,
- });
+ await copyCompletedScanFixture(directory);
if (process.platform !== "win32") await chmod(directory, 0o700);
const findings = [document.findings[0]!, entry(2), entry(3)];
const ids = findings.map((finding) => finding.findingId);
diff --git a/sdk/typescript/tests-ts/finding-embeddings.test.ts b/sdk/typescript/tests-ts/finding-embeddings.test.ts
index d907eeb53..cc5b13fd6 100644
--- a/sdk/typescript/tests-ts/finding-embeddings.test.ts
+++ b/sdk/typescript/tests-ts/finding-embeddings.test.ts
@@ -1,6 +1,7 @@
+import { rejecting } from "./support/errors.js";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
-import { expect, test } from "bun:test";
+import { expect, test, mock } from "bun:test";
import { Tiktoken } from "js-tiktoken/lite";
import cl100kBase from "js-tiktoken/ranks/cl100k_base";
import type { Finding, FindingsDocument } from "../src/models.js";
@@ -123,21 +124,17 @@ test("splits bulk requests at the provider token budget and renews credentials p
test("does not resolve credentials for empty input or reuse a key after renewal fails", async () => {
for (const failure of ["throw", "empty"]) {
let credentials = 0;
- let requests = 0;
- const embedder = new OpenAiFindingEmbedder(
- () => {
- if (++credentials === 1) return "synthetic-key";
- if (failure === "throw") throw new Error("synthetic-private-token");
- return "";
- },
- async () => {
- requests++;
- return Response.json({
- model: EMBEDDING_MODEL,
- data: [{ index: 0, embedding: vector() }],
- });
- },
- );
+ const observeRequests = mock(async () => {
+ return Response.json({
+ model: EMBEDDING_MODEL,
+ data: [{ index: 0, embedding: vector() }],
+ });
+ });
+ const embedder = new OpenAiFindingEmbedder(() => {
+ if (++credentials === 1) return "synthetic-key";
+ if (failure === "throw") throw new Error("synthetic-private-token");
+ return "";
+ }, observeRequests);
expect(await embedder.embed([])).toEqual([]);
expect(credentials).toBe(0);
await embedder.embed([example]);
@@ -146,21 +143,18 @@ test("does not resolve credentials for empty input or reuse a key after renewal
message: "Could not reach the embedding provider.",
});
expect(credentials).toBe(2);
- expect(requests).toBe(1);
+ expect(observeRequests).toHaveBeenCalledTimes(1);
}
});
test("does not call the provider for empty input or missing credentials", async () => {
- let calls = 0;
- const embedder = new OpenAiFindingEmbedder(undefined, async () => {
- calls++;
- throw new Error("Must not call");
- });
+ const observeCalls = mock(rejecting("Must not call"));
+ const embedder = new OpenAiFindingEmbedder(undefined, observeCalls);
expect(await embedder.embed([])).toEqual([]);
await expect(embedder.embed([example])).rejects.toMatchObject({
code: "embedding_unavailable",
});
- expect(calls).toBe(0);
+ expect(observeCalls).toHaveBeenCalledTimes(0);
});
test("reports provider failures without echoing response bodies or credentials", async () => {
diff --git a/sdk/typescript/tests-ts/finding-workflow-integration.test.ts b/sdk/typescript/tests-ts/finding-workflow-integration.test.ts
index 5b299a8c8..4f99a6f2d 100644
--- a/sdk/typescript/tests-ts/finding-workflow-integration.test.ts
+++ b/sdk/typescript/tests-ts/finding-workflow-integration.test.ts
@@ -1,9 +1,10 @@
import { findingFingerprint, sha256 } from "./support/finding-identity.js";
+import { resolving } from "./support/promises.js";
import { execFileSync, spawn } from "node:child_process";
import { once } from "node:events";
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
-import { afterEach, expect, test } from "bun:test";
+import { afterEach, expect, test, mock } from "bun:test";
import type { JsonObject } from "../src/config.js";
import { FindingWorkflow } from "../src/finding-workflow.js";
import { publishScanToCustomInternal } from "../src/custom-publish.js";
@@ -13,7 +14,7 @@ import {
runCodexCommand,
runWorkbench,
} from "../src/runtime.js";
-import type { Finding, ScanManifest } from "../src/models.js";
+import type { Finding, FindingsDocument, ScanManifest } from "../src/models.js";
import type { CodexReview } from "../src/deduplication/codex-review.js";
import { CheckpointedReviewRunner } from "../src/deduplication/checkpointed-review.js";
import {
@@ -24,6 +25,8 @@ import {
import { PLUGIN_ROOT } from "./plugin-root.js";
import { DeduplicationReviewError } from "../src/errors.js";
import { workflowFixture } from "./support/workflow-fixture.js";
+import { rejecting } from "./support/errors.js";
+import { capture } from "./cli-fixtures.js";
const fixtures: Array<Awaited<ReturnType<typeof workflowFixture>>> = [];
afterEach(async () => {
@@ -55,6 +58,26 @@ async function fixture() {
return { ...value, history, workbenchOptions };
}
+function findingFetch(document: FindingsDocument, onGroupWrite?: () => void) {
+ return async function fetch(url: URL) {
+ if (url.pathname.endsWith("/bulk/findings"))
+ return Response.json(
+ document.findings.map((finding) => finding.findingId),
+ );
+ if (onGroupWrite && url.pathname.endsWith("/dedupe-groups")) {
+ onGroupWrite();
+ return Response.json([]);
+ }
+ const id = decodeURIComponent(url.pathname.split("/").at(-2)!);
+ return Response.json({
+ finding: document.findings.find((finding) => finding.findingId === id),
+ potentialDuplicates: document.findings.filter(
+ (finding) => finding.findingId !== id,
+ ),
+ });
+ };
+}
+
async function fixtureWithFindings(count: number) {
const value = await fixture();
const { scanDir, document } = value;
@@ -123,19 +146,7 @@ test("completed workflows preserve refusal outcomes without caching a false verd
});
},
},
- fetch: async (url: URL) => {
- if (url.pathname.endsWith("/bulk/findings"))
- return Response.json(
- document.findings.map((finding) => finding.findingId),
- );
- const id = decodeURIComponent(url.pathname.split("/").at(-2)!);
- return Response.json({
- finding: document.findings.find((finding) => finding.findingId === id),
- potentialDuplicates: document.findings.filter(
- (finding) => finding.findingId !== id,
- ),
- });
- },
+ fetch: findingFetch(document),
};
const result = await deduplicateScanInternal(
document.scanId,
@@ -284,16 +295,14 @@ process.stdout.write("ready\\n");`;
windowsHide: true,
});
const closed = once(child, "close");
- let errors = "";
- child.stderr.on("data", (chunk) => {
- errors += String(chunk);
- });
+ const errors = capture();
+ child.stderr.on("data", errors.stream.write);
try {
const ready = await Promise.race([
once(child.stdout, "data").then(([data]) => String(data)),
closed.then(() => {
throw new Error(
- errors || "Workflow child exited before the checkpoint",
+ errors.text() || "Workflow child exited before the checkpoint",
);
}),
]);
@@ -302,14 +311,11 @@ process.stdout.write("ready\\n");`;
await closed;
const workflow = new FindingWorkflow("interrupted", environment);
expect((await workflow.get())?.stages.publish.status).toBe(status);
- let attempts = 0;
- expect(
- await workflow.run("publish", async () => {
- attempts++;
- return { findingIds: [] };
- }),
- ).toEqual({ findingIds: [] });
- expect(attempts).toBe(status === "running" ? 1 : 0);
+ const attempts = mock(resolving({ findingIds: [] }));
+ expect(await workflow.run("publish", attempts)).toEqual({
+ findingIds: [],
+ });
+ expect(attempts.mock.calls.length).toBe(status === "running" ? 1 : 0);
expect(
(await new FindingWorkflow("interrupted", environment).get())?.stages
.publish,
@@ -381,9 +387,7 @@ test("reuses publication after dedupe failure and persists a successful empty du
await deduplicateScanInternal(document.scanId, options, {
environment,
runWorkbench: history,
- fetch: async () => {
- throw new Error("Completed stages must not make HTTP requests");
- },
+ fetch: rejecting("Completed stages must not make HTTP requests"),
}),
).toEqual(result);
expect(publications).toBe(1);
@@ -408,12 +412,11 @@ test("an empty scan completes publication and dedupe and remains retrievable", a
workflowId: "empty-scan",
findingsUrl: "http://synthetic.test",
};
- let requests = 0;
- const fetch = async (_url: URL, init: RequestInit) => {
- requests++;
+
+ const fetch = mock(async (_url: URL, init: RequestInit) => {
expect(JSON.parse(init.body as string).findings).toEqual([]);
return Response.json([]);
- };
+ });
const result = await deduplicateScanInternal(document.scanId, options, {
environment,
runWorkbench: history,
@@ -432,7 +435,7 @@ test("an empty scan completes publication and dedupe and remains retrievable", a
fetch,
}),
).toEqual(result);
- expect(requests).toBe(1);
+ expect(fetch).toHaveBeenCalledTimes(1);
expect(
(await new FindingWorkflow(options.workflowId, environment).get())?.stages
.publish,
@@ -453,9 +456,7 @@ test("does not write workflow metadata into sealed artifacts", async () => {
...environment,
CODEX_SECURITY_STATE_DIR: join(scanDir, "state"),
},
- fetch: async () => {
- throw new Error("Must not publish");
- },
+ fetch: rejecting("Must not publish"),
},
),
).rejects.toThrow("outside the sealed scan artifacts");
@@ -466,6 +467,11 @@ const sameRecommendation = {
rationale: "REVIEW_OUTPUT_ONLY: one correction covers the supplied paths.",
};
+const screeningRecommendation = (_finding: Finding, index: number) => [
+ screeningPairSlot(index),
+ { ...sameRecommendation },
+];
+
function merged(findings: readonly Finding[]) {
return {
...sameRecommendation,
@@ -529,13 +535,7 @@ test.each(["screen", "pair"])(
stage === "screen"
? {
decisions: Object.fromEntries(
- originals
- .slice(1)
- .map((_finding, index) => [
- screeningPairSlot(index),
- { ...sameRecommendation },
- ])
- .reverse(),
+ originals.slice(1).map(screeningRecommendation).reverse(),
),
}
: originals.some(
@@ -585,9 +585,7 @@ test.each(["screen", "pair"])(
environment,
runWorkbench: history,
reviewRunner,
- fetch: async () => {
- throw new Error("Completed workflow must use its saved result");
- },
+ fetch: rejecting("Completed workflow must use its saved result"),
}),
).toEqual(result);
expect(calls).toHaveLength(count);
@@ -610,7 +608,7 @@ test.each(["screening", "pair-review"] as const)(
const events: string[] = [];
const interruptedKeys: string[] = [];
let interrupt = true;
- let groupWrites = 0;
+ const groupWrites = mock(() => {});
const reviewRunner = {
async run<T>(review: CodexReview<T>): Promise<T> {
const originals = JSON.parse(
@@ -630,12 +628,7 @@ test.each(["screening", "pair-review"] as const)(
review.stage === "screening"
? {
decisions: Object.fromEntries(
- originals
- .slice(1)
- .map((_finding, index) => [
- screeningPairSlot(index),
- { ...sameRecommendation },
- ]),
+ originals.slice(1).map(screeningRecommendation),
),
}
: merged(originals),
@@ -656,25 +649,7 @@ test.each(["screening", "pair-review"] as const)(
events.push("checkpoint-saved");
return response;
},
- fetch: async (url: URL) => {
- if (url.pathname.endsWith("/bulk/findings"))
- return Response.json(
- document.findings.map((finding) => finding.findingId),
- );
- if (url.pathname.endsWith("/dedupe-groups")) {
- groupWrites++;
- return Response.json([]);
- }
- const id = decodeURIComponent(url.pathname.split("/").at(-2)!);
- return Response.json({
- finding: document.findings.find(
- (finding) => finding.findingId === id,
- ),
- potentialDuplicates: document.findings.filter(
- (finding) => finding.findingId !== id,
- ),
- });
- },
+ fetch: findingFetch(document, groupWrites),
};
const failure = new Error("Synthetic concurrent failure");
const first = deduplicateScanInternal(
@@ -705,7 +680,7 @@ test.each(["screening", "pair-review"] as const)(
"checkpoint-saved",
"workflow-failed",
]);
- expect(groupWrites).toBe(0);
+ expect(groupWrites).not.toHaveBeenCalled();
expect(
(await new FindingWorkflow(options.workflowId, environment).get())?.stages
.dedupe,
@@ -720,7 +695,7 @@ test.each(["screening", "pair-review"] as const)(
expect(result.duplicateGroups).toEqual([
document.findings.map((finding) => finding.findingId).sort(),
]);
- expect(groupWrites).toBe(1);
+ expect(groupWrites).toHaveBeenCalledTimes(1);
expect(attempts.get(interruptedKeys[0]!)).toBe(2);
expect(attempts.get(interruptedKeys[1]!)).toBe(1);
expect(attempts.size).toBe(10);
@@ -748,7 +723,7 @@ test("resumes a saved Sol review after overlapping Luna work fails", async () =>
const events: string[] = [];
let interrupt = true;
let failedReviewKey: string | undefined;
- let groupWrites = 0;
+ const groupWrites = mock(() => {});
const dependencies = {
environment,
reviewRunner: {
@@ -776,12 +751,7 @@ test("resumes a saved Sol review after overlapping Luna work fails", async () =>
review.stage === "screening"
? {
decisions: Object.fromEntries(
- originals
- .slice(1)
- .map((_finding, index) => [
- screeningPairSlot(index),
- { ...sameRecommendation },
- ]),
+ originals.slice(1).map(screeningRecommendation),
),
}
: merged(originals),
@@ -801,29 +771,13 @@ test("resumes a saved Sol review after overlapping Luna work fails", async () =>
}
return response;
},
- fetch: async (url: URL) => {
- if (url.pathname.endsWith("/bulk/findings"))
- return Response.json(
- document.findings.map((finding) => finding.findingId),
- );
- if (url.pathname.endsWith("/dedupe-groups")) {
- groupWrites++;
- return Response.json([]);
- }
- const id = decodeURIComponent(url.pathname.split("/").at(-2)!);
- return Response.json({
- finding: document.findings.find((finding) => finding.findingId === id),
- potentialDuplicates: document.findings.filter(
- (finding) => finding.findingId !== id,
- ),
- });
- },
+ fetch: findingFetch(document, groupWrites),
};
await expect(
deduplicateScanInternal(document.scanId, options, dependencies),
).rejects.toThrow(failure.message);
expect(events).toEqual(["luna-pending", "sol-saved", "luna-failed"]);
- expect(groupWrites).toBe(0);
+ expect(groupWrites).not.toHaveBeenCalled();
expect(calls.size).toBe(4);
interrupt = false;
@@ -835,7 +789,7 @@ test("resumes a saved Sol review after overlapping Luna work fails", async () =>
expect(result.duplicateGroups).toEqual([
document.findings.map((finding) => finding.findingId).sort(),
]);
- expect(groupWrites).toBe(1);
+ expect(groupWrites).toHaveBeenCalledTimes(1);
expect(calls.size).toBe(6);
for (const [key, count] of calls)
expect(count).toBe(key === failedReviewKey ? 2 : 1);
diff --git a/sdk/typescript/tests-ts/finding-workflow.test.ts b/sdk/typescript/tests-ts/finding-workflow.test.ts
index ad1d6cb16..43ab8b4d8 100644
--- a/sdk/typescript/tests-ts/finding-workflow.test.ts
+++ b/sdk/typescript/tests-ts/finding-workflow.test.ts
@@ -202,9 +202,7 @@ test("preserves the operation error when recording a failed stage also fails", a
workbench.run,
);
await expect(
- workflow.run("publish", async () => {
- throw failure;
- }),
+ workflow.run("publish", (Promise.reject<never>).bind(Promise, failure)),
).rejects.toBe(failure);
workbench.assertDone();
});
diff --git a/sdk/typescript/tests-ts/findings-client-retry.test.ts b/sdk/typescript/tests-ts/findings-client-retry.test.ts
index d7338751e..a86f74a90 100644
--- a/sdk/typescript/tests-ts/findings-client-retry.test.ts
+++ b/sdk/typescript/tests-ts/findings-client-retry.test.ts
@@ -1,5 +1,7 @@
-import { expect, test } from "bun:test";
+import { responding } from "./support/responses.js";
+import { expect, test, mock } from "bun:test";
import { FindingsClient } from "../src/findings-client.js";
+import { rejecting } from "./support/errors.js";
const scope = { repositoryId: "synthetic-repository" };
const neighborhood = {
@@ -9,7 +11,7 @@ const neighborhood = {
test("lookup retries rate limits and honors Retry-After before continuing", async () => {
let requests = 0;
- const delays: number[] = [];
+ const delays = mock(async (_delay: number) => {});
const client = new FindingsClient(
"http://synthetic.test",
undefined,
@@ -23,9 +25,7 @@ test("lookup retries rate limits and honors Retry-After before continuing", asyn
: Response.json(neighborhood);
},
{
- wait: async (delay) => {
- delays.push(delay);
- },
+ wait: delays,
random: () => 0,
},
);
@@ -33,7 +33,7 @@ test("lookup retries rate limits and honors Retry-After before continuing", asyn
(await client.potentialDuplicates("synthetic", scope)).finding.findingId,
).toBe("synthetic");
expect(requests).toBe(2);
- expect(delays).toEqual([12000]);
+ expect(delays.mock.calls.map(([value]) => value)).toEqual([12000]);
});
test("lookup retries network and truncated JSON responses within the attempt budget", async () => {
@@ -66,50 +66,40 @@ test("lookup retries network and truncated JSON responses within the attempt bud
test.each([400, 401, 403, 404, 409, 501])(
"lookup does not retry HTTP %i",
async (status) => {
- let requests = 0;
+ const observeRequests = mock(responding("failed", status));
const client = new FindingsClient(
"http://synthetic.test",
undefined,
- async () => {
- requests++;
- return new Response("failed", { status });
- },
+ observeRequests,
{
- wait: async () => {
- throw new Error("Unexpected retry");
- },
+ wait: rejecting("Unexpected retry"),
},
);
await expect(
client.potentialDuplicates("synthetic", scope),
).rejects.toThrow(`HTTP ${status}`);
- expect(requests).toBe(1);
+ expect(observeRequests).toHaveBeenCalledTimes(1);
},
);
test.each([408, 429, 500, 502, 503, 504])(
"lookup stops after three HTTP %i failures",
async (status) => {
- let requests = 0;
- let waits = 0;
+ const observeRequests = mock(responding("unavailable", status));
+ const wait = mock(async () => {});
const client = new FindingsClient(
"http://synthetic.test",
undefined,
- async () => {
- requests++;
- return new Response("unavailable", { status });
- },
+ observeRequests,
{
- wait: async () => {
- waits++;
- },
+ wait,
},
);
await expect(
client.potentialDuplicates("synthetic", scope),
).rejects.toThrow(`HTTP ${status}`);
- expect(requests).toBe(3);
- expect(waits).toBe(2);
+ expect(observeRequests).toHaveBeenCalledTimes(3);
+ expect(wait).toHaveBeenCalledTimes(2);
},
);
@@ -135,14 +125,11 @@ test("group write retries the identical payload after a lost acknowledgement", a
test("cancellation interrupts a pending retry and prevents another request", async () => {
const controller = new AbortController();
- let requests = 0;
+ const observeRequests = mock(responding("busy", 429));
const client = new FindingsClient(
"http://synthetic.test",
controller.signal,
- async () => {
- requests++;
- return new Response("busy", { status: 429 });
- },
+ observeRequests,
{
wait: async () => {
controller.abort("synthetic cancellation");
@@ -152,13 +139,13 @@ test("cancellation interrupts a pending retry and prevents another request", asy
await expect(client.potentialDuplicates("synthetic", scope)).rejects.toBe(
"synthetic cancellation",
);
- expect(requests).toBe(1);
+ expect(observeRequests).toHaveBeenCalledTimes(1);
});
test("Retry-After accepts an HTTP date", async () => {
const until = "Fri, 01 Jan 2100 00:00:00 GMT";
let requests = 0;
- let delay = 0;
+ const sleep = mock(async (_value: number) => {});
const client = new FindingsClient(
"http://synthetic.test",
undefined,
@@ -172,11 +159,9 @@ test("Retry-After accepts an HTTP date", async () => {
: Response.json(neighborhood);
},
{
- wait: async (value) => {
- delay = value;
- },
+ wait: sleep,
},
);
await client.potentialDuplicates("synthetic", scope);
- expect(delay).toBeGreaterThan(24 * 60 * 60 * 1000);
+ expect(sleep.mock.lastCall?.[0] ?? 0).toBeGreaterThan(24 * 60 * 60 * 1000);
});
diff --git a/sdk/typescript/tests-ts/findings-dashboard.test.ts b/sdk/typescript/tests-ts/findings-dashboard.test.ts
index d3c917ff1..0d2ec2fea 100644
--- a/sdk/typescript/tests-ts/findings-dashboard.test.ts
+++ b/sdk/typescript/tests-ts/findings-dashboard.test.ts
@@ -1,6 +1,7 @@
-import { expect, test } from "bun:test";
+import { expect, test, mock } from "bun:test";
import { pollDashboard } from "../dashboard/polling.js";
import type { DashboardSnapshot } from "../src/server/dashboard-types.js";
+import { throwing } from "./support/errors.js";
const snapshot: DashboardSnapshot = {
overview: { findings: 0, groups: 0 },
@@ -14,23 +15,19 @@ const snapshot: DashboardSnapshot = {
};
function clock() {
- let tick = () => {};
- let interval = 0;
- let cleared = false;
+ const timers = {
+ setInterval: mock((_callback: () => void, _milliseconds: number) => {
+ return 1 as unknown as ReturnType<typeof setInterval>;
+ }),
+ clearInterval: mock(),
+ };
return {
- timers: {
- setInterval(callback: () => void, milliseconds: number) {
- tick = callback;
- interval = milliseconds;
- return 1 as unknown as ReturnType<typeof setInterval>;
- },
- clearInterval() {
- cleared = true;
- },
- } as Pick<typeof globalThis, "setInterval" | "clearInterval">,
- tick: () => tick(),
- interval: () => interval,
- cleared: () => cleared,
+ timers: timers as unknown as NonNullable<
+ Parameters<typeof pollDashboard>[3]
+ >,
+ tick: () => timers.setInterval.mock.lastCall?.[0](),
+ interval: () => timers.setInterval.mock.lastCall?.[1] ?? 0,
+ cleared: () => timers.clearInterval.mock.calls.length > 0,
};
}
@@ -38,18 +35,15 @@ test("dashboard polls immediately and every five seconds without overlapping req
const timer = clock();
const calls: AbortSignal[] = [];
const received: DashboardSnapshot[] = [];
- let resolve!: (value: DashboardSnapshot) => void;
+ let pending!: PromiseWithResolvers<DashboardSnapshot>;
const stop = pollDashboard(
(signal) => {
calls.push(signal);
- return new Promise((done) => {
- resolve = done;
- });
+ pending = Promise.withResolvers<DashboardSnapshot>();
+ return pending.promise;
},
(value) => received.push(value),
- () => {
- throw new Error("Unexpected poll failure");
- },
+ throwing("Unexpected poll failure"),
timer.timers,
);
try {
@@ -57,7 +51,7 @@ test("dashboard polls immediately and every five seconds without overlapping req
expect(timer.interval()).toBe(5_000);
timer.tick();
expect(calls).toHaveLength(1);
- resolve(snapshot);
+ pending.resolve(snapshot);
await Promise.resolve();
expect(received).toEqual([snapshot]);
timer.tick();
@@ -67,7 +61,7 @@ test("dashboard polls immediately and every five seconds without overlapping req
}
expect(timer.cleared()).toBe(true);
expect(calls.every((signal) => signal.aborted)).toBe(true);
- resolve(snapshot);
+ pending.resolve(snapshot);
await Promise.resolve();
expect(received).toHaveLength(1);
});
@@ -103,20 +97,15 @@ test("dashboard reports refresh errors and retries on the next polling tick", as
test("disposing an old selection suppresses late errors as well as late responses", async () => {
const timer = clock();
const errors: unknown[] = [];
- let reject!: (reason: Error) => void;
+ const pending = Promise.withResolvers<DashboardSnapshot>();
const stop = pollDashboard(
- () =>
- new Promise((_resolve, fail) => {
- reject = fail;
- }),
- () => {
- throw new Error("Unexpected data");
- },
+ () => pending.promise,
+ throwing("Unexpected data"),
(error) => errors.push(error),
timer.timers,
);
stop();
- reject(new Error("Old request failed"));
+ pending.reject(new Error("Old request failed"));
await Promise.resolve();
expect(errors).toEqual([]);
});
diff --git a/sdk/typescript/tests-ts/findings-server.test.ts b/sdk/typescript/tests-ts/findings-server.test.ts
index bf2e17431..bad754cd7 100644
--- a/sdk/typescript/tests-ts/findings-server.test.ts
+++ b/sdk/typescript/tests-ts/findings-server.test.ts
@@ -2,7 +2,7 @@ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import type { Server } from "node:http";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { afterEach, expect, spyOn, test } from "bun:test";
+import { afterEach, expect, spyOn, test, mock } from "bun:test";
import type { Finding, FindingsDocument } from "../src/models.js";
import type { FindingDedupeGroup } from "../src/finding-dedupe-groups.js";
import { resolvePluginPython, runCodexCommand } from "../src/runtime.js";
@@ -13,6 +13,7 @@ import { SqliteFindingsStore } from "../src/server/sqlite-store.js";
import type { EmbeddedFinding, FindingsPage } from "../src/server/storage.js";
import type { DashboardSnapshot } from "../src/server/dashboard-types.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
+import { rejecting } from "./support/errors.js";
const servers: Server[] = [];
const directories: string[] = [];
@@ -130,9 +131,7 @@ async function dashboard(
test("dashboard serves only findings and groups, and never calls an embedding provider", async () => {
const { store } = await fixture();
const base = await start(store, {
- async embed() {
- throw new Error("Read-only dashboard called embeddings");
- },
+ embed: rejecting("Read-only dashboard called embeddings"),
});
for (const path of ["/", "/dashboard"]) {
const redirect = await fetch(`${base}${path}`, { redirect: "manual" });
@@ -503,9 +502,19 @@ ${script}
return JSON.parse(result.stdout);
}
-test("bulk insert preserves complete findings and embeddings without creating scans", async () => {
+test("bulk insert keeps startup dependencies and complete findings without creating scans", async () => {
const { store, environment } = await fixture();
- const base = await start(store);
+ const options = { store, embeddings: embedder, host: "127.0.0.1", port: 0 };
+ const server = await startFindingsServer(options);
+ servers.push(server);
+ const address = server.address();
+ if (address === null || typeof address === "string")
+ throw new Error("No port");
+ const base = `http://127.0.0.1:${address.port}`;
+ options.store = (await fixture()).store;
+ options.embeddings = {
+ embed: rejecting("Replaced server embedder was used"),
+ };
const findings = [finding(1), finding(2)];
const log = spyOn(console, "log").mockImplementation(() => undefined);
try {
@@ -620,9 +629,7 @@ test("persists overlapping dedupe groups idempotently without changing findings
test("rolls back the entire dedupe batch if a finding is missing and rejects invalid groups", async () => {
const { store, environment } = await fixture();
const base = await start(store, {
- embed: async () => {
- throw new Error("Grouping must not embed");
- },
+ embed: rejecting("Grouping must not embed"),
});
await store.insert([embedded(1), embedded(2), embedded(3)]);
const [a, b, c] = [1, 2, 3].map((index) => finding(index).findingId) as [
@@ -936,12 +943,9 @@ test("imports persist repository associations and keep untagged findings in expl
test("rejects invalid requests before embedding and preserves unknown-route behavior", async () => {
const { store } = await fixture();
- let calls = 0;
+ const embed = mock<() => Promise<never[]>>().mockResolvedValue([]);
const base = await start(store, {
- async embed() {
- calls++;
- return [];
- },
+ embed,
});
for (const body of [
"not json",
@@ -993,7 +997,7 @@ test("rejects invalid requests before embedding and preserves unknown-route beha
expect(response.status).toBe(404);
expect(await response.json()).toEqual({ error: "not_found" });
}
- expect(calls).toBe(0);
+ expect(embed).toHaveBeenCalledTimes(0);
});
test("embedding failure leaves no partial findings or vectors", async () => {
diff --git a/sdk/typescript/tests-ts/import-scan.test.ts b/sdk/typescript/tests-ts/import-scan.test.ts
index 28e13b808..dd8ff176c 100644
--- a/sdk/typescript/tests-ts/import-scan.test.ts
+++ b/sdk/typescript/tests-ts/import-scan.test.ts
@@ -13,7 +13,7 @@ import {
import * as filesystem from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { afterEach, expect, spyOn, test } from "bun:test";
+import { afterEach, expect, spyOn, test, mock } from "bun:test";
import { loadContract } from "../src/contract.js";
import { ScanInterruptedError } from "../src/errors.js";
import { importScan, type ImportScanOptions } from "../src/import-scan.js";
@@ -23,13 +23,11 @@ import { runWorkbench } from "../src/runtime.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
import { runCommand } from "./support/shell.js";
import { runTestInSubprocess } from "./support/test-subprocess.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
+import { rejecting } from "./support/errors.js";
-const roots: string[] = [];
-afterEach(async () => {
- await Promise.all(
- roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
- );
-});
+const { temporaryDirectories: roots, cleanup } = createApiTestFixtures();
+afterEach(cleanup);
const description =
'An imported report with a comma, a "quoted value", and Unicode: café.\n\n' +
@@ -84,7 +82,7 @@ async function fixture(format: "csv" | "json" = "csv") {
const root = await mkdtemp(
join(await realpath(tmpdir()), "import-scan-test-"),
);
- roots.push(root);
+ roots.track(root);
const python = Bun.which("python3") ?? Bun.which("python");
expect(python).not.toBeNull();
const stateDirectory = join(root, "state");
@@ -348,7 +346,7 @@ test.each(["regular file", "symbolic link"])(
const context = await fixture("json");
const originalOpen = filesystem.open;
let replaced = false;
- let read = false;
+ const observeRead = mock(rejecting("Read a replaced source"));
let restoreRead: (() => void) | undefined;
const opening = spyOn(filesystem, "open").mockImplementation(
async (...args: Parameters<typeof filesystem.open>) => {
@@ -365,10 +363,7 @@ test.each(["regular file", "symbolic link"])(
}
replaced = true;
const file = await originalOpen(...args);
- const reading = spyOn(file, "readFile").mockImplementation(async () => {
- read = true;
- throw new Error("Read a replaced source");
- });
+ const reading = spyOn(file, "readFile").mockImplementation(observeRead);
restoreRead = () => reading.mockRestore();
return file;
},
@@ -378,7 +373,7 @@ test.each(["regular file", "symbolic link"])(
importScan(context.options, { environment: context.environment }),
).rejects.toThrow();
expect(replaced).toBe(true);
- expect(read).toBe(false);
+ expect(observeRead).not.toHaveBeenCalled();
await expect(stat(context.stateDirectory)).rejects.toMatchObject({
code: "ENOENT",
});
@@ -434,9 +429,7 @@ test("reimporting the retained source preserves finding identities with new scan
test("dry run validates source rows without creating database state or invoking the workbench", async () => {
const context = await fixture();
- const unexpected = async () => {
- throw new Error("Dry run invoked scan persistence");
- };
+ const unexpected = rejecting("Dry run invoked scan persistence");
const result = await importScan(
{ ...context.options, dryRun: true },
{
diff --git a/sdk/typescript/tests-ts/knowledge-base.test.ts b/sdk/typescript/tests-ts/knowledge-base.test.ts
index f850b4ddc..25f371ccd 100644
--- a/sdk/typescript/tests-ts/knowledge-base.test.ts
+++ b/sdk/typescript/tests-ts/knowledge-base.test.ts
@@ -1,42 +1,26 @@
import {
chmod,
mkdir,
- mkdtemp,
readFile,
readdir,
- realpath,
- rm,
stat,
symlink,
writeFile,
} from "node:fs/promises";
import * as filesystem from "node:fs/promises";
import * as os from "node:os";
-import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, spyOn, test } from "bun:test";
import { strToU8, zipSync } from "fflate";
import { prepareKnowledgeBase } from "../src/knowledge-base.js";
import { expandHome } from "../src/runtime.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
-const temporaryDirectories: string[] = [];
+const { temporaryDirectory, cleanup, temporaryDirectories } =
+ createApiTestFixtures("codex-security-knowledge-test-");
const testPosix = process.platform === "win32" ? test.skip : test;
-afterEach(async () => {
- await Promise.all(
- temporaryDirectories
- .splice(0)
- .map((path) => rm(path, { recursive: true, force: true })),
- );
-});
-
-async function temporaryDirectory(): Promise<string> {
- const path = await realpath(
- await mkdtemp(join(tmpdir(), "codex-security-knowledge-test-")),
- );
- temporaryDirectories.push(path);
- return path;
-}
+afterEach(cleanup);
async function extractedDocuments(path: string): Promise<string[]> {
return await Promise.all(
@@ -90,7 +74,7 @@ describe("scan knowledge bases", () => {
await writeFile(join(root, "invalid-utf8.bin"), new Uint8Array([0xff]));
const knowledgeBase = await prepareKnowledgeBase([root, scope, scope]);
- temporaryDirectories.push(knowledgeBase.path);
+ temporaryDirectories.track(knowledgeBase.path);
expect(knowledgeBase.sources).toEqual([root, scope]);
expect((await readdir(knowledgeBase.path)).length).toBe(3);
@@ -124,7 +108,7 @@ describe("scan knowledge bases", () => {
for (const paths of [[source], [root], [root, source]]) {
const knowledgeBase = await prepareKnowledgeBase(paths);
- temporaryDirectories.push(knowledgeBase.path);
+ temporaryDirectories.track(knowledgeBase.path);
expect(await extractedDocuments(knowledgeBase.path)).toEqual([text]);
expect(knowledgeBase.sources).toEqual(paths);
}
@@ -154,7 +138,7 @@ describe("scan knowledge bases", () => {
contents.push("Review application boundaries.");
const knowledgeBase = await prepareKnowledgeBase(paths);
- temporaryDirectories.push(knowledgeBase.path);
+ temporaryDirectories.track(knowledgeBase.path);
expect(knowledgeBase.sources).toEqual(paths);
expect((await extractedDocuments(knowledgeBase.path)).sort()).toEqual(
@@ -191,7 +175,7 @@ describe("scan knowledge bases", () => {
try {
const prepared = prepareKnowledgeBase([root], controller.signal).then(
(knowledgeBase) => {
- temporaryDirectories.push(knowledgeBase.path);
+ temporaryDirectories.track(knowledgeBase.path);
return knowledgeBase;
},
);
@@ -219,7 +203,7 @@ describe("scan knowledge bases", () => {
let knowledgeBase;
try {
knowledgeBase = await prepareKnowledgeBase([root]);
- temporaryDirectories.push(knowledgeBase.path);
+ temporaryDirectories.track(knowledgeBase.path);
} finally {
listingSpy.mockRestore();
}
@@ -270,15 +254,15 @@ describe("scan knowledge bases", () => {
process.env["USERPROFILE"] = home;
try {
const expanded = await prepareKnowledgeBase(["~/docs"]);
- temporaryDirectories.push(expanded.path);
+ temporaryDirectories.track(expanded.path);
expect(expanded.sources).toEqual([documents]);
const bare = await prepareKnowledgeBase(["~"]);
- temporaryDirectories.push(bare.path);
+ temporaryDirectories.track(bare.path);
expect(bare.sources).toEqual([home]);
const absolute = await prepareKnowledgeBase([documents]);
- temporaryDirectories.push(absolute.path);
+ temporaryDirectories.track(absolute.path);
expect(absolute.sources).toEqual([documents]);
expect(expandHome("~other/docs")).toBe("~other/docs");
@@ -299,7 +283,7 @@ describe("scan knowledge bases", () => {
await writeFile(join(root, "threat-model.docx"), docx("SSRF &amp; IDOR"));
const knowledgeBase = await prepareKnowledgeBase([root]);
- temporaryDirectories.push(knowledgeBase.path);
+ temporaryDirectories.track(knowledgeBase.path);
const documents = await extractedDocuments(knowledgeBase.path);
expect(documents).toContain("Payment service boundary");
@@ -318,7 +302,7 @@ describe("scan knowledge bases", () => {
await writeFile(source, "Updated scope");
await writeFile(join(root, "priorities.txt"), "New attack priorities");
const second = await prepareKnowledgeBase(first.sources);
- temporaryDirectories.push(second.path);
+ temporaryDirectories.track(second.path);
const documents = await extractedDocuments(second.path);
expect(documents.sort()).toEqual([
@@ -380,7 +364,7 @@ describe("scan knowledge bases", () => {
await symlink(source, linked);
const knowledgeBase = await prepareKnowledgeBase([root]);
- temporaryDirectories.push(knowledgeBase.path);
+ temporaryDirectories.track(knowledgeBase.path);
expect(await extractedDocuments(knowledgeBase.path)).toEqual([
"External APIs",
]);
diff --git a/sdk/typescript/tests-ts/multiscan.test.ts b/sdk/typescript/tests-ts/multiscan.test.ts
index dee8a02e5..02e08a402 100644
--- a/sdk/typescript/tests-ts/multiscan.test.ts
+++ b/sdk/typescript/tests-ts/multiscan.test.ts
@@ -1,3 +1,6 @@
+import { gitText } from "./support/shell.js";
+import { parseJsonLines, readJsonLines } from "./support/json.js";
+import { resolving } from "./support/promises.js";
import { execFileSync } from "node:child_process";
import {
access,
@@ -5,7 +8,6 @@ import {
chmod,
lstat,
mkdir,
- mkdtemp,
readFile,
readdir,
realpath,
@@ -16,10 +18,10 @@ import {
writeFile,
} from "node:fs/promises";
import * as filesystem from "node:fs/promises";
-import { hostname, tmpdir } from "node:os";
+import { hostname } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
-import { afterEach, describe, expect, spyOn, test } from "bun:test";
+import { afterEach, describe, expect, spyOn, test, mock } from "bun:test";
import { main } from "../src/cli.js";
import { writeThreatModel } from "../src/artifact-export.js";
import { PYTHON } from "./support/security-policy.js";
@@ -32,30 +34,25 @@ import { prepareOutputDir } from "../src/runtime.js";
import * as runtime from "../src/runtime.js";
import { capture, dependencies, fakeResult } from "./cli-fixtures.js";
import { runTestInSubprocess } from "./support/test-subprocess.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
+import { rejecting, throwing } from "./support/errors.js";
type MultiscanOptions = Parameters<typeof runMultiscan>[0];
type SecurityClient = ReturnType<MultiscanOptions["createSecurity"]>;
-const temporaryDirectories: string[] = [];
+const { temporaryDirectory, cleanup } = createApiTestFixtures(
+ "codex-security-multiscan-",
+);
const testPosix = process.platform === "win32" ? test.skip : test;
-afterEach(async () => {
- await Promise.all(
- temporaryDirectories
- .splice(0)
- .map((path) => rm(path, { recursive: true, force: true })),
- );
-});
+afterEach(cleanup);
async function fixture(): Promise<{
root: string;
input: string;
output: string;
}> {
- const root = await realpath(
- await mkdtemp(join(tmpdir(), "codex-security-multiscan-")),
- );
- temporaryDirectories.push(root);
+ const root = await temporaryDirectory();
return {
root,
input: join(root, "repositories.csv"),
@@ -64,8 +61,7 @@ async function fixture(): Promise<{
}
function git(repository: string, ...args: string[]): string {
- return execFileSync("git", ["-C", repository, ...args], {
- encoding: "utf8",
+ return gitText(["-C", repository, ...args], {
stdio: ["ignore", "pipe", "pipe"],
}).trim();
}
@@ -108,10 +104,24 @@ async function completedScan(
return { coverage: { completeness } } as ScanResult;
}
-function client(
- run: SecurityClient["run"],
+const completeRun: SecurityClient["run"] = async (
+ _repository,
+ scanOptions = {},
+) => {
+ return await completedScan(scanOptions.outputDir!);
+};
+
+const completeRunWithoutAwait: SecurityClient["run"] = async (
+ _repository,
+ scanOptions = {},
+) => {
+ return completedScan(scanOptions.outputDir!);
+};
+
+function client<Run extends SecurityClient["run"]>(
+ run: Run,
close: SecurityClient["close"] = async () => {},
-): SecurityClient {
+): SecurityClient & { run: Run } {
return { run, close };
}
@@ -132,12 +142,7 @@ function options(
};
}
-async function results(path: string): Promise<Record<string, unknown>[]> {
- return (await readFile(path, "utf8"))
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line) as Record<string, unknown>);
-}
+const results = readJsonLines<Record<string, unknown>>;
describe("multiscan", () => {
test.each([false, true])(
@@ -264,24 +269,23 @@ describe("multiscan", () => {
paths.input,
`id,repository,revision\nexample,${source.path},${source.revision}\n`,
);
- let initialized = false;
- const security = client(async () => {
- throw new Error("The unsupported target must not reach a scan.");
+ const createSecurity = mock(() => {
+ return security;
});
+ const security = client(
+ rejecting("The unsupported target must not reach a scan."),
+ );
await expect(
runMultiscan(
options(paths, security, {
scanOptionsByMode: { standard: { target } },
- createSecurity: () => {
- initialized = true;
- return security;
- },
+ createSecurity,
}),
),
).rejects.toThrow(
"Bulk scans do not support diff or working-tree scopes",
);
- expect(initialized).toBe(false);
+ expect(createSecurity).not.toHaveBeenCalled();
await expect(access(paths.output)).rejects.toMatchObject({
code: "ENOENT",
});
@@ -296,13 +300,7 @@ describe("multiscan", () => {
`id,repository,revision\nrepo,${source.path},${source.revision}\n`,
);
await runMultiscan(
- options(
- paths,
- client(async () => {
- throw new Error("Stopped");
- }),
- { maxAttempts: 1 },
- ),
+ options(paths, client(rejecting("Stopped")), { maxAttempts: 1 }),
);
const before = await readFile(join(paths.output, "results.jsonl"), "utf8");
const controller = new AbortController();
@@ -422,9 +420,7 @@ describe("multiscan", () => {
...deps,
createSecurity: (config) => ({
...deps.createSecurity(config),
- run: async () => {
- throw new Error(failure);
- },
+ run: rejecting(failure),
}),
},
),
@@ -465,16 +461,9 @@ describe("multiscan", () => {
outputDir: join(paths.output, "artifacts", "failed", "attempt-1"),
}) + "\n";
await writeFile(join(paths.output, "results.jsonl"), original + tail);
- let runs = 0;
+ const runs = mock(completeRunWithoutAwait);
const result = await runMultiscan(
- options(
- paths,
- client(async (_repo, scan = {}) => {
- runs++;
- return completedScan(scan.outputDir!);
- }),
- { recoverScan: async () => undefined },
- ),
+ options(paths, client(runs), { recoverScan: async () => undefined }),
);
expect(result).toMatchObject({
total: 2,
@@ -482,7 +471,7 @@ describe("multiscan", () => {
failed: 0,
skipped: 1,
});
- expect(runs).toBe(1);
+ expect(runs.mock.calls.length).toBe(1);
expect(
(await readFile(result.resultsPath, "utf8")).startsWith(original),
).toBe(true);
@@ -519,29 +508,21 @@ describe("multiscan", () => {
await writeFile(join(orphan, "checkpoint"), "keep checkpoint");
await writeFile(join(checkout, "source"), "keep checkout");
const inode = (await lstat(checkout)).ino;
- const resumed: string[] = [];
- let runs = 0;
+ const resumed = mock(resolving<undefined, [string]>(undefined));
+ const runs = mock<SecurityClient["run"]>(async (repo, scan = {}) => {
+ expect(repo).not.toBe(checkout);
+ expect(git(repo, "rev-parse", "HEAD")).toBe(source.revision);
+ expect(scan.mode).toBe("deep");
+ expect(scan.outputDir).toBe(
+ join(paths.output, "artifacts", "failed", "attempt-6"),
+ );
+ return completedScan(scan.outputDir!);
+ });
const result = await runMultiscan(
- options(
- paths,
- client(async (repo, scan = {}) => {
- runs++;
- expect(repo).not.toBe(checkout);
- expect(git(repo, "rev-parse", "HEAD")).toBe(source.revision);
- expect(scan.mode).toBe("deep");
- expect(scan.outputDir).toBe(
- join(paths.output, "artifacts", "failed", "attempt-6"),
- );
- return completedScan(scan.outputDir!);
- }),
- {
- maxAttempts: 1,
- recoverScan: async (dir) => {
- resumed.push(dir);
- return undefined;
- },
- },
- ),
+ options(paths, client(runs), {
+ maxAttempts: 1,
+ recoverScan: resumed,
+ }),
);
expect(result).toMatchObject({
completed: 1,
@@ -549,8 +530,8 @@ describe("multiscan", () => {
failed: 0,
skipped: 1,
});
- expect(runs).toBe(1);
- expect(resumed).toEqual([orphan]);
+ expect(runs.mock.calls.length).toBe(1);
+ expect(resumed.mock.calls.map(([value]) => value)).toEqual([orphan]);
expect((await lstat(checkout)).ino).toBe(inode);
expect(await readFile(join(checkout, "source"), "utf8")).toBe(
"keep checkout",
@@ -583,13 +564,7 @@ describe("multiscan", () => {
`id,repository,revision\nretained,${source.path},${source.revision}\n`,
);
await runMultiscan(
- options(
- paths,
- client(async () => {
- throw new Error("Stopped");
- }),
- { maxAttempts: 1 },
- ),
+ options(paths, client(rejecting("Stopped")), { maxAttempts: 1 }),
);
const dir = join(paths.output, "artifacts", "retained", "attempt-1");
await mkdir(dir, { recursive: true });
@@ -602,29 +577,21 @@ describe("multiscan", () => {
);
await mkdir(checkout, { recursive: true });
await writeFile(join(checkout, "source"), "keep checkout");
- let resumed = 0;
- let runs = 0;
+ const recoverScan = mock(async (scanDir: string) => {
+ expect(scanDir).toBe(dir);
+ if (failure) throw new Error("Resume transport failed");
+ return completedScan(scanDir);
+ });
+ const runs = mock(completeRunWithoutAwait);
const python = spyOn(runtime, "resolvePluginPython");
let summary;
try {
summary = await runMultiscan(
- options(
- paths,
- client(async (_repo, scan = {}) => {
- runs++;
- return completedScan(scan.outputDir!);
- }),
- {
- maxAttempts: 3,
- config: { pythonPath: PYTHON },
- recoverScan: async (scanDir) => {
- resumed++;
- expect(scanDir).toBe(dir);
- if (failure) throw new Error("Resume transport failed");
- return completedScan(scanDir);
- },
- },
- ),
+ options(paths, client(runs), {
+ maxAttempts: 3,
+ config: { pythonPath: PYTHON },
+ recoverScan,
+ }),
);
expect(python).toHaveBeenCalledWith(
expect.objectContaining({
@@ -638,8 +605,8 @@ describe("multiscan", () => {
expect(await readFile(join(checkout, "source"), "utf8")).toBe(
"keep checkout",
);
- expect(runs).toBe(0);
- expect(resumed).toBe(1);
+ expect(runs.mock.calls.length).toBe(0);
+ expect(recoverScan).toHaveBeenCalledTimes(1);
expect(summary.failed).toBe(failure ? 1 : 0);
expect(summary.completed).toBe(failure ? 0 : 1);
expect((await results(summary.resultsPath)).at(-1)).toMatchObject({
@@ -660,17 +627,11 @@ describe("multiscan", () => {
paths.input,
`id,repository,revision\nrepo,${source.path},${source.revision}\n`,
);
- const configured = options(
- paths,
- client(async () => {
- throw new Error("Interrupted scan");
- }),
- {
- maxAttempts: 1,
- scanPrompt: "Shared scan instructions.",
- scanOptionsByMode: { standard: { failureSeverity: "high" } },
- },
- );
+ const configured = options(paths, client(rejecting("Interrupted scan")), {
+ maxAttempts: 1,
+ scanPrompt: "Shared scan instructions.",
+ scanOptionsByMode: { standard: { failureSeverity: "high" } },
+ });
await runMultiscan(configured);
await mkdir(join(paths.output, "artifacts", "repo", "attempt-1"), {
recursive: true,
@@ -761,27 +722,21 @@ describe("multiscan", () => {
paths.input,
`id,repository,revision\nprivate,${source.path},${source.revision}\n`,
);
- const configured = execFileSync(
- "git",
+ const configured = gitText(
[
...buildGitHubCredentialArgs("github.acme.example"),
"config",
"--get-all",
"credential.https://github.acme.example.helper",
],
- { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] },
+ { stdio: ["ignore", "pipe", "pipe"] },
);
expect(configured.trim()).toBe("!gh auth git-credential");
const summary = await runMultiscan(
- options(
- paths,
- client(
- async (_checkout, scanOptions = {}) =>
- await completedScan(scanOptions.outputDir!),
- ),
- { githubHost: "github.acme.example" },
- ),
+ options(paths, client(completeRun), {
+ githubHost: "github.acme.example",
+ }),
);
expect(summary).toMatchObject({ total: 1, completed: 1, failed: 0 });
@@ -881,24 +836,17 @@ describe("multiscan", () => {
outputTokens: 30,
estimatedUsd: 25.25,
};
- let attempts = 0;
+ const attempts = mock<SecurityClient["run"]>(
+ async (_repository, scanOptions = {}) => {
+ throw new ScanCostLimitExceededError(25, cost, scanOptions.outputDir!);
+ },
+ );
const summary = await runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- attempts += 1;
- throw new ScanCostLimitExceededError(
- 25,
- cost,
- scanOptions.outputDir!,
- );
- }),
- { maxAttempts: 3, maxCostUsd: 25 },
- ),
+ options(paths, client(attempts), { maxAttempts: 3, maxCostUsd: 25 }),
);
- expect(attempts).toBe(1);
+ expect(attempts.mock.calls.length).toBe(1);
expect(summary).toMatchObject({ completed: 0, failed: 1 });
expect(await results(summary.resultsPath)).toMatchObject([
{ id: "over-budget", status: "failed", attempt: 1, cost },
@@ -989,30 +937,27 @@ describe("multiscan", () => {
paths.input,
`id,repository,revision\nobserver-failure,${source.path},${source.revision}\n`,
);
- let attempts = 0;
+ const attempts = mock<SecurityClient["run"]>(
+ async (_repository, scanOptions = {}) => {
+ scanOptions.onWarning?.("Optional post-scan warning.");
+ return await completedScan(scanOptions.outputDir!);
+ },
+ );
const progress: string[] = [];
const summary = await runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- attempts += 1;
- scanOptions.onWarning?.("Optional post-scan warning.");
- return await completedScan(scanOptions.outputDir!);
- }),
- {
- onProgress: (event) => {
- progress.push(event.warning ?? event.status);
- const error = new Error("Optional progress observer failed.");
- if (asynchronous) return Promise.reject(error);
- throw error;
- },
+ options(paths, client(attempts), {
+ onProgress: (event) => {
+ progress.push(event.warning ?? event.status);
+ const error = new Error("Optional progress observer failed.");
+ if (asynchronous) return Promise.reject(error);
+ throw error;
},
- ),
+ }),
);
expect(summary).toMatchObject({ completed: 1, incomplete: 0, failed: 0 });
- expect(attempts).toBe(1);
+ expect(attempts.mock.calls.length).toBe(1);
expect(progress).toEqual([
"started",
"Optional post-scan warning.",
@@ -1110,9 +1055,7 @@ describe("multiscan", () => {
const resumed = await runMultiscan(
options(paths, security, {
maxAttempts: 3,
- onProgress: () => {
- throw new Error("Optional progress observer failed.");
- },
+ onProgress: throwing("Optional progress observer failed."),
}),
);
expect(resumed).toMatchObject({
@@ -1167,11 +1110,7 @@ describe("multiscan", () => {
const progress: Parameters<
NonNullable<MultiscanOptions["onProgress"]>
>[0][] = [];
- let attempts = 0;
- const security = client(async (_repository, scanOptions = {}) => {
- attempts += 1;
- return await completedScan(scanOptions.outputDir!);
- });
+ const security = client(mock(completeRun));
const summary = await runMultiscan(
options(paths, security, {
@@ -1187,7 +1126,7 @@ describe("multiscan", () => {
failed: 0,
skipped: 1,
});
- expect(attempts).toBe(0);
+ expect(security.run.mock.calls.length).toBe(0);
expect(progress).toEqual([
{
repository: "legacy",
@@ -1199,7 +1138,7 @@ describe("multiscan", () => {
expect(await results(summary.resultsPath)).toEqual([receipt]);
await runMultiscan(options(paths, security, { maxAttempts: 3 }));
- expect(attempts).toBe(0);
+ expect(security.run.mock.calls.length).toBe(0);
expect(await results(summary.resultsPath)).toEqual([receipt]);
},
);
@@ -1255,17 +1194,9 @@ describe("multiscan", () => {
error,
})}\n`,
);
- let attempts = 0;
+ const attempts = mock(completeRun);
- const summary = await runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- attempts += 1;
- return await completedScan(scanOptions.outputDir!);
- }),
- ),
- );
+ const summary = await runMultiscan(options(paths, client(attempts)));
expect(summary).toMatchObject({
completed: 1,
@@ -1273,7 +1204,7 @@ describe("multiscan", () => {
failed: 0,
skipped: 0,
});
- expect(attempts).toBe(1);
+ expect(attempts.mock.calls.length).toBe(1);
expect(await results(summary.resultsPath)).toMatchObject([
{ status: "failed", attempt: 1, error },
{ status: "completed", attempt: 2, coverage: "complete" },
@@ -1293,7 +1224,7 @@ describe("multiscan", () => {
const outputDir = join(paths.output, "artifacts", "sample", "attempt-1");
const stdout = capture();
const stderr = capture();
- let attempts = 0;
+ const onRun = mock();
const arguments_ = [
"bulk-scan",
"repositories.csv",
@@ -1306,9 +1237,7 @@ describe("multiscan", () => {
const clientDependencies = dependencies({
currentDirectory: paths.root,
result: fakeResult([], completeness),
- onRun: () => {
- attempts += 1;
- },
+ onRun,
});
const createSecurity = clientDependencies.createSecurity;
clientDependencies.createSecurity = (config) => {
@@ -1330,7 +1259,7 @@ describe("multiscan", () => {
clientDependencies,
),
).toBe(2);
- expect(attempts).toBe(1);
+ expect(onRun).toHaveBeenCalledTimes(1);
expect(JSON.parse(stdout.text())).toMatchObject({
total: 1,
completed: 0,
@@ -1369,7 +1298,7 @@ describe("multiscan", () => {
skipped: 1,
});
expect(resumedError.text()).toContain(warning);
- expect(attempts).toBe(1);
+ expect(onRun).toHaveBeenCalledTimes(1);
},
);
@@ -1381,25 +1310,21 @@ describe("multiscan", () => {
`id,repository,revision\nmissing,${source.path},${source.revision}\n`,
);
- let attempts = 0;
- const summary = await runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- attempts += 1;
- const result = await completedScan(
- scanOptions.outputDir!,
- attempts === 1 ? "partial" : "complete",
- );
- if (attempts === 1) {
- await rm(join(scanOptions.outputDir!, "report.md"));
- }
- return result;
- }),
- ),
+ const attempts = mock<SecurityClient["run"]>(
+ async (_repository, scanOptions = {}) => {
+ const result = await completedScan(
+ scanOptions.outputDir!,
+ attempts.mock.calls.length === 1 ? "partial" : "complete",
+ );
+ if (attempts.mock.calls.length === 1) {
+ await rm(join(scanOptions.outputDir!, "report.md"));
+ }
+ return result;
+ },
);
+ const summary = await runMultiscan(options(paths, client(attempts)));
- expect(attempts).toBe(2);
+ expect(attempts.mock.calls.length).toBe(2);
expect(summary).toMatchObject({ completed: 1, incomplete: 0, failed: 0 });
expect(await results(summary.resultsPath)).toMatchObject([
{
@@ -1421,49 +1346,33 @@ describe("multiscan", () => {
`id,repository,revision,id\npayments,${source.path},${source.revision},again\n`,
`id,repository,revision\npayments,${source.path}\n`,
];
- let scans = 0;
+ const scans = mock(completeRun);
for (const input of invalid) {
await writeFile(paths.input, input);
- await expect(
- runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- scans += 1;
- return await completedScan(scanOptions.outputDir!);
- }),
- ),
- ),
- ).rejects.toThrow(/CSV/);
+ await expect(runMultiscan(options(paths, client(scans)))).rejects.toThrow(
+ /CSV/,
+ );
}
- expect(scans).toBe(0);
+ expect(scans.mock.calls.length).toBe(0);
});
test("rejects task IDs that collide with Windows path names", async () => {
const paths = await fixture();
- let scans = 0;
+ const scans = mock(completeRun);
for (const id of ["task.", "CON", "nul.txt", "COM1", "LPT9.log"]) {
await writeFile(
paths.input,
`id,repository,revision\n${id},./repository,${"0".repeat(40)}\n`,
);
- await expect(
- runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- scans += 1;
- return await completedScan(scanOptions.outputDir!);
- }),
- ),
- ),
- ).rejects.toThrow("safe, unique path names");
+ await expect(runMultiscan(options(paths, client(scans)))).rejects.toThrow(
+ "safe, unique path names",
+ );
}
- expect(scans).toBe(0);
+ expect(scans.mock.calls.length).toBe(0);
});
test("materializes the pinned commit, applies row options, and removes its checkout", async () => {
@@ -1489,35 +1398,26 @@ describe("multiscan", () => {
`id,repository,revision,scope,mode\npayments,${source.path},${source.revision},src,deep\n`,
);
- let checkout = "";
- let closed = 0;
+ const run = mock<SecurityClient["run"]>(async (path, scanOptions = {}) => {
+ expect(git(path, "rev-parse", "HEAD")).toBe(source.revision);
+ expect(await readFile(join(path, "src", "app.ts"), "utf8")).toContain(
+ 'name = "payments"',
+ );
+ expect(scanOptions.target).toEqual(["src"]);
+ expect(scanOptions.mode).toBe("deep");
+ expect(scanOptions.outputDir).toBe(
+ join(paths.output, "artifacts", "payments", "attempt-1"),
+ );
+ return await completedScan(scanOptions.outputDir!);
+ });
+ const observeClosed = mock(async () => {});
const summary = await runMultiscan(
- options(
- paths,
- client(
- async (path, scanOptions = {}) => {
- checkout = path;
- expect(git(path, "rev-parse", "HEAD")).toBe(source.revision);
- expect(
- await readFile(join(path, "src", "app.ts"), "utf8"),
- ).toContain('name = "payments"');
- expect(scanOptions.target).toEqual(["src"]);
- expect(scanOptions.mode).toBe("deep");
- expect(scanOptions.outputDir).toBe(
- join(paths.output, "artifacts", "payments", "attempt-1"),
- );
- return await completedScan(scanOptions.outputDir!);
- },
- async () => {
- closed += 1;
- },
- ),
- ),
+ options(paths, client(run, observeClosed)),
);
expect(summary).toMatchObject({ completed: 1, failed: 0, skipped: 0 });
- expect(closed).toBe(1);
- await expect(access(checkout)).rejects.toThrow();
+ expect(observeClosed).toHaveBeenCalledTimes(1);
+ await expect(access(run.mock.lastCall?.[0] ?? "")).rejects.toThrow();
expect(await readdir(join(paths.output, "checkouts"))).toEqual([]);
expect(await results(summary.resultsPath)).toMatchObject([
{
@@ -1552,16 +1452,28 @@ describe("multiscan", () => {
let active = 0;
let maximum = 0;
- let created = 0;
- let closed = 0;
- const { promise: simultaneous, resolve: release } =
- Promise.withResolvers<void>();
+ const createSecurity = mock(() => {
+ let running = false;
+ return client(async (repository, scanOptions) => {
+ if (running) {
+ throw new Error("A scan is already running for this client.");
+ }
+ running = true;
+ try {
+ return await security.run(repository, scanOptions);
+ } finally {
+ running = false;
+ }
+ }, close);
+ });
+ const close = mock(async () => {});
+ const simultaneous = Promise.withResolvers<void>();
const security = client(async (_repository, scanOptions = {}) => {
expect(scanOptions.knowledgeBasePaths).toEqual(knowledgeBasePaths);
active += 1;
maximum = Math.max(maximum, active);
- if (active === 2) release();
- await simultaneous;
+ if (active === 2) simultaneous.resolve();
+ await simultaneous.promise;
active -= 1;
return await completedScan(scanOptions.outputDir!);
});
@@ -1569,32 +1481,13 @@ describe("multiscan", () => {
options(paths, security, {
workers: 2,
knowledgeBasePaths,
- createSecurity: () => {
- created += 1;
- let running = false;
- return client(
- async (repository, scanOptions) => {
- if (running) {
- throw new Error("A scan is already running for this client.");
- }
- running = true;
- try {
- return await security.run(repository, scanOptions);
- } finally {
- running = false;
- }
- },
- async () => {
- closed += 1;
- },
- );
- },
+ createSecurity,
}),
);
expect(maximum).toBe(2);
- expect(created).toBe(2);
- expect(closed).toBe(2);
+ expect(createSecurity).toHaveBeenCalledTimes(2);
+ expect(close).toHaveBeenCalledTimes(2);
expect(summary).toMatchObject({ total: 3, completed: 3, failed: 0 });
expect(await results(summary.resultsPath)).toHaveLength(3);
});
@@ -1606,16 +1499,15 @@ describe("multiscan", () => {
paths.input,
`id,repository,revision\nexclusive,${source.path},${source.revision}\n`,
);
- const { promise: running, resolve: started } =
- Promise.withResolvers<void>();
- const { promise: finish, resolve: release } = Promise.withResolvers<void>();
+ const running = Promise.withResolvers<void>();
+ const finish = Promise.withResolvers<void>();
const security = client(async (_repository, scanOptions = {}) => {
- started();
- await finish;
+ running.resolve();
+ await finish.promise;
return await completedScan(scanOptions.outputDir!);
});
const first = runMultiscan(options(paths, security));
- await running;
+ await running.promise;
try {
const lock = join(paths.output, ".lock");
const ownerPath = join(lock, "owner.json");
@@ -1633,7 +1525,7 @@ describe("multiscan", () => {
/running|locked|supervisor/iu,
);
} finally {
- release();
+ finish.resolve();
await first;
}
@@ -1671,12 +1563,7 @@ describe("multiscan", () => {
await utimes(ownerPath, beforeProcessStarted, beforeProcessStarted);
const summary = await runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- ),
- ),
+ options(paths, client(completeRunWithoutAwait)),
);
expect(summary).toMatchObject({ completed: 1, failed: 0 });
@@ -1703,14 +1590,7 @@ describe("multiscan", () => {
});
await expect(
- runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- ),
- ),
- ),
+ runMultiscan(options(paths, client(completeRunWithoutAwait))),
).rejects.toThrow("A multiscan supervisor is already running.");
expect(JSON.parse(await readFile(ownerPath, "utf8"))).toEqual({
pid: process.pid,
@@ -1742,12 +1622,7 @@ describe("multiscan", () => {
await utimes(ownerPath, expired, expired);
const summary = await runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- ),
- ),
+ options(paths, client(completeRunWithoutAwait)),
);
expect(summary).toMatchObject({ completed: 1, failed: 0 });
@@ -1777,14 +1652,7 @@ describe("multiscan", () => {
);
await expect(
- runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- ),
- ),
- ),
+ runMultiscan(options(paths, client(completeRunWithoutAwait))),
).rejects.toThrow("A multiscan supervisor is already running.");
expect(JSON.parse(await readFile(ownerPath, "utf8"))).toMatchObject({
ownerId: "live-remote-supervisor",
@@ -1804,12 +1672,7 @@ describe("multiscan", () => {
await utimes(lock, expired, expired);
const summary = await runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- ),
- ),
+ options(paths, client(completeRunWithoutAwait)),
);
expect(summary).toMatchObject({ completed: 1, failed: 0 });
@@ -1827,14 +1690,7 @@ describe("multiscan", () => {
await mkdir(lock, { recursive: true, mode: 0o700 });
await expect(
- runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- ),
- ),
- ),
+ runMultiscan(options(paths, client(completeRunWithoutAwait))),
).rejects.toThrow("A multiscan supervisor is already running.");
expect((await lstat(lock)).isDirectory()).toBe(true);
});
@@ -1861,12 +1717,7 @@ describe("multiscan", () => {
await utimes(recoveryPath, expired, expired);
const summary = await runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- ),
- ),
+ options(paths, client(completeRunWithoutAwait)),
);
expect(summary).toMatchObject({ completed: 1, failed: 0 });
@@ -1889,16 +1740,15 @@ describe("multiscan", () => {
mode: 0o600,
},
);
- const { promise: running, resolve: started } =
- Promise.withResolvers<void>();
- const { promise: finish, resolve: release } = Promise.withResolvers<void>();
+ const running = Promise.withResolvers<void>();
+ const finish = Promise.withResolvers<void>();
let active = 0;
let maximum = 0;
const security = client(async (_repository, scanOptions = {}) => {
active += 1;
maximum = Math.max(maximum, active);
- started();
- await finish;
+ running.resolve();
+ await finish.promise;
active -= 1;
return completedScan(scanOptions.outputDir!);
});
@@ -1907,8 +1757,8 @@ describe("multiscan", () => {
runMultiscan(options(paths, security)),
]);
- await running;
- release();
+ await running.promise;
+ finish.resolve();
const outcomes = await contenders;
expect(maximum).toBe(1);
@@ -1927,20 +1777,19 @@ describe("multiscan", () => {
paths.input,
`id,repository,revision\nreplacement,${source.path},${source.revision}\n`,
);
- const { promise: running, resolve: started } =
- Promise.withResolvers<void>();
- const { promise: finish, resolve: release } = Promise.withResolvers<void>();
+ const running = Promise.withResolvers<void>();
+ const finish = Promise.withResolvers<void>();
const first = runMultiscan(
options(
paths,
client(async (_repository, scanOptions = {}) => {
- started();
- await finish;
+ running.resolve();
+ await finish.promise;
return completedScan(scanOptions.outputDir!);
}),
),
);
- await running;
+ await running.promise;
const lock = join(paths.output, ".lock");
const abandoned = join(paths.output, ".lock.stale-interrupted");
await rename(lock, abandoned);
@@ -1955,7 +1804,7 @@ describe("multiscan", () => {
mode: 0o600,
});
- release();
+ finish.resolve();
await first;
expect(
@@ -1984,9 +1833,7 @@ describe("multiscan", () => {
});
},
);
- const security = client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- );
+ const security = client(completeRunWithoutAwait);
try {
await expect(runMultiscan(options(paths, security))).rejects.toThrow(
@@ -2063,14 +1910,7 @@ describe("multiscan", () => {
try {
await expect(
- runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- ),
- ),
- ),
+ runMultiscan(options(paths, client(completeRunWithoutAwait))),
).rejects.toThrow("replacement already owns the lock");
await access(lock);
if (ownerPublished) {
@@ -2122,17 +1962,13 @@ describe("multiscan", () => {
const source = await repository(paths.root, "resume");
const csv = `id,repository,revision\nresume,${source.path},${source.revision}\n`;
await writeFile(paths.input, csv);
- let calls = 0;
- const security = client(async (_repository, scanOptions = {}) => {
- calls += 1;
- return await completedScan(scanOptions.outputDir!);
- });
+ const security = client(mock(completeRun));
const initial = await runMultiscan(options(paths, security));
await appendFile(initial.resultsPath, '{"id":"interrupted"');
const resumed = await runMultiscan(options(paths, security));
expect(resumed).toMatchObject({ completed: 1, failed: 0, skipped: 1 });
- expect(calls).toBe(1);
+ expect(security.run.mock.calls.length).toBe(1);
for (const prompts of [
{ scanPrompt: "Review different boundaries." },
{ postScanPrompt: "Draft confirmed fixes." },
@@ -2142,13 +1978,13 @@ describe("multiscan", () => {
runMultiscan(options(paths, security, prompts)),
).rejects.toThrow("manifest does not match");
}
- expect(calls).toBe(1);
+ expect(security.run.mock.calls.length).toBe(1);
const [receipt] = await results(initial.resultsPath);
await rm(join(receipt!["outputDir"] as string, "report.md"));
const repaired = await runMultiscan(options(paths, security));
expect(repaired).toMatchObject({ completed: 1, failed: 0, skipped: 0 });
- expect(calls).toBe(2);
+ expect(security.run.mock.calls.length).toBe(2);
expect((await results(repaired.resultsPath)).at(-1)?.["outputDir"]).toBe(
join(paths.output, "artifacts", "resume", "attempt-2"),
);
@@ -2157,7 +1993,7 @@ describe("multiscan", () => {
await expect(runMultiscan(options(paths, security))).rejects.toThrow(
"manifest does not match",
);
- expect(calls).toBe(2);
+ expect(security.run.mock.calls.length).toBe(2);
});
test.skipIf(process.platform !== "win32")(
@@ -2167,11 +2003,7 @@ describe("multiscan", () => {
const source = await repository(paths.root, "resume-alias");
const inventory = (repositoryPath: string) =>
`id,repository,revision\nresume,${repositoryPath},${source.revision}\n`;
- let calls = 0;
- const security = client(async (_repository, scanOptions = {}) => {
- calls += 1;
- return await completedScan(scanOptions.outputDir!);
- });
+ const security = client(mock(completeRun));
await writeFile(paths.input, inventory(source.path));
await runMultiscan(options(paths, security));
@@ -2181,7 +2013,7 @@ describe("multiscan", () => {
completed: 1,
skipped: 1,
});
- expect(calls).toBe(1);
+ expect(security.run.mock.calls.length).toBe(1);
},
);
@@ -2292,32 +2124,20 @@ describe("multiscan", () => {
process.env["GIT_TRACE2_ENV_VARS"] = repositoryVariables.join(",");
const summary = await runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- ),
- ),
+ options(paths, client(completeRunWithoutAwait)),
);
expect(summary).toMatchObject({ completed: 1, failed: 0 });
- const leakedVariables = (await readFile(trace, "utf8"))
- .trim()
- .split("\n")
- .map(
- (line) =>
- JSON.parse(line) as {
- event: string;
- param?: string;
- value?: string;
- },
- )
- .filter(
- (event) =>
- event.event === "def_param" &&
- repositoryVariables.includes(event.param ?? "") &&
- event.value === join(paths.root, `missing-${event.param}`),
- );
+ const leakedVariables = parseJsonLines<{
+ event: string;
+ param?: string;
+ value?: string;
+ }>(await readFile(trace, "utf8")).filter(
+ (event) =>
+ event.event === "def_param" &&
+ repositoryVariables.includes(event.param ?? "") &&
+ event.value === join(paths.root, `missing-${event.param}`),
+ );
expect(leakedVariables).toEqual([]);
} finally {
for (const [name, value] of previous) {
@@ -2345,19 +2165,11 @@ describe("multiscan", () => {
directory ? join(paths.output, directory) : paths.output,
);
- let scans = 0;
- await expect(
- runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- scans += 1;
- return await completedScan(scanOptions.outputDir!);
- }),
- ),
- ),
- ).rejects.toThrow("symbolic links");
- expect(scans).toBe(0);
+ const scans = mock(completeRun);
+ await expect(runMultiscan(options(paths, client(scans)))).rejects.toThrow(
+ "symbolic links",
+ );
+ expect(scans.mock.calls.length).toBe(0);
expect(await readFile(preserved, "utf8")).toBe("preserved\n");
}
});
@@ -2382,20 +2194,13 @@ describe("multiscan", () => {
process.platform === "win32" ? "junction" : "dir",
);
- let scans = 0;
+ const scans = mock(completeRun);
const summary = await runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- scans += 1;
- return await completedScan(scanOptions.outputDir!);
- }),
- { maxAttempts: 1 },
- ),
+ options(paths, client(scans), { maxAttempts: 1 }),
);
expect(summary).toMatchObject({ total: 1, completed: 0, failed: 1 });
- expect(scans).toBe(0);
+ expect(scans.mock.calls.length).toBe(0);
expect((await results(summary.resultsPath))[0]?.["error"]).toContain(
"symbolic links",
);
@@ -2436,19 +2241,11 @@ describe("multiscan", () => {
})}\n`,
);
- let scans = 0;
- await expect(
- runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- scans += 1;
- return await completedScan(scanOptions.outputDir!);
- }),
- ),
- ),
- ).rejects.toThrow("symbolic links");
- expect(scans).toBe(0);
+ const scans = mock(completeRun);
+ await expect(runMultiscan(options(paths, client(scans)))).rejects.toThrow(
+ "symbolic links",
+ );
+ expect(scans.mock.calls.length).toBe(0);
expect(await readdir(external)).toEqual(["attempt-1"]);
});
@@ -2484,21 +2281,13 @@ describe("multiscan", () => {
) as never;
},
);
- let scans = 0;
+ const scans = mock(completeRun);
try {
- await expect(
- runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- scans += 1;
- return await completedScan(scanOptions.outputDir!);
- }),
- ),
- ),
- ).rejects.toThrow("changed during preparation");
- expect(scans).toBe(0);
+ await expect(runMultiscan(options(paths, client(scans)))).rejects.toThrow(
+ "changed during preparation",
+ );
+ expect(scans.mock.calls.length).toBe(0);
} finally {
inspectOutput.mockRestore();
}
@@ -2514,18 +2303,14 @@ describe("multiscan", () => {
`id,repository,revision\nsample,${source.path},${source.revision}\n`,
);
await mkdir(paths.output, { mode: 0o755 });
- let scans = 0;
- const security = client(async (_repository, scanOptions = {}) => {
- scans += 1;
- return await completedScan(scanOptions.outputDir!);
- });
+ const security = client(mock(completeRun));
for (const mode of [0o770, 0o777]) {
await chmod(paths.output, mode);
await expect(runMultiscan(options(paths, security))).rejects.toThrow(
"must not be group- or world-writable",
);
- expect(scans).toBe(0);
+ expect(security.run.mock.calls.length).toBe(0);
}
await chmod(paths.output, 0o755);
@@ -2534,7 +2319,7 @@ describe("multiscan", () => {
completed: 1,
failed: 0,
});
- expect(scans).toBe(1);
+ expect(security.run.mock.calls.length).toBe(1);
},
);
@@ -2548,23 +2333,16 @@ describe("multiscan", () => {
const parent = join(paths.root, "shared");
await mkdir(parent, { mode: 0o777 });
await chmod(parent, 0o777);
- let scans = 0;
+ const scans = mock(completeRun);
await expect(
runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) => {
- scans += 1;
- return await completedScan(scanOptions.outputDir!);
- }),
- { outputDir: join(parent, "results") },
- ),
+ options(paths, client(scans), { outputDir: join(parent, "results") }),
),
).rejects.toThrow(
"must not be group- or world-writable without the sticky bit",
);
- expect(scans).toBe(0);
+ expect(scans.mock.calls.length).toBe(0);
});
test("preserves trusted user-selected campaign parent aliases", async () => {
@@ -2583,11 +2361,7 @@ describe("multiscan", () => {
process.platform === "win32" ? "junction" : "dir",
);
const output = join(linkedParent, "results");
- let scans = 0;
- const security = client(async (_repository, scanOptions = {}) => {
- scans += 1;
- return await completedScan(scanOptions.outputDir!);
- });
+ const security = client(mock(completeRun));
const summary = await runMultiscan(
options(paths, security, { outputDir: output }),
@@ -2609,7 +2383,7 @@ describe("multiscan", () => {
expect(
await runMultiscan(options(paths, security, { outputDir: output })),
).toMatchObject({ completed: 1, skipped: 1 });
- expect(scans).toBe(1);
+ expect(security.run.mock.calls.length).toBe(1);
expect(await readdir(join(canonicalParent, "results"))).toContain(
"results.jsonl",
);
@@ -2719,11 +2493,7 @@ describe("multiscan", () => {
row: `safe,https://user:${secret}@example.test/private.git,${source.revision},.`,
},
];
- let scans = 0;
- const security = client(async (_repository, scanOptions = {}) => {
- scans += 1;
- return await completedScan(scanOptions.outputDir!);
- });
+ const security = client(mock(completeRun));
for (const entry of invalid) {
await writeFile(
@@ -2741,7 +2511,7 @@ describe("multiscan", () => {
expect(String(error)).not.toContain(secret);
}
- expect(scans).toBe(0);
+ expect(security.run.mock.calls.length).toBe(0);
});
test("records incomplete coverage separately and still finishes other repositories", async () => {
diff --git a/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts b/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts
index 1b4d43192..b5af235dd 100644
--- a/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts
+++ b/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts
@@ -1,12 +1,12 @@
import { spawn } from "node:child_process";
import { join } from "node:path";
-import { createInterface } from "node:readline";
import { describe, expect, test } from "bun:test";
import Ajv from "ajv";
import Ajv2020 from "ajv/dist/2020.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
import { runPython } from "./support/python-probe.js";
import { readJson as readJsonFile } from "./support/json.js";
+import { initializeMcpClient } from "./support/mcp-client.js";
type JsonObject = Record<string, unknown>;
@@ -124,60 +124,7 @@ async function startMcp() {
[join(PLUGIN_ROOT, "mcp", "server.mjs"), "--stdio"],
{ stdio: ["pipe", "pipe", "pipe"] },
);
- const messages = createInterface({ input: child.stdout })[
- Symbol.asyncIterator
- ]();
- let stderr = "";
- child.stderr.setEncoding("utf8");
- child.stderr.on("data", (chunk: string) => {
- stderr += chunk;
- });
- let nextId = 0;
-
- async function request(
- method: string,
- params: JsonObject,
- ): Promise<JsonObject> {
- const id = ++nextId;
- child.stdin.write(
- `${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`,
- );
- while (true) {
- const message = await messages.next();
- if (message.done) {
- throw new Error(`MCP server exited before replying: ${stderr}`);
- }
- const response = JSON.parse(message.value) as JsonObject;
- if (response["id"] !== id) continue;
- if (response["error"] !== undefined) {
- throw new Error(JSON.stringify(response["error"]));
- }
- return response["result"] as JsonObject;
- }
- }
-
- await request("initialize", {
- protocolVersion: "2025-11-25",
- capabilities: {},
- clientInfo: { name: "finding-detail-contract-test", version: "1.0.0" },
- });
- child.stdin.write(
- `${JSON.stringify({
- jsonrpc: "2.0",
- method: "notifications/initialized",
- params: {},
- })}\n`,
- );
-
- return {
- request,
- async close(): Promise<void> {
- child.stdin.end();
- await new Promise<void>((resolve) => {
- child.once("close", () => resolve());
- });
- },
- };
+ return initializeMcpClient(child, "finding-detail-contract-test", false);
}
describe("bundled plugin finding detail contracts", () => {
diff --git a/sdk/typescript/tests-ts/project-config.test.ts b/sdk/typescript/tests-ts/project-config.test.ts
index 85d9b44b0..4c0535cdf 100644
--- a/sdk/typescript/tests-ts/project-config.test.ts
+++ b/sdk/typescript/tests-ts/project-config.test.ts
@@ -12,15 +12,11 @@ import {
type ProjectConfigInput,
} from "../src/project-config-schema.js";
import { DEFAULT_DEEP_SCAN_SETTINGS } from "../src/deep-scan-defaults.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
-import { createTemporaryDirectories } from "./support/temporary-directories.js";
-
-const directories = createTemporaryDirectories();
-afterEach(directories.cleanup);
-async function temporaryDirectory() {
- const directory = await directories.create("project-config-");
- return directory;
-}
+const { temporaryDirectory, cleanup } =
+ createApiTestFixtures("project-config-");
+afterEach(cleanup);
const cases: [string, unknown, boolean][] = [
["minimal file", {}, true],
diff --git a/sdk/typescript/tests-ts/scan-comparison.test.ts b/sdk/typescript/tests-ts/scan-comparison.test.ts
index 508c051c7..36bec6e48 100644
--- a/sdk/typescript/tests-ts/scan-comparison.test.ts
+++ b/sdk/typescript/tests-ts/scan-comparison.test.ts
@@ -1,14 +1,13 @@
+import { once } from "node:events";
import {
copyFile,
mkdir,
- mkdtemp,
+ rm,
readFile,
realpath,
- rm,
symlink,
writeFile,
} from "node:fs/promises";
-import { tmpdir } from "node:os";
import { join, relative, win32 } from "node:path";
import { parse, stringify } from "smol-toml";
import {
@@ -17,7 +16,7 @@ import {
type ThreadOptions,
type TurnOptions,
} from "@openai/codex-sdk";
-import { afterEach, describe, expect, spyOn, test } from "bun:test";
+import { afterEach, describe, expect, spyOn, test, mock } from "bun:test";
import { resolveCodexCommand, runCodexCommand } from "../src/runtime.js";
import {
comparisonForScan,
@@ -30,6 +29,8 @@ import {
type ScanComparisonOptions,
type ScanComparisonResult,
} from "../src/scan-comparison.js";
+import { temporaryDirectory as createTemporaryDirectory } from "./support/temporary-directories.js";
+import { fail } from "./support/errors.js";
const temporaryDirectories: string[] = [];
@@ -54,6 +55,12 @@ afterEach(async () => {
);
});
+async function temporaryDirectory(prefix = "codex-security-comparison-") {
+ const path = await createTemporaryDirectory(prefix, false);
+ temporaryDirectories.push(path);
+ return path;
+}
+
function finding(occurrenceId: string): ScanComparisonInput["before"][number] {
return { occurrenceId };
}
@@ -123,10 +130,7 @@ describe("semantic scan comparison", () => {
] as const)(
"supplies %s authentication to Codex matching",
async (_name, keys, expected) => {
- const home = await mkdtemp(
- join(tmpdir(), "codex-security-matcher-auth-"),
- );
- temporaryDirectories.push(home);
+ const home = await temporaryDirectory("codex-security-matcher-auth-");
let captured: CodexOptions | undefined;
const { codex } = fakeCodex({ matches: [], uncertain: [] });
const startThread = spyOn(
@@ -161,10 +165,7 @@ describe("semantic scan comparison", () => {
test.each(["unselected", "default", "home", "overrides", "override-profile"])(
"preserves Cyber selection and %s feature gates at read-only SDK boundaries",
async (selection) => {
- const home = await mkdtemp(
- join(tmpdir(), "codex-security-cyber-helper-"),
- );
- temporaryDirectories.push(home);
+ const home = await temporaryDirectory("codex-security-cyber-helper-");
const disabled = {
api_key_cyber_access_programs: false,
api_key_model_discovery: false,
@@ -252,10 +253,9 @@ describe("semantic scan comparison", () => {
test.each(["home", "profile", "overrides", "override-away"])(
"preserves native command auth selection from %s",
async (selection) => {
- const home = await mkdtemp(
- join(tmpdir(), "codex-security-command-comparison-"),
+ const home = await temporaryDirectory(
+ "codex-security-command-comparison-",
);
- temporaryDirectories.push(home);
const commandAuth = selection !== "override-away";
const provider = {
name: "Synthetic",
@@ -370,8 +370,7 @@ describe("semantic scan comparison", () => {
);
test("does not substitute managed login for an explicitly configured command provider", async () => {
- const home = await mkdtemp(join(tmpdir(), "codex-security-command-login-"));
- temporaryDirectories.push(home);
+ const home = await temporaryDirectory("codex-security-command-login-");
const state = join(home, "state");
await mkdir(join(state, "codex-home"), { recursive: true });
// Invalid auth remains native Codex's responsibility, without login fallback.
@@ -381,19 +380,16 @@ describe("semantic scan comparison", () => {
);
const environment = { CODEX_HOME: home, CODEX_SECURITY_STATE_DIR: state };
expect(
- await comparisonEnvironment(environment, async () => {
- throw new Error("Must not probe managed login");
- }),
+ await comparisonEnvironment(environment, async () =>
+ fail("Must not probe managed login"),
+ ),
).toEqual(environment);
});
test.each(["chatgpt", "api-key"] as const)(
"rejects ambient command auth that conflicts with explicit %s authentication",
async (auth) => {
- const home = await mkdtemp(
- join(tmpdir(), "codex-security-auth-conflict-"),
- );
- temporaryDirectories.push(home);
+ const home = await temporaryDirectory("codex-security-auth-conflict-");
const provider = {
name: "Synthetic",
base_url: "https://provider.example/v1",
@@ -464,8 +460,7 @@ describe("semantic scan comparison", () => {
);
test("disables explicit and inherited MCP servers for read-only helper turns", async () => {
- const home = await mkdtemp(join(tmpdir(), "codex-security-comparison-"));
- temporaryDirectories.push(home);
+ const home = await temporaryDirectory("codex-security-comparison-");
await writeFile(
join(home, "config.toml"),
'[mcp_servers.inherited]\ncommand = "synthetic-inherited"\n',
@@ -559,16 +554,14 @@ describe("semantic scan comparison", () => {
});
test("preserves environment API-key precedence over managed credentials", async () => {
- const root = await mkdtemp(join(tmpdir(), "codex-security-comparison-"));
- temporaryDirectories.push(root);
+ const root = await temporaryDirectory("codex-security-comparison-");
const stateDirectory = join(root, "state");
const credentialHome = join(stateDirectory, "codex-home");
await mkdir(credentialHome, { recursive: true, mode: 0o700 });
- let statusProbed = false;
- const account = async () => {
- statusProbed = true;
+
+ const account = mock(async () => {
return { authenticated: true, details: "Logged in using ChatGPT" };
- };
+ });
const environment = await comparisonEnvironment(
{
@@ -596,21 +589,22 @@ describe("semantic scan comparison", () => {
FIREWORKS_API_KEY: "provider-key",
};
expect(await comparisonEnvironment(provider, account)).toEqual(provider);
- expect(statusProbed).toBe(false);
+ expect(account).not.toHaveBeenCalled();
});
test.skipIf(process.platform !== "win32")(
"recognizes provider scan variables regardless of Windows casing",
async () => {
- const root = await mkdtemp(join(tmpdir(), "codex-security-comparison-"));
- temporaryDirectories.push(root);
+ const root = await temporaryDirectory("codex-security-comparison-");
const stateDirectory = join(root, "state");
const providerHome = join(root, "provider-home");
await mkdir(join(stateDirectory, "codex-home"), {
recursive: true,
mode: 0o700,
});
- let statusProbed = false;
+ const statusProbed = mock(async () => {
+ return { authenticated: true, details: "Logged in using ChatGPT" };
+ });
const provider = {
codex_security_scan_id: "scan",
CODEX_SECURITY_STATE_DIR: stateDirectory,
@@ -618,21 +612,17 @@ describe("semantic scan comparison", () => {
FIREWORKS_API_KEY: "synthetic-provider-key",
};
- const environment = await comparisonEnvironment(provider, async () => {
- statusProbed = true;
- return { authenticated: true, details: "Logged in using ChatGPT" };
- });
+ const environment = await comparisonEnvironment(provider, statusProbed);
expect(environment).toEqual(provider);
- expect(statusProbed).toBe(false);
+ expect(statusProbed).not.toHaveBeenCalled();
},
);
test.skipIf(process.platform !== "win32")(
"replaces differently cased Windows CODEX_HOME variables",
async () => {
- const root = await mkdtemp(join(tmpdir(), "codex-security-comparison-"));
- temporaryDirectories.push(root);
+ const root = await temporaryDirectory("codex-security-comparison-");
const stateDirectory = join(root, "state");
const credentialHome = join(stateDirectory, "codex-home");
await mkdir(credentialHome, { recursive: true, mode: 0o700 });
@@ -656,8 +646,7 @@ describe("semantic scan comparison", () => {
);
test("reuses managed keyring credentials when no environment key is present", async () => {
- const root = await mkdtemp(join(tmpdir(), "codex-security-comparison-"));
- temporaryDirectories.push(root);
+ const root = await temporaryDirectory("codex-security-comparison-");
const stateDirectory = join(root, "state");
const credentialHome = join(stateDirectory, "codex-home");
await mkdir(credentialHome, { recursive: true, mode: 0o700 });
@@ -678,8 +667,7 @@ describe("semantic scan comparison", () => {
test.skipIf(process.platform === "win32")(
"uses the canonical keyring identity when the state parent is symlinked",
async () => {
- const root = await mkdtemp(join(tmpdir(), "codex-security-comparison-"));
- temporaryDirectories.push(root);
+ const root = await temporaryDirectory("codex-security-comparison-");
const actualState = join(root, "actual-state");
const linkedState = join(root, "linked-state");
const credentialHome = join(actualState, "codex-home");
@@ -701,8 +689,7 @@ describe("semantic scan comparison", () => {
);
test("forwards cancellation to managed credential-status checks", async () => {
- const root = await mkdtemp(join(tmpdir(), "codex-security-comparison-"));
- temporaryDirectories.push(root);
+ const root = await temporaryDirectory("codex-security-comparison-");
const stateDirectory = join(root, "state");
await mkdir(join(stateDirectory, "codex-home"), {
recursive: true,
@@ -710,23 +697,19 @@ describe("semantic scan comparison", () => {
});
const controller = new AbortController();
let observedSignal: AbortSignal | undefined;
- const { promise: started, resolve: statusStarted } =
- Promise.withResolvers<void>();
+ const started = Promise.withResolvers<void>();
const waiting = comparisonEnvironment(
{ CODEX_SECURITY_STATE_DIR: stateDirectory },
async (_command, _environment, signal) => {
observedSignal = signal;
- statusStarted();
- return await new Promise((_resolve, reject) => {
- signal?.addEventListener("abort", () => reject(signal.reason), {
- once: true,
- });
- });
+ started.resolve();
+ await once(signal!, "abort");
+ throw signal!.reason;
},
controller.signal,
);
- await started;
+ await started.promise;
controller.abort(new DOMException("canceled", "AbortError"));
await expect(waiting).rejects.toMatchObject({ name: "AbortError" });
@@ -734,8 +717,7 @@ describe("semantic scan comparison", () => {
});
test("retains API-key authentication when the managed home is not signed in", async () => {
- const root = await mkdtemp(join(tmpdir(), "codex-security-comparison-"));
- temporaryDirectories.push(root);
+ const root = await temporaryDirectory("codex-security-comparison-");
const stateDirectory = join(root, "state");
const ambientHome = join(root, "ambient-codex-home");
await mkdir(ambientHome, { mode: 0o700 });
@@ -760,8 +742,7 @@ describe("semantic scan comparison", () => {
test.skipIf(process.platform !== "win32")(
"recognizes stored credentials under a backslash home-relative path",
async () => {
- const root = await mkdtemp(join(tmpdir(), "codex-security-comparison-"));
- temporaryDirectories.push(root);
+ const root = await temporaryDirectory("codex-security-comparison-");
const ambientHome = join(root, "ambient-codex-home");
await mkdir(ambientHome);
await writeFile(join(ambientHome, "auth.json"), "{}");
@@ -971,7 +952,9 @@ describe("semantic scan comparison", () => {
const unselected = { findingId: "unselected", occurrenceId: "unselected" };
const after = { findingId: "renamed", occurrenceId: "current-renamed" };
const saved = new Map<string, ScanComparisonResult>();
- let observed: ScanComparisonInput | undefined;
+ const matchFindings = mock<typeof matchScanFindings>((input, options) => {
+ return matchScanFindings(input, { ...options, codex: model.codex });
+ });
const model = fakeCodex({
matches: [],
uncertain: [
@@ -1009,13 +992,10 @@ describe("semantic scan comparison", () => {
saved.set(args[2]!, JSON.parse(commandInput!) as ScanComparisonResult);
return {};
},
- matchFindings(input, options) {
- observed = input;
- return matchScanFindings(input, { ...options, codex: model.codex });
- },
+ matchFindings,
});
- expect(observed).toEqual({
+ expect(matchFindings.mock.lastCall?.[0]).toEqual({
before: [firstShared, firstOther, latestShared],
after: [after],
});
@@ -1057,7 +1037,19 @@ describe("semantic scan comparison", () => {
findingId: stable ? "previous" : "new",
occurrenceId: "new",
};
- let calls = 0;
+ const workbench = mock(async (args: readonly string[]) => {
+ return args[0] === "list-unmatched-scan-pairs"
+ ? {
+ batches: [
+ {
+ afterScanId: "current",
+ afterFindings: [after],
+ beforeScans: [{ scanId: "prior", findings: [before] }],
+ },
+ ],
+ }
+ : {};
+ });
const model = fakeCodex({ matches: [], uncertain: [] });
await matchCompletedScan({
scanId: "current",
@@ -1067,24 +1059,11 @@ describe("semantic scan comparison", () => {
? [{ findingId: "previous", sourceScanId: "prior" }]
: [],
findings: [after],
- async workbench(args) {
- calls += 1;
- return args[0] === "list-unmatched-scan-pairs"
- ? {
- batches: [
- {
- afterScanId: "current",
- afterFindings: [after],
- beforeScans: [{ scanId: "prior", findings: [before] }],
- },
- ],
- }
- : {};
- },
+ workbench,
matchFindings: (input, options) =>
matchScanFindings(input, { ...options, codex: model.codex }),
});
- expect(calls).toBe(expectedCalls);
+ expect(workbench).toHaveBeenCalledTimes(expectedCalls);
expect(model.calls.prompt !== undefined).toBe(expectedModel);
},
);
@@ -1289,9 +1268,7 @@ describe("semantic scan comparison", () => {
test("does not start Codex when either scan has no findings", async () => {
const codex: NonNullable<ScanComparisonOptions["codex"]> = {
- startThread() {
- throw new Error("No model is needed.");
- },
+ startThread: () => fail("No model is needed."),
};
for (const input of [
{ before: [], after: [finding("after")] },
@@ -1319,9 +1296,7 @@ describe("semantic scan comparison", () => {
],
])("rejects %s occurrence IDs before matching", async (_, input) => {
const codex: NonNullable<ScanComparisonOptions["codex"]> = {
- startThread() {
- throw new Error("No model should start for invalid input.");
- },
+ startThread: () => fail("No model should start for invalid input."),
};
await expect(matchScanFindings(input, { codex })).rejects.toThrow(
diff --git a/sdk/typescript/tests-ts/scan-matching-e2e.test.ts b/sdk/typescript/tests-ts/scan-matching-e2e.test.ts
index 803bf9b91..ed58b7847 100644
--- a/sdk/typescript/tests-ts/scan-matching-e2e.test.ts
+++ b/sdk/typescript/tests-ts/scan-matching-e2e.test.ts
@@ -1,14 +1,5 @@
-import { createHash } from "node:crypto";
-import {
- cp,
- mkdir,
- mkdtemp,
- readFile,
- realpath,
- rm,
- writeFile,
-} from "node:fs/promises";
-import { tmpdir } from "node:os";
+import { hash } from "node:crypto";
+import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, test } from "bun:test";
import { main } from "../src/cli.js";
@@ -26,8 +17,10 @@ import {
type ScanMatchingBatch,
} from "../src/scan-comparison.js";
import { capture, dependencies } from "./cli-fixtures.js";
-import { PLUGIN_ROOT } from "./plugin-root.js";
+import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";
+import { temporaryDirectory } from "./support/temporary-directories.js";
import { readJson } from "./support/json.js";
+import { fail } from "./support/errors.js";
const empty = { matches: [], uncertain: [] } satisfies ScanComparisonResult;
@@ -49,9 +42,7 @@ function confirmed(
}
test("matches sealed scan history end to end without merging related findings", async () => {
- const root = await realpath(
- await mkdtemp(join(tmpdir(), "codex-security-matching-")),
- );
+ const root = await temporaryDirectory("codex-security-matching-", true);
try {
const python = await resolvePluginPython();
const repository = join(root, "repository");
@@ -97,9 +88,7 @@ test("matches sealed scan history end to end without merging related findings",
}),
]);
const scanId = String(registered["scanId"]);
- await cp(join(PLUGIN_ROOT, "examples", "completed-scan"), scanDir, {
- recursive: true,
- });
+ await copyCompletedScanFixture(scanDir);
const manifest = await readJson<ScanManifest>(
join(scanDir, "scan-manifest.json"),
);
@@ -188,11 +177,7 @@ test("matches sealed scan history end to end without merging related findings",
];
const digest = async () =>
Promise.all(
- artifacts.map(async (path) =>
- createHash("sha256")
- .update(await readFile(path))
- .digest("hex"),
- ),
+ artifacts.map(async (path) => hash("sha256", await readFile(path))),
);
const originalArtifacts = await digest();
const save = (
@@ -252,9 +237,8 @@ test("matches sealed scan history end to end without merging related findings",
resumedPair["matchingInputs"] as unknown as ScanComparisonInput,
{
codex: {
- startThread() {
- throw new Error("An already-confirmed alias must not need Codex.");
- },
+ startThread: () =>
+ fail("An already-confirmed alias must not need Codex."),
},
},
);
@@ -397,9 +381,8 @@ test("matches sealed scan history end to end without merging related findings",
matchScanFindings(input, {
...options,
codex: {
- startThread() {
- throw new Error("Cached transitive links must not need Codex.");
- },
+ startThread: () =>
+ fail("Cached transitive links must not need Codex."),
},
}),
),
diff --git a/sdk/typescript/tests-ts/scan-recipe.test.ts b/sdk/typescript/tests-ts/scan-recipe.test.ts
index 50020aaa6..cbb0ff69a 100644
--- a/sdk/typescript/tests-ts/scan-recipe.test.ts
+++ b/sdk/typescript/tests-ts/scan-recipe.test.ts
@@ -1,15 +1,14 @@
+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 { PLUGIN_ROOT } from "./plugin-root.js";
import { TestClient } from "./support/api-client.js";
-import {
- createApiTestFixtures,
- preparedRuntime,
-} from "./support/api-events.js";
+import { preparedRuntime } from "./support/api-events.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
+import { throwing } from "./support/errors.js";
const { temporaryDirectory, cleanup } = createApiTestFixtures();
afterEach(cleanup);
@@ -70,9 +69,7 @@ test.each(["standard", "deep"])(
prepareRuntime: async () => preparedRuntime(codexHome),
resolvePluginPython: async () => python,
runWorkbench,
- createCodex: () => {
- throw new Error("Synthetic stop after registration");
- },
+ createCodex: throwing("Synthetic stop after registration"),
}),
},
);
diff --git a/sdk/typescript/tests-ts/scan-recovery.test.ts b/sdk/typescript/tests-ts/scan-recovery.test.ts
index b86e424cc..1d4d2b578 100644
--- a/sdk/typescript/tests-ts/scan-recovery.test.ts
+++ b/sdk/typescript/tests-ts/scan-recovery.test.ts
@@ -1,11 +1,11 @@
import { spawnSync } from "node:child_process";
-import { cp, mkdir, readFile, writeFile } from "node:fs/promises";
+import { mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, describe, expect, test } from "bun:test";
import { runWorkbench } from "../src/runtime.js";
-import { PLUGIN_ROOT } from "./plugin-root.js";
import { runNodePython } from "./support/python-probe.js";
-import { createTemporaryDirectories } from "./support/temporary-directories.js";
+import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
import { readJson } from "./support/json.js";
type Finding = Record<string, unknown> & {
@@ -78,9 +78,11 @@ type ScanFixture = {
registration: Record<string, unknown>;
};
-const temporaryDirectories = createTemporaryDirectories();
+const { temporaryDirectory, cleanup } = createApiTestFixtures(
+ "codex-security-scan-recovery-",
+);
-afterEach(temporaryDirectories.cleanup);
+afterEach(cleanup);
async function writeJson(path: string, value: unknown): Promise<void> {
await writeFile(path, `${JSON.stringify(value)}\n`);
@@ -109,9 +111,7 @@ async function startDraftScan(
repositoryKind: "directory" | "clean" | "dirty" | "nested" = "directory",
recipeFromStdin = false,
): Promise<ScanFixture> {
- const root = await temporaryDirectories.create(
- "codex-security-scan-recovery-",
- );
+ const root = await temporaryDirectory();
const python =
process.env["PYTHON"] ?? Bun.which("python3") ?? Bun.which("python");
expect(python).not.toBeNull();
@@ -190,9 +190,7 @@ async function startDraftScan(
fixture.scanId = String(registration["scanId"]);
fixture.registration = registration;
- await cp(join(PLUGIN_ROOT, "examples", "completed-scan"), scanDir, {
- recursive: true,
- });
+ await copyCompletedScanFixture(scanDir);
const manifestPath = join(scanDir, "scan-manifest.json");
const manifest = await readJson<{
scan: {
diff --git a/sdk/typescript/tests-ts/scan-resume.test.ts b/sdk/typescript/tests-ts/scan-resume.test.ts
index b2300ab0b..b28af9c23 100644
--- a/sdk/typescript/tests-ts/scan-resume.test.ts
+++ b/sdk/typescript/tests-ts/scan-resume.test.ts
@@ -1,5 +1,6 @@
+import { gitText } from "./support/shell.js";
+import { readJsonLines } from "./support/json.js";
import { randomUUID } from "node:crypto";
-import { execFileSync } from "node:child_process";
import {
appendFile,
cp,
@@ -16,14 +17,12 @@ import { main } from "../src/cli.js";
import type { ScanOptions } from "../src/api.js";
import { runWorkbench } from "../src/runtime.js";
import { capture, dependencies } from "./cli-fixtures.js";
-import { PLUGIN_ROOT } from "./plugin-root.js";
import { runPython } from "./support/python-probe.js";
+import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";
import { TestClient } from "./support/api-client.js";
-import {
- completedEvents,
- createApiTestFixtures,
- preparedRuntime,
-} from "./support/api-events.js";
+import { completedEvents, preparedRuntime } from "./support/api-events.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
+import { fail } from "./support/errors.js";
const { temporaryDirectory, cleanup } = createApiTestFixtures();
afterEach(cleanup);
@@ -53,9 +52,7 @@ async function interruptedScan(
const input = join(root, "repositories.csv");
if (bulk) {
const git = (...args: string[]) =>
- execFileSync("git", ["-C", repository, ...args], {
- encoding: "utf8",
- }).trim();
+ gitText(["-C", repository, ...args]).trim();
git("init", "-q");
git("add", ".");
git(
@@ -352,9 +349,7 @@ test("CLI resumes the owning Codex thread and preserves running state on a trans
resolvePluginPython: async () => f.python,
runWorkbench,
createCodex: (options) => ({
- startThread() {
- throw new Error("Resume must not create a new thread.");
- },
+ startThread: () => fail("Resume must not create a new thread."),
resumeThread(threadId, threadOptions) {
resumedThread = threadId;
expect(threadOptions.workingDirectory).toBe(f.scanDir);
@@ -488,9 +483,7 @@ async function finishDiscovery(f: Awaited<ReturnType<typeof interruptedScan>>) {
f.scanId,
]);
expect(expired.exitCode, new TextDecoder().decode(expired.stderr)).toBe(0);
- await cp(join(PLUGIN_ROOT, "examples", "completed-scan"), f.scanDir, {
- recursive: true,
- });
+ await copyCompletedScanFixture(f.scanDir);
for (const name of ["scan-manifest.json", "findings.json", "coverage.json"]) {
const path = join(f.scanDir, name);
const doc = JSON.parse(await readFile(path, "utf8"));
@@ -562,9 +555,7 @@ test.each([
}),
runWorkbench: f.command,
createSecurity: resumeClient(f, () => ({
- startThread() {
- throw new Error("Unexpected new session");
- },
+ startThread: () => fail("Unexpected new session"),
resumeThread(threadId) {
expect(threadId).toBe(f.threadId);
return {
@@ -583,10 +574,7 @@ test.each([
const result = JSON.parse(stdout.text());
if (bulk) {
expect(result, stderr.text()).toMatchObject({ incomplete: 1, failed: 0 });
- const receipts = (await readFile(result.resultsPath, "utf8"))
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line));
+ const receipts = await readJsonLines(result.resultsPath);
expect(receipts).toHaveLength(2);
expect(receipts[1]).toMatchObject({
attempt: 1,
@@ -604,9 +592,7 @@ test.each([
stderr.stream,
{
...dependencies({
- onRun() {
- throw new Error("A sealed scan needs no Codex invocation");
- },
+ onRun: () => fail("A sealed scan needs no Codex invocation"),
}),
runWorkbench: f.command,
},
@@ -616,10 +602,7 @@ test.each([
incomplete: 1,
failed: 0,
});
- const reconciled = (await readFile(result.resultsPath, "utf8"))
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line));
+ const reconciled = await readJsonLines(result.resultsPath);
expect(reconciled[1]).toMatchObject({
attempt: 1,
cost: { inputTokens: 10000, outputTokens: 2000 },
@@ -728,9 +711,7 @@ test.each([
}),
runWorkbench: f.command,
createSecurity: resumeClient(f, () => ({
- startThread() {
- throw new Error("Unexpected new session");
- },
+ startThread: () => fail("Unexpected new session"),
resumeThread(threadId) {
expect(threadId).toBe(f.threadId);
return {
@@ -765,10 +746,7 @@ test.each([
incomplete: 1,
failed: 0,
});
- const receipts = (await readFile(join(f.root, "results.jsonl"), "utf8"))
- .trim()
- .split("\n")
- .map((line) => JSON.parse(line));
+ const receipts = await readJsonLines(join(f.root, "results.jsonl"));
expect(receipts).toHaveLength(2);
expect(receipts[1]).toMatchObject({
attempt: 1,
@@ -841,9 +819,7 @@ test.each(["chatgpt", "api-key"] as const)(
prepareRuntime: async () => preparedRuntime(codexHome),
resolvePluginPython: async () => python,
runWorkbench,
- createCodex: () => {
- throw new Error("Synthetic stop after registration");
- },
+ createCodex: () => fail("Synthetic stop after registration"),
}),
},
);
@@ -929,9 +905,7 @@ test.each([
api_key_cyber_access_programs: true,
});
return {
- startThread() {
- throw new Error("Resume must use the original session.");
- },
+ startThread: () => fail("Resume must use the original session."),
resumeThread(threadId) {
expect(threadId).toBe(f.threadId);
return {
@@ -1011,9 +985,9 @@ test("missing session logs do not create another session or fail the original sc
{
...dependencies({ environment: f.environment, currentDirectory: f.root }),
runWorkbench: f.command,
- createSecurity: resumeClient(f, () => {
- throw new Error("Must not invoke Codex without the original session");
- }),
+ createSecurity: resumeClient(f, () =>
+ fail("Must not invoke Codex without the original session"),
+ ),
},
);
expect(code).not.toBe(0);
@@ -1103,9 +1077,7 @@ test("resume requires an explicit scan ID", async () => {
const stderr = capture();
const code = await main(["scans", "resume"], stdout.stream, stderr.stream, {
...dependencies(),
- runWorkbench: async () => {
- throw new Error("Must select a scan explicitly");
- },
+ runWorkbench: async () => fail("Must select a scan explicitly"),
});
expect(code).toBe(2);
expect(stderr.text()).toContain("scanId");
diff --git a/sdk/typescript/tests-ts/suggest-owners.test.ts b/sdk/typescript/tests-ts/suggest-owners.test.ts
index 05e9f7df2..2a61f8299 100644
--- a/sdk/typescript/tests-ts/suggest-owners.test.ts
+++ b/sdk/typescript/tests-ts/suggest-owners.test.ts
@@ -1,15 +1,5 @@
import { execFile as execFileCallback } from "node:child_process";
-import {
- cp,
- mkdir,
- mkdtemp,
- readFile,
- realpath,
- rm,
- symlink,
- writeFile,
-} from "node:fs/promises";
-import { tmpdir } from "node:os";
+import { cp, mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises";
import { join, relative } from "node:path";
import { promisify } from "node:util";
import type { ThreadOptions, TurnOptions } from "@openai/codex-sdk";
@@ -21,16 +11,12 @@ import {
type OwnerFinding,
type SuggestOwnersOptions,
} from "../src/suggest-owners.js";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
const execFile = promisify(execFileCallback);
-const directories: string[] = [];
-afterEach(async () => {
- await Promise.all(
- directories
- .splice(0)
- .map((directory) => rm(directory, { recursive: true, force: true })),
- );
-});
+const { temporaryDirectory, cleanup } =
+ createApiTestFixtures("owner-repository-");
+afterEach(cleanup);
const finding: OwnerFinding = {
findingId: "finding-one",
@@ -40,10 +26,7 @@ const finding: OwnerFinding = {
};
async function repository() {
- const path = await realpath(
- await mkdtemp(join(tmpdir(), "owner-repository-")),
- );
- directories.push(path);
+ const path = await temporaryDirectory();
const git = async (...args: string[]) =>
(await execFile("git", ["-C", path, ...args])).stdout.trim();
await git("init", "-q");
@@ -117,10 +100,7 @@ test.each(["borrowed-gitfile", "forged-commondir"])(
"rejects %s before sending another checkout's evidence to Codex",
async (kind) => {
const other = await repository();
- const selected = await realpath(
- await mkdtemp(join(tmpdir(), "owner-unbound-")),
- );
- directories.push(selected);
+ const selected = await temporaryDirectory("owner-unbound-");
const metadata = await other.git("rev-parse", "--absolute-git-dir");
if (kind === "borrowed-gitfile") {
await writeFile(join(selected, ".git"), `gitdir: ${metadata}\n`);
@@ -148,10 +128,7 @@ test.each(["refs", "refs/heads", "HEAD", "packed-refs"])(
"rejects borrowed %s before sending another checkout's evidence to Codex",
async (reference) => {
const other = await repository();
- const selected = await realpath(
- await mkdtemp(join(tmpdir(), "owner-borrowed-ref-")),
- );
- directories.push(selected);
+ const selected = await temporaryDirectory("owner-borrowed-ref-");
const branch = await other.git("symbolic-ref", "--short", "HEAD");
await execFile("git", [
"init",
@@ -195,10 +172,7 @@ test.each([
"alternate-pack-link",
])("rejects %s before collecting another checkout's objects", async (kind) => {
const other = await repository();
- const selected = await realpath(
- await mkdtemp(join(tmpdir(), "owner-borrowed-objects-")),
- );
- directories.push(selected);
+ const selected = await temporaryDirectory("owner-borrowed-objects-");
await execFile("git", ["init", "--quiet", selected]);
const objects = join(selected, ".git", "objects");
const borrowed = join(
@@ -248,8 +222,7 @@ test.each([
"in-tree-alternate",
])("supports a bound %s", async (kind) => {
const repo = await repository();
- const root = await realpath(await mkdtemp(join(tmpdir(), "owner-bound-")));
- directories.push(root);
+ const root = await temporaryDirectory("owner-bound-");
let selected = repo.path;
if (kind === "linked-worktree") {
selected = join(root, "checkout");
diff --git a/sdk/typescript/tests-ts/support/deduplication.ts b/sdk/typescript/tests-ts/support/deduplication.ts
new file mode 100644
index 000000000..0c64067cb
--- /dev/null
+++ b/sdk/typescript/tests-ts/support/deduplication.ts
@@ -0,0 +1,7 @@
+import { rejecting } from "./errors.js";
+export function emptyNeighborhoodReviewer() {
+ return {
+ screen: rejecting("No review for an empty neighborhood"),
+ reviewPair: rejecting("No pair to review"),
+ };
+}
diff --git a/sdk/typescript/tests-ts/support/json.ts b/sdk/typescript/tests-ts/support/json.ts
index 8671f4163..ed6ab677d 100644
--- a/sdk/typescript/tests-ts/support/json.ts
+++ b/sdk/typescript/tests-ts/support/json.ts
@@ -20,3 +20,7 @@ export function parseJsonLines<T = ReturnType<typeof JSON.parse>>(
export function jsonLines(values: readonly unknown[]): string {
return values.map((value) => JSON.stringify(value)).join("\n");
}
+
+export const readJsonLines = async <T = ReturnType<typeof JSON.parse>>(
+ path: string,
+) => parseJsonLines<T>(await readFile(path, "utf8"));
diff --git a/sdk/typescript/tests-ts/support/mcp-client.ts b/sdk/typescript/tests-ts/support/mcp-client.ts
new file mode 100644
index 000000000..0a0ef0832
--- /dev/null
+++ b/sdk/typescript/tests-ts/support/mcp-client.ts
@@ -0,0 +1,69 @@
+import type { ChildProcessWithoutNullStreams } from "node:child_process";
+import { createInterface } from "node:readline";
+import { capture } from "../cli-fixtures.js";
+
+type JsonObject = Record<string, unknown>;
+
+export async function initializeMcpClient(
+ child: ChildProcessWithoutNullStreams,
+ clientName: string,
+ splitRequestWrites: boolean,
+) {
+ const messages = createInterface({ input: child.stdout })[
+ Symbol.asyncIterator
+ ]();
+ const stderr = capture();
+ child.stderr.setEncoding("utf8");
+ child.stderr.on("data", stderr.stream.write);
+ let nextId = 0;
+
+ async function request(
+ method: string,
+ params: JsonObject,
+ ): Promise<JsonObject> {
+ const id = ++nextId;
+ if (splitRequestWrites) {
+ child.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, params }));
+ child.stdin.write("\n");
+ } else {
+ child.stdin.write(
+ `${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`,
+ );
+ }
+ while (true) {
+ const message = await messages.next();
+ if (message.done) {
+ throw new Error(`MCP server exited before replying: ${stderr.text()}`);
+ }
+ const response = JSON.parse(message.value) as JsonObject;
+ if (response["id"] !== id) continue;
+ if (response["error"] !== undefined) {
+ throw new Error(JSON.stringify(response["error"]));
+ }
+ return response["result"] as JsonObject;
+ }
+ }
+
+ await request("initialize", {
+ protocolVersion: "2025-11-25",
+ capabilities: {},
+ clientInfo: { name: clientName, version: "1.0.0" },
+ });
+ child.stdin.write(
+ `${JSON.stringify({
+ jsonrpc: "2.0",
+ method: "notifications/initialized",
+ params: {},
+ })}\n`,
+ );
+
+ return {
+ request,
+ async close(): Promise<void> {
+ child.stdin.end();
+ await new Promise<void>((resolve) => {
+ child.once("close", () => resolve());
+ });
+ },
+ };
+}
diff --git a/sdk/typescript/tests-ts/support/workflow-fixture.ts b/sdk/typescript/tests-ts/support/workflow-fixture.ts
index 376421f84..118bd0307 100644
--- a/sdk/typescript/tests-ts/support/workflow-fixture.ts
+++ b/sdk/typescript/tests-ts/support/workflow-fixture.ts
@@ -1,28 +1,16 @@
-import {
- chmod,
- cp,
- mkdir,
- mkdtemp,
- readFile,
- realpath,
- rm,
-} from "node:fs/promises";
-import { tmpdir } from "node:os";
+import { chmod, mkdir, readFile, rm } from "node:fs/promises";
import { join } from "node:path";
import type { FindingsDocument } from "../../src/models.js";
-import { PLUGIN_ROOT } from "../plugin-root.js";
+import { copyCompletedScanFixture } from "../plugin-root.js";
+import { temporaryDirectory } from "./temporary-directories.js";
export async function workflowFixture() {
- const root = await realpath(
- await mkdtemp(join(tmpdir(), "findings-workflow-")),
- );
+ const root = await temporaryDirectory("findings-workflow-", true);
const scanDir = join(root, "scan");
const repository = join(root, "repository");
try {
await mkdir(repository);
- await cp(join(PLUGIN_ROOT, "examples/completed-scan"), scanDir, {
- recursive: true,
- });
+ await copyCompletedScanFixture(scanDir);
if (process.platform !== "win32") await chmod(scanDir, 0o700);
const environment = {
PATH: process.env["PATH"],