mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
test(browser): consolidate local artifact provenance checks (#13221)
This commit is contained in:
1 parent
63c50ecfc0
commit
e94eb2cb64
4 files changed
+123
-171
No files matched your search
@@ -1,83 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { expect } from "vitest";
|
||||
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
|
||||
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import { webSourceLocationFixture } from "../fixtures/webSourceLocation.js";
|
||||
import { cliTest } from "../support/cli/cliFixture.js";
|
||||
|
||||
cliTest(
|
||||
"delivers completed web acquisition identities through CLI and stdio MCP when a later map is missing",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-web-acquisition-cli-");
|
||||
const fixture = webSourceLocationFixture();
|
||||
const manifestPath = join(root, "manifest.json");
|
||||
const sourcePath = join(root, "files", "modules", "main.js");
|
||||
const mapPath = join(root, "missing.map");
|
||||
const manifest = JSON.stringify(fixture.manifest);
|
||||
await mkdir(join(root, "files", "modules"), { recursive: true });
|
||||
await writeFile(manifestPath, manifest);
|
||||
await writeFile(sourcePath, fixture.source);
|
||||
const environment = {
|
||||
REA_LOG_LEVEL: "silent",
|
||||
REA_BROWSER_EXECUTABLE: "relative-unconfigured-browser",
|
||||
};
|
||||
const result = await cli.run({
|
||||
arguments: [
|
||||
"trace-web-source-location",
|
||||
manifestPath,
|
||||
"0",
|
||||
mapPath,
|
||||
fixture.sourceMap.url,
|
||||
"1",
|
||||
"0",
|
||||
"--json",
|
||||
],
|
||||
environment,
|
||||
});
|
||||
expect(result.exitCode, result.stdout + result.stderr).toBe(1);
|
||||
expect(result.json).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
partial_observation: {
|
||||
kind: "web-artifact-acquisition",
|
||||
analysis: "not-performed",
|
||||
files: [
|
||||
{
|
||||
role: "manifest",
|
||||
file: {
|
||||
path: manifestPath,
|
||||
sha256: createHash("sha256").update(manifest).digest("hex"),
|
||||
bytes: Buffer.byteLength(manifest),
|
||||
},
|
||||
admission: "manifest-validated",
|
||||
},
|
||||
{
|
||||
role: "source",
|
||||
file: {
|
||||
path: sourcePath,
|
||||
sha256: createHash("sha256")
|
||||
.update(fixture.source)
|
||||
.digest("hex"),
|
||||
bytes: Buffer.byteLength(fixture.source),
|
||||
},
|
||||
admission: "source-verified",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
const { call } = await connectLocalToolsMcp(environment);
|
||||
const response = await call("trace_web_source_location", {
|
||||
manifest_path: manifestPath,
|
||||
script_index: 0,
|
||||
source_map: { path: mapPath, url: fixture.sourceMap.url },
|
||||
generated_position: { line: 1, column: 0 },
|
||||
});
|
||||
expect(parseMcpToolError(response).error).toEqual(result.json);
|
||||
expect(response.structuredContent).toBeUndefined();
|
||||
expect(result.json).not.toHaveProperty("normalized_result");
|
||||
},
|
||||
);
|
||||
@@ -3,12 +3,42 @@ import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { dirname, join } from "node:path";
|
||||
import { expect } from "vitest";
|
||||
|
||||
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
|
||||
import { lazyLocalToolsMcp } from "../fixtures/localToolsMcp.js";
|
||||
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import { webModuleArtifactsFixture } from "../fixtures/webModuleTrace.js";
|
||||
import { cliTest } from "../support/cli/cliFixture.js";
|
||||
|
||||
const environment = { REA_BROWSER_EXECUTABLE: "relative-unconfigured-browser" };
|
||||
const browserMcp = lazyLocalToolsMcp(environment);
|
||||
const url = "https://app.test/main.js.map";
|
||||
|
||||
const fileReceipt = (path: string, bytes: Buffer | string) => ({
|
||||
path,
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
bytes: Buffer.byteLength(bytes),
|
||||
});
|
||||
|
||||
const locationSelection = (manifestPath: string, mapPath: string) => ({
|
||||
operation: "trace_web_source_location",
|
||||
arguments: [
|
||||
"trace-web-source-location",
|
||||
manifestPath,
|
||||
"0",
|
||||
mapPath,
|
||||
url,
|
||||
"1",
|
||||
"0",
|
||||
"--json",
|
||||
],
|
||||
input: {
|
||||
manifest_path: manifestPath,
|
||||
script_index: 0,
|
||||
source_map: { path: mapPath, url },
|
||||
generated_position: { line: 1, column: 0 },
|
||||
},
|
||||
});
|
||||
|
||||
const capturedSource = async (bytes: Buffer, reportedUrl: string) => {
|
||||
const root = await createTestTempDirectory("rea-web-error-provenance-");
|
||||
const { manifest } = webModuleArtifactsFixture("import(name)");
|
||||
@@ -26,14 +56,27 @@ const capturedSource = async (bytes: Buffer, reportedUrl: string) => {
|
||||
const manifestPath = join(root, "manifest.json");
|
||||
await mkdir(dirname(sourcePath), { recursive: true });
|
||||
await writeFile(sourcePath, bytes);
|
||||
await writeFile(manifestPath, JSON.stringify(manifest));
|
||||
return { root, manifest, manifestPath, sourcePath };
|
||||
const text = JSON.stringify(manifest);
|
||||
await writeFile(manifestPath, text);
|
||||
const completedFiles = [
|
||||
{
|
||||
role: "manifest",
|
||||
file: fileReceipt(manifestPath, text),
|
||||
admission: "manifest-validated",
|
||||
},
|
||||
{
|
||||
role: "source",
|
||||
file: fileReceipt(sourcePath, bytes),
|
||||
admission: "source-verified",
|
||||
},
|
||||
];
|
||||
return { root, manifest, manifestPath, sourcePath, completedFiles };
|
||||
};
|
||||
|
||||
cliTest(
|
||||
"attributes captured source faults and caller-selected map faults through CLI and stdio MCP",
|
||||
async ({ cli }) => {
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
const { call } = await browserMcp();
|
||||
for (const reason of ["format", "path"] as const) {
|
||||
const fixture = await capturedSource(
|
||||
reason === "format"
|
||||
@@ -64,28 +107,13 @@ cliTest(
|
||||
],
|
||||
input: { manifest_path: fixture.manifestPath, script_index: 0 },
|
||||
},
|
||||
{
|
||||
operation: "trace_web_source_location",
|
||||
arguments: [
|
||||
"trace-web-source-location",
|
||||
fixture.manifestPath,
|
||||
"0",
|
||||
mapPath,
|
||||
"https://app.test/main.js.map",
|
||||
"1",
|
||||
"0",
|
||||
"--json",
|
||||
],
|
||||
input: {
|
||||
manifest_path: fixture.manifestPath,
|
||||
script_index: 0,
|
||||
source_map: { path: mapPath, url: "https://app.test/main.js.map" },
|
||||
generated_position: { line: 1, column: 0 },
|
||||
},
|
||||
},
|
||||
locationSelection(fixture.manifestPath, mapPath),
|
||||
];
|
||||
for (const selection of selections) {
|
||||
const response = await cli.run({ arguments: selection.arguments });
|
||||
const response = await cli.run({
|
||||
arguments: selection.arguments,
|
||||
environment,
|
||||
});
|
||||
expect(response.exitCode, response.stdout + response.stderr).toBe(1);
|
||||
const mcp = parseMcpToolError(
|
||||
await call(selection.operation, selection.input),
|
||||
@@ -109,7 +137,30 @@ cliTest(
|
||||
"https://app.test/main.js",
|
||||
);
|
||||
const mapPath = join(fixture.root, "invalid.map");
|
||||
await writeFile(mapPath, Buffer.from([255, 254]));
|
||||
const location = locationSelection(fixture.manifestPath, mapPath);
|
||||
const missing = await cli.run({
|
||||
arguments: location.arguments,
|
||||
environment,
|
||||
});
|
||||
expect(missing.exitCode, missing.stdout + missing.stderr).toBe(1);
|
||||
expect(missing.json).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [{ path: ["source_map", "path"], reason: "invalid_value" }],
|
||||
partial_observation: {
|
||||
kind: "web-artifact-acquisition",
|
||||
analysis: "not-performed",
|
||||
files: fixture.completedFiles,
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(missing.json).not.toHaveProperty("normalized_result");
|
||||
const missingMcp = await call(location.operation, location.input);
|
||||
expect(missingMcp.structuredContent).toBeUndefined();
|
||||
expect(parseMcpToolError(missingMcp).error).toEqual(missing.json);
|
||||
|
||||
const invalidBytes = Buffer.from([255, 254]);
|
||||
await writeFile(mapPath, invalidBytes);
|
||||
for (const selection of [
|
||||
{
|
||||
operation: "trace_web_module_imports",
|
||||
@@ -129,29 +180,14 @@ cliTest(
|
||||
import_map: { path: mapPath, base_url: "https://app.test/" },
|
||||
},
|
||||
field: ["import_map", "path"],
|
||||
role: "import-map",
|
||||
},
|
||||
{
|
||||
operation: "trace_web_source_location",
|
||||
arguments: [
|
||||
"trace-web-source-location",
|
||||
fixture.manifestPath,
|
||||
"0",
|
||||
mapPath,
|
||||
"https://app.test/main.js.map",
|
||||
"1",
|
||||
"0",
|
||||
"--json",
|
||||
],
|
||||
input: {
|
||||
manifest_path: fixture.manifestPath,
|
||||
script_index: 0,
|
||||
source_map: { path: mapPath, url: "https://app.test/main.js.map" },
|
||||
generated_position: { line: 1, column: 0 },
|
||||
},
|
||||
field: ["source_map", "path"],
|
||||
},
|
||||
{ ...location, field: ["source_map", "path"], role: "source-map" },
|
||||
]) {
|
||||
const response = await cli.run({ arguments: selection.arguments });
|
||||
const response = await cli.run({
|
||||
arguments: selection.arguments,
|
||||
environment,
|
||||
});
|
||||
expect(response.exitCode, response.stdout + response.stderr).toBe(1);
|
||||
const mcp = parseMcpToolError(
|
||||
await call(selection.operation, selection.input),
|
||||
@@ -161,6 +197,18 @@ cliTest(
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [{ path: selection.field, reason: "invalid_format" }],
|
||||
partial_observation: {
|
||||
kind: "web-artifact-acquisition",
|
||||
analysis: "not-performed",
|
||||
files: [
|
||||
...fixture.completedFiles,
|
||||
{
|
||||
role: selection.role,
|
||||
file: fileReceipt(mapPath, invalidBytes),
|
||||
admission: "read",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -170,7 +218,7 @@ cliTest(
|
||||
cliTest(
|
||||
"requires usable reported context and honors an explicit importer through CLI and stdio MCP",
|
||||
async ({ cli }) => {
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
const { call } = await browserMcp();
|
||||
for (const reportedUrl of ["not a URL", "file:///captured/main.js"]) {
|
||||
const fixture = await capturedSource(
|
||||
Buffer.from("import(name)"),
|
||||
|
||||
@@ -101,6 +101,7 @@ cliTest(
|
||||
// The CLI renders native separators; MCP keeps the caller's spelling and
|
||||
// still selects the same manifest root from a forward-slash Windows path.
|
||||
const forwardSlash = manifestPath.replaceAll("\\", "/");
|
||||
if (forwardSlash === manifestPath) return;
|
||||
const forward = await client.callTool({
|
||||
name: "trace_web_module_imports",
|
||||
arguments: { manifest_path: forwardSlash, script_index: 0 },
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
import { lazyLocalToolsMcp } from "../fixtures/localToolsMcp.js";
|
||||
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
|
||||
import { expect, onTestFinished } from "vitest";
|
||||
import { Client } from "@modelcontextprotocol/client";
|
||||
import {
|
||||
StdioClientTransport,
|
||||
getDefaultEnvironment,
|
||||
} from "@modelcontextprotocol/client/stdio";
|
||||
import { fileURLToPath } from "node:url";
|
||||
connectLocalToolsMcp,
|
||||
lazyLocalToolsMcp,
|
||||
} from "../fixtures/localToolsMcp.js";
|
||||
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
|
||||
import { expect } from "vitest";
|
||||
import type { Client } from "@modelcontextprotocol/client";
|
||||
import { parseEvidence } from "../../src/domain/evidence.js";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdir, symlink, writeFile, readFile, unlink } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { webSourceLocationFixture } from "../fixtures/webSourceLocation.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import { cliTest, type TestCli } from "../support/cli/cliFixture.js";
|
||||
import { createMcpSchemaValidation } from "../fixtures/mcpSchemaValidation.js";
|
||||
|
||||
const silentCliEnv = { REA_LOG_LEVEL: "silent" } as const;
|
||||
const defaultMcp = lazyLocalToolsMcp();
|
||||
@@ -64,6 +62,7 @@ const expectRetainedSourceMap = async (
|
||||
const expectEqualPositionSummary = async (
|
||||
client: Client,
|
||||
input: Record<string, unknown>,
|
||||
evidenceId: string,
|
||||
) => {
|
||||
const compact = await client.callTool({
|
||||
name: "trace_web_source_location",
|
||||
@@ -75,6 +74,7 @@ const expectEqualPositionSummary = async (
|
||||
expect(compact.isError).not.toBe(true);
|
||||
expect(compact.structuredContent).toMatchObject({
|
||||
normalized_result: {
|
||||
parent_evidence_id: evidenceId,
|
||||
summary: {
|
||||
match_count: 2,
|
||||
matches: [
|
||||
@@ -207,6 +207,7 @@ const expectForwardSlashManifestTrace = async (
|
||||
cliJson: unknown,
|
||||
) => {
|
||||
const forwardSlash = input.manifest_path.replaceAll("\\", "/");
|
||||
if (forwardSlash === input.manifest_path) return;
|
||||
const forward = await client.callTool({
|
||||
name: "trace_web_source_location",
|
||||
arguments: { ...input, manifest_path: forwardSlash },
|
||||
@@ -299,48 +300,33 @@ cliTest(
|
||||
},
|
||||
});
|
||||
expectSourceLocationLimitations(response.json);
|
||||
const transport = new StdioClientTransport({
|
||||
command: process.execPath,
|
||||
args: [
|
||||
fileURLToPath(new URL("../../scripts/rea.mjs", import.meta.url)),
|
||||
"mcp",
|
||||
],
|
||||
env: {
|
||||
...getDefaultEnvironment(),
|
||||
REA_LOG_LEVEL: "silent",
|
||||
CLANG_MODULE_CACHE_PATH: map,
|
||||
},
|
||||
stderr: "pipe",
|
||||
const { client, call } = await connectLocalToolsMcp({
|
||||
CLANG_MODULE_CACHE_PATH: map,
|
||||
REA_BROWSER_EXECUTABLE: "relative-unconfigured-browser",
|
||||
});
|
||||
const client = new Client(
|
||||
{
|
||||
name: "source-location-cli-parity",
|
||||
version: "1",
|
||||
},
|
||||
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
||||
);
|
||||
onTestFinished(async () => {
|
||||
try {
|
||||
await client.close();
|
||||
} finally {
|
||||
await transport.close();
|
||||
}
|
||||
});
|
||||
await client.connect(transport);
|
||||
const input = {
|
||||
manifest_path: manifest,
|
||||
script_index: 0,
|
||||
source_map: { path: map, url: fixture.sourceMap.url },
|
||||
generated_position: { line: 1, column: 0 },
|
||||
};
|
||||
const mcpResponse = await client.callTool({
|
||||
name: "trace_web_source_location",
|
||||
arguments: input,
|
||||
});
|
||||
const evidenceId = parseEvidence(response.json).evidence_id;
|
||||
await expectEqualPositionSummary(client, input, evidenceId);
|
||||
await unlink(map);
|
||||
const exportPath = join(root, "summary-parent.json");
|
||||
expect(
|
||||
(await call("export_evidence_bundle", { path: exportPath })).isError,
|
||||
).not.toBe(true);
|
||||
await expectRetainedSourceMap(
|
||||
exportPath,
|
||||
evidenceId,
|
||||
fixture.sourceMap.file.sha256,
|
||||
);
|
||||
await writeFile(map, fixture.sourceMap.text);
|
||||
const mcpResponse = await call("trace_web_source_location", input);
|
||||
expect(mcpResponse.isError, JSON.stringify(mcpResponse)).not.toBe(true);
|
||||
expect(mcpResponse.structuredContent).toEqual(response.json);
|
||||
await expectForwardSlashManifestTrace(client, input, response.json);
|
||||
await expectEqualPositionSummary(client, input);
|
||||
const pastColumn = await client.callTool({
|
||||
name: "trace_web_source_location",
|
||||
arguments: {
|
||||
|
||||
Reference in new issue
Block a user