mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 14:35:45 +00:00
test(web): consolidate module syntax and capture identity fixtures (#13073)
This commit is contained in:
1 parent
f63915d6f2
commit
8322b55552
3 files changed
+140
-179
No files matched your search
@@ -2,10 +2,9 @@ import { describe, expect, it } from "vitest";
|
||||
import { WebModuleTraceService } from "./WebModuleTraceService.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import {
|
||||
webModuleTraceResultSchema,
|
||||
type WebModuleResolution,
|
||||
type WebModuleResolutionRequest,
|
||||
import type {
|
||||
WebModuleResolution,
|
||||
WebModuleResolutionRequest,
|
||||
} from "../domain/webModuleTrace.js";
|
||||
import {
|
||||
webModuleArtifactsFixture,
|
||||
@@ -403,26 +402,3 @@ it("retains a completed engine-free trace when artifact loading finishes under c
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves imports from relational JavaScript using the selected source grammar", async () => {
|
||||
const artifacts = webModuleArtifactsFixture(
|
||||
'const order = left < import("./dep.js") > (right);',
|
||||
);
|
||||
const result = await new WebModuleTraceService(
|
||||
{ load: async () => ok(artifacts) },
|
||||
webModuleResolverFixture,
|
||||
).trace(args);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(
|
||||
webModuleTraceResultSchema.parse(result.value.normalized_result),
|
||||
).toMatchObject({
|
||||
parser: { state: "parsed", diagnostics: [] },
|
||||
imports: [
|
||||
{
|
||||
kind: "dynamic-import",
|
||||
specifier: "./dep.js",
|
||||
resolution: { state: "resolved", url: "https://app.test/dep.js" },
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
@@ -54,43 +54,36 @@ describe("native module syntax observations", () => {
|
||||
]);
|
||||
expect(collectWebModuleImports(source, "main.ts").imports).toEqual([]);
|
||||
});
|
||||
it("uses UTF-16 source locations after astral characters", () => {
|
||||
const source = 'const emoji = "😀";\nimport "./x.js";';
|
||||
const item = collectWebModuleImports(source, "main.js").imports[0];
|
||||
expect(item?.start).toEqual({
|
||||
offset: source.indexOf('"./x.js"'),
|
||||
line: 2,
|
||||
column: 7,
|
||||
});
|
||||
});
|
||||
it.each([
|
||||
['"./dep.js" as const', "./dep.js"],
|
||||
['"./dep.js" satisfies string', "./dep.js"],
|
||||
['"./dep.js"!', "./dep.js"],
|
||||
["`./dep.js` as const", "./dep.js"],
|
||||
["`./${name}.js` as const", null],
|
||||
] as const)(
|
||||
"retains exact import identity through erased syntax: %s",
|
||||
(expression, specifier) => {
|
||||
const source = `import(${expression});`;
|
||||
const result = collectWebModuleImports(source, "main.ts");
|
||||
expect(result.state).toBe("parsed");
|
||||
expect(result.diagnostics).toEqual([]);
|
||||
expect(result.imports).toEqual([
|
||||
{
|
||||
it("retains exact import identity and ranges through erased syntax", () => {
|
||||
const imports = [
|
||||
['"./dep.js" as const', "./dep.js"],
|
||||
['"./dep.js" satisfies string', "./dep.js"],
|
||||
['"./dep.js"!', "./dep.js"],
|
||||
["`./dep.js` as const", "./dep.js"],
|
||||
["`./${name}.js` as const", null],
|
||||
] as const;
|
||||
const source = imports
|
||||
.map(([expression]) => `import(${expression});`)
|
||||
.join("\n");
|
||||
expect(collectWebModuleImports(source, "main.ts")).toEqual({
|
||||
state: "parsed",
|
||||
diagnostics: [],
|
||||
imports: imports.map(([expression, specifier], index) => {
|
||||
const offset = source.indexOf(expression);
|
||||
return {
|
||||
kind: "dynamic-import",
|
||||
specifier,
|
||||
expression,
|
||||
start: { offset: 7, line: 1, column: 7 },
|
||||
start: { offset, line: index + 1, column: 7 },
|
||||
end: {
|
||||
offset: 7 + expression.length,
|
||||
line: 1,
|
||||
offset: offset + expression.length,
|
||||
line: index + 1,
|
||||
column: 7 + expression.length,
|
||||
},
|
||||
},
|
||||
]);
|
||||
},
|
||||
);
|
||||
};
|
||||
}),
|
||||
});
|
||||
});
|
||||
it("reports unparseable input without asserting an empty complete graph", () => {
|
||||
const result = collectWebModuleImports(
|
||||
'import "./ok.js"; function {',
|
||||
@@ -166,35 +159,35 @@ const script = (
|
||||
},
|
||||
});
|
||||
describe("captured module candidates", () => {
|
||||
it("keeps query versions separate and response fragment matching weaker", () => {
|
||||
it("retains exact capture versions and separates query and weaker fragment matches", () => {
|
||||
const url = "https://app.test/a.js?v=1";
|
||||
const scripts = [
|
||||
script("https://app.test/a.js?v=1#one", "page-script"),
|
||||
script("https://app.test/a.js?v=1#two", "page-script"),
|
||||
script("https://app.test/a.js?v=1", "scenario-response"),
|
||||
script("https://app.test/a.js?v=2", "scenario-response"),
|
||||
];
|
||||
expect(
|
||||
matchCapturedModules("https://app.test/a.js?v=1#one", scripts).map(
|
||||
(item) => [item.script_index, item.match],
|
||||
),
|
||||
).toEqual([
|
||||
[0, "exact-reported-url"],
|
||||
[2, "response-url-without-fragment"],
|
||||
]);
|
||||
});
|
||||
it("preserves every captured version at the same URL and never picks one", () => {
|
||||
const url = "https://app.test/a.js";
|
||||
const candidates = matchCapturedModules(url, [
|
||||
script(`${url}#one`, "page-script"),
|
||||
script(`${url}#two`, "page-script"),
|
||||
script(url, "scenario-response"),
|
||||
script(url, "scenario-response", "b".repeat(64)),
|
||||
script("https://app.test/a.js?v=2", "scenario-response"),
|
||||
];
|
||||
expect(matchCapturedModules(url, scripts)).toEqual([
|
||||
{ script_index: 2, match: "exact-reported-url", script: scripts[2] },
|
||||
{ script_index: 3, match: "exact-reported-url", script: scripts[3] },
|
||||
]);
|
||||
expect(candidates).toHaveLength(2);
|
||||
expect(
|
||||
matchCapturedModules(
|
||||
"https://app.test/lazy.js",
|
||||
candidates.map((item) => item.script),
|
||||
),
|
||||
).toEqual([]);
|
||||
expect(matchCapturedModules(`${url}#one`, scripts)).toEqual([
|
||||
{ script_index: 0, match: "exact-reported-url", script: scripts[0] },
|
||||
{
|
||||
script_index: 2,
|
||||
match: "response-url-without-fragment",
|
||||
script: scripts[2],
|
||||
},
|
||||
{
|
||||
script_index: 3,
|
||||
match: "response-url-without-fragment",
|
||||
script: scripts[3],
|
||||
},
|
||||
]);
|
||||
expect(matchCapturedModules("https://app.test/lazy.js", scripts)).toEqual(
|
||||
[],
|
||||
);
|
||||
});
|
||||
it("retains exact non-URL identities without matching response fragments", () => {
|
||||
const scripts = [
|
||||
|
||||
@@ -17,100 +17,92 @@ import {
|
||||
webModuleResolverFixture,
|
||||
} from "../../fixtures/webModuleTrace.js";
|
||||
|
||||
it.each([
|
||||
{
|
||||
source: 'const order = left < import("./dep.js") > (right);',
|
||||
expected: ["./dep.js"],
|
||||
},
|
||||
{
|
||||
source:
|
||||
'const fn = c ? x => import("./a.js") : y => import("./b.js"); import("./c.js");',
|
||||
expected: ["./a.js", "./b.js", "./c.js"],
|
||||
},
|
||||
])(
|
||||
"retains JavaScript imports and requests resolution through public MCP: $source",
|
||||
async ({ source, expected }) => {
|
||||
const root = await createTestTempDirectory("rea-module-grammar-");
|
||||
const original = webModuleArtifactsFixture(source);
|
||||
const manifest = {
|
||||
...original.manifest,
|
||||
output_directory: root,
|
||||
scripts: original.manifest.scripts.map((script) => ({
|
||||
...script,
|
||||
url: "https://app.test/main.ts?build=1#entry",
|
||||
})),
|
||||
};
|
||||
const sourcePath = join(root, "files", "modules", "main.js");
|
||||
const manifestPath = join(root, "manifest.json");
|
||||
await mkdir(join(root, "files", "modules"), { recursive: true });
|
||||
await writeFile(sourcePath, source);
|
||||
const manifestText = JSON.stringify(manifest);
|
||||
await writeFile(manifestPath, manifestText);
|
||||
const requests: WebModuleResolutionRequest[] = [];
|
||||
const service = new WebModuleTraceService(new LocalWebModuleArtifacts(), {
|
||||
resolve: async (request) => {
|
||||
requests.push(request);
|
||||
return webModuleResolverFixture.resolve(request);
|
||||
},
|
||||
});
|
||||
const session = createTestBinarySession(() => {
|
||||
throw new Error(
|
||||
"Retained module tracing must not acquire a binary provider",
|
||||
);
|
||||
});
|
||||
const { client } = await createApplicationMcpHarness({
|
||||
session,
|
||||
serverOptions: { webModuleTrace: service },
|
||||
clientIdentity: { name: "module-grammar", version: "1" },
|
||||
});
|
||||
const response = await client.callTool({
|
||||
name: "trace_web_module_imports",
|
||||
arguments: { manifest_path: manifestPath, script_index: 0 },
|
||||
});
|
||||
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
||||
const evidence = parseEvidence(response.structuredContent);
|
||||
const result = webModuleTraceResultSchema.parse(evidence.normalized_result);
|
||||
expect(requests).toEqual([
|
||||
{
|
||||
importerUrl: manifest.scripts[0]!.url,
|
||||
importMap: null,
|
||||
specifiers: expected,
|
||||
},
|
||||
]);
|
||||
expect(result).toMatchObject({
|
||||
source: {
|
||||
path: sourcePath,
|
||||
sha256: original.sourceFile.sha256,
|
||||
bytes: original.sourceFile.bytes,
|
||||
},
|
||||
parser: { state: "parsed", diagnostics: [] },
|
||||
engine: { id: "fixture", version: "1" },
|
||||
});
|
||||
expect(
|
||||
result.imports.map(({ kind, specifier, resolution, execution }) => ({
|
||||
kind,
|
||||
specifier,
|
||||
resolution,
|
||||
execution,
|
||||
})),
|
||||
).toEqual(
|
||||
expected.map((specifier) => ({
|
||||
kind: "dynamic-import",
|
||||
specifier,
|
||||
resolution: {
|
||||
state: "resolved",
|
||||
url: new URL(specifier, manifest.scripts[0]!.url).href,
|
||||
},
|
||||
execution: "unknown",
|
||||
})),
|
||||
it("selects the retained JavaScript grammar and resolves exact imports through MCP", async () => {
|
||||
const source = [
|
||||
'const order = left < import("./dep.js") > (right);',
|
||||
'const fn = c ? x => import("./a.js") : y => import("./b.js"); import("./c.js");',
|
||||
].join("\n");
|
||||
const expected = ["./dep.js", "./a.js", "./b.js", "./c.js"];
|
||||
const root = await createTestTempDirectory("rea-module-grammar-");
|
||||
const original = webModuleArtifactsFixture(source);
|
||||
const manifest = {
|
||||
...original.manifest,
|
||||
output_directory: root,
|
||||
scripts: original.manifest.scripts.map((script) => ({
|
||||
...script,
|
||||
url: "https://app.test/main.ts?build=1#entry",
|
||||
})),
|
||||
};
|
||||
const sourcePath = join(root, "files", "modules", "main.js");
|
||||
const manifestPath = join(root, "manifest.json");
|
||||
await mkdir(join(root, "files", "modules"), { recursive: true });
|
||||
await writeFile(sourcePath, source);
|
||||
const manifestText = JSON.stringify(manifest);
|
||||
await writeFile(manifestPath, manifestText);
|
||||
const requests: WebModuleResolutionRequest[] = [];
|
||||
const service = new WebModuleTraceService(new LocalWebModuleArtifacts(), {
|
||||
resolve: async (request) => {
|
||||
requests.push(request);
|
||||
return webModuleResolverFixture.resolve(request);
|
||||
},
|
||||
});
|
||||
const session = createTestBinarySession(() => {
|
||||
throw new Error(
|
||||
"Retained module tracing must not acquire a binary provider",
|
||||
);
|
||||
for (const reference of result.imports)
|
||||
expect(source.slice(reference.start.offset, reference.end.offset)).toBe(
|
||||
reference.expression,
|
||||
);
|
||||
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
|
||||
expect(await readFile(sourcePath, "utf8")).toBe(source);
|
||||
expect(await readFile(manifestPath, "utf8")).toBe(manifestText);
|
||||
await client.ping();
|
||||
},
|
||||
);
|
||||
});
|
||||
const { client } = await createApplicationMcpHarness({
|
||||
session,
|
||||
serverOptions: { webModuleTrace: service },
|
||||
clientIdentity: { name: "module-grammar", version: "1" },
|
||||
});
|
||||
const response = await client.callTool({
|
||||
name: "trace_web_module_imports",
|
||||
arguments: { manifest_path: manifestPath, script_index: 0 },
|
||||
});
|
||||
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
||||
const evidence = parseEvidence(response.structuredContent);
|
||||
const result = webModuleTraceResultSchema.parse(evidence.normalized_result);
|
||||
expect(requests).toEqual([
|
||||
{
|
||||
importerUrl: manifest.scripts[0]!.url,
|
||||
importMap: null,
|
||||
specifiers: expected,
|
||||
},
|
||||
]);
|
||||
expect(result).toMatchObject({
|
||||
source: {
|
||||
path: sourcePath,
|
||||
sha256: original.sourceFile.sha256,
|
||||
bytes: original.sourceFile.bytes,
|
||||
},
|
||||
parser: { state: "parsed", diagnostics: [] },
|
||||
engine: { id: "fixture", version: "1" },
|
||||
});
|
||||
expect(
|
||||
result.imports.map(({ kind, specifier, resolution, execution }) => ({
|
||||
kind,
|
||||
specifier,
|
||||
resolution,
|
||||
execution,
|
||||
})),
|
||||
).toEqual(
|
||||
expected.map((specifier) => ({
|
||||
kind: "dynamic-import",
|
||||
specifier,
|
||||
resolution: {
|
||||
state: "resolved",
|
||||
url: new URL(specifier, manifest.scripts[0]!.url).href,
|
||||
},
|
||||
execution: "unknown",
|
||||
})),
|
||||
);
|
||||
for (const reference of result.imports)
|
||||
expect(source.slice(reference.start.offset, reference.end.offset)).toBe(
|
||||
reference.expression,
|
||||
);
|
||||
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
|
||||
expect(await readFile(sourcePath, "utf8")).toBe(source);
|
||||
expect(await readFile(manifestPath, "utf8")).toBe(manifestText);
|
||||
await client.ping();
|
||||
});
|
||||
Reference in new issue
Block a user