Files
frontend/Client/tests/automatic-assembly.test.mjs
2026-10-15 10:35:55 +08:00

337 lines
10 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { Matrix4, Euler } from "three";
import { modelResourceUrl } from "../src/scene/assembly-plan.ts";
import {
resolveAssembly,
interfaceCategory,
componentCategory,
componentPorts,
} from "../src/scene/automatic-assembly.ts";
const identity = () => new Matrix4();
const torsoFrame = new Matrix4().makeRotationFromEuler(
new Euler(-Math.PI / 2, -Math.PI / 2, 0, "ZYX"),
);
torsoFrame.setPosition(0, 0.0434269852498788, 0);
function part(id, category, ports, matrices = {}) {
return {
item: {
instanceId: id,
component: {
componentName: id,
category,
urdfInfo: { rootLink: "ROOT" },
},
},
rootLink: "ROOT",
ports: new Map(Object.entries(ports)),
frames: new Map(Object.entries({ ROOT: identity(), ...matrices })),
};
}
const parts = () => [
part(
"torso",
"torso_0",
{ LINK_HEAD_0: "head_0" },
{ LINK_HEAD_0: torsoFrame },
),
part(
"head",
"head_0",
{ LINK_HEAD_0: "head_0" },
{ LINK_HEAD_0: identity() },
),
];
const blocked = () => ({
status: "blocked",
rootInstanceId: "torso",
matrixLayout: "column-major",
connections: [],
issues: [{ code: "MISSING_MOUNTING_REFERENCE" }],
});
const equalMatrix = (a, b) =>
a.elements.forEach((value, index) =>
assert.ok(Math.abs(value - b.elements[index]) < 1e-10),
);
test("actual torso and head port coordinates assemble despite backend completeness blockers", () => {
const result = resolveAssembly(parts(), blocked());
assert.equal(result.world.size, 2);
assert.equal(result.connections.length, 1);
assert.deepEqual(result.warnings, []);
equalMatrix(result.world.get("head"), torsoFrame);
});
test("backend matrices do not override the file-defined interface alignment", () => {
const assembly = blocked();
assembly.connections = [
{
parentInstanceId: "torso",
childInstanceId: "head",
parentLink: "LINK_HEAD_0",
childFrame: "LINK_HEAD_0",
childRootRelativeToParentRoot: new Matrix4()
.makeTranslation(0.2, 0.3, 0.4)
.toArray(),
},
];
equalMatrix(resolveAssembly(parts(), assembly).world.get("head"), torsoFrame);
});
test("explicit coincident convention preserves orientation and cancels child frame offset", () => {
const input = parts();
input[1].item.component.urdfInfo.assembly = {
mountingFrame: "LINK_HEAD_0",
pairingConvention: "coincident_frames",
};
const childFrame = new Matrix4()
.makeRotationZ(0.7)
.setPosition(0.03, 0.02, 0.01);
input[1].frames.set("LINK_HEAD_0", childFrame);
const result = resolveAssembly(input, blocked());
equalMatrix(
result.world.get("head").clone().multiply(childFrame),
torsoFrame,
);
});
test("chain resolves by topology, unrelated parts remain available for separate display", () => {
const input = [
part(
"torso",
"torso_0",
{ LINK_ARM_R: "arm_r" },
{ LINK_ARM_R: new Matrix4().makeTranslation(0.1, 0, 0) },
),
part(
"hand",
"hand_r",
{ LINK_HAND_R: "hand_r" },
{ LINK_HAND_R: identity() },
),
part(
"arm",
"arm_r",
{ LINK_ARM_R: "arm_r", LINK_HAND_R: "hand_r" },
{
LINK_ARM_R: identity(),
LINK_HAND_R: new Matrix4().makeTranslation(0.2, 0, 0),
},
),
part("tail", "tail_0", {}),
];
const result = resolveAssembly(input, blocked());
assert.equal(result.connections.length, 2);
assert.ok(Math.abs(result.world.get("hand").elements[12] - 0.3) < 1e-10);
assert.equal(result.world.has("tail"), false);
assert.match(result.warnings[0], /tail.*单独展示/);
});
test("file-defined inverted orientation is preserved without a corrective flip", () => {
const input = parts();
input[1].item.component.urdfInfo.assembly = {
pairingConvention: "opposed_frames",
};
input[1].frames.set("LINK_HEAD_0", new Matrix4().makeRotationX(Math.PI));
const before = [...input[1].frames.get("LINK_HEAD_0").elements];
const result = resolveAssembly(input, blocked());
equalMatrix(
result.world
.get("head")
.clone()
.multiply(input[1].frames.get("LINK_HEAD_0")),
torsoFrame,
);
assert.deepEqual(input[1].frames.get("LINK_HEAD_0").elements, before);
});
test("a component without a mounting port is shown separately instead of using its root by guess", () => {
const input = parts();
input[1].ports.clear();
const result = resolveAssembly(input, blocked());
assert.equal(result.world.has("head"), false);
assert.match(result.warnings[0], /子安装节点.*单独展示/);
});
test("backend scale or reflection matrices are ignored and cannot distort a component", () => {
for (const placement of [
new Matrix4().makeScale(2, 1, 1),
new Matrix4().makeScale(-1, 1, 1),
]) {
const assembly = blocked();
assembly.connections = [
{
parentInstanceId: "torso",
childInstanceId: "head",
childRootRelativeToParentRoot: placement.toArray(),
},
];
const result = resolveAssembly(parts(), assembly);
equalMatrix(result.world.get("head"), torsoFrame);
assert.equal(result.connections.length, 1);
assert.deepEqual(result.warnings, []);
}
});
test("single component does not require a torso or any confirmation", () => {
const result = resolveAssembly([parts()[1]], blocked());
assert.equal(result.world.size, 1);
assert.deepEqual(result.warnings, []);
});
test("ambiguous parent ports do not silently choose a connection", () => {
const input = parts();
input[0].ports.set("second-head", "head_0");
input[0].frames.set("second-head", identity());
const result = resolveAssembly(input, blocked());
assert.equal(result.world.size, 1);
assert.match(result.warnings[0], /多个同类/);
});
test("all twelve current port names classify and LEG never matches FOOT", () => {
for (const [type, sides] of [
["TORSO", ["0"]],
["HEAD", ["0"]],
["WAIST", ["0"]],
["ARM", ["L", "R"]],
["HAND", ["L", "R"]],
["LEG", ["L", "R"]],
["FOOT", ["L", "R"]],
["TAIL", ["0"]],
])
for (const side of sides)
assert.equal(
interfaceCategory(`LINK_${type}_${side}`),
`${type}_${side}`.toLowerCase(),
);
for (const invalid of [
"LINK_head",
"Link-head",
"LINK_PEDAL_L",
"LINK_ARM_0",
"LINK_HEAD_L",
"base_link",
])
assert.equal(interfaceCategory(invalid), undefined);
});
test("current main filename supplies classification even if metadata is stale", () => {
const item = {
urdfMainPath: "urdf/" + encodeURIComponent("足_L_型号.urdf"),
component: { category: "pedal_l" },
};
assert.equal(componentCategory(item), "leg_l");
});
test("left and right legs and feet align only their own matching sockets", () => {
const torso = part(
"body",
"torso_0",
{ LINK_LEG_L: "leg_l", LINK_LEG_R: "leg_r" },
{
LINK_LEG_L: new Matrix4().makeTranslation(0, 0.1, 0),
LINK_LEG_R: new Matrix4().makeTranslation(0, -0.1, 0),
},
);
const input = [torso];
for (const side of ["L", "R"]) {
const own = `LINK_LEG_${side}`,
footPort = `LINK_FOOT_${side}`;
input.push(
part(
`leg_${side}`,
`leg_${side.toLowerCase()}`,
{
[own]: `leg_${side.toLowerCase()}`,
[footPort]: `foot_${side.toLowerCase()}`,
},
{
[own]: new Matrix4().makeRotationX(0.3).setPosition(0.02, 0, 0.01),
[footPort]: new Matrix4().makeTranslation(0, 0, -0.2),
},
),
);
input.push(
part(
`foot_${side}`,
`foot_${side.toLowerCase()}`,
{ [footPort]: `foot_${side.toLowerCase()}` },
{ [footPort]: identity() },
),
);
}
const result = resolveAssembly(input, blocked());
assert.equal(result.connections.length, 4);
assert.deepEqual(result.warnings, []);
for (const edge of result.connections) {
const parent = input.find((p) => p.item.instanceId === edge.parent),
child = input.find((p) => p.item.instanceId === edge.child);
equalMatrix(
result.world
.get(edge.parent)
.clone()
.multiply(parent.frames.get(edge.parentFrame)),
result.world
.get(edge.child)
.clone()
.multiply(child.frames.get(edge.childFrame)),
);
}
});
test("returned model assets are loaded directly from the bucket", () => {
assert.equal(
modelResourceUrl(
"https://dev-cadworld-admin.robotquan.com/admin-api/component-assets/head/%E5%A4%B4.STL?v=2",
"http://127.0.0.1:8091",
),
"https://linkerbot.oss-cn-beijing.aliyuncs.com/head/%E5%A4%B4.STL?v=2",
);
assert.equal(
modelResourceUrl(
"https://assets.example/head.stl",
"http://127.0.0.1:8091",
),
"https://assets.example/head.stl",
);
assert.throws(
() => modelResourceUrl("javascript:alert(1)", "http://127.0.0.1:8091"),
/无效/,
);
});
test("existing returned head sockets use explicit metadata without renaming or rotating file frames", () => {
const input = [
part("body", "torso_0", {}, { LINK_head: torsoFrame }),
part("head", "head_0", {}, { "Link-head": identity() }),
];
input[0].ports = componentPorts(
["BASE", "LINK_head"],
[{ child: "LINK_head", interfaceType: "head_0" }],
);
input[1].ports = componentPorts(
["base_link", "Link-head"],
[{ child: "Link-head", interfaceType: "head_0" }],
);
const result = resolveAssembly(input, blocked());
assert.equal(result.connections.length, 1);
equalMatrix(result.world.get("head"), torsoFrame);
assert.equal(result.connections[0].parentFrame, "LINK_head");
assert.equal(result.connections[0].childFrame, "Link-head");
assert.equal(
componentPorts(
["LINK_HEAD_0"],
[{ child: "LINK_HEAD_0", interfaceType: "foot_l" }],
).get("LINK_HEAD_0"),
"head_0",
);
assert.equal(
componentPorts(["BASE"], [{ child: "missing", interfaceType: "head_0" }])
.size,
0,
);
});
test("stale node names cannot block a unique pair of actual URDF sockets", () => {
const assembly = blocked();
assembly.connections = [
{
parentInstanceId: "torso",
childInstanceId: "head",
parentFrame: "old-parent",
childFrame: "old-child",
},
];
const input = parts();
input[1].item.component.urdfInfo.assembly = { mountingFrame: "old-root" };
const result = resolveAssembly(input, assembly);
assert.equal(result.connections.length, 1);
equalMatrix(result.world.get("head"), torsoFrame);
});