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

orchestrationCache.test.ts

143 lines5.0 KB

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