Files
rea/src/domain/webModuleImports.test.ts
T
ysopwnableandGitHub 01be082e9e feat(browser): trace captured website module imports (#879)
* feat(browser): trace captured website module imports

Compose verified exported scripts, inert native import syntax and a narrow
Chromium resolution port. Preserve query/fragment identity, import-map context,
all reported capture candidates and explicit unknown execution. Share named
CLI/MCP contracts and retain native reports, diagnostics and cleanup failures.

Add focused boundary regressions and a real module-loading oracle to the
existing conditional Chrome lane. No captured application code is executed
by module tracing, and no selected website asset is refetched.

Refs #851, #774

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): preserve module contexts and source bindings

Keep capture completeness inline, validate actual source text against its
digest, and separate unsupported URL contexts from malformed input.
Use the importer origin for native module resolution and the selected map
base independently, including mixed HTTP/HTTPS and escaped HTML attributes.

Refs #851, #774

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
2026-10-07 14:59:52 +08:00

121 lines
4.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
collectWebModuleImports,
matchCapturedModules,
} from "./webModuleImports.js";
import { exportedWebScriptSchema } from "./webScriptExport.js";
describe("native module syntax observations", () => {
it("collects native imports and re-exports, preserving empty strings and computed unknowns", () => {
const source =
'import ""; import x from "./x.js"; export * from "./all.js"; export {x} from "pkg"; import(`./literal.js`); import(`./${name}.js`); import(expr);';
const result = collectWebModuleImports(source);
expect(result.state).toBe("parsed");
expect(result.imports.map((item) => [item.kind, item.specifier])).toEqual([
["static-import", ""],
["static-import", "./x.js"],
["re-export", "./all.js"],
["re-export", "pkg"],
["dynamic-import", "./literal.js"],
["dynamic-import", null],
["dynamic-import", null],
]);
for (const item of result.imports)
expect(source.slice(item.start.offset, item.end.offset)).toBe(
item.expression,
);
});
it("excludes erased TypeScript imports/exports and bundler inference", () => {
const result = collectWebModuleImports(
'import type {T} from "types"; import {type X} from "also-types"; export type {T} from "export-types"; export {type Y} from "only-types"; import {type T, value} from "mixed"; __webpack_require__.e(4); __vitePreload(() => 1, ["chunk.js"]);',
);
expect(result.imports.map((item) => item.specifier)).toEqual(["mixed"]);
});
it("uses UTF-16 source locations after astral characters", () => {
const source = 'const emoji = "😀";\nimport "./x.js";';
const item = collectWebModuleImports(source).imports[0];
expect(item?.start).toEqual({
offset: source.indexOf('"./x.js"'),
line: 2,
column: 7,
});
});
it("reports unparseable input without asserting an empty complete graph", () => {
const result = collectWebModuleImports('import "./ok.js"; function {');
expect(result).toMatchObject({ state: "unparseable", imports: [] });
expect(result.diagnostics.length).toBeGreaterThan(0);
});
});
const script = (
url: string,
kind: "page-script" | "scenario-response",
sha256 = "a".repeat(64),
) =>
exportedWebScriptSchema.parse({
url,
source:
kind === "page-script"
? {
kind,
script_key: "script1",
frame_id: null,
is_module: true,
language: "JavaScript",
source_map_url: null,
}
: {
kind,
transaction_id: "txn",
request_sequence: 1,
response_sequence: 2,
status: 200,
},
content: {
state: "exported",
relative_path: "modules/a.js",
layout: "isolated",
layout_reason: "fixture",
sha256,
bytes: 1,
media_type: "text/javascript",
redacted: false,
representation:
kind === "page-script"
? "debugger-source-utf8"
: "browser-decoded-response-bytes",
},
});
describe("captured module candidates", () => {
it("keeps query versions separate and response fragment matching weaker", () => {
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, "scenario-response"),
script(url, "scenario-response", "b".repeat(64)),
]);
expect(candidates).toHaveLength(2);
expect(
matchCapturedModules(
"https://app.test/lazy.js",
candidates.map((item) => item.script),
),
).toEqual([]);
});
});