Files
frontend/Client/tests/assembly-catalog.test.mjs

122 lines
3.4 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import {
componentEntries,
componentBoms,
bomColumns,
bomCell,
bomCsv,
} from "../src/workspace/assembly-catalog.ts";
import fixture from "./demand-fixtures.json" with { type: "json" };
const row = {
partName: "H-DOF0-R-C",
position: "头",
dof: 0,
material: "POM",
function: "摄像头",
shape: "圆形",
size: "—",
torque: "—",
};
const part = (id, bomList) => ({
instanceId: id,
component: {
componentCode: "CW-" + id,
componentName: id,
category: "HEAD_0",
bomList,
},
});
test("library lists returned components once, not their internal URDF links or mesh files", () => {
const parts = [part("torso", []), part("head", [])];
const model = {
root: "torso/BASE",
links: Object.fromEntries(
[
"torso/BASE",
"torso/LINK_head",
"torso/LINK_ARM_R",
"head/base_link",
"head/Link-head",
].map((key) => [key, {}]),
),
joints: [
{ parent: "torso/BASE", child: "torso/LINK_head" },
{ parent: "torso/BASE", child: "torso/LINK_ARM_R" },
{ parent: "torso/BASE", child: "head/base_link" },
{ parent: "head/base_link", child: "head/Link-head" },
],
};
const entries = componentEntries(parts, model);
assert.equal(entries.length, 2);
assert.deepEqual(
entries.map((entry) => [entry.node, entry.nodeCount]),
[
["torso/BASE", 3],
["head/base_link", 2],
],
);
assert.equal(
componentEntries(parts, { root: "", links: {}, joints: [] }).length,
2,
);
});
test("BOM preserves Excel fields, zero DOF and repeated component instances", () => {
const parts = [part("head-1", [row]), part("head-2", [row])];
const sections = componentBoms(parts);
assert.deepEqual(
sections.map((section) => section.rows.length),
[1, 1],
);
assert.equal(sections[0].rows[0].dof, 0);
assert.equal(bomCell(0), "0");
assert.deepEqual(bomColumns(sections), [
"partName",
"position",
"dof",
"material",
"function",
"shape",
"size",
"torque",
]);
assert.ok(!bomCsv(sections).includes("Mesh"));
assert.equal(bomCsv(sections).split("\r\n").length, 3);
assert.ok(bomCsv(sections).includes('"0"'));
});
test("missing, empty and non-tabular BOM remain distinguishable without fabricated rows", () => {
const sections = componentBoms([
part("missing"),
part("null", null),
part("empty", []),
part("object", { parts: [row] }),
part("string", "bad-json"),
]);
assert.deepEqual(
sections.map((s) => s.status),
["missing", "empty", "empty", "other", "other"],
);
assert.equal(sections.flatMap((s) => s.rows).length, 0);
assert.deepEqual(sections[3].value, { parts: [row] });
});
test("CSV quotes commas, line breaks, quotes and preserves additional returned fields", () => {
const sections = componentBoms([
part("head", [{ ...row, partName: 'a,"b"\nc', extra: { quantity: 2 } }]),
]);
const csv = bomCsv(sections);
assert.ok(csv.includes('"a,""b""\nc"'));
assert.ok(csv.includes('"{""quantity"":2}"'));
assert.ok(csv.includes('"extra"'));
});
test("matching API preserves the uploaded Excel bomList rather than replacing it with model nodes", async () => {
const match = structuredClone(fixture.match);
match.components[0].component.bomList = [row];
assert.deepEqual(componentBoms(match.components)[0].rows, [row]);
});