52 lines
2.7 KiB
JavaScript
52 lines
2.7 KiB
JavaScript
import assert from "node:assert/strict";
|
|
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(), "obj-import-unit-"));
|
|
const sourcePath = path.join(root, "web/protocol/obj-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, "obj-import.mjs");
|
|
fs.writeFileSync(modulePath, transpiled.outputText);
|
|
const protocol = await import(pathToFileURL(modulePath));
|
|
|
|
const obj = new TextEncoder().encode(`# test\nmtllib materials.mtl\no Test\nv 0 0 0\nv 1 0 0\nv 0 1 0\nvt 0 0\nvt 1 0\nvt 0 1\nvn 0 0 1\ng TestGroup\nusemtl TestMaterial\nf -3/-3/-1 -2/-2/-1 -1/-1/-1\n`).buffer;
|
|
const mtl = new TextEncoder().encode("newmtl TestMaterial\nmap_Kd texture.png\n").buffer;
|
|
|
|
test("M12-07C resolves negative indices and serializes deterministic OBJ", () => {
|
|
const imported = protocol.importOBJ(obj, mtl);
|
|
assert.deepEqual(imported.faces[0].vertices.map((vertex) => vertex.position), [1, 2, 3]);
|
|
assert.deepEqual(imported.faces[0].vertices.map((vertex) => vertex.texcoord), [1, 2, 3]);
|
|
assert.deepEqual(imported.materials, [{ name: "TestMaterial", mapKd: "texture.png" }]);
|
|
const serialized = protocol.serializeOBJ(imported);
|
|
assert.match(serialized.obj, /f 1\/1\/1 2\/2\/1 3\/3\/1/);
|
|
assert.equal(serialized.mtl, "# Web Blender MTL export\n# schema 1\nnewmtl TestMaterial\nmap_Kd texture.png\n");
|
|
});
|
|
|
|
test("M12-07C reports unresolved texture origin without blocking geometry", () => {
|
|
const imported = protocol.importOBJ(obj, mtl);
|
|
const missing = protocol.createOBJLossReport(imported);
|
|
assert.equal(missing.canRoundTrip, true);
|
|
assert.deepEqual(missing.warnings.map((warning) => warning.code), ["OBJ_TEXTURE_ORIGIN_UNRESOLVED"]);
|
|
const bound = protocol.createOBJLossReport(imported, ["texture.png"]);
|
|
assert.deepEqual(bound.warnings, []);
|
|
});
|
|
|
|
test("M12-07C rejects malformed face arity and out-of-range indices", () => {
|
|
const malformed = new TextEncoder().encode("v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2\n").buffer;
|
|
assert.throws(() => protocol.importOBJ(malformed), /OBJ_FACE_ARITY_INVALID/);
|
|
const outOfRange = new TextEncoder().encode("v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 4\n").buffer;
|
|
assert.throws(() => protocol.importOBJ(outOfRange), /OBJ_INDEX_OUT_OF_RANGE/);
|
|
});
|
|
|
|
test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));
|