pi-mono/packages/chord/test/registries/entries.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

44 lines
1.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { keyedEntries, orderedEntries, type RegistryConflict, type RegistryTable } from "../../src/registries/index.ts";
const table: RegistryTable<{ readonly routes?: Readonly<Record<string, string>>; readonly items?: readonly string[] }> =
[
{ plugin: "@a/one", version: 1, value: { routes: { "/x": "one", "/y": "one" }, items: ["late", "first"] } },
{ plugin: "@b/two", type: "b.store", version: 1, value: { routes: { "/x": "two" }, items: ["second"] } },
{ plugin: "@c/three", version: 1, value: {} },
];
describe("keyedEntries", () => {
it("maps every entry's keys with their plugin; a duplicate is reported and the first by plugin name keeps it", () => {
const conflicts: RegistryConflict[] = [];
const map = keyedEntries(
table,
(value) => value.routes,
(conflict) => conflicts.push(conflict),
);
expect([...map]).toEqual([
["/x", { value: "one", plugin: "@a/one" }],
["/y", { value: "one", plugin: "@a/one" }],
]);
expect(conflicts).toEqual([{ message: '"/x" is contributed twice', plugins: ["@a/one", "@b/two"] }]);
});
});
describe("orderedEntries", () => {
it("lists every entry's items in table order, with their plugin and type", () => {
expect(orderedEntries(table, (value) => value.items)).toEqual([
{ value: "late", plugin: "@a/one" },
{ value: "first", plugin: "@a/one" },
{ value: "second", plugin: "@b/two", type: "b.store" },
]);
});
it("sorts by `orderOf`, table order kept among equals", () => {
const order = (item: string) => (item === "first" ? 0 : 1);
expect(orderedEntries(table, (value) => value.items, order).map(({ value }) => value)).toEqual([
"first",
"late",
"second",
]);
});
});