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

useThreadActions.undo.test.ts

223 lines8.2 KB

1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import { useThreadActions } from "./useThreadActions";
5import { threadEnvironment } from "../state/threads";
6import { toastManager } from "../components/ui/toast";
7import { useThreadUndoNotice } from "./showThreadUndoNotice";
8​
9const commands = vi.hoisted(() => ({
10 pin: vi.fn(),
11 unpin: vi.fn(),
12 archive: vi.fn(),
13 unarchive: vi.fn(),
14 settle: vi.fn(),
15 unsettle: vi.fn(),
16 snooze: vi.fn(),
17 unsnooze: vi.fn(),
18}));
19const router = vi.hoisted(() => ({
20 navigate: vi.fn(async () => {}),
21 state: { matches: [{ params: {} as Record<string, string> }] },
22}));
23vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() }));
24vi.mock("../state/session", async (original) => ({
25 ...(await original<typeof import("../state/session")>()),
26 readEnvironmentScope: () => true,
27}));
28vi.mock("react", async (original) => ({
29 ...(await original<typeof import("react")>()),
30 useCallback: (callback: unknown) => callback,
31 useMemo: (create: () => unknown) => create(),
32 useRef: (value: unknown) => ({ current: value }),
33}));
34vi.mock("@tanstack/react-router", () => ({ useRouter: () => router }));
35vi.mock("./useSettings", () => ({ useClientSettings: () => false }));
36vi.mock("./useHandleNewThread", () => ({ useNewThreadHandler: () => vi.fn() }));
37vi.mock("../composerDraftStore", () => ({ useComposerDraftStore: () => vi.fn() }));
38vi.mock("../terminalUiStateStore", () => ({ useTerminalUiStateStore: () => vi.fn() }));
39vi.mock("../uiStateStore", () => ({ useUiStateStore: () => vi.fn() }));
40vi.mock("../lib/archivedThreadsState", () => ({ refreshArchivedThreadsForEnvironment: vi.fn() }));
41const threadShell = vi.hoisted(() => ({
42 title: "Thread",
43 pinOrderKey: "a0",
44 pinnedAt: null as string | null,
45 snoozedUntil: null as string | null,
46 projectId: "project",
47 environmentId: "undo-env",
48 session: null,
49}));
50vi.mock("../state/entities", async (original) => ({
51 ...(await original<typeof import("../state/entities")>()),
52 readEnvironmentSupportsPinning: () => true,
53 readEnvironmentSupportsPinReorder: () => true,
54 readEnvironmentSupportsSettlement: () => true,
55 readEnvironmentSupportsSnooze: () => true,
56 readThreadShell: () => threadShell,
57}));
58vi.mock("../state/use-atom-command", () => ({
59 useAtomCommand: (command: unknown) => {
60 switch (command) {
61 case threadEnvironment.pin:
62 return commands.pin;
63 case threadEnvironment.unpin:
64 return commands.unpin;
65 case threadEnvironment.archive:
66 return commands.archive;
67 case threadEnvironment.unarchive:
68 return commands.unarchive;
69 case threadEnvironment.settle:
70 return commands.settle;
71 case threadEnvironment.unsettle:
72 return commands.unsettle;
73 case threadEnvironment.snooze:
74 return commands.snooze;
75 case threadEnvironment.unsnooze:
76 return commands.unsnooze;
77 default:
78 return vi.fn();
79 }
80 },
81}));
82​
83const target = {
84 environmentId: EnvironmentId.make("undo-env"),
85 threadId: ThreadId.make("thread"),
86};
87function currentUndo() {
88 const notice = useThreadUndoNotice.getState().notice;
89 expect(notice).not.toBeNull();
90 return notice!.undo;
92​
93beforeEach(() => {
94 vi.useFakeTimers();
95 for (const command of Object.values(commands)) {
96 command.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });
97 }
98 router.navigate.mockClear();
99 router.state.matches[0]!.params = {};
100 threadShell.pinnedAt = null;
101 threadShell.snoozedUntil = null;
102});
103afterEach(() => {
104 vi.runAllTimers();
105 vi.useRealTimers();
106 vi.restoreAllMocks();
107});
108​
109describe("unpin Undo", () => {
110 it("ignores an old notice across hook instances and still restores the latest unpin", async () => {
111 const sidebar = useThreadActions();
112 const header = useThreadActions();
113 await sidebar.unpinThread(target);
114 const staleUndo = currentUndo();
115 await header.pinThread(target, { orderKey: "a1" });
116 await header.unpinThread(target);
117 const latestUndo = currentUndo();
118 await staleUndo();
119 expect(commands.pin).toHaveBeenCalledTimes(1);
120 await latestUndo();
121 expect(commands.pin).toHaveBeenCalledTimes(2);
122 expect(commands.pin).toHaveBeenLastCalledWith({
123 environmentId: target.environmentId,
124 input: { threadId: target.threadId, orderKey: "a0" },
125 });
126 await latestUndo();
127 expect(commands.pin).toHaveBeenCalledTimes(2);
128 });
129});
130​
131describe("archive Undo", () => {
132 it("unarchives and returns to the thread when archiving left it", async () => {
133 const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");
134 router.state.matches[0]!.params = {
135 environmentId: target.environmentId,
136 threadId: target.threadId,
137 };
138 const actions = useThreadActions();
139 await actions.archiveThread(target);
140 expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Archived", count: 1 });
141 expect(add).not.toHaveBeenCalled();
142 await currentUndo()();
143 expect(commands.unarchive).toHaveBeenCalledExactlyOnceWith({
144 environmentId: target.environmentId,
145 input: { threadId: target.threadId },
146 });
147 expect(router.navigate).toHaveBeenCalledWith(
148 expect.objectContaining({
149 to: "/$environmentId/$threadId",
150 params: { environmentId: target.environmentId, threadId: target.threadId },
151 }),
152 );
153 });
154​
155 it("stays put when the archived thread was not open", async () => {
156 const actions = useThreadActions();
157 await actions.archiveThread(target);
158 await currentUndo()();
159 expect(commands.unarchive).toHaveBeenCalledOnce();
160 expect(router.navigate).not.toHaveBeenCalled();
161 });
162​
163 it("shows no Undo when the archive failed", async () => {
164 commands.archive.mockResolvedValue({ _tag: "Failure", cause: new Error("nope") });
165 const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");
166 await useThreadActions().archiveThread(target);
167 expect(add).not.toHaveBeenCalled();
168 });
169});
170​
171describe("settle and snooze Undo", () => {
172 it("un-settles from the notice and expires the Undo after a manual un-settle", async () => {
173 const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");
174 const actions = useThreadActions();
175 await actions.settleThread(target);
176 expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Settled", count: 1 });
177 expect(add).not.toHaveBeenCalled();
178 const undo = currentUndo();
179 await actions.unsettleThread(target);
180 await undo();
181 expect(commands.unsettle).toHaveBeenCalledOnce();
182 });
183​
184 it("re-pins and re-snoozes a thread that settling had cleared", async () => {
185 const snoozedUntil = "2030-01-01T09:00:00.000Z";
186 threadShell.pinnedAt = "2026-01-01T00:00:00.000Z";
187 threadShell.snoozedUntil = snoozedUntil;
188 const actions = useThreadActions();
189 await actions.settleThread(target);
190 await currentUndo()();
191 expect(commands.unsettle).toHaveBeenCalledOnce();
192 expect(commands.pin).toHaveBeenCalledExactlyOnceWith({
193 environmentId: target.environmentId,
194 input: { threadId: target.threadId, orderKey: "a0" },
195 });
196 expect(commands.snooze).toHaveBeenCalledExactlyOnceWith({
197 environmentId: target.environmentId,
198 input: { threadId: target.threadId, snoozedUntil },
199 });
200 });
201​
202 it("expires an older unpin Undo when the thread is settled", async () => {
203 const actions = useThreadActions();
204 await actions.unpinThread(target);
205 const staleUnpinUndo = currentUndo();
206 await actions.settleThread(target);
207 await staleUnpinUndo();
208 expect(commands.pin).not.toHaveBeenCalled();
209 });
210​
211 it("wakes the thread from the snooze notice", async () => {
212 const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");
213 const actions = useThreadActions();
214 await actions.snoozeThread(target, new Date(Date.now() + 60_000).toISOString());
215 expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Snoozed", count: 1 });
216 expect(add).not.toHaveBeenCalled();
217 await currentUndo()();
218 expect(commands.unsnooze).toHaveBeenCalledExactlyOnceWith({
219 environmentId: target.environmentId,
220 input: { threadId: target.threadId, reason: "user" },
221 });
222 });
223});