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

persistence.test.ts

55 lines2.2 KB

1import {
2 OrchestrationProjectShell,
3 OrchestrationShellSnapshot,
4 OrchestrationThreadShell,
5} from "@t3tools/contracts";
6import { describe, expect, it } from "@effect/vitest";
7import * as Arr from "effect/Array";
8import * as Effect from "effect/Effect";
9import * as Schema from "effect/Schema";
10import * as Arbitrary from "effect/unstable/arbitrary/Arbitrary";
11​
12import { encodeShellSnapshotForCache } from "./persistence.ts";
13​
14// Generated values can hold untrimmed strings, which a decoded value never
15// has. One encode and decode gives a value a client can hold; values that
16// fail are dropped. Size 30 makes the generator fill optional fields.
17const sampleDecoded = <S extends Schema.Constraint>(schema: S) =>
18 Effect.gen(function* () {
19 const encode = Schema.encodeEffect(schema);
20 const decode = Schema.decodeEffect(schema);
21 const generated = yield* Arbitrary.sampleEffect(Arbitrary.schema(schema), {
22 count: 1000,
23 size: 30,
24 });
25 const decoded = yield* Effect.forEach(generated, (value) =>
26 encode(value).pipe(Effect.flatMap(decode), Effect.option),
27 );
28 return Arr.getSomes(decoded);
29 });
30const encodeSnapshot = Schema.encodeEffect(OrchestrationShellSnapshot);
31​
32describe("encodeShellSnapshotForCache", () => {
33 it.effect("matches the Schema encoding of a generated snapshot", () =>
34 Effect.gen(function* () {
35 const threads = yield* sampleDecoded(OrchestrationThreadShell);
36 const projects = yield* sampleDecoded(OrchestrationProjectShell);
37 const snapshot: OrchestrationShellSnapshot = {
38 snapshotSequence: 1,
39 // The generator rarely makes monogram icons, and they are the one
40 // project field whose encoding differs from the decoded value.
41 projects: projects.map((project, index) =>
42 index % 2 === 0
43 ? { ...project, projectIcon: { kind: "monogram", text: "T3", color: "blue" } }
44 : project,
45 ),
46 threads,
47 updatedAt: "2026-09-25T00:00:00.000Z",
48 };
49​
50 expect(threads.length).toBeGreaterThan(0);
51 expect(projects.length).toBeGreaterThan(0);
52 expect(yield* encodeShellSnapshotForCache(snapshot)).toEqual(yield* encodeSnapshot(snapshot));
53 }),
54 );
55});