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

baseSchemas.test.ts

121 lines4.4 KB

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