pi-mono/packages/chord/test/store/allocator.test.ts
Mario Zechner e9c0ff4d84 feat(chord): Chord v2 is the package's root
src/next is src (the main export), src/next-node is src/node with short subpaths (/http, /workers, /plugins,
/plugins/runtime, /app, /surfaces, /web, /react, /tui, /tui/launcher); the foundations get /json and /watch;
Chord v1 moves to src/old (/old, /old/node) until p2 runs on v2; tests and docs move likewise (test/, docs/,
test/old, docs/old). Durable and env import the foundations from their subpaths.
2026-11-02 22:36:55 +00:00

24 lines
1,002 B
TypeScript

// Tests of the id allocator (slice 6b D3, §1.2): from 2, raised by explicit ids, saturating at the safe range.
import { describe, expect, it } from "vitest";
import { FIRST_ID, IdAllocator } from "../../src/store/allocator.ts";
describe("the allocator", () => {
it("mints from its value, and an explicit id raises it above itself, never lowering it", () => {
const ids = new IdAllocator(FIRST_ID);
expect([ids.mint(), ids.mint()]).toEqual([2, 3]);
ids.raise(100);
expect(ids.mint()).toBe(101);
ids.raise(50);
expect(ids.value).toBe(102);
});
it("saturates at Number.MAX_SAFE_INTEGER: that id may be written, every later mint is refused", () => {
const ids = new IdAllocator(Number.MAX_SAFE_INTEGER - 1);
expect(ids.mint()).toBe(Number.MAX_SAFE_INTEGER - 1);
expect(() => ids.mint()).toThrow("ID space is exhausted");
ids.raise(Number.MAX_SAFE_INTEGER);
expect(ids.value).toBe(Number.MAX_SAFE_INTEGER);
expect(() => ids.mint()).toThrow("ID space is exhausted");
});
});