fix(javascript): preserve package and source-map boundary identity (#1939)
* fix(javascript): preserve bare package specifier suffixes * fix(javascript): resolve ESM package self references * fix(javascript): recover retained prefixed source maps * test(javascript): cover suffixed ESM self references * fix(javascript): retain legacy ESM package suffix behavior * fix(javascript): reject legacy ESM directory and extension fallback * refactor(javascript): consolidate package resolution and workflow coverage * fix(javascript): report exact ESM package subpath misses * fix(javascript): compare parsed names for ESM self references * refactor(javascript): parse bare package requests once * fix(javascript): derive ESM dependency lookup from package URLs --------- Co-authored-by: hhang <169027046+xu91102@users.noreply.github.com>
This commit is contained in:
@@ -4,12 +4,10 @@ import { createRequire } from "node:module";
|
||||
import { dirname, join, relative } from "node:path";
|
||||
|
||||
import { expect } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
|
||||
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
|
||||
|
||||
const literalAliases = ["#/x", "#x%2Fy", "#x%2Ey", "#x\\y", "#x\0y"];
|
||||
const fallbackAliases = {
|
||||
@@ -158,26 +156,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
expect.objectContaining({ code: "ERR_PACKAGE_IMPORT_NOT_DEFINED" }),
|
||||
);
|
||||
|
||||
let document: unknown;
|
||||
if (surface === "CLI") {
|
||||
const response = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--format", "json"],
|
||||
environment: { REA_LOG_LEVEL: "silent" },
|
||||
});
|
||||
expect(response.exitCode, response.stderr).toBe(0);
|
||||
document = response.json;
|
||||
} else {
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
const response = await call("analyze_javascript_application", {
|
||||
input_path: root,
|
||||
format: "directory",
|
||||
});
|
||||
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
||||
document = response.structuredContent;
|
||||
}
|
||||
const { graph } = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(document).normalized_result;
|
||||
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
|
||||
for (const specifier of resolvedAliases)
|
||||
for (const [moduleKind, sourcePath, source] of [
|
||||
["require", "main.cjs", mainSource],
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { dirname, join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { expect } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
|
||||
|
||||
const requests = [
|
||||
{ specifier: "legacy?x", plainName: "legacy", exports: false },
|
||||
{ specifier: "exported#entry", plainName: "exported", exports: true },
|
||||
{ specifier: "@scope/legacy?x", plainName: "@scope/legacy", exports: false },
|
||||
{ specifier: "@scope#entry/pkg", plainName: "@scope/pkg", exports: true },
|
||||
] as const;
|
||||
|
||||
// Windows cannot materialize the question-mark directories in this fixture.
|
||||
cliTest.skipIf(process.platform === "win32").for(["CLI", "stdio MCP"] as const)(
|
||||
"keeps ESM root URL suffixes out of dependency filesystem lookup through %s",
|
||||
async (surface, { cli }) => {
|
||||
const root = await createTestTempDirectory("rea-package-root-suffix-");
|
||||
const sources = {
|
||||
"main.mjs": requests
|
||||
.map(({ specifier }) => `import ${JSON.stringify(specifier)};`)
|
||||
.join("\n"),
|
||||
"main.cjs": requests
|
||||
.map(({ specifier }) => `require(${JSON.stringify(specifier)});`)
|
||||
.join("\n"),
|
||||
};
|
||||
const files: Record<string, string> = { ...sources };
|
||||
for (const item of requests)
|
||||
for (const name of [item.specifier, item.plainName]) {
|
||||
const directory = `node_modules/${name}`;
|
||||
files[`${directory}/package.json`] = JSON.stringify(
|
||||
item.exports ? { exports: "./entry.cjs" } : { main: "./entry.cjs" },
|
||||
);
|
||||
files[`${directory}/entry.cjs`] =
|
||||
`module.exports = ${JSON.stringify(name)};`;
|
||||
}
|
||||
await Promise.all(
|
||||
Object.entries(files).map(async ([path, text]) => {
|
||||
const absolute = join(root, path);
|
||||
await mkdir(dirname(absolute), { recursive: true });
|
||||
await writeFile(absolute, text);
|
||||
}),
|
||||
);
|
||||
if (surface === "CLI") {
|
||||
const native = await promisify(execFile)(
|
||||
process.execPath,
|
||||
[
|
||||
"--input-type=module",
|
||||
"--eval",
|
||||
`import { createRequire } from "node:module";
|
||||
const require = createRequire(new URL("./main.cjs", import.meta.url));
|
||||
const requests = ${JSON.stringify(requests.map(({ specifier }) => specifier))};
|
||||
const results = await Promise.all(requests.map(async (specifier) => {
|
||||
let esm;
|
||||
try { esm = { value: (await import(specifier)).default, error: null }; }
|
||||
catch (error) { esm = { value: null, error: error.code }; }
|
||||
return { esm, require: require(specifier) };
|
||||
}));
|
||||
console.log(JSON.stringify(results));`,
|
||||
],
|
||||
{ cwd: root, timeout: 5_000 },
|
||||
);
|
||||
expect(JSON.parse(native.stdout)).toEqual(
|
||||
requests.map(({ specifier }) => ({
|
||||
esm: { value: null, error: "ERR_MODULE_NOT_FOUND" },
|
||||
require: specifier,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
|
||||
for (const { specifier } of requests)
|
||||
for (const [moduleKind, sourcePath] of [
|
||||
["import", "main.mjs"],
|
||||
["require", "main.cjs"],
|
||||
] as const)
|
||||
expect(graph.edges).toContainEqual(
|
||||
expect.objectContaining({
|
||||
relation: "imports",
|
||||
properties: expect.objectContaining({
|
||||
module_link_kind: moduleKind,
|
||||
specifier,
|
||||
resolved_path:
|
||||
moduleKind === "import"
|
||||
? null
|
||||
: `node_modules/${specifier}/entry.cjs`,
|
||||
resolution_status:
|
||||
moduleKind === "import" ? "external" : "resolved",
|
||||
}),
|
||||
evidence: expect.objectContaining({
|
||||
artifact: expect.objectContaining({
|
||||
sha256: createHash("sha256")
|
||||
.update(sources[sourcePath])
|
||||
.digest("hex"),
|
||||
}),
|
||||
location: expect.objectContaining({
|
||||
value: expect.objectContaining({ source: sourcePath }),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,207 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { dirname, join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { expect } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
|
||||
|
||||
type SelfNameCase = {
|
||||
readonly directory: string;
|
||||
readonly packageName: string;
|
||||
readonly exportKey: string;
|
||||
readonly specifier: string;
|
||||
readonly selfValue: string;
|
||||
readonly dependency?: {
|
||||
readonly packagePath: string;
|
||||
readonly exportKey: string;
|
||||
readonly value: string;
|
||||
};
|
||||
readonly esmTarget: string;
|
||||
readonly requireTarget: string;
|
||||
readonly esmValue: string;
|
||||
readonly requireValue: string;
|
||||
};
|
||||
|
||||
const cases = [
|
||||
{
|
||||
directory: "scope-prefix",
|
||||
packageName: "@scope",
|
||||
exportKey: "./pkg",
|
||||
specifier: "@scope/pkg",
|
||||
selfValue: "scope self",
|
||||
dependency: {
|
||||
packagePath: "node_modules/@scope/pkg",
|
||||
exportKey: ".",
|
||||
value: "scope dependency",
|
||||
},
|
||||
esmTarget: "scope-prefix/node_modules/@scope/pkg/dependency.cjs",
|
||||
requireTarget: "scope-prefix/self.cjs",
|
||||
esmValue: "scope dependency",
|
||||
requireValue: "scope self",
|
||||
},
|
||||
{
|
||||
directory: "slash-name",
|
||||
packageName: "fixture/sub",
|
||||
exportKey: "./feature",
|
||||
specifier: "fixture/sub/feature",
|
||||
selfValue: "slash self",
|
||||
dependency: {
|
||||
packagePath: "node_modules/fixture",
|
||||
exportKey: "./sub/feature",
|
||||
value: "slash dependency",
|
||||
},
|
||||
esmTarget: "slash-name/node_modules/fixture/dependency.cjs",
|
||||
requireTarget: "slash-name/self.cjs",
|
||||
esmValue: "slash dependency",
|
||||
requireValue: "slash self",
|
||||
},
|
||||
{
|
||||
directory: "slash-exact-name",
|
||||
packageName: "fixture/sub",
|
||||
exportKey: ".",
|
||||
specifier: "fixture/sub",
|
||||
selfValue: "slash exact self",
|
||||
dependency: {
|
||||
packagePath: "node_modules/fixture",
|
||||
exportKey: "./sub",
|
||||
value: "slash exact dependency",
|
||||
},
|
||||
esmTarget: "slash-exact-name/node_modules/fixture/dependency.cjs",
|
||||
requireTarget: "slash-exact-name/self.cjs",
|
||||
esmValue: "slash exact dependency",
|
||||
requireValue: "slash exact self",
|
||||
},
|
||||
{
|
||||
directory: "valid-scoped",
|
||||
packageName: "@scope/pkg",
|
||||
exportKey: "./feature",
|
||||
specifier: "@scope/pkg/feature",
|
||||
selfValue: "valid scoped self",
|
||||
esmTarget: "valid-scoped/self.cjs",
|
||||
requireTarget: "valid-scoped/self.cjs",
|
||||
esmValue: "valid scoped self",
|
||||
requireValue: "valid scoped self",
|
||||
},
|
||||
{
|
||||
directory: "query-self-name",
|
||||
packageName: "self?x",
|
||||
exportKey: ".",
|
||||
specifier: "self?x",
|
||||
selfValue: "query self",
|
||||
esmTarget: "query-self-name/self.cjs",
|
||||
requireTarget: "query-self-name/self.cjs",
|
||||
esmValue: "query self",
|
||||
requireValue: "query self",
|
||||
},
|
||||
{
|
||||
directory: "fragment-self-name",
|
||||
packageName: "@scope/self#entry",
|
||||
exportKey: ".",
|
||||
specifier: "@scope/self#entry",
|
||||
selfValue: "fragment self",
|
||||
esmTarget: "fragment-self-name/self.cjs",
|
||||
requireTarget: "fragment-self-name/self.cjs",
|
||||
esmValue: "fragment self",
|
||||
requireValue: "fragment self",
|
||||
},
|
||||
] satisfies readonly SelfNameCase[];
|
||||
|
||||
const sourceFor = (specifier: string, moduleKind: "import" | "require") =>
|
||||
moduleKind === "import"
|
||||
? `import value from ${JSON.stringify(specifier)};\nexport default value;`
|
||||
: `const value = require(${JSON.stringify(specifier)});\nmodule.exports = value;`;
|
||||
|
||||
const createSelfNameFixture = async () => {
|
||||
const root = await createTestTempDirectory("rea-package-self-name-boundary-");
|
||||
const files: Record<string, string> = {};
|
||||
for (const item of cases) {
|
||||
const prefix = item.directory;
|
||||
files[`${prefix}/package.json`] = JSON.stringify({
|
||||
name: item.packageName,
|
||||
exports: { [item.exportKey]: "./self.cjs" },
|
||||
});
|
||||
files[`${prefix}/main.mjs`] = sourceFor(item.specifier, "import");
|
||||
files[`${prefix}/main.cjs`] = sourceFor(item.specifier, "require");
|
||||
files[`${prefix}/self.cjs`] =
|
||||
`module.exports = ${JSON.stringify(item.selfValue)};`;
|
||||
if (item.dependency !== undefined) {
|
||||
const dependencyPrefix = `${prefix}/${item.dependency.packagePath}`;
|
||||
files[`${dependencyPrefix}/package.json`] = JSON.stringify({
|
||||
exports: { [item.dependency.exportKey]: "./dependency.cjs" },
|
||||
});
|
||||
files[`${dependencyPrefix}/dependency.cjs`] =
|
||||
`module.exports = ${JSON.stringify(item.dependency.value)};`;
|
||||
}
|
||||
}
|
||||
files["node-control.mjs"] = `import { createRequire } from "node:module";
|
||||
const cases = ${JSON.stringify(cases.map(({ directory, specifier }) => ({ directory, specifier })))};
|
||||
const esm = await Promise.all(
|
||||
cases.map(async ({ directory }) =>
|
||||
(await import(new URL(directory + "/main.mjs", import.meta.url))).default,
|
||||
),
|
||||
);
|
||||
const require = cases.map(({ directory, specifier }) =>
|
||||
createRequire(new URL(directory + "/main.cjs", import.meta.url))(specifier),
|
||||
);
|
||||
console.log(JSON.stringify({ esm, require }));`;
|
||||
await Promise.all(
|
||||
Object.entries(files).map(async ([path, content]) => {
|
||||
const absolute = join(root, path);
|
||||
await mkdir(dirname(absolute), { recursive: true });
|
||||
await writeFile(absolute, content);
|
||||
}),
|
||||
);
|
||||
return root;
|
||||
};
|
||||
|
||||
cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
"matches self references by parsed ESM name and preserves CommonJS prefix matching through %s",
|
||||
async (surface, { cli }) => {
|
||||
const root = await createSelfNameFixture();
|
||||
if (surface === "CLI") {
|
||||
const native = await promisify(execFile)(
|
||||
process.execPath,
|
||||
[join(root, "node-control.mjs")],
|
||||
{ cwd: root, timeout: 5_000 },
|
||||
);
|
||||
expect(JSON.parse(native.stdout)).toEqual({
|
||||
esm: cases.map(({ esmValue }) => esmValue),
|
||||
require: cases.map(({ requireValue }) => requireValue),
|
||||
});
|
||||
}
|
||||
|
||||
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
|
||||
for (const item of cases)
|
||||
for (const [moduleKind, target] of [
|
||||
["import", item.esmTarget],
|
||||
["require", item.requireTarget],
|
||||
] as const) {
|
||||
const sourcePath = `${item.directory}/main.${moduleKind === "import" ? "mjs" : "cjs"}`;
|
||||
const source = sourceFor(item.specifier, moduleKind);
|
||||
expect(graph.edges).toContainEqual(
|
||||
expect.objectContaining({
|
||||
relation: "imports",
|
||||
properties: expect.objectContaining({
|
||||
module_link_kind: moduleKind,
|
||||
specifier: item.specifier,
|
||||
resolved_path: target,
|
||||
resolution_status: "resolved",
|
||||
}),
|
||||
evidence: expect.objectContaining({
|
||||
artifact: expect.objectContaining({
|
||||
sha256: createHash("sha256").update(source).digest("hex"),
|
||||
}),
|
||||
location: expect.objectContaining({
|
||||
value: expect.objectContaining({ source: sourcePath }),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,278 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { expect } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
|
||||
|
||||
type PackageSpecifierCase = {
|
||||
readonly specifier: string;
|
||||
readonly target: string | null;
|
||||
readonly status: "resolved" | "external" | "rejected" | "not-found";
|
||||
readonly value: string | number | null;
|
||||
readonly error: string | null;
|
||||
};
|
||||
|
||||
const requests = [
|
||||
{
|
||||
specifier: "rea-self-fixture",
|
||||
target: "root.cjs",
|
||||
status: "resolved",
|
||||
value: 11,
|
||||
error: null,
|
||||
},
|
||||
{
|
||||
specifier: "rea-self-fixture/feature",
|
||||
target: "feature.cjs",
|
||||
status: "resolved",
|
||||
value: 22,
|
||||
error: null,
|
||||
},
|
||||
{
|
||||
specifier: "rea-self-fixture/feature?cache=1",
|
||||
target: "punctuated.cjs",
|
||||
status: "resolved",
|
||||
value: 33,
|
||||
error: null,
|
||||
},
|
||||
{
|
||||
specifier: "rea-self-fixture/feature?cache=2",
|
||||
target: null,
|
||||
status: "external",
|
||||
value: null,
|
||||
error: "ERR_PACKAGE_PATH_NOT_EXPORTED",
|
||||
},
|
||||
{
|
||||
specifier: "rea-self-fixture/feature#entry",
|
||||
target: null,
|
||||
status: "external",
|
||||
value: null,
|
||||
error: "ERR_PACKAGE_PATH_NOT_EXPORTED",
|
||||
},
|
||||
{
|
||||
specifier: "fixture/feature?cache=1",
|
||||
target: null,
|
||||
status: "external",
|
||||
value: null,
|
||||
error: "ERR_PACKAGE_PATH_NOT_EXPORTED",
|
||||
},
|
||||
{
|
||||
specifier: "fixture/feature#entry",
|
||||
target: null,
|
||||
status: "external",
|
||||
value: null,
|
||||
error: "ERR_PACKAGE_PATH_NOT_EXPORTED",
|
||||
},
|
||||
{
|
||||
specifier: "fixture/literal?cache=1",
|
||||
target: "node_modules/fixture/literal.cjs",
|
||||
status: "resolved",
|
||||
value: "literal",
|
||||
error: null,
|
||||
},
|
||||
{
|
||||
specifier: "legacy/?cache=1",
|
||||
target: null,
|
||||
status: "rejected",
|
||||
value: null,
|
||||
error: "ERR_UNSUPPORTED_DIR_IMPORT",
|
||||
},
|
||||
{
|
||||
specifier: "legacy/#entry",
|
||||
target: null,
|
||||
status: "rejected",
|
||||
value: null,
|
||||
error: "ERR_UNSUPPORTED_DIR_IMPORT",
|
||||
},
|
||||
{
|
||||
specifier: "legacy/nested/?cache=1",
|
||||
target: null,
|
||||
status: "rejected",
|
||||
value: null,
|
||||
error: "ERR_UNSUPPORTED_DIR_IMPORT",
|
||||
},
|
||||
{
|
||||
specifier: "legacy/nested/#entry",
|
||||
target: null,
|
||||
status: "rejected",
|
||||
value: null,
|
||||
error: "ERR_UNSUPPORTED_DIR_IMPORT",
|
||||
},
|
||||
{
|
||||
specifier: "legacy/feature.js",
|
||||
target: "node_modules/legacy/feature.js",
|
||||
status: "resolved",
|
||||
value: "legacy feature",
|
||||
error: null,
|
||||
},
|
||||
{
|
||||
specifier: "legacy/feature.js?cache=1",
|
||||
target: "node_modules/legacy/feature.js",
|
||||
status: "resolved",
|
||||
value: "legacy feature",
|
||||
error: null,
|
||||
},
|
||||
{
|
||||
specifier: "legacy/feature.js#entry",
|
||||
target: "node_modules/legacy/feature.js",
|
||||
status: "resolved",
|
||||
value: "legacy feature",
|
||||
error: null,
|
||||
},
|
||||
{
|
||||
specifier: "legacy/with%20space.js?cache=1",
|
||||
target: "node_modules/legacy/with space.js",
|
||||
status: "resolved",
|
||||
value: "legacy spaced feature",
|
||||
error: null,
|
||||
},
|
||||
{
|
||||
specifier: "legacy/feature",
|
||||
target: null,
|
||||
status: "not-found",
|
||||
value: null,
|
||||
error: "ERR_MODULE_NOT_FOUND",
|
||||
},
|
||||
] satisfies readonly PackageSpecifierCase[];
|
||||
|
||||
const createPackageSpecifierFixture = async (): Promise<{
|
||||
readonly root: string;
|
||||
readonly source: string;
|
||||
}> => {
|
||||
const root = await createTestTempDirectory("rea-package-specifier-suffix-");
|
||||
const packagePath = join(root, "node_modules", "fixture");
|
||||
const legacyPackagePath = join(root, "node_modules", "legacy");
|
||||
await Promise.all([
|
||||
mkdir(packagePath, { recursive: true }),
|
||||
mkdir(join(legacyPackagePath, "nested"), { recursive: true }),
|
||||
]);
|
||||
await writeFile(
|
||||
join(root, "package.json"),
|
||||
JSON.stringify({
|
||||
name: "rea-self-fixture",
|
||||
exports: {
|
||||
".": { import: "./root.cjs", require: "./require.cjs" },
|
||||
"./feature": { import: "./feature.cjs", require: "./require.cjs" },
|
||||
"./feature?cache=1": {
|
||||
import: "./punctuated.cjs",
|
||||
require: "./require.cjs",
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
const source = requests
|
||||
.map(
|
||||
({ specifier }, index) =>
|
||||
`import value${index} from ${JSON.stringify(specifier)};`,
|
||||
)
|
||||
.join("\n");
|
||||
await Promise.all([
|
||||
writeFile(join(root, "main.mjs"), source),
|
||||
writeFile(join(root, "root.cjs"), "module.exports = 11;"),
|
||||
writeFile(join(root, "feature.cjs"), "module.exports = 22;"),
|
||||
writeFile(join(root, "punctuated.cjs"), "module.exports = 33;"),
|
||||
writeFile(join(root, "require.cjs"), "module.exports = -1;"),
|
||||
]);
|
||||
await writeFile(
|
||||
join(packagePath, "package.json"),
|
||||
JSON.stringify({
|
||||
exports: {
|
||||
"./feature": "./feature.cjs",
|
||||
"./literal?cache=1": "./literal.cjs",
|
||||
},
|
||||
}),
|
||||
);
|
||||
await Promise.all([
|
||||
writeFile(join(packagePath, "feature.cjs"), 'module.exports = "feature";'),
|
||||
writeFile(join(packagePath, "literal.cjs"), 'module.exports = "literal";'),
|
||||
writeFile(
|
||||
join(legacyPackagePath, "package.json"),
|
||||
JSON.stringify({ name: "legacy", main: "./main.cjs" }),
|
||||
),
|
||||
writeFile(
|
||||
join(legacyPackagePath, "main.cjs"),
|
||||
'module.exports = "legacy root";',
|
||||
),
|
||||
writeFile(
|
||||
join(legacyPackagePath, "feature.js"),
|
||||
'module.exports = "legacy feature";',
|
||||
),
|
||||
writeFile(
|
||||
join(legacyPackagePath, "with space.js"),
|
||||
'module.exports = "legacy spaced feature";',
|
||||
),
|
||||
writeFile(
|
||||
join(legacyPackagePath, "nested", "package.json"),
|
||||
JSON.stringify({ main: "./main.cjs" }),
|
||||
),
|
||||
writeFile(
|
||||
join(legacyPackagePath, "nested", "main.cjs"),
|
||||
'module.exports = "legacy nested";',
|
||||
),
|
||||
]);
|
||||
return { root, source };
|
||||
};
|
||||
|
||||
cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
"resolves package self references and suffix keys through %s",
|
||||
async (surface, { cli }) => {
|
||||
const { root, source } = await createPackageSpecifierFixture();
|
||||
if (surface === "CLI") {
|
||||
const native = await promisify(execFile)(
|
||||
process.execPath,
|
||||
[
|
||||
"--input-type=module",
|
||||
"-e",
|
||||
`const requests = ${JSON.stringify(requests.map(({ specifier }) => specifier))};
|
||||
const results = await Promise.all(requests.map(async (specifier) => {
|
||||
try {
|
||||
const result = await import(specifier);
|
||||
return { value: result.default, error: null };
|
||||
} catch (error) {
|
||||
return { value: null, error: error.code };
|
||||
}
|
||||
}));
|
||||
console.log(JSON.stringify(results));`,
|
||||
],
|
||||
{ cwd: root, timeout: 5_000 },
|
||||
);
|
||||
expect(JSON.parse(native.stdout)).toEqual(
|
||||
requests.map(({ value, error }) => ({ value, error })),
|
||||
);
|
||||
}
|
||||
|
||||
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
|
||||
for (const { specifier, target, status } of requests)
|
||||
expect(graph.edges).toContainEqual(
|
||||
expect.objectContaining({
|
||||
relation: "imports",
|
||||
properties: expect.objectContaining({
|
||||
module_link_kind: "import",
|
||||
specifier,
|
||||
resolved_path: target,
|
||||
resolution_status: status,
|
||||
}),
|
||||
evidence: expect.objectContaining({
|
||||
...(specifier === "legacy/feature"
|
||||
? {
|
||||
limitations: expect.arrayContaining([
|
||||
"The selected package ESM subpath target node_modules/legacy/feature does not exist in the inventoried artifact container; ESM subpath targets are exact files, so no extension or directory index was tried.",
|
||||
]),
|
||||
}
|
||||
: {}),
|
||||
artifact: expect.objectContaining({
|
||||
sha256: createHash("sha256").update(source).digest("hex"),
|
||||
}),
|
||||
location: expect.objectContaining({
|
||||
value: expect.objectContaining({ source: "main.mjs" }),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -7,9 +7,9 @@ import { expect } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
|
||||
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
|
||||
|
||||
const createPackageSubpathFixture = async () => {
|
||||
const root = await createTestTempDirectory("rea-package-subpaths-");
|
||||
@@ -124,26 +124,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
).replaceAll("\\", "/"),
|
||||
).toBe("node_modules/fi%xture/actual.cjs");
|
||||
|
||||
let document: unknown;
|
||||
if (surface === "CLI") {
|
||||
const response = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--format", "json"],
|
||||
environment: { REA_LOG_LEVEL: "silent" },
|
||||
});
|
||||
expect(response.exitCode, response.stderr).toBe(0);
|
||||
document = response.json;
|
||||
} else {
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
const response = await call("analyze_javascript_application", {
|
||||
input_path: root,
|
||||
format: "directory",
|
||||
});
|
||||
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
||||
document = response.structuredContent;
|
||||
}
|
||||
const { graph } = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(document).normalized_result;
|
||||
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
|
||||
const imports = graph.edges.filter(
|
||||
({ properties }) => properties.module_link_kind === "require",
|
||||
);
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { expect } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
|
||||
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
"recovers retained prefixed source-map originals and byte identity through %s",
|
||||
async (surface, { cli }) => {
|
||||
const root = await createTestTempDirectory("rea-retained-map-prefix-");
|
||||
const original = "export const answer: number = 42;";
|
||||
const mapText = `\uFEFF)]}'retained response\r\n${JSON.stringify({
|
||||
version: 3,
|
||||
sources: ["original.ts"],
|
||||
sourcesContent: [original],
|
||||
names: [],
|
||||
mappings: "AAAA",
|
||||
})}`;
|
||||
const mapPath = join(root, "bundle.js.map");
|
||||
await writeFile(
|
||||
join(root, "bundle.js"),
|
||||
"export const answer = 42;\n//# sourceMappingURL=bundle.js.map\n",
|
||||
);
|
||||
await writeFile(mapPath, mapText);
|
||||
|
||||
let document: unknown;
|
||||
if (surface === "CLI") {
|
||||
const response = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--format", "json"],
|
||||
environment: { REA_LOG_LEVEL: "silent" },
|
||||
});
|
||||
expect(response.exitCode, response.stderr).toBe(0);
|
||||
document = response.json;
|
||||
} else {
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
const response = await call("analyze_javascript_application", {
|
||||
input_path: root,
|
||||
format: "directory",
|
||||
});
|
||||
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
||||
document = response.structuredContent;
|
||||
}
|
||||
const { graph } = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(document).normalized_result;
|
||||
const mapDigest = createHash("sha256").update(mapText).digest("hex");
|
||||
expect(graph.nodes).toContainEqual(
|
||||
expect.objectContaining({
|
||||
kind: "source-module",
|
||||
identity: expect.objectContaining({
|
||||
strategy: "source-map-original",
|
||||
source_map_sha256: mapDigest,
|
||||
original_source: "original.ts",
|
||||
source_sha256: createHash("sha256").update(original).digest("hex"),
|
||||
}),
|
||||
observations: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
source_map_reference: expect.objectContaining({
|
||||
source_name: "original.ts",
|
||||
map_path: "bundle.js.map",
|
||||
}),
|
||||
properties: { content_available: true },
|
||||
evidence: expect.objectContaining({
|
||||
artifact: expect.objectContaining({ sha256: mapDigest }),
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
);
|
||||
expect(graph.coverage.status).toBe("complete");
|
||||
expect(await readFile(mapPath, "utf8")).toBe(mapText);
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,34 @@
|
||||
import { expect } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
|
||||
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
|
||||
import type { TestCli } from "./cliFixture.js";
|
||||
|
||||
/** Analyze a directory through a public adapter and validate its result contract. */
|
||||
export const analyzeJavaScriptDirectory = async (
|
||||
surface: "CLI" | "stdio MCP",
|
||||
cli: TestCli,
|
||||
root: string,
|
||||
) => {
|
||||
let document: unknown;
|
||||
if (surface === "CLI") {
|
||||
const response = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--format", "json"],
|
||||
environment: { REA_LOG_LEVEL: "silent" },
|
||||
});
|
||||
expect(response.exitCode, response.stderr).toBe(0);
|
||||
document = response.json;
|
||||
} else {
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
const response = await call("analyze_javascript_application", {
|
||||
input_path: root,
|
||||
format: "directory",
|
||||
});
|
||||
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
||||
document = response.structuredContent;
|
||||
}
|
||||
return z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(document).normalized_result;
|
||||
};
|
||||
Reference in New Issue
Block a user