@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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