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

orchestrationCache.test.ts

133 lines4.7 KB

1import { describe, expect, it } from "@effect/vitest";
2import {
3 EnvironmentId,
4 MessageId,
5 RuntimeRequestId,
6 type OrchestrationV2ShellSnapshot,
7} from "@t3tools/contracts";
8import * as DateTime from "effect/DateTime";
9import * as Effect from "effect/Effect";
10import * as Option from "effect/Option";
11import * as Schema from "effect/Schema";
12​
13import {
14 ORCHESTRATION_CACHE_SCHEMA_VERSION,
15 StoredOrchestrationShellSnapshot,
16 StoredOrchestrationThreadSnapshot,
17 decodeOrDiscardOrchestrationCache,
18} from "./orchestrationCache.ts";
19import {
20 v2Now,
21 v2Projection,
22 v2ShellSnapshot,
23 v2ThreadShell,
24 v2ThreadId,
25} from "../state/orchestrationV2TestFixtures.ts";
26​
27const environmentId = EnvironmentId.make("environment-cache-test");
28const StoredShellSnapshotJson = Schema.fromJsonString(StoredOrchestrationShellSnapshot);
29const StoredThreadSnapshotJson = Schema.fromJsonString(StoredOrchestrationThreadSnapshot);
30const encodeStoredShellSnapshot = Schema.encodeSync(StoredOrchestrationShellSnapshot);
31const decodeStoredShellSnapshotSync = Schema.decodeUnknownSync(StoredOrchestrationShellSnapshot);
32const encodeStoredShellSnapshotJson = Schema.encodeSync(StoredShellSnapshotJson);
33const decodeStoredShellSnapshotJson = Schema.decodeUnknownSync(StoredShellSnapshotJson);
34const encodeStoredThreadSnapshotJson = Schema.encodeSync(StoredThreadSnapshotJson);
35const decodeStoredThreadSnapshotJson = Schema.decodeUnknownSync(StoredThreadSnapshotJson);
36​
37class TestCacheDecodeError extends Schema.TaggedErrorClass<TestCacheDecodeError>()(
38 "TestCacheDecodeError",
39 {
40 message: Schema.String,
41 },
42) {}
43​
44const shellSnapshotWithSummaries: OrchestrationV2ShellSnapshot = {
45 ...v2ShellSnapshot,
46 threads: [
47 {
48 ...v2ThreadShell,
49 pendingRuntimeRequest: {
50 id: RuntimeRequestId.make("runtime-request-v2"),
51 kind: "command",
52 createdAt: v2Now,
53 },
54 latestVisibleMessage: {
55 id: MessageId.make("message-v2"),
56 role: "assistant",
57 text: "Done",
58 updatedAt: v2Now,
59 },
60 latestUserMessageAt: v2Now,
61 settledAt: v2Now,
62 },
63 ],
64};
65​
66describe("orchestration cache envelopes", () => {
67 it.effect("round-trips V2 shell and thread cache envelopes as JSON", () =>
68 Effect.sync(() => {
69 const encodedShell = encodeStoredShellSnapshotJson({
70 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
71 environmentId,
72 snapshot: shellSnapshotWithSummaries,
73 });
74 const shell = decodeStoredShellSnapshotJson(encodedShell);
75 const encodedThread = encodeStoredThreadSnapshotJson({
76 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
77 environmentId,
78 threadId: v2ThreadId,
79 snapshot: { snapshotSequence: 4, projection: v2Projection },
80 });
81 const thread = decodeStoredThreadSnapshotJson(encodedThread);
82 const [threadShell] = shell.snapshot.threads;
83​
84 expect(threadShell?.latestVisibleMessage?.text).toBe("Done");
85 expect(
86 threadShell?.latestVisibleMessage === null ||
87 threadShell?.latestVisibleMessage === undefined
88 ? null
89 : DateTime.formatIso(threadShell.latestVisibleMessage.updatedAt),
90 ).toBe("2026-06-20T00:00:00.000Z");
91 expect(
92 threadShell?.latestUserMessageAt === null || threadShell?.latestUserMessageAt === undefined
93 ? null
94 : DateTime.formatIso(threadShell.latestUserMessageAt),
95 ).toBe("2026-06-20T00:00:00.000Z");
96 expect(
97 threadShell?.settledAt === null || threadShell?.settledAt === undefined
98 ? null
99 : DateTime.formatIso(threadShell.settledAt),
100 ).toBe("2026-06-20T00:00:00.000Z");
101 expect(DateTime.formatIso(thread.snapshot.projection.thread.updatedAt)).toBe(
102 "2026-06-20T00:00:00.000Z",
103 );
104 expect(thread.snapshot.projection).toEqual(v2Projection);
105 expect(thread.snapshot.snapshotSequence).toBe(4);
106 }),
107 );
108​
109 it.effect("discards V1-versioned cache envelopes after a decode failure", () =>
110 Effect.gen(function* () {
111 let discardCount = 0;
112 const encodedShell = encodeStoredShellSnapshot({
113 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
114 environmentId,
115 snapshot: v2ShellSnapshot,
116 });
117 const decoded = Effect.try({
118 try: () => decodeStoredShellSnapshotSync({ ...encodedShell, schemaVersion: 1 }),
119 catch: (cause) => new TestCacheDecodeError({ message: String(cause) }),
120 }).pipe(Effect.map(Option.some));
121​
122 const result = yield* decodeOrDiscardOrchestrationCache(
123 decoded,
124 Effect.sync(() => {
125 discardCount += 1;
126 }),
127 );
128​
129 expect(Option.isNone(result)).toBe(true);
130 expect(discardCount).toBe(1);
131 }),
132 );
133});