68 lines
3.5 KiB
JavaScript
68 lines
3.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import crypto from "node:crypto";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { pathToFileURL } from "node:url";
|
|
import test from "node:test";
|
|
import ts from "typescript";
|
|
|
|
const root = path.resolve(import.meta.dirname, "../../..");
|
|
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "library-path-normalization-unit-"));
|
|
for (const name of ["asset-path.ts", "library-source-origin.ts"]) {
|
|
const sourcePath = path.join(root, "web/protocol", name);
|
|
const transpiled = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), {
|
|
compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 },
|
|
fileName: sourcePath,
|
|
reportDiagnostics: true,
|
|
});
|
|
assert.deepEqual(transpiled.diagnostics, []);
|
|
fs.writeFileSync(path.join(temporary, name.replace(".ts", ".mjs")), transpiled.outputText.replaceAll('from "./asset-path"', 'from "./asset-path.mjs"'));
|
|
}
|
|
const paths = await import(pathToFileURL(path.join(temporary, "asset-path.mjs")));
|
|
const origin = await import(pathToFileURL(path.join(temporary, "library-source-origin.mjs")));
|
|
const manifest = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-04B/manifest.json"), "utf8"));
|
|
const sha256 = (file) => crypto.createHash("sha256").update(fs.readFileSync(path.join(root, file))).digest("hex");
|
|
const policy = { schemaVersion: 1, declaredHttpsOrigins: ["https://assets.example.test"] };
|
|
|
|
test("M12-04B binds the shared path normalizer and evidence artifacts", () => {
|
|
assert.equal(manifest.task, "M12-04B");
|
|
assert.equal(manifest.nextTask, "M12-04C");
|
|
for (const artifact of Object.values(manifest.artifacts)) assert.equal(sha256(artifact.path), artifact.sha256, artifact.path);
|
|
});
|
|
|
|
test("M12-04B canonicalizes POSIX and Windows separators with dot segments", () => {
|
|
const aliases = [
|
|
"libraries/characters/main.blend",
|
|
"libraries\\characters\\.\\hero\\..\\main.blend",
|
|
"libraries//characters/models/../main.blend",
|
|
"//libraries/characters/./main.blend",
|
|
];
|
|
for (const alias of aliases) assert.equal(paths.normalizeProjectAssetPath(alias), "libraries/characters/main.blend");
|
|
assert.equal(paths.normalizeProjectAssetPath(paths.normalizeProjectAssetPath(aliases[1])), "libraries/characters/main.blend");
|
|
});
|
|
|
|
test("M12-04B decodes percent octets and NFC-normalizes Unicode names", () => {
|
|
const canonical = "libraries/角色/caf\u00e9.blend";
|
|
const aliases = [
|
|
"libraries/%E8%A7%92%E8%89%B2/caf%65%CC%81.blend",
|
|
"libraries%2F%E8%A7%92%E8%89%B2%5Ccafe%CC%81.blend",
|
|
"libraries/角色/cafe\u0301.blend",
|
|
];
|
|
for (const alias of aliases) assert.equal(paths.normalizeProjectAssetPath(alias), canonical);
|
|
assert.deepEqual(origin.acceptLibrarySource(policy, { schemaVersion: 1, kind: "PROJECT_ASSET", path: aliases[1] }), {
|
|
status: "READY",
|
|
kind: "PROJECT_ASSET",
|
|
canonicalLocator: `project-assets/${canonical}`,
|
|
});
|
|
});
|
|
|
|
test("M12-04B resolves encoded dot segments once and fails closed on escape or re-decoding", () => {
|
|
assert.equal(paths.normalizeProjectAssetPath("libraries/temp/%2E%2E/main.blend"), "libraries/main.blend");
|
|
assert.throws(() => paths.normalizeProjectAssetPath("libraries/%2E%2E/%2E%2E/outside.blend"), /ASSET_PATH_OUTSIDE_PROJECT/);
|
|
assert.throws(() => paths.normalizeProjectAssetPath("libraries/%252E%252E/outside.blend"), /ASSET_PATH_OUTSIDE_PROJECT/);
|
|
assert.throws(() => paths.normalizeProjectAssetPath("libraries/%GG/outside.blend"), /ASSET_PATH_INVALID/);
|
|
});
|
|
|
|
test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));
|