Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

baseSchemas.test.ts

104 lines3.6 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as DateTime from "effect/DateTime";
3import * as Schema from "effect/Schema";
4​
5import {
6 ForwardCompatibleArray,
7 ForwardCompatibleUnion,
8 ForwardCompatibleUnionArray,
9 isUnknownUnionMember,
10 TrimmedNonEmptyString,
11 UnknownUnionMember,
12} from "./baseSchemas.ts";
13​
14const at = "2026-10-05T00:00:00.000Z";
15const Shape = Schema.Union([
16 Schema.Struct({ kind: Schema.Literal("circle"), radius: Schema.Number, at: Schema.DateTimeUtc }),
17 Schema.Struct({ kind: Schema.Literal("square"), side: Schema.Number }),
18]);
19const members = Shape.members;
20const fromWire = <S extends Schema.Top>(schema: S) =>
21 Schema.decodeUnknownSync(Schema.toCodecJson(schema) as never) as (value: unknown) => S["Type"];
22​
23describe("ForwardCompatibleArray", () => {
24 it("decodes elements through their own codec and drops ones it cannot read", () => {
25 const decoded = fromWire(ForwardCompatibleArray(Shape))([
26 { kind: "circle", radius: 1, at },
27 { kind: "triangle" },
28 { kind: "square", side: "wide" },
29 ]);
30 expect(decoded).toHaveLength(1);
31 expect(DateTime.isDateTime((decoded[0] as { at: unknown }).at)).toBe(true);
32 });
33​
34 const Named = ForwardCompatibleArray(Schema.Struct({ name: TrimmedNonEmptyString }));
35​
36 it("drops an element it cannot encode instead of failing the array", () => {
37 const wire = JSON.parse(
38 JSON.stringify(
39 Schema.encodeUnknownSync(Schema.toCodecJson(Named))([
40 { name: "a" },
41 { name: " " },
42 { name: "b" },
43 ]),
44 ),
45 );
46 expect(wire).toEqual([{ name: "a" }, { name: "b" }]);
47 expect(fromWire(Named)(wire)).toEqual([{ name: "a" }, { name: "b" }]);
48 });
49​
50 it("still drops the null holes a server on an earlier build sends", () => {
51 expect(fromWire(Named)([{ name: "a" }, null, { name: "b" }])).toEqual([
52 { name: "a" },
53 { name: "b" },
54 ]);
55 });
56});
57​
58describe("ForwardCompatibleUnion", () => {
59 const decode = fromWire(ForwardCompatibleUnion(members, "kind"));
60​
61 it("decodes a member from a newer server as unknown", () => {
62 const decoded = decode({ kind: "triangle", corners: 3 });
63 expect(isUnknownUnionMember(decoded)).toBe(true);
64 expect(decoded).toEqual(new UnknownUnionMember("kind", "triangle"));
65 });
66​
67 it("decodes known members in full, transformations included", () => {
68 const decoded = decode({ kind: "circle", radius: 1, at });
69 expect(isUnknownUnionMember(decoded)).toBe(false);
70 expect(DateTime.isDateTime((decoded as { at: unknown }).at)).toBe(true);
71 });
72​
73 it("still fails a known member whose payload is broken", () => {
74 expect(() => decode({ kind: "square", side: "wide" })).toThrow();
75 });
76});
77​
78describe("ForwardCompatibleUnionArray", () => {
79 it("keeps a known member however its fields are named", () => {
80 const Flagged = Schema.Struct({
81 kind: Schema.Literal("flag"),
82 _unknown: Schema.Literal(true),
83 tag: Schema.String,
84 value: Schema.String,
85 });
86 const flag = { kind: "flag", _unknown: true, tag: "kind", value: "x" };
87 expect(fromWire(ForwardCompatibleUnionArray([Flagged], "kind"))([flag])).toEqual([flag]);
88 });
89​
90 const decode = fromWire(ForwardCompatibleUnionArray(members, "kind"));
91​
92 it("drops members a newer server added and keeps the rest", () => {
93 const decoded = decode([
94 { kind: "triangle" },
95 { kind: "circle", radius: 1, at },
96 { kind: "square", side: 2 },
97 ]);
98 expect(decoded.map((shape) => shape.kind)).toEqual(["circle", "square"]);
99 });
100​
101 it("fails when a known member is broken, so real bugs stay visible", () => {
102 expect(() => decode([{ kind: "square", side: "wide" }])).toThrow();
103 });
104});