Skip to content
Source
Sign inSign up

orchestrationV2IdTable.test.ts

63 lines2.1 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Schema from "effect/Schema";
3​
4import { withRecordIdTable } from "./orchestrationV2IdTable.ts";
5​
6const Page = withRecordIdTable(
7 Schema.Struct({
8 rows: Schema.Array(
9 Schema.Struct({
10 id: Schema.String,
11 count: Schema.Number,
12 parentIds: Schema.Array(Schema.String),
13 mergedId: Schema.optional(Schema.String),
14 item: Schema.Struct({ ownerId: Schema.NullOr(Schema.String), legacyId: Schema.Number }),
15 }),
16 ),
17 idTable: Schema.optionalKey(Schema.Literal(true)),
18 }),
19 () => null,
20);
21const encode = Schema.encodeSync(Schema.toCodecJson(Page));
22const decode = Schema.decodeUnknownSync(Schema.toCodecJson(Page));
23​
24describe("record ID table", () => {
25 it("interns record ID slots and restores them exactly, numbers included", () => {
26 const value = {
27 rows: [
28 { id: "row:a", count: 7, parentIds: ["row:b"], item: { ownerId: "row:b", legacyId: 3 } },
29 { id: "row:b", count: 3, parentIds: [], item: { ownerId: null, legacyId: 0 } },
30 ],
31 };
32 const wire = JSON.parse(JSON.stringify(encode({ ...value, idTable: true })));
33​
34 expect(wire).toEqual({
35 rows: [
36 { id: 0, count: 7, parentIds: [1], item: { ownerId: 1, legacyId: 2 } },
37 { id: 1, count: 3, parentIds: [], item: { ownerId: null, legacyId: 3 } },
38 ],
39 ids: ["row:a", "row:b", 3, 0],
40 });
41 expect(decode(wire)).toEqual(value);
42 // The table is consumed, so a second decode of the same object cannot remap numbers.
43 expect(decode(wire)).toEqual(value);
44 });
45​
46 it("rejects an index outside the table, even in an optional slot", () => {
47 const wire = {
48 rows: [{ id: 0, count: 1, parentIds: [], mergedId: 5, item: { ownerId: null, legacyId: 0 } }],
49 ids: ["row:a"],
50 };
51​
52 expect(() => decode(wire)).toThrow(/mergedId/);
53 });
54​
55 it("sends and reads the plain form without the marker", () => {
56 const value = {
57 rows: [{ id: "row:a", count: 1, parentIds: [], item: { ownerId: null, legacyId: 1 } }],
58 };
59​
60 expect(encode(value)).toEqual(value);
61 expect(decode(value)).toEqual(value);
62 });
63});