Open History to see the last commit
1import { expect, it, vi } from "vite-plus/test";
2
3import { createNativeReviewDiffSourceLoader } from "./nativeReviewDiffSource";
4import * as DiffHighlightTrace from "./nativeReviewDiffTrace";
5import type { NativeReviewDiffSource } from "./nativeReviewDiffTypes";
6
7const SOURCE = { oldContents: "const before = true;", newContents: "const after = false;" };
8
9vi.mock("expo-file-system", () => ({
10 Paths: { document: "test-documents" },
11 File: class {
12 create() {}
13 async write() {}
14 },
15}));
16
17it("retains fetched stress sources above 500k characters within the aggregate budget", async () => {
18 const source = { oldContents: "", newContents: "x".repeat(3_270_000) };
19 const fetch = vi.fn(async () => source);
20 const load = createNativeReviewDiffSourceLoader(fetch);
21 expect(await load("generated.c")).toBe(source);
22 expect(await load("generated.c")).toBe(source);
23 expect(fetch).toHaveBeenCalledTimes(1);
24});
25
26it("evicts source text by aggregate size and returns an over-budget response uncached", async () => {
27 const source = { oldContents: "", newContents: "x".repeat(4_500_000) };
28 const huge = { oldContents: "", newContents: "x".repeat(9_000_000) };
29 const fetch = vi.fn(async (id: string) => (id === "huge" ? huge : source));
30 const load = createNativeReviewDiffSourceLoader(fetch);
31 expect(await load("first")).toBe(source);
32 expect(await load("second")).toBe(source);
33 expect(await load("first")).toBe(source);
34 expect(fetch).toHaveBeenCalledTimes(3);
35 expect(await load("huge")).toBe(huge);
36 expect(await load("huge")).toBe(huge);
37 expect(fetch).toHaveBeenCalledTimes(5);
38 expect(await load("first")).toBe(source);
39 expect(fetch).toHaveBeenCalledTimes(5);
40});
41
42it("lets later viewport requests retry a failed source fetch", async () => {
43 const fetch = vi.fn().mockRejectedValueOnce(new Error("Offline")).mockResolvedValueOnce(SOURCE);
44 const load = createNativeReviewDiffSourceLoader(fetch);
45 await expect(load("retry.ts")).rejects.toThrow("Offline");
46 expect(await load("retry.ts")).toBe(SOURCE);
47});
48
49const failureKeys = [
50 ["string", () => "retry.ts"],
51 ["number", () => 1],
52 ["boolean", () => false],
53 ["bigint", () => 1n],
54 ["symbol", () => Symbol("retry.ts")],
55 ["null", () => null],
56 ["undefined", () => undefined],
57 ["object", () => ({ id: "retry.ts" })],
58 ["function", () => () => SOURCE],
59] as const;
60
61it.each(failureKeys)(
62 "recovers after two transient failures on a later load for a %s key",
63 async (_name, createKey) => {
64 const key = createKey();
65 const fetch = vi
66 .fn()
67 .mockRejectedValueOnce(new Error("Offline"))
68 .mockRejectedValueOnce(new Error("Still offline"))
69 .mockResolvedValue(SOURCE);
70 const load = createNativeReviewDiffSourceLoader(fetch);
71 await expect(load(key)).rejects.toThrow("Offline");
72 await expect(load(key)).rejects.toThrow("Still offline");
73 expect(fetch).toHaveBeenCalledTimes(2);
74 expect(await load(key)).toBe(SOURCE);
75 expect(await load(key)).toBe(SOURCE);
76 expect(fetch).toHaveBeenCalledTimes(3);
77 },
78);
79
80it.each(failureKeys)(
81 "remembers permanent unavailability for a %s key",
82 async (_name, createKey) => {
83 const key = createKey();
84 const fetch = vi.fn(async () => {
85 throw { reason: "unsupported" };
86 });
87 const load = createNativeReviewDiffSourceLoader(fetch);
88 expect(await load(key)).toBeNull();
89 expect(await load(key)).toBeNull();
90 expect(fetch).toHaveBeenCalledOnce();
91 },
92);
93
94it.each(failureKeys)(
95 "resets retries after success and eviction for a %s key",
96 async (_name, createKey) => {
97 const key = createKey();
98 const fetch = vi
99 .fn()
100 .mockRejectedValueOnce(new Error("Offline"))
101 .mockResolvedValueOnce(SOURCE)
102 .mockResolvedValueOnce({ oldContents: "", newContents: "x".repeat(8_000_000) })
103 .mockRejectedValueOnce(new Error("Offline again"))
104 .mockResolvedValueOnce(SOURCE);
105 const load = createNativeReviewDiffSourceLoader(fetch);
106 await expect(load(key)).rejects.toThrow("Offline");
107 expect(await load(key)).toBe(SOURCE);
108 await load({ id: "evict" });
109 await expect(load(key)).rejects.toThrow("Offline again");
110 expect(await load(key)).toBe(SOURCE);
111 expect(fetch).toHaveBeenCalledTimes(5);
112 },
113);
114
115it("shares an in-flight function request while one subscriber cancels", async () => {
116 const started = Promise.withResolvers<void>();
117 const response = Promise.withResolvers<NativeReviewDiffSource>();
118 let calls = 0;
119 const request = () => {
120 calls++;
121 started.resolve();
122 return response.promise;
123 };
124 const load = createNativeReviewDiffSourceLoader((sourceRequest: typeof request) =>
125 sourceRequest(),
126 );
127 const controller = new AbortController();
128 const first = load(request, controller.signal);
129 await started.promise;
130 const second = load(request);
131 controller.abort();
132 expect(await first).toBeNull();
133 response.resolve(SOURCE);
134 expect(await second).toBe(SOURCE);
135 expect(await load(request)).toBe(SOURCE);
136 expect(calls).toBe(1);
137});
138
139it("serializes RPCs and drops cancelled queued files", async () => {
140 const started = Promise.withResolvers<void>();
141 const response = Promise.withResolvers<NativeReviewDiffSource>();
142 const fetch = vi.fn((file: string) => {
143 if (file !== "active.ts") return Promise.resolve(SOURCE);
144 started.resolve();
145 return response.promise;
146 });
147 const load = createNativeReviewDiffSourceLoader(fetch);
148 const active = load("active.ts");
149 await started.promise;
150 const controller = new AbortController();
151 const queued = load("cancelled.ts", controller.signal);
152 controller.abort();
153 expect(await queued).toBeNull();
154 expect(fetch.mock.calls).toEqual([["active.ts"]]);
155 response.resolve(SOURCE);
156 await active;
157 await load("next.ts");
158 expect(fetch.mock.calls).toEqual([["active.ts"], ["next.ts"]]);
159});
160
161it("starts a shared fresh request for a file whose cancelled request was dropped", async () => {
162 const started = Promise.withResolvers<void>();
163 const response = Promise.withResolvers<NativeReviewDiffSource>();
164 const dropped = Promise.withResolvers<void>();
165 const replacementStarted = Promise.withResolvers<void>();
166 const replacementResponse = Promise.withResolvers<NativeReviewDiffSource>();
167 using _trace = vi.spyOn(DiffHighlightTrace, "traceDiffHighlight").mockImplementation((event) => {
168 if (event === "source-dropped") dropped.resolve();
169 });
170 const fetch = vi.fn((file: string) => {
171 if (file === "active.ts") {
172 started.resolve();
173 return response.promise;
174 }
175 replacementStarted.resolve();
176 return replacementResponse.promise;
177 });
178 const load = createNativeReviewDiffSourceLoader(fetch);
179 const active = load("active.ts");
180 await started.promise;
181 const controller = new AbortController();
182 const queued = load("cancelled.ts", controller.signal);
183 controller.abort();
184 expect(await queued).toBeNull();
185 response.resolve(SOURCE);
186 await dropped.promise;
187 const fresh = load("cancelled.ts");
188 // Settling first means the fresh caller joined the dropped request instead of fetching.
189 await Promise.race([replacementStarted.promise, fresh]);
190 const shared = load("cancelled.ts");
191 replacementResponse.resolve(SOURCE);
192 expect(await fresh).toBe(SOURCE);
193 expect(await shared).toBe(SOURCE);
194 expect(await active).toBe(SOURCE);
195 expect(fetch.mock.calls).toEqual([["active.ts"], ["cancelled.ts"]]);
196});