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