Repository navigation
Expand file tree
/
Copy pathuseThreadActions.undo.test.ts
More file actions
369 lines (350 loc) · 14 KB
/
Copy pathuseThreadActions.undo.test.ts
File metadata and controls
369 lines (350 loc) · 14 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { useThreadActions } from "./useThreadActions";
import { threadEnvironment } from "../state/threads";
import { toastManager } from "../components/ui/toast";
import { useThreadUndoNotice } from "./showThreadUndoNotice";
const commands = vi.hoisted(() => ({
pin: vi.fn(),
unpin: vi.fn(),
archive: vi.fn(),
unarchive: vi.fn(),
delete: vi.fn(),
settle: vi.fn(),
unsettle: vi.fn(),
snooze: vi.fn(),
unsnooze: vi.fn(),
}));
const router = vi.hoisted(() => ({
navigate: vi.fn(async () => {}),
state: { location: { href: "/" }, matches: [{ params: {} as Record<string, string> }] },
}));
vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() }));
vi.mock("../state/session", async (original) => ({
...(await original<typeof import("../state/session")>()),
readEnvironmentScope: () => true,
}));
vi.mock("react", async (original) => ({
...(await original<typeof import("react")>()),
useCallback: (callback: unknown) => callback,
useMemo: (create: () => unknown) => create(),
useRef: (value: unknown) => ({ current: value }),
}));
vi.mock("@tanstack/react-router", () => ({ useRouter: () => router }));
vi.mock("./useSettings", () => ({ useClientSettings: () => false }));
vi.mock("./useHandleNewThread", () => ({
useNewThreadHandler: () => async () => {
router.state.location.href = "/draft/project";
},
}));
vi.mock("../composerDraftStore", () => ({ useComposerDraftStore: () => vi.fn() }));
vi.mock("../lib/composerDraftUploads", () => ({ releaseComposerDraftUploads: vi.fn() }));
vi.mock("../terminalUiStateStore", () => ({ useTerminalUiStateStore: () => vi.fn() }));
vi.mock("../uiStateStore", () => ({ useUiStateStore: () => vi.fn() }));
vi.mock("../lib/archivedThreadsState", () => ({ refreshArchivedThreadsForEnvironment: vi.fn() }));
const threadShell = vi.hoisted(() => ({
title: "Thread",
pinOrderKey: "a0",
pinnedAt: null as string | null,
snoozedUntil: null as string | null,
projectId: "project",
environmentId: "undo-env",
session: null,
}));
// Archived rows have no loaded shell, so deleting one takes the unresolved path.
const shellLoaded = vi.hoisted(() => ({ current: true }));
vi.mock("../state/entities", async (original) => ({
...(await original<typeof import("../state/entities")>()),
readEnvironmentServerConfigLoaded: () => true,
readThreadShell: () => (shellLoaded.current ? threadShell : null),
}));
vi.mock("../state/use-atom-command", () => ({
useAtomCommand: (command: unknown) => {
switch (command) {
case threadEnvironment.pin:
return commands.pin;
case threadEnvironment.unpin:
return commands.unpin;
case threadEnvironment.archive:
return commands.archive;
case threadEnvironment.unarchive:
return commands.unarchive;
case threadEnvironment.delete:
return commands.delete;
case threadEnvironment.settle:
return commands.settle;
case threadEnvironment.unsettle:
return commands.unsettle;
case threadEnvironment.snooze:
return commands.snooze;
case threadEnvironment.unsnooze:
return commands.unsnooze;
default:
return vi.fn();
}
},
}));
const target = {
environmentId: EnvironmentId.make("undo-env"),
threadId: ThreadId.make("thread"),
};
function currentUndo() {
const notice = useThreadUndoNotice.getState().notice;
expect(notice).not.toBeNull();
return notice!.undo;
}
beforeEach(() => {
vi.useFakeTimers();
for (const command of Object.values(commands)) {
command.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });
}
router.navigate.mockClear();
router.state.location.href = "/";
router.state.matches[0]!.params = {};
threadShell.pinnedAt = null;
threadShell.snoozedUntil = null;
shellLoaded.current = true;
});
afterEach(() => {
vi.runAllTimers();
vi.useRealTimers();
vi.restoreAllMocks();
});
describe("concurrent thread action Undo", () => {
it.each([
["settleThread", "settle", "unsettle", "Settled"],
["snoozeThread", "snooze", "unsnooze", "Snoozed"],
["unpinThread", "unpin", "pin", "Unpinned"],
["archiveThread", "archive", "unarchive", "Archived"],
] as const)(
"%s retains one undo for concurrent successful requests",
async (handler, action, inverse, notice) => {
const actions = useThreadActions();
const run = () =>
handler === "snoozeThread"
? actions.snoozeThread(target, "2099-01-01T09:00:00.000Z")
: actions[handler](target);
await Promise.all([run(), run()]);
expect(commands[action]).toHaveBeenCalledTimes(2);
expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: notice, count: 1 });
await currentUndo()();
expect(commands[inverse]).toHaveBeenCalledOnce();
},
);
});
describe("unpin Undo", () => {
it("restores the original snooze after restoring the pin", async () => {
threadShell.pinnedAt = "2026-01-01T00:00:00.000Z";
threadShell.snoozedUntil = "2030-01-01T09:00:00.000Z";
await useThreadActions().unpinThread(target);
await currentUndo()();
expect(commands.pin).toHaveBeenCalledExactlyOnceWith({
environmentId: target.environmentId,
input: { threadId: target.threadId, orderKey: "a0" },
});
expect(commands.snooze).toHaveBeenCalledExactlyOnceWith({
environmentId: target.environmentId,
input: { threadId: target.threadId, snoozedUntil: threadShell.snoozedUntil },
});
expect(commands.pin.mock.invocationCallOrder[0]).toBeLessThan(
commands.snooze.mock.invocationCallOrder[0]!,
);
});
it("restores the pin without an already expired snooze", async () => {
threadShell.pinnedAt = "2026-01-01T00:00:00.000Z";
threadShell.snoozedUntil = "2000-01-01T09:00:00.000Z";
await useThreadActions().unpinThread(target);
await currentUndo()();
expect(commands.pin).toHaveBeenCalledOnce();
expect(commands.snooze).not.toHaveBeenCalled();
});
it("ignores an old notice across hook instances and still restores the latest unpin", async () => {
const sidebar = useThreadActions();
const header = useThreadActions();
await sidebar.unpinThread(target);
const staleUndo = currentUndo();
await header.pinThread(target, { orderKey: "a1" });
await header.unpinThread(target);
const latestUndo = currentUndo();
await staleUndo();
expect(commands.pin).toHaveBeenCalledTimes(1);
await latestUndo();
expect(commands.pin).toHaveBeenCalledTimes(2);
expect(commands.pin).toHaveBeenLastCalledWith({
environmentId: target.environmentId,
input: { threadId: target.threadId, orderKey: "a0" },
});
await latestUndo();
expect(commands.pin).toHaveBeenCalledTimes(2);
});
});
describe("archive Undo", () => {
it("unarchives and returns to the thread when archiving left it", async () => {
const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");
router.state.matches[0]!.params = {
environmentId: target.environmentId,
threadId: target.threadId,
};
const actions = useThreadActions();
await actions.archiveThread(target);
expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Archived", count: 1 });
expect(add).not.toHaveBeenCalled();
await currentUndo()();
expect(commands.unarchive).toHaveBeenCalledExactlyOnceWith({
environmentId: target.environmentId,
input: { threadId: target.threadId },
});
expect(router.navigate).toHaveBeenCalledWith(
expect.objectContaining({
to: "/$environmentId/$threadId",
params: { environmentId: target.environmentId, threadId: target.threadId },
}),
);
});
});
describe("archive navigation", () => {
it.each([
{ destination: "/undo-env/thread", expected: "/draft/project" },
{ destination: "/undo-env/other-thread", expected: "/undo-env/other-thread" },
{ destination: "/settings", expected: "/settings" },
])("ends at $expected after opening $destination while archiving", async (route) => {
router.state.location.href = "/undo-env/thread";
router.state.matches[0]!.params = {
environmentId: target.environmentId,
threadId: target.threadId,
};
let finishArchive!: () => void;
commands.archive.mockReturnValue(
new Promise((resolve) => {
finishArchive = () => resolve({ _tag: "Success", value: undefined });
}),
);
const archived = useThreadActions().archiveThread(target);
router.state.location.href = route.destination;
finishArchive();
expect((await archived)._tag).toBe("Success");
expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Archived" });
expect(router.state.location.href).toBe(route.expected);
});
});
describe("settle and snooze Undo", () => {
it("un-settles from the notice and expires the Undo after a manual un-settle", async () => {
const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");
const actions = useThreadActions();
await actions.settleThread(target);
expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Settled", count: 1 });
expect(add).not.toHaveBeenCalled();
const undo = currentUndo();
await actions.unsettleThread(target);
await undo();
expect(commands.unsettle).toHaveBeenCalledOnce();
});
it("re-pins and re-snoozes a thread that settling had cleared", async () => {
const snoozedUntil = "2030-01-01T09:00:00.000Z";
threadShell.pinnedAt = "2026-01-01T00:00:00.000Z";
threadShell.snoozedUntil = snoozedUntil;
const actions = useThreadActions();
await actions.settleThread(target);
await currentUndo()();
expect(commands.unsettle).toHaveBeenCalledOnce();
expect(commands.pin).toHaveBeenCalledExactlyOnceWith({
environmentId: target.environmentId,
input: { threadId: target.threadId, orderKey: "a0" },
});
expect(commands.snooze).toHaveBeenCalledExactlyOnceWith({
environmentId: target.environmentId,
input: { threadId: target.threadId, snoozedUntil },
});
});
it("stops settle restoration if restoring the pin fails", async () => {
threadShell.pinnedAt = "2026-01-01T00:00:00.000Z";
threadShell.snoozedUntil = "2030-01-01T09:00:00.000Z";
commands.pin.mockResolvedValue({ _tag: "Failure", cause: Cause.fail(new Error("Pin failed")) });
vi.spyOn(toastManager, "add").mockReturnValue("toast");
await useThreadActions().settleThread(target);
await currentUndo()();
expect(commands.unsettle).toHaveBeenCalledOnce();
expect(commands.pin).toHaveBeenCalledOnce();
expect(commands.snooze).not.toHaveBeenCalled();
});
it("expires an older unpin Undo when the thread is settled", async () => {
const actions = useThreadActions();
await actions.unpinThread(target);
const staleUnpinUndo = currentUndo();
await actions.settleThread(target);
await staleUnpinUndo();
expect(commands.pin).not.toHaveBeenCalled();
});
// regression: #12972 the sidebar undo notice must reverse a snooze
it("wakes the thread from the snooze notice", async () => {
const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");
const actions = useThreadActions();
await actions.snoozeThread(target, "2100-01-01T00:00:00.000Z");
expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Snoozed", count: 1 });
expect(add).not.toHaveBeenCalled();
await currentUndo()();
expect(commands.unsnooze).toHaveBeenCalledExactlyOnceWith({
environmentId: target.environmentId,
input: { threadId: target.threadId, reason: "user" },
});
});
});
describe("archive and delete Undo expiry", () => {
const otherThread = { environmentId: target.environmentId, threadId: ThreadId.make("other") };
const otherEnvironment = {
environmentId: EnvironmentId.make("other-env"),
threadId: target.threadId,
};
const failure = { _tag: "Failure", cause: Cause.fail(new Error("Request failed")) };
describe.each(["archiveThread", "deleteThread"] as const)("%s", (lifecycle) => {
it.each([
["settleThread", "unsettle"],
["snoozeThread", "unsnooze"],
["unpinThread", "pin"],
] as const)(
"expires an earlier %s Undo for that thread only, across hook instances",
async (handler, inverse) => {
const sidebar = useThreadActions();
const header = useThreadActions();
for (const ref of [otherThread, otherEnvironment, target]) {
await (handler === "snoozeThread"
? sidebar.snoozeThread(ref, "2099-01-01T09:00:00.000Z")
: sidebar[handler](ref));
}
const staleUndo = currentUndo();
await header[lifecycle](target);
await staleUndo();
expect(commands[inverse]).toHaveBeenCalledTimes(2);
expect(commands[inverse]).not.toHaveBeenCalledWith(
expect.objectContaining({
environmentId: target.environmentId,
input: expect.objectContaining({ threadId: target.threadId }),
}),
);
},
);
it("keeps the earlier Undo when the request fails", async () => {
const actions = useThreadActions();
await actions.settleThread(target);
commands[lifecycle === "archiveThread" ? "archive" : "delete"].mockResolvedValue(failure);
await actions[lifecycle](target);
expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Settled" });
await currentUndo()();
expect(commands.unsettle).toHaveBeenCalledOnce();
});
});
it.each([true, false])(
"expires the Archived Undo when deleting with shell loaded: %s",
async (loaded) => {
const actions = useThreadActions();
await actions.archiveThread(target);
const staleUndo = currentUndo();
shellLoaded.current = loaded;
await actions.deleteThread(target);
expect(useThreadUndoNotice.getState().notice).toBeNull();
await staleUndo();
expect(commands.unarchive).not.toHaveBeenCalled();
},
);
});