Open History to see the last commit
1import { EnvironmentId, ThreadId, type EnvironmentApi } from "@t3tools/contracts";
2import { QueryClient } from "@tanstack/react-query";
3import { afterEach, describe, expect, it, vi } from "vitest";
4import { checkpointDiffQueryOptions, providerQueryKeys } from "./providerReactQuery";
5import * as environmentApi from "../environmentApi";
6
7const threadId = ThreadId.make("thread-id");
8const environmentId = EnvironmentId.make("environment-local");
9
10function mockNativeApi(input: {
11 getTurnDiff: ReturnType<typeof vi.fn>;
12 getFullThreadDiff: ReturnType<typeof vi.fn>;
13}) {
14 vi.spyOn(environmentApi, "ensureEnvironmentApi").mockReturnValue({
15 orchestration: {
16 getTurnDiff: input.getTurnDiff,
17 getFullThreadDiff: input.getFullThreadDiff,
18 },
19 } as unknown as EnvironmentApi);
20}
21
22afterEach(() => {
23 vi.restoreAllMocks();
24});
25
26describe("providerQueryKeys.checkpointDiff", () => {
27 it("includes cacheScope so reused turn counts do not collide", () => {
28 const baseInput = {
29 environmentId,
30 threadId,
31 fromTurnCount: 1,
32 toTurnCount: 2,
33 ignoreWhitespace: false,
34 } as const;
35
36 expect(
37 providerQueryKeys.checkpointDiff({
38 ...baseInput,
39 cacheScope: "turn:old-turn",
40 }),
41 ).not.toEqual(
42 providerQueryKeys.checkpointDiff({
43 ...baseInput,
44 cacheScope: "turn:new-turn",
45 }),
46 );
47 });
48
49 it("includes ignoreWhitespace so normal and whitespace-hidden diffs do not collide", () => {
50 const baseInput = {
51 environmentId,
52 threadId,
53 fromTurnCount: 1,
54 toTurnCount: 2,
55 cacheScope: "turn:abc",
56 } as const;
57
58 expect(
59 providerQueryKeys.checkpointDiff({
60 ...baseInput,
61 ignoreWhitespace: false,
62 }),
63 ).not.toEqual(
64 providerQueryKeys.checkpointDiff({
65 ...baseInput,
66 ignoreWhitespace: true,
67 }),
68 );
69 });
70});
71
72describe("checkpointDiffQueryOptions", () => {
73 it("forwards checkpoint range to the provider API by default", async () => {
74 const getTurnDiff = vi.fn().mockResolvedValue({ diff: "patch" });
75 const getFullThreadDiff = vi.fn().mockResolvedValue({ diff: "patch" });
76 mockNativeApi({ getTurnDiff, getFullThreadDiff });
77
78 const options = checkpointDiffQueryOptions({
79 environmentId,
80 threadId,
81 fromTurnCount: 3,
82 toTurnCount: 4,
83 ignoreWhitespace: false,
84 cacheScope: "turn:abc",
85 });
86
87 const queryClient = new QueryClient();
88 await queryClient.fetchQuery(options);
89
90 expect(getTurnDiff).toHaveBeenCalledWith({
91 threadId,
92 fromTurnCount: 3,
93 toTurnCount: 4,
94 ignoreWhitespace: false,
95 });
96 expect(getFullThreadDiff).not.toHaveBeenCalled();
97 });
98
99 it("forwards whitespace-hidden checkpoint range to the provider API", async () => {
100 const getTurnDiff = vi.fn().mockResolvedValue({ diff: "patch" });
101 const getFullThreadDiff = vi.fn().mockResolvedValue({ diff: "patch" });
102 mockNativeApi({ getTurnDiff, getFullThreadDiff });
103
104 const options = checkpointDiffQueryOptions({
105 environmentId,
106 threadId,
107 fromTurnCount: 3,
108 toTurnCount: 4,
109 ignoreWhitespace: true,
110 cacheScope: "turn:abc",
111 });
112
113 const queryClient = new QueryClient();
114 await queryClient.fetchQuery(options);
115
116 expect(getTurnDiff).toHaveBeenCalledWith({
117 threadId,
118 fromTurnCount: 3,
119 toTurnCount: 4,
120 ignoreWhitespace: true,
121 });
122 expect(getFullThreadDiff).not.toHaveBeenCalled();
123 });
124
125 it("uses explicit full thread diff API when range starts from zero", async () => {
126 const getTurnDiff = vi.fn().mockResolvedValue({ diff: "patch" });
127 const getFullThreadDiff = vi.fn().mockResolvedValue({ diff: "patch" });
128 mockNativeApi({ getTurnDiff, getFullThreadDiff });
129
130 const options = checkpointDiffQueryOptions({
131 environmentId,
132 threadId,
133 fromTurnCount: 0,
134 toTurnCount: 2,
135 ignoreWhitespace: true,
136 cacheScope: "thread:all",
137 });
138
139 const queryClient = new QueryClient();
140 await queryClient.fetchQuery(options);
141
142 expect(getFullThreadDiff).toHaveBeenCalledWith({
143 threadId,
144 toTurnCount: 2,
145 ignoreWhitespace: true,
146 });
147 expect(getTurnDiff).not.toHaveBeenCalled();
148 });
149
150 it("fails fast on invalid range and does not call provider RPC", async () => {
151 const getTurnDiff = vi.fn().mockResolvedValue({ diff: "patch" });
152 const getFullThreadDiff = vi.fn().mockResolvedValue({ diff: "patch" });
153 mockNativeApi({ getTurnDiff, getFullThreadDiff });
154
155 const options = checkpointDiffQueryOptions({
156 environmentId,
157 threadId,
158 fromTurnCount: 4,
159 toTurnCount: 3,
160 ignoreWhitespace: false,
161 cacheScope: "turn:invalid",
162 });
163
164 const queryClient = new QueryClient();
165
166 await expect(queryClient.fetchQuery(options)).rejects.toThrow(
167 "Checkpoint diff is unavailable.",
168 );
169 expect(getTurnDiff).not.toHaveBeenCalled();
170 expect(getFullThreadDiff).not.toHaveBeenCalled();
171 });
172
173 it("retries checkpoint-not-ready errors longer than generic failures", () => {
174 const options = checkpointDiffQueryOptions({
175 environmentId,
176 threadId,
177 fromTurnCount: 1,
178 toTurnCount: 2,
179 ignoreWhitespace: false,
180 cacheScope: "turn:abc",
181 });
182 const retry = options.retry;
183 expect(typeof retry).toBe("function");
184 if (typeof retry !== "function") {
185 throw new Error("Expected retry to be a function.");
186 }
187
188 expect(retry(1, new Error("Checkpoint turn count 2 exceeds current turn count 1."))).toBe(true);
189 expect(
190 retry(11, new Error("Filesystem checkpoint is unavailable for turn 2 in thread thread-1.")),
191 ).toBe(true);
192 expect(
193 retry(12, new Error("Filesystem checkpoint is unavailable for turn 2 in thread thread-1.")),
194 ).toBe(false);
195 expect(retry(2, new Error("Something else failed."))).toBe(true);
196 expect(retry(3, new Error("Something else failed."))).toBe(false);
197 });
198
199 it("backs off longer for checkpoint-not-ready errors", () => {
200 const options = checkpointDiffQueryOptions({
201 environmentId,
202 threadId,
203 fromTurnCount: 1,
204 toTurnCount: 2,
205 ignoreWhitespace: false,
206 cacheScope: "turn:abc",
207 });
208 const retryDelay = options.retryDelay;
209 expect(typeof retryDelay).toBe("function");
210 if (typeof retryDelay !== "function") {
211 throw new Error("Expected retryDelay to be a function.");
212 }
213
214 const checkpointDelay = retryDelay(
215 4,
216 new Error("Checkpoint turn count 2 exceeds current turn count 1."),
217 );
218 const genericDelay = retryDelay(4, new Error("Network failure"));
219
220 expect(typeof checkpointDelay).toBe("number");
221 expect(typeof genericDelay).toBe("number");
222 expect((checkpointDelay ?? 0) > (genericDelay ?? 0)).toBe(true);
223 });
224});