Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.
import { describe, expect, it } from "vitest";
import {
describeBomPayload,
firstOwnAssemblyPart,
flattenBomTree,
missingBomColumnsMessage,
parseBomTree,
withoutProperties
} from "./bom";
const H = [
{ id: "h-item", name: "Item" },
{ id: "h-pn", name: "Part number" },
{ id: "h-rev", name: "Revision" },
{ id: "h-name", name: "Name" },
{ id: "h-qty", name: "Quantity" },
{ id: "h-pl", name: "Purchasing Level" }
];
const row = (
item: string,
pn: string | null,
qty: unknown,
purchasing = "Made",
extra: Record<string, unknown> = {}
) => ({
headerIdToValue: {
"h-item": item,
"h-pn": pn,
"h-rev": { displayName: "A" },
"h-name": `Name ${item}`,
"h-qty": qty,
"h-pl": purchasing
},
...extra
});
describe("parseBomTree", () => {
it("builds the tree from dotted indexes and separates the root row", () => {
const { root, lines } = parseBomTree({
headers: H,
rows: [
row("0", "WB-100", 1),
row("1.1", "LEG-003", 4),
row("1", "SUB-001", 2),
row("2", "TOP-001", 1, "Purchased")
]
});
expect(root?.partNumber).toBe("WB-100");
expect(lines.map((l) => l.index)).toEqual(["1", "2"]);
expect(lines[0]?.children.map((c) => c.partNumber)).toEqual(["LEG-003"]);
expect(lines[0]?.quantity).toBe(2);
expect(lines[0]?.revision).toBe("A");
});
it("keeps each row's non-empty cells as properties", () => {
const { lines } = parseBomTree({
headers: [...H, { id: "p-make", name: "Make or buy", valueType: "ENUM" }],
rows: [
{
headerIdToValue: {
"h-item": "1",
"h-pn": "BRG-6203",
"h-qty": 2,
"h-pl": "",
"p-make": { displayName: "Buy" }
}
}
]
});
expect(lines[0]?.properties).toEqual([
{ propertyId: "h-item", name: "Item", valueType: "STRING", value: "1" },
{
propertyId: "h-pn",
name: "Part number",
valueType: "STRING",
value: "BRG-6203"
},
{ propertyId: "h-qty", name: "Quantity", valueType: "STRING", value: 2 },
{
propertyId: "p-make",
name: "Make or buy",
valueType: "ENUM",
value: { displayName: "Buy" }
}
]);
expect(withoutProperties(lines)[0]).not.toHaveProperty("properties");
});
it("drops a cell Onshape fills with N/A, a property the row does not have", () => {
const { lines } = parseBomTree({
headers: [
...H,
{ id: "p-make", name: "Make or Buy", valueType: "ENUM" },
{ id: "p-mpn", name: "MPN", valueType: "STRING" }
],
rows: [
{
headerIdToValue: {
"h-item": "1",
"h-pn": "ASM-100",
"h-qty": 1,
"h-pl": "",
"p-make": "N/A",
"p-mpn": "N/A"
}
}
]
});
expect(lines[0]?.properties?.map((property) => property.name)).toEqual([
"Item",
"Part number",
"Quantity"
]);
});
it("does not read the Purchasing Level column as Buy", () => {
const { lines } = parseBomTree({
headers: H,
rows: [row("1", "TOP-001", 1, "Purchased")]
});
expect(lines[0]).not.toHaveProperty("purchased");
});
it("marks a row from Standard content", () => {
const { lines } = parseBomTree({
headers: H,
rows: [
row("1", "SCREW-M4", 8, "", {
itemSource: { documentId: "d", isStandardContent: true }
}),
row("2", "SCREW-M5", 8, "", { isStandardContent: true }),
row("3", "BRACKET-1", 1)
]
});
expect(lines.map((line) => line.standardContent)).toEqual([
true,
true,
undefined
]);
});
it("defaults a missing or bad quantity to 1 and survives missing columns", () => {
const { lines } = parseBomTree({
headers: H,
rows: [row("1", null, "not-a-number"), row("2", "X-1", null)]
});
expect(lines[0]?.quantity).toBe(1);
expect(lines[0]?.partNumber).toBeNull();
expect(lines[1]?.quantity).toBe(1);
});
it("keeps itemSource when present and flattens depth-first", () => {
const { lines } = parseBomTree({
headers: H,
rows: [
row("1", "A-1", 1, "Made", {
itemSource: { documentId: "d1", elementId: "e1", partId: "p1" }
}),
row("1.1", "A-2", 3),
row("2", "B-1", 1)
]
});
expect(lines[0]?.itemSource?.partId).toBe("p1");
expect(flattenBomTree(lines).map((n) => n.index)).toEqual([
"1",
"1.1",
"2"
]);
});
it("keeps the row's configuration, which a configured part's identity needs", () => {
const { lines } = parseBomTree({
headers: H,
rows: [
row("1", "A-1", 1, "Made", {
itemSource: {
documentId: "d1",
elementId: "e1",
partId: "p1",
configuration: "List_abc=Large"
}
})
]
});
expect(lines[0]?.itemSource?.configuration).toBe("List_abc=Large");
});
it("tolerates an empty payload", () => {
const { root, lines, missingColumns } = parseBomTree(null);
expect(root).toBeNull();
expect(lines).toEqual([]);
expect(missingColumns).toEqual([]);
});
it("names a missing Item column instead of returning an empty tree as if valid", () => {
// Without "Item" every row reads as the root; the lines come back empty.
const { lines, missingColumns } = parseBomTree({
headers: H.filter((header) => header.name !== "Item"),
rows: [row("1", "A-1", 1), row("2", "B-1", 1)]
});
expect(lines).toEqual([]);
expect(missingColumns).toEqual(["Item"]);
});
it("names every missing required column", () => {
const { missingColumns } = parseBomTree({
headers: H.filter(
(header) => header.name !== "Item" && header.name !== "Part number"
),
rows: [row("1", "A-1", 1)]
});
expect(missingColumns).toEqual(["Item", "Part number"]);
expect(missingBomColumnsMessage(missingColumns)).toContain(
'"Item" and "Part number"'
);
});
it("names a missing Quantity column instead of pushing every line as 1", () => {
const { missingColumns } = parseBomTree({
headers: H.filter((header) => header.name !== "Quantity"),
rows: [row("1", "A-1", 3)]
});
expect(missingColumns).toEqual(["Quantity"]);
});
it("lists three missing columns", () => {
expect(
missingBomColumnsMessage(["Item", "Part number", "Quantity"])
).toContain('"Item", "Part number", and "Quantity"');
});
it("does not flag the headers of a BOM with no rows", () => {
expect(parseBomTree({ headers: [], rows: [] }).missingColumns).toEqual([]);
expect(
parseBomTree({ headers: H, rows: [row("1", "A-1", 1)] }).missingColumns
).toEqual([]);
});
it("reads a template that relabels its columns, by Onshape's property name", () => {
const { lines, missingColumns } = parseBomTree({
headers: [
{ id: "h-item", name: "Pos.", propertyName: "item" },
{ id: "h-qty", name: "Qty", propertyName: "quantity" },
{ id: "h-pn", name: "PART NUMBER", propertyName: "partNumber" },
{ id: "h-rev", name: "REVISION", propertyName: "revision" },
{ id: "h-name", name: "Benennung", propertyName: "name" },
{ id: "h-pl", name: "Purchasing Level", propertyName: "h-pl" }
],
rows: [row("1", "A-1", 2), row("1.1", "B-1", 3)]
});
expect(missingColumns).toEqual([]);
expect(lines).toHaveLength(1);
expect(lines[0]).toMatchObject({
partNumber: "A-1",
revision: "A",
name: "Name 1",
quantity: 2
});
expect(lines[0]!.children[0]).toMatchObject({
partNumber: "B-1",
quantity: 3
});
});
it("matches a label ignoring case when Onshape sends no property name", () => {
const { lines, missingColumns } = parseBomTree({
headers: H.map((header) => ({
...header,
name: header.name.toUpperCase()
})),
rows: [row("1", "A-1", 4)]
});
expect(missingColumns).toEqual([]);
expect(lines[0]).toMatchObject({ partNumber: "A-1", quantity: 4 });
});
it("prefers an exact label over one that differs only in case", () => {
const { lines } = parseBomTree({
headers: [...H, { id: "h-custom", name: "PART NUMBER" }],
rows: [
{
...row("1", "A-1", 1),
headerIdToValue: {
...row("1", "A-1", 1).headerIdToValue,
"h-custom": "LEGACY-1"
}
}
]
});
expect(lines[0]!.partNumber).toBe("A-1");
});
it("prefers Onshape's part number over a custom column with the same label", () => {
const { lines } = parseBomTree({
headers: [
...H.filter((header) => header.id !== "h-pn"),
{ id: "h-custom", name: "Part number", propertyName: "h-custom" },
{ id: "h-pn", name: "PART NUMBER", propertyName: "partNumber" }
],
rows: [
{
...row("1", "A-1", 1),
headerIdToValue: {
...row("1", "A-1", 1).headerIdToValue,
"h-custom": "LEGACY-1"
}
}
]
});
expect(lines[0]!.partNumber).toBe("A-1");
});
});
describe("parseBomTree Subassembly BOM behavior", () => {
const headers = [...H, { id: "h-sbb", name: "Subassembly BOM behavior" }];
const withBehavior = (item: string, pn: string, behavior: string) => {
const r = row(item, pn, 1);
return {
...r,
headerIdToValue: { ...r.headerIdToValue, "h-sbb": behavior }
};
};
it("marks a sub-assembly Onshape shows as one unit, for structure only", () => {
const { lines } = parseBomTree({
headers,
rows: [
withBehavior("1", "ASM-009", "Show Assembly only"),
withBehavior("2", "ASM-008", "Show Assembly and components"),
withBehavior("3", "PAD-005", "N/A")
]
});
expect(lines.map((line) => [line.partNumber, line.shownAsUnit])).toEqual([
["ASM-009", true],
["ASM-008", undefined],
["PAD-005", undefined]
]);
});
});
describe("firstOwnAssemblyPart", () => {
const part = (extra: Record<string, unknown>) => ({
documentId: "d1",
elementId: "e1",
partId: "JHD",
...extra
});
it("takes the first part that is not Standard content, at its linked version", () => {
expect(
firstOwnAssemblyPart({
parts: [
part({ partId: "NUT", isStandardContent: true }),
part({ documentVersion: "v1", documentMicroversion: "m1" })
]
})
).toEqual({
documentId: "d1",
wvm: "v",
wvmId: "v1",
elementId: "e1",
partId: "JHD"
});
});
it("names no version for a part in the same document", () => {
expect(firstOwnAssemblyPart({ parts: [part({})] })).toEqual({
documentId: "d1",
wvm: null,
wvmId: null,
elementId: "e1",
partId: "JHD"
});
});
it("skips malformed entries and is null when no part is left", () => {
expect(
firstOwnAssemblyPart({
parts: [
part({ partId: "" }),
"garbage",
part({ isStandardContent: true })
]
})
).toBeNull();
expect(firstOwnAssemblyPart({})).toBeNull();
expect(firstOwnAssemblyPart(null)).toBeNull();
});
});
describe("describeBomPayload", () => {
it("reports the headers Onshape sent, with their property names", () => {
const report = describeBomPayload({
formatVersion: "1.0",
templateId: "tpl-1",
headers: [
{ id: "h-item", name: "Pos.", propertyName: "item" },
{ id: "h-qty", name: "Qty", propertyName: "quantity" }
],
rows: [row("1", "A-1", 1), row("2", "B-1", 2)],
topLevelAssemblyRow: {}
});
expect(report).toEqual({
payloadKeys: [
"formatVersion",
"templateId",
"headers",
"rows",
"topLevelAssemblyRow"
],
headersType: "array",
headerCount: 2,
headers: [
{ id: "h-item", name: "Pos.", propertyName: "item" },
{ id: "h-qty", name: "Qty", propertyName: "quantity" }
],
rowCount: 2,
formatVersion: "1.0",
templateId: "tpl-1",
topLevelAssemblyRow: true
});
});
it("says when the headers are missing or not a list", () => {
expect(describeBomPayload({ rows: [row("1", "A-1", 1)] })).toMatchObject({
headersType: "missing",
headerCount: 0,
rowCount: 1
});
expect(describeBomPayload({ headers: {}, rows: [] }).headersType).toBe(
"object"
);
expect(describeBomPayload("<html>").payloadKeys).toEqual([]);
expect(describeBomPayload(null).headersType).toBe("missing");
});
it("never copies a cell value", () => {
const report = describeBomPayload({
headers: H,
rows: [row("1", "SECRET-PART", 1)]
});
expect(JSON.stringify(report)).not.toContain("SECRET-PART");
});
});