Files
workinf_Blender_Wasm/web/tests/unit/mesh-element-islands.test.mjs
mes123456 c4be807726
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled
test(web): lock edit-mode island detection for INDIVIDUAL_ORIGINS
the INDIVIDUAL_ORIGINS modal and commit rely on selectionIslands splitting the
selected vertices into connected components. cover it with node:test against
the transpiled module: two disconnected cubes produce two islands with their
own centres, a single-cube selection is one island, and a partial selection
keeps only the selected vertices.

driving the transform modal keys in a headed probe did not work (canvas focus
and menu state interference), so the per-island rotation headed case remains
open and is recorded in the inventory.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-12-06 16:20:43 +08:00

61 lines
2.5 KiB
JavaScript

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]);
});