Files
reverse-engineering-anything/tests/boundary/filesystem/nodeRuntimeCharacterization.test.ts
T
morlutoandDevin e91c8c39c2 refactor(test): colocate pure-domain tests and reduce test debt (#471)
* refactor(test): colocate pure-domain tests and reduce test debt

Move focused immutable domain/contracts/adapter tests from
tests/composition/** beside their production owners under src/** as
colocated *.test.ts/*.fixture.ts, and relocate persistence-shaped
composition tests under tests/boundary/filesystem/**. Update import
paths, the Vitest project topology (shared worker context for pure
domain/contracts and recording-port service projects), the test
migration ledger, testing docs, and AGENTS.md to match.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* docs: remove test migration ledger

The cutover is complete and the ledger is no longer referenced anywhere
in source, scripts, or the docs gate. Drop the standalone artifact.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* docs: clean up stale migration artifacts

Remove empty directories left by the test migration and replace the
"final cutover" language in docs/testing.md with standing PR guidance
that no longer references the completed migration.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: repair CI failures from schema, knip, and stale generated artifacts

Three root causes fixed:

1. catalog-schemas.mjs: valuesAt only traversed anyOf from Zod's
   toJSONSchema, not oneOf. The webScreenshotDiffSchema change to a
   discriminated union produces oneOf, so the product catalog generator
   could not derive the web_screenshot_diff schema version. Add oneOf
   traversal.

2. conformancePackage.ts: the test rewrite removed the only imports of
   ScenarioManifest, ReplayPlan, ShimPlan, and ExpectedEvidence, making
   them unused exports that knip flags. Remove the dead type aliases.

3. Regenerate stale artifacts (product catalog, MCP tool catalog, skill
   metadata, conformance manifest) that drifted from the source changes.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-08-10 14:57:44 +08:00

289 lines
8.4 KiB
TypeScript

import { createHash } from "node:crypto";
import { readFile, realpath, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import {
executeNodeCharacterization,
prepareNodeCharacterization,
} from "../../../src/application/NodeRuntimeCharacterizationService.js";
import type {
EnabledJavaScriptReplayPolicy,
JavaScriptReplayHost,
JavaScriptReplayRunner,
} from "../../../src/application/JavaScriptReplayPlanning.js";
import { PermissionAuthority } from "../../../src/application/PermissionAuthority.js";
import { createPermissionPolicy } from "../../../src/domain/permissionPolicy.js";
import {
nodeCharacterizationPreparationInputSchema,
nodeCharacterizationPreparationOutputSchema,
} from "../../../src/domain/nodeRuntimeCharacterization.js";
const sha256 = (value: Uint8Array): string =>
createHash("sha256").update(value).digest("hex");
describe("Node runtime characterization", () => {
it("prepares without execution, then executes only the approved exact plan", async () => {
const fixture = await createFixture();
let executions = 0;
const dependencies = dependenciesFor(fixture.root, () => {
executions += 1;
});
const input = preparationInput(fixture);
const prepared = await prepareNodeCharacterization(dependencies, input);
expect(prepared.ok).toBe(true);
expect(executions).toBe(0);
if (!prepared.ok) throw prepared.error;
const output = nodeCharacterizationPreparationOutputSchema.parse(
prepared.value,
);
expect(output).toMatchObject({
phase: "preparation",
plan: {
preparation_sha256: expect.stringMatching(/^[a-f0-9]{64}$/u),
runtime: { family: "ecmascript", provider_id: "node-javascript" },
authority: {
preparation_approved: true,
execution_approved: false,
},
},
transformation: {
original_sha256: fixture.sha256,
output_format: "commonjs-factory",
},
transformation_evidence: {
predicate_type: "rea.javascript-export-transformation/v1",
authority: "shipped-artifact",
},
replay: { phase: "plan" },
});
expect(() =>
nodeCharacterizationPreparationOutputSchema.parse({
...output,
plan: { ...output.plan, expected_effect: "bounded-effects" },
}),
).toThrow();
const stale = await executeNodeCharacterization(dependencies, {
execution_approved: true,
approved_plan_sha256: "0".repeat(64),
preparation: input,
});
expect(stale.ok).toBe(false);
expect(!stale.ok && stale.error._tag).toBe("ReplayPlanStaleError");
expect(executions).toBe(0);
const executed = await executeNodeCharacterization(dependencies, {
execution_approved: true,
approved_plan_sha256: output.plan.plan_sha256,
preparation: input,
});
expect(executed.ok && executed.value).toMatchObject({
phase: "execution",
replay: {
phase: "execute",
evidence: { authority: "controlled-replay" },
},
evidence: {
predicate_type: "rea.runtime-characterization/v1",
authority: "controlled-replay",
},
});
expect(executions).toBe(1);
expect(await readFile(fixture.path)).toEqual(Buffer.from(fixture.bytes));
});
it("rejects aliases that cannot fit the provider-neutral plan identity", async () => {
const input = preparationInput(await createFixture());
expect(() =>
nodeCharacterizationPreparationInputSchema.parse({
...input,
selected_alias: "a".repeat(201),
}),
).toThrow();
});
it("maps replay-only alias syntax to a provider-neutral module identity", async () => {
const fixture = await createFixture();
const input = preparationInput(fixture);
const alias = "@scope/pkg";
const prepared = await prepareNodeCharacterization(
dependenciesFor(fixture.root, () => undefined),
{
...input,
selected_alias: alias,
replay: {
...input.replay,
left: {
...input.replay.left,
entry_alias: alias,
modules: input.replay.left.modules.map((module) => ({
...module,
alias,
})),
},
},
},
);
expect(prepared.ok && prepared.value).toMatchObject({
plan: {
callable: {
module_id: expect.stringMatching(/^module\/[a-f0-9]{64}$/u),
},
},
});
});
});
const createFixture = async () => {
const root = await createTestTempDirectory("rea-characterization-");
const path = join(root, "bundle.js");
const source = "const hidden = (value) => value.trim();";
const selected = "(value) => value.trim()";
const bytes = new TextEncoder().encode(source);
await writeFile(path, bytes);
const byteStart = source.indexOf(selected);
return {
root,
path,
bytes,
sha256: sha256(bytes),
byteStart,
selectedBytes: new TextEncoder().encode(selected),
};
};
const preparationInput = (
fixture: Awaited<ReturnType<typeof createFixture>>,
) => ({
preparation_approved: true,
selected_alias: "bundle",
expected_effect: "pure" as const,
instrumentation: {
artifact_path: fixture.path,
artifact_sha256: fixture.sha256,
selection: {
byte_start: fixture.byteStart,
byte_end: fixture.byteStart + fixture.selectedBytes.byteLength,
selected_sha256: sha256(fixture.selectedBytes),
export_name: "selected",
},
},
replay: {
mode: "plan" as const,
left: {
modules: [
{
alias: "bundle",
path: fixture.path,
format: "commonjs-factory" as const,
role: "module" as const,
dependencies: {},
},
],
entry_alias: "bundle",
entry_export: "selected",
},
cases: [{ case_id: "trim", arguments: [" value "] }],
},
});
const dependenciesFor = (root: string, onExecute: () => void) => {
const policy: EnabledJavaScriptReplayPolicy = {
status: "enabled",
roots: [root],
nodePath: process.execPath,
bubblewrapPath: process.execPath,
systemdRunPath: process.execPath,
systemctlPath: process.execPath,
shellPath: process.execPath,
};
const host: JavaScriptReplayHost = {
readSource: async (path, maximumBytes) => {
const canonicalPath = await realpath(path);
const bytes = await readFile(canonicalPath);
if (bytes.byteLength > maximumBytes)
throw new RangeError("fixture limit");
return { canonicalPath, bytes };
},
identifyExecutable: async (path) => ({
path,
version: "fixture-node-24",
sha256: "1".repeat(64),
}),
identifyWorker: async () => ({
path: "/fixture/worker.js",
version: "fixture-worker",
sha256: "2".repeat(64),
}),
identifyRuntimeClosure: async (path) => [
{
sourcePath: path,
destinationPath: "/runtime/node",
sha256: "1".repeat(64),
},
],
seccompDigest: () => "3".repeat(64),
probe: async () => undefined,
};
const runner: JavaScriptReplayRunner = {
execute: async (prepared) => {
onExecute();
return {
schema_version: 1,
plan_digest: prepared.publicPlan.plan_digest,
outcomes: [
{
case_id: "trim",
outcome: "return",
value: "value",
input_sha256:
prepared.publicPlan.cases[0]?.sha256 ?? "0".repeat(64),
output_sha256: "4".repeat(64),
truncated: false,
},
],
stderr: "",
termination: "completed",
cleanup: { state: "complete", residual_resources: [] },
limitations: ["fixture runner"],
reproducer: null,
};
},
};
const ceiling = {
capability: "javascript_replay" as const,
roots: [root],
executables: [
policy.nodePath,
policy.bubblewrapPath,
policy.systemdRunPath,
policy.systemctlPath,
policy.shellPath,
],
environment_names: [],
network: "none" as const,
mount: true,
};
const authority = new PermissionAuthority(
createPermissionPolicy(
[ceiling],
[
{
...ceiling,
grant_id: "administrator:javascript_replay",
lifetime: "administrator",
operation_identity: null,
expires_at: null,
},
],
),
);
return { policy: () => policy, host, runner, authority };
};