Repository navigation
Expand file tree
/
Copy pathfilesViewedStore.test.ts
More file actions
213 lines (193 loc) · 7.13 KB
/
Copy pathfilesViewedStore.test.ts
File metadata and controls
213 lines (193 loc) · 7.13 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {
EnvironmentId,
ProjectId,
type PullRequestFilesViewedResult,
type PullRequestRef,
} from "@t3tools/contracts";
import { Cause } from "effect";
import { AsyncResult } from "effect/reactivity";
import { describe, expect, it, vi } from "vite-plus/test";
import {
createFilesViewedStore,
type FilesViewedStoreDeps,
type FilesViewedTarget,
} from "./filesViewedStore";
const reference = (number: number): PullRequestRef => ({
projectId: ProjectId.make("project"),
repository: "t3/code",
number,
});
const first: FilesViewedTarget = {
environmentId: EnvironmentId.make("files-viewed"),
reference: reference(1),
};
const second: FilesViewedTarget = { ...first, reference: reference(2) };
const answer = (viewed: boolean): PullRequestFilesViewedResult => ({
files: [{ path: "a.ts", state: viewed ? "viewed" : "unviewed" }],
truncated: false,
});
function setup(initial: PullRequestFilesViewedResult | null = answer(false)) {
vi.useFakeTimers();
const answers = new Map<string, (answer: PullRequestFilesViewedResult | null) => void>();
const settles: Array<(result: AsyncResult.AsyncResult<unknown, unknown>) => void> = [];
const send = vi.fn<FilesViewedStoreDeps["send"]>(
() =>
new Promise((resolve) => {
settles.push((result) => resolve(result as never));
}),
);
const refresh = vi.fn();
const reportFailure = vi.fn();
const store = createFilesViewedStore({
flushDelayMs: 400,
subscribeAnswers: (target, onAnswer) => {
const key = String(target.reference.number);
answers.set(key, onAnswer);
onAnswer(initial);
return () => answers.delete(key);
},
send,
refresh,
reportFailure,
});
const answerFor = (target: FilesViewedTarget, next: PullRequestFilesViewedResult | null) =>
answers.get(String(target.reference.number))?.(next);
return {
store,
send,
refresh,
reportFailure,
settles,
answerFor,
answers,
[Symbol.dispose]: () => vi.useRealTimers(),
};
}
describe("createFilesViewedStore", () => {
it("batches presses and holds them until a read that could have seen them", async () => {
using t = setup();
const scope = t.store.scope(first);
const release = scope.subscribe(() => {});
scope.setViewed("a.ts", true);
expect(scope.getSnapshot().get("a.ts")).toBe(true);
await vi.advanceTimersByTimeAsync(400);
expect(t.send).toHaveBeenCalledTimes(1);
expect(t.send.mock.calls[0]?.[1]).toEqual([{ path: "a.ts", viewed: true }]);
t.settles[0]?.(AsyncResult.success(undefined));
await vi.advanceTimersByTimeAsync(0);
expect(t.refresh).toHaveBeenCalledWith(first);
expect(scope.getSnapshot().get("a.ts")).toBe(true);
// A push landed between the write and the next read: the host's answer wins.
t.answerFor(first, {
files: [{ path: "a.ts", state: "dismissed" }],
truncated: false,
});
expect(scope.getSnapshot().size).toBe(0);
release();
});
it("puts a failed press back and reports it", async () => {
using t = setup();
const scope = t.store.scope(first);
const release = scope.subscribe(() => {});
scope.setViewed("a.ts", true);
await vi.advanceTimersByTimeAsync(400);
t.settles[0]?.(AsyncResult.failure(Cause.fail("nope")));
await vi.advanceTimersByTimeAsync(0);
expect(scope.getSnapshot().size).toBe(0);
expect(t.reportFailure).toHaveBeenCalledTimes(1);
expect(t.refresh).toHaveBeenCalledWith(first);
release();
});
it("refreshes the answer for a press that lands after its scope was left", async () => {
using t = setup();
const left = t.store.scope(first);
const release = left.subscribe(() => {});
left.setViewed("a.ts", true);
release();
expect(t.send).toHaveBeenCalledTimes(1);
t.settles[0]?.(AsyncResult.success(undefined));
await vi.advanceTimersByTimeAsync(0);
expect(t.refresh).toHaveBeenCalledWith(first);
});
it("forgets a scope's handle once its last reader leaves", () => {
using t = setup();
const handle = t.store.scope(first);
const release = handle.subscribe(() => {});
expect(t.store.scope(first)).toBe(handle);
release();
expect(t.store.scope(first)).not.toBe(handle);
});
it("keeps the handle and the debounce across a release and re-subscribe through it", async () => {
using t = setup();
const handle = t.store.scope(first);
// StrictMode subscribes, releases, then subscribes again through the same handle.
handle.subscribe(() => {})();
const release = handle.subscribe(() => {});
expect(t.store.scope(first)).toBe(handle);
handle.setViewed("a.ts", true);
expect(t.store.scope(first)).toBe(handle);
expect(t.send).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(400);
expect(t.send).toHaveBeenCalledTimes(1);
release();
});
it("does not keep a handle no reader subscribed through", () => {
using t = setup();
const discarded = t.store.scope(first);
const committed = t.store.scope(first);
expect(committed).not.toBe(discarded);
const release = committed.subscribe(() => {});
expect(t.store.scope(first)).toBe(committed);
release();
});
it("sends the last presses to the scope being left, and ignores their answer", async () => {
using t = setup();
const left = t.store.scope(first);
const releaseLeft = left.subscribe(() => {});
left.setViewed("a.ts", true);
releaseLeft();
const entered = t.store.scope(second);
const releaseEntered = entered.subscribe(() => {});
expect(t.send).toHaveBeenCalledTimes(1);
expect(t.send.mock.calls[0]?.[0]).toEqual(first);
expect(t.answers.has("1")).toBe(false);
expect(left.getSnapshot().size).toBe(0);
t.settles[0]?.(AsyncResult.failure(Cause.fail("nope")));
await vi.advanceTimersByTimeAsync(400);
expect(t.reportFailure).not.toHaveBeenCalled();
expect(t.refresh).toHaveBeenCalledWith(first);
expect(t.send).toHaveBeenCalledTimes(1);
releaseEntered();
});
it("keeps the scope while another reader still holds it", async () => {
using t = setup();
const scope = t.store.scope(first);
const releaseA = scope.subscribe(() => {});
const releaseB = scope.subscribe(() => {});
scope.setViewed("a.ts", true);
releaseA();
expect(t.send).not.toHaveBeenCalled();
expect(scope.getSnapshot().get("a.ts")).toBe(true);
await vi.advanceTimersByTimeAsync(400);
expect(t.send).toHaveBeenCalledTimes(1);
releaseB();
});
it("holds a queued press over an answer that cannot have seen it", async () => {
using t = setup();
const scope = t.store.scope(first);
const release = scope.subscribe(() => {});
scope.setViewed("a.ts", true);
const agreeing = answer(true);
t.answerFor(first, agreeing);
expect(scope.getSnapshot().get("a.ts")).toBe(true);
await vi.advanceTimersByTimeAsync(400);
t.settles[0]?.(AsyncResult.success(undefined));
await vi.advanceTimersByTimeAsync(0);
// The same answer published again is not a new read.
t.answerFor(first, agreeing);
expect(scope.getSnapshot().get("a.ts")).toBe(true);
t.answerFor(first, answer(false));
expect(scope.getSnapshot().size).toBe(0);
release();
});
});