50 lines
2.6 KiB
JavaScript
50 lines
2.6 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(), "stl-import-unit-"));
|
|
const sourcePath = path.join(root, "web/protocol/stl-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, "stl-import.mjs");
|
|
fs.writeFileSync(modulePath, transpiled.outputText);
|
|
const protocol = await import(pathToFileURL(modulePath));
|
|
const fixtureRoot = path.join(root, "tests/files/web/m12_stl_edges_v1");
|
|
const bytes = (name) => {
|
|
const value = fs.readFileSync(path.join(fixtureRoot, name));
|
|
return value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength);
|
|
};
|
|
|
|
test("M12-07E parses binary/ASCII normals and explicit unit scales", () => {
|
|
const binary = protocol.importSTL(bytes("capability-binary.stl"), { variant: "STL_BINARY", unitScale: 1 });
|
|
assert.equal(binary.triangleCount, 2);
|
|
assert.deepEqual(binary.normals, [[0, 1, 0], [0, 1, 0]]);
|
|
assert.deepEqual(binary.bounds, { min: [-1, 0, -1], max: [1, 0, 1] });
|
|
const scaled = protocol.importSTL(bytes("capability-binary.stl"), { variant: "STL_BINARY", unitScale: 0.001 });
|
|
assert.deepEqual(scaled.bounds, { min: [-0.001, 0, -0.001], max: [0.001, 0, 0.001] });
|
|
const ascii = protocol.importSTL(bytes("../m12_stl_capability_v1/capability-ascii.stl"), { variant: "STL_ASCII", unitScale: 1 });
|
|
assert.equal(ascii.triangleCount, 2);
|
|
assert.deepEqual(ascii.normals, binary.normals);
|
|
assert.deepEqual(ascii.bounds, binary.bounds);
|
|
});
|
|
|
|
test("M12-07E matches Blender's degenerate removal and blocks trailing bytes", () => {
|
|
const degenerate = protocol.importSTL(bytes("degenerate-binary.stl"), { variant: "STL_BINARY", unitScale: 1 });
|
|
assert.equal(degenerate.declaredTriangleCount, 2);
|
|
assert.equal(degenerate.removedDegenerateTriangles, 1);
|
|
assert.equal(degenerate.triangleCount, 1);
|
|
assert.throws(() => protocol.importSTL(bytes("trailing-binary.stl"), { variant: "STL_BINARY", unitScale: 1 }), /STL_TRAILING_BYTES/);
|
|
assert.throws(() => protocol.importSTL(bytes("capability-binary.stl"), { variant: "STL_BINARY", unitScale: 0 }), /STL_UNIT_SCALE_INVALID/);
|
|
});
|
|
|
|
test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));
|