Open History to see the last commit
1import { EnvironmentId, ThreadId, type EnvironmentApi } from "@workbench/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 } as const;
34
35 expect(
36 providerQueryKeys.checkpointDiff({
37 ...baseInput,
38 cacheScope: "turn:old-turn",
39 }),
40 ).not.toEqual(
41 providerQueryKeys.checkpointDiff({
42 ...baseInput,
43 cacheScope: "turn:new-turn",
44 }),
45 );
46 });
47});
48
49describe("checkpointDiffQueryOptions", () => {
50 it("forwards checkpoint range to the provider API", async () => {
51 const getTurnDiff = vi.fn().mockResolvedValue({ diff: "patch" });
52 const getFullThreadDiff = vi.fn().mockResolvedValue({ diff: "patch" });
53 mockNativeApi({ getTurnDiff, getFullThreadDiff });
54
55 const options = checkpointDiffQueryOptions({
56 environmentId,
57 threadId,
58 fromTurnCount: 3,
59 toTurnCount: 4,
60 cacheScope: "turn:abc",
61 });
62
63 const queryClient = new QueryClient();
64 await queryClient.fetchQuery(options);
65
66 expect(getTurnDiff).toHaveBeenCalledWith({
67 threadId,
68 fromTurnCount: 3,
69 toTurnCount: 4,
70 });
71 expect(getFullThreadDiff).not.toHaveBeenCalled();
72 });
73
74 it("uses explicit full thread diff API when range starts from zero", async () => {
75 const getTurnDiff = vi.fn().mockResolvedValue({ diff: "patch" });
76 const getFullThreadDiff = vi.fn().mockResolvedValue({ diff: "patch" });
77 mockNativeApi({ getTurnDiff, getFullThreadDiff });
78
79 const options = checkpointDiffQueryOptions({
80 environmentId,
81 threadId,
82 fromTurnCount: 0,
83 toTurnCount: 2,
84 cacheScope: "thread:all",
85 });
86
87 const queryClient = new QueryClient();
88 await queryClient.fetchQuery(options);
89
90 expect(getFullThreadDiff).toHaveBeenCalledWith({
91 threadId,
92 toTurnCount: 2,
93 });
94 expect(getTurnDiff).not.toHaveBeenCalled();
95 });
96
97 it("fails fast on invalid range and does not call provider RPC", async () => {
98 const getTurnDiff = vi.fn().mockResolvedValue({ diff: "patch" });
99 const getFullThreadDiff = vi.fn().mockResolvedValue({ diff: "patch" });
100 mockNativeApi({ getTurnDiff, getFullThreadDiff });
101
102 const options = checkpointDiffQueryOptions({
103 environmentId,
104 threadId,
105 fromTurnCount: 4,
106 toTurnCount: 3,
107 cacheScope: "turn:invalid",
108 });
109
110 const queryClient = new QueryClient();
111
112 await expect(queryClient.fetchQuery(options)).rejects.toThrow(
113 "Checkpoint diff is unavailable.",
114 );
115 expect(getTurnDiff).not.toHaveBeenCalled();
116 expect(getFullThreadDiff).not.toHaveBeenCalled();
117 });
118
119 it("retries checkpoint-not-ready errors longer than generic failures", () => {
120 const options = checkpointDiffQueryOptions({
121 environmentId,
122 threadId,
123 fromTurnCount: 1,
124 toTurnCount: 2,
125 cacheScope: "turn:abc",
126 });
127 const retry = options.retry;
128 expect(typeof retry).toBe("function");
129 if (typeof retry !== "function") {
130 throw new Error("Expected retry to be a function.");
131 }
132
133 expect(retry(1, new Error("Checkpoint turn count 2 exceeds current turn count 1."))).toBe(true);
134 expect(
135 retry(11, new Error("Filesystem checkpoint is unavailable for turn 2 in thread thread-1.")),
136 ).toBe(true);
137 expect(
138 retry(12, new Error("Filesystem checkpoint is unavailable for turn 2 in thread thread-1.")),
139 ).toBe(false);
140 expect(retry(2, new Error("Something else failed."))).toBe(true);
141 expect(retry(3, new Error("Something else failed."))).toBe(false);
142 });
143
144 it("backs off longer for checkpoint-not-ready errors", () => {
145 const options = checkpointDiffQueryOptions({
146 environmentId,
147 threadId,
148 fromTurnCount: 1,
149 toTurnCount: 2,
150 cacheScope: "turn:abc",
151 });
152 const retryDelay = options.retryDelay;
153 expect(typeof retryDelay).toBe("function");
154 if (typeof retryDelay !== "function") {
155 throw new Error("Expected retryDelay to be a function.");
156 }
157
158 const checkpointDelay = retryDelay(
159 4,
160 new Error("Checkpoint turn count 2 exceeds current turn count 1."),
161 );
162 const genericDelay = retryDelay(4, new Error("Network failure"));
163
164 expect(typeof checkpointDelay).toBe("number");
165 expect(typeof genericDelay).toBe("number");
166 expect((checkpointDelay ?? 0) > (genericDelay ?? 0)).toBe(true);
167 });
168});