2026-10-05 07:28:51 -07:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
|
2026-10-07 17:57:33 +08:00
|
|
|
import type { JavaScriptArtifactFile } from "../../domain/javascript/javascriptArtifactFiles.js";
|
2026-10-05 07:28:51 -07:00
|
|
|
import { resolveArtifactPathByContext } from "./JavaScriptArtifactPathResolution.js";
|
|
|
|
|
|
|
|
|
|
const filesFor = (paths: readonly string[]) =>
|
|
|
|
|
new Map<string, JavaScriptArtifactFile>(
|
|
|
|
|
["app/main.js", ...paths].map((path) => [
|
|
|
|
|
path,
|
|
|
|
|
{
|
|
|
|
|
path,
|
|
|
|
|
container_sha256: "a".repeat(64),
|
|
|
|
|
sha256: "b".repeat(64),
|
|
|
|
|
bytes: 0,
|
|
|
|
|
inventory_artifact_id: `artifact-${path}`,
|
|
|
|
|
kind: "javascript",
|
|
|
|
|
unpacked: false,
|
|
|
|
|
text: { included: true, value: "" },
|
|
|
|
|
},
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
describe("module specifier punctuation", () => {
|
2026-10-08 03:57:54 +08:00
|
|
|
it.each(["#", "?"])(
|
|
|
|
|
"keeps CommonJS punctuation and strips URL suffixes for ESM (%s)",
|
|
|
|
|
(punctuation) => {
|
2026-10-05 07:28:51 -07:00
|
|
|
const literal = `app/dep.cjs${punctuation}literal.cjs`;
|
|
|
|
|
const stripped = "app/dep.cjs";
|
2026-10-08 03:57:54 +08:00
|
|
|
const commonJs = resolveArtifactPathByContext({
|
|
|
|
|
declaredPath: `./dep.cjs${punctuation}literal.cjs`,
|
|
|
|
|
sourcePath: "app/main.js",
|
|
|
|
|
context: "module-specifier",
|
|
|
|
|
files: filesFor([literal, stripped]),
|
|
|
|
|
moduleKind: "require",
|
|
|
|
|
});
|
|
|
|
|
expect(commonJs).toMatchObject({
|
|
|
|
|
resolution_status: "resolved",
|
|
|
|
|
resolved_path: literal,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const esm = resolveArtifactPathByContext({
|
|
|
|
|
declaredPath: `./dep.cjs${punctuation}literal.cjs`,
|
|
|
|
|
sourcePath: "app/main.js",
|
|
|
|
|
context: "module-specifier",
|
|
|
|
|
files: filesFor([literal, stripped]),
|
|
|
|
|
moduleKind: "import",
|
|
|
|
|
});
|
|
|
|
|
expect(esm).toMatchObject({
|
|
|
|
|
resolution_status: "resolved",
|
|
|
|
|
resolved_path: stripped,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const noStrippedTarget = resolveArtifactPathByContext({
|
2026-10-05 07:28:51 -07:00
|
|
|
declaredPath: `./dep.cjs${punctuation}literal.cjs`,
|
|
|
|
|
sourcePath: "app/main.js",
|
|
|
|
|
context: "module-specifier",
|
2026-10-08 03:57:54 +08:00
|
|
|
files: filesFor([literal]),
|
|
|
|
|
moduleKind: "import",
|
2026-10-05 07:28:51 -07:00
|
|
|
});
|
2026-10-08 03:57:54 +08:00
|
|
|
expect(noStrippedTarget).toMatchObject({
|
|
|
|
|
resolution_status: "not-found",
|
|
|
|
|
resolved_path: null,
|
2026-10-05 07:28:51 -07:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it.each(["#", "?"])(
|
|
|
|
|
"keeps HTML URL suffix handling for %s",
|
|
|
|
|
(punctuation) => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveArtifactPathByContext({
|
|
|
|
|
declaredPath: `./dep.cjs${punctuation}literal.cjs`,
|
|
|
|
|
sourcePath: "app/main.js",
|
|
|
|
|
context: "html-reference",
|
|
|
|
|
files: filesFor([
|
|
|
|
|
"app/dep.cjs",
|
|
|
|
|
`app/dep.cjs${punctuation}literal.cjs`,
|
|
|
|
|
]),
|
|
|
|
|
}),
|
|
|
|
|
).toMatchObject({
|
|
|
|
|
resolution_status: "resolved",
|
|
|
|
|
resolved_path: "app/dep.cjs",
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|