import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import test from "node:test"; import ts from "typescript"; const repoRoot = path.resolve(import.meta.dirname, "../../.."); const sourcePath = path.join(repoRoot, "web/app/src/three-adapter/mesh-element-transform.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 moduleUrl = "data:text/javascript;base64," + Buffer.from(transpiled.outputText).toString("base64"); const { selectionIslands } = await import(moduleUrl); /* Two disconnected unit cubes centred at (-2,0,0) and (+2,0,0). */ function twoCubeGeometry() { const positions = []; for (const cx of [-2, 2]) { for (let index = 0; index < 8; index += 1) { positions.push(cx + ((index & 1) ? 0.5 : -0.5), (index & 2) ? 0.5 : -0.5, (index & 4) ? 0.5 : -0.5); } } const edges = []; for (const offset of [0, 8]) { for (let index = 0; index < 8; index += 1) { for (const bit of [1, 2, 4]) { if ((index & bit) === 0) edges.push(offset + index, offset + (index | bit)); } } } return { positions, edgeVertexIndices: edges }; } test("two disconnected cubes split into two islands with their own centres", () => { const geometry = twoCubeGeometry(); const islands = selectionIslands([...Array(16).keys()], geometry); assert.equal(islands.length, 2); assert.deepEqual(islands.map((island) => island.vertexIndices.length).sort(), [8, 8]); const centres = islands.map((island) => island.center[0]).sort((a, b) => a - b); assert.ok(Math.abs(centres[0] + 2) < 1e-6, `first centre ${centres[0]}`); assert.ok(Math.abs(centres[1] - 2) < 1e-6, `second centre ${centres[1]}`); }); test("a single-cube selection is one island centred on that cube", () => { const geometry = twoCubeGeometry(); const islands = selectionIslands([...Array(8).keys()], geometry); assert.equal(islands.length, 1); assert.equal(islands[0].vertexIndices.length, 8); assert.ok(Math.abs(islands[0].center[0] + 2) < 1e-6, `centre ${islands[0].center[0]}`); }); test("a partial island keeps only the selected vertices", () => { const geometry = twoCubeGeometry(); const islands = selectionIslands([0, 1, 2], geometry); assert.equal(islands.length, 1); assert.deepEqual(islands[0].vertexIndices, [0, 1, 2]); });