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