64 lines
3.6 KiB
JavaScript
64 lines
3.6 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(), "glb-desktop-import-unit-"));
|
|
const sourcePath = path.join(root, "web/protocol/glb-import.ts");
|
|
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, []);
|
|
const modulePath = path.join(temporary, "glb-import.mjs");
|
|
fs.writeFileSync(modulePath, transpiled.outputText);
|
|
const protocol = await import(pathToFileURL(modulePath));
|
|
const report = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-06A/desktop-fixtures.json"), "utf8"));
|
|
const fixtureRoot = path.join(root, "tests/files/web/m12_glb_desktop_v1");
|
|
const arrayBuffer = (bytes) => bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
|
|
test("M12-06B imports every desktop fixture with exact Web semantics", () => {
|
|
assert.deepEqual(report.fixtures.map((fixture) => fixture.id), ["mesh", "pbr", "uv", "skin", "animation"]);
|
|
for (const fixture of report.fixtures) {
|
|
const bytes = fs.readFileSync(path.join(fixtureRoot, fixture.file));
|
|
assert.equal(crypto.createHash("sha256").update(bytes).digest("hex"), fixture.sha256);
|
|
const imported = protocol.importGLBDesktopFixtureSemantics(arrayBuffer(bytes));
|
|
assert.deepEqual(protocol.compareGLBDesktopFixtureSemantics(fixture.semantic, imported), { compatible: true, mismatches: [] }, fixture.id);
|
|
}
|
|
});
|
|
|
|
test("M12-06B exposes topology, attributes, materials, nodes and animations separately", () => {
|
|
const imported = Object.fromEntries(report.fixtures.map((fixture) => {
|
|
const bytes = fs.readFileSync(path.join(fixtureRoot, fixture.file));
|
|
return [fixture.id, protocol.importGLBDesktopFixtureSemantics(arrayBuffer(bytes))];
|
|
}));
|
|
assert.deepEqual(Object.keys(imported.mesh.meshes[0].primitives[0].attributes), ["COLOR_0", "NORMAL", "POSITION"]);
|
|
assert.equal(imported.mesh.meshes[0].primitives[0].indices.count, 6);
|
|
assert.equal(Number(imported.pbr.materials[0].pbr.metallicFactor.toFixed(3)), 0.72);
|
|
assert.ok(imported.uv.meshes[0].primitives[0].attributes.TEXCOORD_0);
|
|
assert.equal(imported.uv.materials[0].pbr.baseColorTexture.index, 0);
|
|
assert.deepEqual(imported.skin.nodeNames, ["Tip", "Root", "M12 Skin Fixture", "M12 Skin Armature"]);
|
|
assert.equal(imported.skin.skins[0].inverseBindMatrices.type, "MAT4");
|
|
assert.deepEqual(imported.animation.animations[0].channels.map((channel) => channel.target.path), ["translation", "rotation"]);
|
|
assert.equal(imported.animation.animations[0].samplers[0].input.count, 25);
|
|
});
|
|
|
|
test("M12-06B reports a field-level semantic mismatch", () => {
|
|
const fixture = report.fixtures.find((candidate) => candidate.id === "pbr");
|
|
const bytes = fs.readFileSync(path.join(fixtureRoot, fixture.file));
|
|
const imported = protocol.importGLBDesktopFixtureSemantics(arrayBuffer(bytes));
|
|
const expected = structuredClone(fixture.semantic);
|
|
expected.materials[0].pbr.metallicFactor = 0.5;
|
|
const comparison = protocol.compareGLBDesktopFixtureSemantics(expected, imported);
|
|
assert.equal(comparison.compatible, false);
|
|
assert.deepEqual(comparison.mismatches, ["$.materials[0].pbr.metallicFactor: expected 0.5 got 0.7200000286102295"]);
|
|
});
|
|
|
|
test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));
|