imrishabh18/rp2040-motor-controller
This circuit integrates a Raspberry Pi RP2040 microcontroller with multiple power management components, USB interface, drivers, sensors (including an AS5600 encoder), protection elements, and various passive and active components to facilitate device control, measurement, and communication.
- Version
- 1.0.14
- License
- unset
- Stars
- 0
tests/jlcpcb-stock-audit.test.ts
import { expect, test } from "bun:test";
import { parseJlcProduct } from "../scripts/audit-jlcpcb-stock.mjs";
const product = {
componentCode: "C42377749",
componentModelEn: "WJ500V-5.08-04P-14-00A",
componentBrandEn: "Ningbo Kangnex Elec",
componentSpecificationEn: "Plugin,P=5.08mm",
overseasStockCount: 150,
canPresaleNumber: 0,
isBuyComponent: "1",
preMinPurchaseNum: 28,
};
const page = (chunks: string[]) =>
chunks.map((chunk) => `<script>self.__next_f.push(${JSON.stringify([1, chunk])})</script>`).join("");
test("keeps displayed stock separate from available order quantity", () => {
const result = parseJlcProduct(page([`73:${JSON.stringify(product)}\n`]), "C42377749");
expect(result.displayedStock).toBe(150);
expect(result.orderableStock).toBe(0);
expect(result.preorderMinimum).toBe(28);
});
test("ignores price records and other suggested components", () => {
const stream = `1:${JSON.stringify({ componentCode: "C42377749", productPrice: 0.3 })}\n` +
`2:${JSON.stringify({ ...product, componentCode: "C123", canPresaleNumber: 1000 })}\n` +
`3:${JSON.stringify(product)}\n`;
expect(parseJlcProduct(page([stream]), "C42377749").orderableStock).toBe(0);
});
test("reassembles records split between script chunks", () => {
const stream = `73:${JSON.stringify(product)}\n`;
expect(parseJlcProduct(page([stream.slice(0, 80), stream.slice(80)]), "C42377749").displayedStock).toBe(150);
});
test("fails closed instead of assuming missing stock means available", () => {
expect(() => parseJlcProduct(page([`73:${JSON.stringify(product)}\n`]), "C2040")).toThrow();
expect(() => parseJlcProduct("<html>Service unavailable</html>", "C2040")).toThrow();
});