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

revisionedQuery.test.ts

237 lines7.8 KB

1import * as Effect from "effect/Effect";
2import { Atom, AtomRegistry } from "effect/reactivity";
3import { describe, expect, it } from "vite-plus/test";
4​
5import { advanceWorkspaceVisit, workspaceRevisionOf } from "../hooks/useWorkspaceRevision";
6import { revisionedQuery } from "./revisionedQuery";
7​
8// A stand-in query that counts its fetches.
9function setup(readQuery: (cache: Atom.Atom<number>) => Atom.Atom<number> = (cache) => cache) {
10 let fetches = 0;
11 // Node removal runs on the registry's scheduler; releasing flushes it so the
12 // next mount of the same revision builds a fresh node, as a React remount does.
13 const scheduled: Array<() => void> = [];
14 const registry = AtomRegistry.make({
15 scheduleTask: (task) => {
16 scheduled.push(task);
17 return () => {};
18 },
19 });
20 const query = readQuery(
21 Atom.make(() => {
22 fetches += 1;
23 return fetches;
24 }).pipe(Atom.keepAlive),
25 );
26 const mount = async (
27 revision: string | null | undefined,
28 options?: Parameters<typeof revisionedQuery>[2],
29 ) => {
30 const release = registry.mount(revisionedQuery(query, revision, options));
31 // A stale read refetches in a microtask, after the render that read it.
32 await Promise.resolve();
33 return () => {
34 release();
35 for (const task of scheduled.splice(0)) task();
36 };
37 };
38 return { registry, query, mount, fetches: () => fetches };
40​
41describe("revisionedQuery", () => {
42 it("refetches once per new revision, not on the first read or a remount", async () => {
43 const { mount, fetches } = setup();
44​
45 (await mount("mutation-1"))();
46 expect(fetches()).toBe(1);
47​
48 (await mount("mutation-1"))();
49 expect(fetches()).toBe(1);
50​
51 (await mount("mutation-2"))();
52 expect(fetches()).toBe(2);
53 });
54​
55 it("does not ping-pong while readers of the old and new revision overlap", async () => {
56 const { mount, fetches } = setup();
57​
58 const releaseOld = await mount("mutation-1");
59 const releaseNew = await mount("mutation-2");
60 expect(fetches()).toBe(2);
61 releaseOld();
62 releaseNew();
63 });
64​
65 it("refetches when an earlier revision is read again", async () => {
66 const { mount, fetches } = setup();
67​
68 (await mount("mutation-1"))();
69 (await mount("mutation-2"))();
70 (await mount("mutation-1"))();
71 expect(fetches()).toBe(3);
72 });
73​
74 it("refetches a query another reader already cached on its first revisioned read", async () => {
75 const { mount, fetches } = setup();
76​
77 (await mount(undefined))();
78 expect(fetches()).toBe(1);
79​
80 (await mount("mutation-1"))();
81 expect(fetches()).toBe(2);
82​
83 (await mount("mutation-1"))();
84 expect(fetches()).toBe(2);
85 });
86​
87 it("counts a query as cached while the atom it reads through keeps the value", async () => {
88 // Like an environment query: its own node goes with its last reader, the cache stays.
89 const { mount, fetches } = setup((cache) =>
90 Atom.transform(cache, (get) => get(cache), { initialValueTarget: cache }),
91 );
92​
93 (await mount(undefined))();
94 (await mount("mutation-1"))();
95 expect(fetches()).toBe(2);
96 });
97​
98 it("does not refetch a cached query on a first read before any workspace change", async () => {
99 const { mount, fetches } = setup();
100​
101 (await mount(undefined))();
102 (await mount(null))();
103 expect(fetches()).toBe(1);
104 });
105​
106 it("leaves the query alone when read without a revision", async () => {
107 const { mount, query, fetches } = setup();
108​
109 (await mount("mutation-1"))();
110 expect(revisionedQuery(query, undefined)).toBe(query);
111 (await mount(undefined))();
112 (await mount("mutation-1"))();
113 expect(fetches()).toBe(1);
114 });
115​
116 it("refetches the query when the reader is refreshed", async () => {
117 const { registry, query, mount, fetches } = setup();
118​
119 const release = await mount("mutation-1");
120 registry.refresh(revisionedQuery(query, "mutation-1"));
121 expect(fetches()).toBe(2);
122 release();
123 });
124​
125 it("drops a pending refetch when the registry is disposed before it runs", async () => {
126 const { registry, query, mount } = setup();
127​
128 (await mount("mutation-1"))();
129 const release = registry.mount(revisionedQuery(query, "mutation-2"));
130 release();
131 registry.dispose();
132​
133 // A refetch that ran now would throw "registry is disposed" as an uncaught error.
134 await Promise.resolve();
135 });
136​
137 it("records a fetch started without a revision under the one it names", async () => {
138 const fresh = setup();
139 (await fresh.mount(undefined, { startedUnder: "visit-b" }))();
140 (await fresh.mount("visit-b"))();
141 expect(fresh.fetches()).toBe(1);
142​
143 // Cached before that read: the revision still refetches it.
144 const cached = setup();
145 (await cached.mount("thread-a-1"))();
146 (await cached.mount(undefined, { startedUnder: "visit-b" }))();
147 expect(cached.fetches()).toBe(1);
148 (await cached.mount("visit-b"))();
149 expect(cached.fetches()).toBe(2);
150 });
151​
152 it("refetches once for two readers of one query, and not when a thread's history loads", async () => {
153 const { mount, fetches } = setup();
154 (await mount("thread-a-1", { detail: "status" }))();
155 (await mount("thread-a-1"))();
156 expect(fetches()).toBe(1);
157​
158 let visit = advanceWorkspaceVisit(
159 null,
160 { threadKey: "thread-b", unloaded: true, mutationId: null },
161 "view",
162 );
163 // The Changes totals, which add status totals of their own, then the Diff panel.
164 const render = async (unloaded: boolean, mutationId: string | null) => {
165 visit = advanceWorkspaceVisit(visit, { threadKey: "thread-b", unloaded, mutationId }, "view");
166 const { revision } = workspaceRevisionOf(visit, mutationId);
167 (await mount(revision, { detail: "status" }))();
168 (await mount(revision))();
169 };
170​
171 await render(true, null);
172 expect(fetches()).toBe(2);
173 await render(false, "thread-b-1");
174 expect(fetches()).toBe(2);
175 await render(false, "thread-b-2");
176 expect(fetches()).toBe(3);
177 });
178​
179 it("refetches when a reader's own detail changes, whatever the other readers read", async () => {
180 const { mount, fetches } = setup();
181​
182 (await mount("mutation-1", { detail: "status-1" }))();
183 (await mount("mutation-1"))();
184 (await mount("mutation-1", { detail: "status-1" }))();
185 expect(fetches()).toBe(1);
186​
187 (await mount("mutation-1", { detail: "status-2" }))();
188 expect(fetches()).toBe(2);
189 (await mount("mutation-1"))();
190 expect(fetches()).toBe(2);
191 });
192​
193 it("leaves a fetch that began after the revision changed to finish", async () => {
194 let fetches = 0;
195 const registry = AtomRegistry.make();
196 const query = Atom.make(
197 Effect.suspend(() => ((fetches += 1) === 1 ? Effect.succeed(fetches) : Effect.never)),
198 ).pipe(Atom.keepAlive);
199​
200 registry.mount(revisionedQuery(query, "mutation-1"));
201 await Promise.resolve();
202 expect(fetches).toBe(1);
203​
204 // Another reader's own refresh lands between the read and its deferred refetch.
205 registry.mount(revisionedQuery(query, "mutation-2"));
206 registry.refresh(query);
207 await Promise.resolve();
208 expect(fetches).toBe(2);
209​
210 // A fetch already in flight when the revision changes started before the change.
211 registry.mount(revisionedQuery(query, "mutation-3"));
212 await Promise.resolve();
213 expect(fetches).toBe(3);
214 });
215​
216 it("does not restart the fetch of a query whose cache is gone", async () => {
217 let fetches = 0;
218 const scheduled: Array<() => void> = [];
219 const registry = AtomRegistry.make({
220 scheduleTask: (task) => {
221 scheduled.push(task);
222 return () => {};
223 },
224 });
225 const query = Atom.make(() => (fetches += 1));
226 const read = async (revision: string) => {
227 const release = registry.mount(revisionedQuery(query, revision));
228 await Promise.resolve();
229 release();
230 for (const task of scheduled.splice(0)) task();
231 };
232​
233 await read("mutation-1");
234 await read("mutation-2");
235 expect(fetches).toBe(2);
236 });
237});