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

checkpointDiffState.test.ts

135 lines3.8 KB

1import { EnvironmentId, ThreadId, type OrchestrationGetTurnDiffResult } from "@t3tools/contracts";
2import { AtomRegistry } from "effect/unstable/reactivity";
3import { afterEach, describe, expect, it, vi } from "vite-plus/test";
4​
5import {
6 type CheckpointDiffClient,
7 createCheckpointDiffManager,
8 EMPTY_CHECKPOINT_DIFF_STATE,
9 getCheckpointDiffTargetKey,
10} from "./checkpointDiffState.ts";
11​
12let registry = AtomRegistry.make();
13​
14function resetAtomRegistry() {
15 registry.dispose();
16 registry = AtomRegistry.make();
18​
19const TARGET = {
20 environmentId: EnvironmentId.make("env-local"),
21 threadId: ThreadId.make("thread-1"),
22 fromTurnCount: 1,
23 toTurnCount: 2,
24 ignoreWhitespace: false,
25};
26​
27const PATCH_RESULT: OrchestrationGetTurnDiffResult = {
28 threadId: TARGET.threadId,
29 diff: "patch",
30 fromTurnCount: 1,
31 toTurnCount: 2,
32};
33​
34function createClient() {
35 return {
36 getTurnDiff: vi.fn(async () => PATCH_RESULT),
37 getFullThreadDiff: vi.fn(async () => PATCH_RESULT),
38 } satisfies CheckpointDiffClient;
40​
41describe("createCheckpointDiffManager", () => {
42 afterEach(() => {
43 resetAtomRegistry();
44 });
45​
46 it("loads a turn checkpoint diff into atom state", async () => {
47 const client = createClient();
48 const manager = createCheckpointDiffManager({
49 getRegistry: () => registry,
50 getClient: () => client,
51 });
52​
53 await expect(manager.load(TARGET)).resolves.toEqual(PATCH_RESULT);
54​
55 expect(client.getTurnDiff).toHaveBeenCalledWith({
56 threadId: TARGET.threadId,
57 fromTurnCount: 1,
58 toTurnCount: 2,
59 ignoreWhitespace: false,
60 });
61 expect(client.getFullThreadDiff).not.toHaveBeenCalled();
62 expect(manager.getSnapshot(TARGET)).toEqual({
63 data: PATCH_RESULT,
64 error: null,
65 isPending: false,
66 });
67 });
68​
69 it("loads a full thread diff when the range starts at zero", async () => {
70 const client = createClient();
71 const manager = createCheckpointDiffManager({
72 getRegistry: () => registry,
73 getClient: () => client,
74 });
75​
76 await manager.load({ ...TARGET, fromTurnCount: 0 });
77​
78 expect(client.getFullThreadDiff).toHaveBeenCalledWith({
79 threadId: TARGET.threadId,
80 toTurnCount: 2,
81 ignoreWhitespace: false,
82 });
83 expect(client.getTurnDiff).not.toHaveBeenCalled();
84 });
85​
86 it("returns empty state for invalid targets", () => {
87 const manager = createCheckpointDiffManager({
88 getRegistry: () => registry,
89 getClient: () => createClient(),
90 });
91​
92 expect(manager.getSnapshot({ ...TARGET, threadId: null })).toBe(EMPTY_CHECKPOINT_DIFF_STATE);
93 expect(getCheckpointDiffTargetKey({ ...TARGET, threadId: null })).toBeNull();
94 });
95​
96 it("deduplicates in-flight requests and reuses successful cached data", async () => {
97 const client = createClient();
98 const manager = createCheckpointDiffManager({
99 getRegistry: () => registry,
100 getClient: () => client,
101 });
102​
103 const first = manager.load(TARGET);
104 const second = manager.load(TARGET);
105​
106 expect(first).toBe(second);
107 await first;
108 await manager.load(TARGET);
109​
110 expect(client.getTurnDiff).toHaveBeenCalledTimes(1);
111 });
112​
113 it("retries temporarily unavailable checkpoint diffs", async () => {
114 let attempts = 0;
115 const client = {
116 getFullThreadDiff: vi.fn(async () => PATCH_RESULT),
117 getTurnDiff: vi.fn(async () => {
118 attempts += 1;
119 if (attempts < 3) {
120 throw new Error("checkpoint is unavailable for turn");
121 }
122 return PATCH_RESULT;
123 }),
124 } satisfies CheckpointDiffClient;
125 const manager = createCheckpointDiffManager({
126 getRegistry: () => registry,
127 getClient: () => client,
128 retryDelay: async () => undefined,
129 });
130​
131 await expect(manager.load(TARGET)).resolves.toEqual(PATCH_RESULT);
132​
133 expect(client.getTurnDiff).toHaveBeenCalledTimes(3);
134 });
135});