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

filesViewedStore.test.ts

215 lines7.2 KB

1import {
2 EnvironmentId,
3 ProjectId,
4 type PullRequestFilesViewedResult,
5 type PullRequestRef,
6} from "@t3tools/contracts";
7import { Cause } from "effect";
8import { AsyncResult } from "effect/reactivity";
9import { describe, expect, it, vi } from "vite-plus/test";
10​
11import {
12 createFilesViewedStore,
13 type FilesViewedStoreDeps,
14 type FilesViewedTarget,
15} from "./filesViewedStore";
16​
17const reference = (number: number): PullRequestRef => ({
18 projectId: ProjectId.make("project"),
19 repository: "t3/code",
20 number,
21});
22​
23const first: FilesViewedTarget = {
24 environmentId: EnvironmentId.make("files-viewed"),
25 reference: reference(1),
26};
27const second: FilesViewedTarget = { ...first, reference: reference(2) };
28​
29const answer = (viewed: boolean): PullRequestFilesViewedResult => ({
30 files: [{ path: "a.ts", state: viewed ? "viewed" : "unviewed" }],
31 truncated: false,
32});
33​
34function setup(initial: PullRequestFilesViewedResult | null = answer(false)) {
35 vi.useFakeTimers();
36 const answers = new Map<string, (answer: PullRequestFilesViewedResult | null) => void>();
37 const settles: Array<(result: AsyncResult.AsyncResult<unknown, unknown>) => void> = [];
38 const send = vi.fn<FilesViewedStoreDeps["send"]>(
39 () =>
40 new Promise((resolve) => {
41 settles.push((result) => resolve(result as never));
42 }),
43 );
44 const refresh = vi.fn();
45 const reportFailure = vi.fn();
46 const store = createFilesViewedStore({
47 flushDelayMs: 400,
48 subscribeAnswers: (target, onAnswer) => {
49 const key = String(target.reference.number);
50 answers.set(key, onAnswer);
51 onAnswer(initial);
52 return () => answers.delete(key);
53 },
54 send,
55 refresh,
56 reportFailure,
57 });
58 const answerFor = (target: FilesViewedTarget, next: PullRequestFilesViewedResult | null) =>
59 answers.get(String(target.reference.number))?.(next);
60 return {
61 store,
62 send,
63 refresh,
64 reportFailure,
65 settles,
66 answerFor,
67 answers,
68 [Symbol.dispose]: () => vi.useRealTimers(),
69 };
71​
72describe("createFilesViewedStore", () => {
73 it("batches presses and holds them until a read that could have seen them", async () => {
74 using t = setup();
75 const scope = t.store.scope(first);
76 const release = scope.subscribe(() => {});
77 scope.setViewed("a.ts", true, "shown-a");
78 expect(scope.getSnapshot().get("a.ts")).toBe(true);
79 await vi.advanceTimersByTimeAsync(400);
80 expect(t.send).toHaveBeenCalledTimes(1);
81 expect(t.send.mock.calls[0]?.[1]).toEqual([
82 { path: "a.ts", viewed: true, revision: "shown-a" },
83 ]);
84​
85 t.settles[0]?.(AsyncResult.success(undefined));
86 await vi.advanceTimersByTimeAsync(0);
87 expect(t.refresh).toHaveBeenCalledWith(first);
88 expect(scope.getSnapshot().get("a.ts")).toBe(true);
89​
90 // A push landed between the write and the next read: the host's answer wins.
91 t.answerFor(first, {
92 files: [{ path: "a.ts", state: "dismissed" }],
93 truncated: false,
94 });
95 expect(scope.getSnapshot().size).toBe(0);
96 release();
97 });
98​
99 it("puts a failed press back and reports it", async () => {
100 using t = setup();
101 const scope = t.store.scope(first);
102 const release = scope.subscribe(() => {});
103 scope.setViewed("a.ts", true);
104 await vi.advanceTimersByTimeAsync(400);
105 t.settles[0]?.(AsyncResult.failure(Cause.fail("nope")));
106 await vi.advanceTimersByTimeAsync(0);
107 expect(scope.getSnapshot().size).toBe(0);
108 expect(t.reportFailure).toHaveBeenCalledTimes(1);
109 expect(t.refresh).toHaveBeenCalledWith(first);
110 release();
111 });
112​
113 it("refreshes the answer for a press that lands after its scope was left", async () => {
114 using t = setup();
115 const left = t.store.scope(first);
116 const release = left.subscribe(() => {});
117 left.setViewed("a.ts", true);
118 release();
119 expect(t.send).toHaveBeenCalledTimes(1);
120​
121 t.settles[0]?.(AsyncResult.success(undefined));
122 await vi.advanceTimersByTimeAsync(0);
123 expect(t.refresh).toHaveBeenCalledWith(first);
124 });
125​
126 it("forgets a scope's handle once its last reader leaves", () => {
127 using t = setup();
128 const handle = t.store.scope(first);
129 const release = handle.subscribe(() => {});
130 expect(t.store.scope(first)).toBe(handle);
131 release();
132 expect(t.store.scope(first)).not.toBe(handle);
133 });
134​
135 it("keeps the handle and the debounce across a release and re-subscribe through it", async () => {
136 using t = setup();
137 const handle = t.store.scope(first);
138 // StrictMode subscribes, releases, then subscribes again through the same handle.
139 handle.subscribe(() => {})();
140 const release = handle.subscribe(() => {});
141 expect(t.store.scope(first)).toBe(handle);
142​
143 handle.setViewed("a.ts", true);
144 expect(t.store.scope(first)).toBe(handle);
145 expect(t.send).not.toHaveBeenCalled();
146 await vi.advanceTimersByTimeAsync(400);
147 expect(t.send).toHaveBeenCalledTimes(1);
148 release();
149 });
150​
151 it("does not keep a handle no reader subscribed through", () => {
152 using t = setup();
153 const discarded = t.store.scope(first);
154 const committed = t.store.scope(first);
155 expect(committed).not.toBe(discarded);
156 const release = committed.subscribe(() => {});
157 expect(t.store.scope(first)).toBe(committed);
158 release();
159 });
160​
161 it("sends the last presses to the scope being left, and ignores their answer", async () => {
162 using t = setup();
163 const left = t.store.scope(first);
164 const releaseLeft = left.subscribe(() => {});
165 left.setViewed("a.ts", true);
166 releaseLeft();
167 const entered = t.store.scope(second);
168 const releaseEntered = entered.subscribe(() => {});
169​
170 expect(t.send).toHaveBeenCalledTimes(1);
171 expect(t.send.mock.calls[0]?.[0]).toEqual(first);
172 expect(t.answers.has("1")).toBe(false);
173 expect(left.getSnapshot().size).toBe(0);
174​
175 t.settles[0]?.(AsyncResult.failure(Cause.fail("nope")));
176 await vi.advanceTimersByTimeAsync(400);
177 expect(t.reportFailure).not.toHaveBeenCalled();
178 expect(t.refresh).toHaveBeenCalledWith(first);
179 expect(t.send).toHaveBeenCalledTimes(1);
180 releaseEntered();
181 });
182​
183 it("keeps the scope while another reader still holds it", async () => {
184 using t = setup();
185 const scope = t.store.scope(first);
186 const releaseA = scope.subscribe(() => {});
187 const releaseB = scope.subscribe(() => {});
188 scope.setViewed("a.ts", true);
189 releaseA();
190 expect(t.send).not.toHaveBeenCalled();
191 expect(scope.getSnapshot().get("a.ts")).toBe(true);
192 await vi.advanceTimersByTimeAsync(400);
193 expect(t.send).toHaveBeenCalledTimes(1);
194 releaseB();
195 });
196​
197 it("holds a queued press over an answer that cannot have seen it", async () => {
198 using t = setup();
199 const scope = t.store.scope(first);
200 const release = scope.subscribe(() => {});
201 scope.setViewed("a.ts", true);
202 const agreeing = answer(true);
203 t.answerFor(first, agreeing);
204 expect(scope.getSnapshot().get("a.ts")).toBe(true);
205 await vi.advanceTimersByTimeAsync(400);
206 t.settles[0]?.(AsyncResult.success(undefined));
207 await vi.advanceTimersByTimeAsync(0);
208 // The same answer published again is not a new read.
209 t.answerFor(first, agreeing);
210 expect(scope.getSnapshot().get("a.ts")).toBe(true);
211 t.answerFor(first, answer(false));
212 expect(scope.getSnapshot().size).toBe(0);
213 release();
214 });
215});