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: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import { describe, expect, it } from "vitest";
import { fetchAllByIds, fetchAllRecords } from "./utils";
// `fetchAllRecords` is the only thing standing between a >1000-row read and a
// silently truncated one, and the local dev stack does not enforce `max_rows`, so
// the paging arithmetic can only be pinned here. The contract: every row in
// order, one request when the table fits in a page, pages past the first issued
// concurrently, an error on any page propagating instead of partial data, and a
// server that ignores `Range` tripping the backstop rather than hanging.
type Row = { id: number };
/**
* Mimics the two calls fetchAllRecords makes: the factory produces a fresh
* builder, then `.range(from, to)` resolves to a PostgREST-shaped result. A
* single shared builder would be wrong — the real one is mutable, which is why
* the function takes a factory at all.
*/
function fakeTable(
rows: Row[],
opts: { ignoreRange?: boolean; errorOnPage?: number } = {}
) {
const ranges: [number, number][] = [];
let inFlight = 0;
let maxInFlight = 0;
const build = () =>
({
range: (from: number, to: number) => {
ranges.push([from, to]);
inFlight++;
maxInFlight = Math.max(maxInFlight, inFlight);
return new Promise((resolve) => {
setTimeout(() => {
inFlight--;
if (opts.errorOnPage === from / 1000) {
resolve({ data: null, error: { message: "boom" } });
return;
}
resolve({
data: opts.ignoreRange
? rows.slice(0, to - from + 1)
: rows.slice(from, to + 1),
error: null
});
}, 0);
});
}
// biome-ignore lint/suspicious/noExplicitAny: test double for a postgrest builder
}) as any;
return { build, ranges, maxInFlight: () => maxInFlight };
}
const rowsOf = (n: number) => Array.from({ length: n }, (_, i) => ({ id: i }));
describe("fetchAllRecords", () => {
it("makes exactly one request when the table fits in a page", async () => {
const { build, ranges } = fakeTable(rowsOf(77));
const result = await fetchAllRecords<Row>(build);
expect(ranges).toEqual([[0, 999]]);
expect(result.data).toHaveLength(77);
expect(result.count).toBe(77);
});
it("returns every row in order across pages", async () => {
const { build } = fakeTable(rowsOf(2500));
const result = await fetchAllRecords<Row>(build);
expect(result.error).toBeNull();
expect(result.data).toHaveLength(2500);
expect(result.data?.map((r) => r.id)).toEqual(
rowsOf(2500).map((r) => r.id)
);
});
it("stops on an exact multiple without duplicating rows", async () => {
const { build } = fakeTable(rowsOf(2000));
const result = await fetchAllRecords<Row>(build);
expect(result.data).toHaveLength(2000);
expect(new Set(result.data?.map((r) => r.id)).size).toBe(2000);
});
it("issues the pages past the first concurrently", async () => {
const { build, maxInFlight } = fakeTable(rowsOf(4500));
await fetchAllRecords<Row>(build);
expect(maxInFlight()).toBeGreaterThan(1);
});
it("propagates an error rather than returning a partial read", async () => {
const { build } = fakeTable(rowsOf(4500), { errorOnPage: 2 });
const result = await fetchAllRecords<Row>(build);
expect(result.data).toBeNull();
expect(result.error?.message).toBe("boom");
});
it("ignores a failure on a speculative page past the end of the data", async () => {
// 1500 rows: page 1 is short, so it ends the read. Pages 2-4 went out in
// the same wave before that was known, and a read past the end can fail
// (PostgREST answers an out-of-range `Range` with 416) — that must not
// discard rows already in hand.
const { build } = fakeTable(rowsOf(1500), { errorOnPage: 2 });
const result = await fetchAllRecords<Row>(build);
expect(result.error).toBeNull();
expect(result.data).toHaveLength(1500);
});
it("refuses to return a partial read when the server ignores Range", async () => {
const { build } = fakeTable(rowsOf(1000), { ignoreRange: true });
const result = await fetchAllRecords<Row>(build);
expect(result.data).toBeNull();
expect(result.error?.message).toContain("exceeded");
});
});
// `fetchAllByIds` guards the other limit: `.in()` writes every id into the URL,
// and the gateway rejects a request line it cannot buffer (HTTP 431). Each
// group must stay small, every group must be paged past the row cap, and a
// failed group must fail the read instead of dropping its rows.
describe("fetchAllByIds", () => {
/** Rows per id; the factory records each group it is asked for. */
function fakeById(rowsPerId: number, opts: { failGroup?: number } = {}) {
const groups: string[][] = [];
const build = (batch: string[]) => {
groups.push(batch);
const group = groups.length - 1;
const rows = batch.flatMap((id) =>
Array.from({ length: rowsPerId }, (_, i) => ({ id: `${id}:${i}` }))
);
return {
range: (from: number, to: number) =>
Promise.resolve(
opts.failGroup === group
? { data: null, error: { message: "boom" } }
: { data: rows.slice(from, to + 1), error: null }
)
} as any;
};
return { build, groups };
}
const idsOf = (n: number) => Array.from({ length: n }, (_, i) => `u${i}`);
it("sends at most 100 ids per request and returns every row", async () => {
const { build, groups } = fakeById(3);
const result = await fetchAllByIds(idsOf(250), build);
expect(groups.map((g) => g.length)).toEqual([100, 100, 50]);
expect(result.error).toBeNull();
expect(result.data).toHaveLength(750);
});
it("pages a group whose rows pass the row cap", async () => {
const { build } = fakeById(30);
const result = await fetchAllByIds(idsOf(100), build);
expect(result.data).toHaveLength(3000);
});
it("makes no request for an empty list", async () => {
const { build, groups } = fakeById(1);
const result = await fetchAllByIds([], build);
expect(groups).toEqual([]);
expect(result.data).toEqual([]);
});
it("fails the whole read when one group fails", async () => {
const { build } = fakeById(1, { failGroup: 1 });
const result = await fetchAllByIds(idsOf(250), build);
expect(result.data).toBeNull();
expect(result.error?.message).toBe("boom");
});
});