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

baseSchemas.test.ts

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