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/viewport-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 { axisLabel, beginTransform, cancelTransform, commitTransform, composeLocalMatrix, formatValue, inputNumeric, pointerDelta, pressAxis, quatFromEuler, quatToEuler, } = await import(moduleUrl); const transform = () => ({ translation: [1, 2, 3], rotationEuler: [0, 0, 0], scale: [1, 1, 1], rotationMode: 1, }); /* The modal always works on a target list; the single-object wrapper keeps the * one-object tests readable. */ const start = (tool, value = transform(), context = {}) => beginTransform(tool, [{ objectId: "object:A", transform: value }], context); const current = (state) => state.targets[0].current; const basis = () => ({ axes: [[1, 0, 0], [0, 1, 0], [0, 0, 1]], right: [1, 0, 0], up: [0, 0, 1], forward: [0, 1, 0], worldPerPixel: 0.01, }); const typeKeys = (state, keys) => keys.reduce((currentState, key) => inputNumeric(currentState, key), state); /* `+ 0` folds trigenomic signed zero (-0 vs 0) without weakening the check. */ const round3 = (value) => value.map((entry) => Number(entry.toFixed(6)) + 0); test("axis key starts global and repeats toggle local space", () => { const started = start("translate"); const global = pressAxis(started, 0); assert.equal(global.axis, 0); assert.equal(global.space, "global"); assert.equal(axisLabel(global), "X (全局)"); const local = pressAxis(global, 0); assert.equal(local.space, "local"); assert.equal(axisLabel(local), "X (局部)"); const other = pressAxis(local, 1); assert.equal(other.axis, 1); assert.equal(other.space, "global"); assert.equal(other.numeric, ""); }); test("numeric translation moves exactly along the constrained axis", () => { const state = typeKeys(pressAxis(start("translate"), 0), ["1", ".", "5"]); assert.equal(state.value, 1.5); assert.deepEqual(round3(current(state).translation), [2.5, 2, 3]); assert.equal(formatValue(state), "1.500"); assert.equal(inputNumeric(state, "-"), null); assert.equal(inputNumeric(state, "Backspace").numeric, "1."); assert.deepEqual(round3(current(inputNumeric(state, "Backspace")).translation), [2, 2, 3]); }); test("numeric entry without an axis defaults to X like Blender", () => { const state = typeKeys(start("translate"), ["2"]); assert.equal(current(state).translation[0], 3); assert.equal(current(state).translation[1], 2); }); test("pointer translation projects screen motion onto the axis", () => { const state = pointerDelta(pressAxis(start("translate"), 0), 100, 0, basis()); assert.equal(Number(state.value.toFixed(6)), 1); assert.deepEqual(round3(current(state).translation), [2, 2, 3]); const free = pointerDelta(start("translate"), 50, -20, basis()); assert.deepEqual(round3(current(free).translation), [1.5, 2, 3.2]); }); test("rotated object local axis translation follows the object orientation", () => { const rotated = { ...transform(), rotationEuler: [0, 0, Math.PI / 2] }; const localBasis = basis(); localBasis.axes = [[0, 1, 0], [-1, 0, 0], [0, 0, 1]]; let state = pressAxis(start("translate", rotated), 0); state = pressAxis(state, 0); assert.equal(state.space, "local"); state = typeKeys(state, ["4"]); assert.deepEqual(round3(current(state).translation), [1, 6, 3]); }); test("rotation numeric ninety degrees produces a quarter turn", () => { const state = typeKeys(pressAxis(start("rotate"), 0), ["9", "0"]); const euler = current(state).rotationEuler.map((value) => Number(((value * 180) / Math.PI).toFixed(4))); assert.deepEqual(euler, [90, 0, 0]); assert.equal(formatValue(state), "90.0°"); }); test("scale numeric scales one axis or all axes", () => { const single = typeKeys(pressAxis(start("scale"), 1), ["2"]); assert.deepEqual(current(single).scale, [1, 2, 1]); assert.equal(formatValue(single), "2.000×"); const uniform = typeKeys(start("scale"), ["3"]); assert.deepEqual(current(uniform).scale, [3, 3, 3]); }); test("cancel restores the initial transform and commit copies the current one", () => { const state = typeKeys(pressAxis(start("translate"), 2), ["7"]); assert.deepEqual(cancelTransform(state)[0].transform.translation, [1, 2, 3]); const committed = commitTransform(state); assert.deepEqual(committed[0].transform.translation, [1, 2, 10]); committed[0].transform.translation[0] = 99; assert.equal(current(state).translation[0], 1); }); test("multi-object MEDIAN rotation orbits every origin around the shared pivot", () => { const state = beginTransform("rotate", [ { objectId: "object:A", transform: transform() }, { objectId: "object:B", transform: { ...transform(), translation: [-1, 2, 3] } }, ], { pivot: [0, 2, 3], pivotKind: "MEDIAN_POINT" }); const rotated = typeKeys(pressAxis(state, 2), ["9", "0"]); /* +90° about Z at (0, 2, 3): (1, 2, 3) → (0, 3, 3), (−1, 2, 3) → (0, 1, 3). */ assert.deepEqual(round3(rotated.targets[0].current.translation), [0, 3, 3]); assert.deepEqual(round3(rotated.targets[1].current.translation), [0, 1, 3]); for (const target of rotated.targets) { assert.deepEqual(round3(target.current.rotationEuler.map((value) => (value * 180) / Math.PI)), [0, 0, 90]); } }); test("multi-object INDIVIDUAL_ORIGINS rotates in place, unlike MEDIAN", () => { const state = beginTransform("rotate", [ { objectId: "object:A", transform: transform() }, { objectId: "object:B", transform: { ...transform(), translation: [-1, 2, 3] } }, ], { pivotKind: "INDIVIDUAL_ORIGINS" }); const rotated = typeKeys(pressAxis(state, 2), ["9", "0"]); assert.deepEqual(round3(rotated.targets[0].current.translation), [1, 2, 3]); assert.deepEqual(round3(rotated.targets[1].current.translation), [-1, 2, 3]); for (const target of rotated.targets) { assert.deepEqual(round3(target.current.rotationEuler.map((value) => (value * 180) / Math.PI)), [0, 0, 90]); } }); test("translation ignores the pivot for every selected object", () => { const state = beginTransform("translate", [ { objectId: "object:A", transform: transform() }, { objectId: "object:B", transform: { ...transform(), translation: [-1, 2, 3] } }, ], { pivot: [0, 2, 3], pivotKind: "MEDIAN_POINT" }); const moved = typeKeys(pressAxis(state, 0), ["2"]); assert.deepEqual(round3(moved.targets[0].current.translation), [3, 2, 3]); assert.deepEqual(round3(moved.targets[1].current.translation), [1, 2, 3]); }); test("local matrix composes translation, euler and scale in Blender order", () => { const identity = composeLocalMatrix(transform()); assert.deepEqual(identity, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1]); const quarter = composeLocalMatrix({ ...transform(), translation: [0, 0, 0], rotationEuler: [0, 0, Math.PI / 2], scale: [2, 3, 4] }); const turn = quarter.map((value) => Number(value.toFixed(6))); assert.deepEqual(turn, [0, 2, 0, 0, -3, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1]); }); test("XYZ euler and quaternion helpers round trip", () => { for (const euler of [[0, 0, 0], [0.3, -0.4, 0.5], [Math.PI / 3, Math.PI / 5, -Math.PI / 7]]) { const round = quatToEuler(quatFromEuler(euler)).map((value) => Number(value.toFixed(6))); assert.deepEqual(round, euler.map((value) => Number(value.toFixed(6)))); } });