Open History to see the last commit
1import { AsyncResult } from "effect/reactivity";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4import { toastManager } from "../components/ui/toast";
5import { announce, cancelAnnouncement } from "../lib/announce";
6import {
7 releaseThreadUndoShortcut,
8 showThreadUndoNotice,
9 threadUndoClaimsShortcut,
10 useThreadUndoNotice,
11} from "./showThreadUndoNotice";
12import * as ThreadUndo from "@t3tools/client-runtime/state/thread-undo";
13
14vi.mock("../lib/announce", () => ({ announce: vi.fn(), cancelAnnouncement: vi.fn() }));
15
16beforeEach(() => vi.useFakeTimers());
17afterEach(() => {
18 vi.runAllTimers();
19 vi.useRealTimers();
20 vi.restoreAllMocks();
21});
22
23function setup() {
24 const add = vi.spyOn(toastManager, "add").mockReturnValue("error-toast");
25 const undo = vi.fn(async () => AsyncResult.success(undefined));
26 const claim = ThreadUndo.begin("pin", "env/thread");
27 const options = { action: "Unpinned" as const, failureTitle: "Restore failed", undo, claim };
28 return { add, undo, claim, options };
29}
30
31function notice() {
32 const value = useThreadUndoNotice.getState().notice;
33 if (!value) throw new Error("Undo notice is missing");
34 return value;
35}
36
37describe("thread undo notice", () => {
38 it("drops invalidated claims immediately and rejects a captured stale undo", async () => {
39 const { undo, options } = setup();
40 showThreadUndoNotice(options);
41 const stale = notice();
42 ThreadUndo.invalidate("pin", "env/thread");
43 expect(useThreadUndoNotice.getState().notice).toBeNull();
44 showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/thread") });
45 await stale.undo();
46 expect(undo).not.toHaveBeenCalled();
47 await notice().undo();
48 expect(undo).toHaveBeenCalledOnce();
49 });
50
51 it("claims mod+z from text fields until the user types, then again for a new notice", () => {
52 const { options } = setup();
53 expect(threadUndoClaimsShortcut()).toBe(false);
54 showThreadUndoNotice(options);
55 expect(threadUndoClaimsShortcut()).toBe(true);
56 releaseThreadUndoShortcut();
57 expect(threadUndoClaimsShortcut()).toBe(false);
58 showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/next") });
59 expect(threadUndoClaimsShortcut()).toBe(true);
60 });
61
62 it("stops claiming mod+z once nothing is left to undo", async () => {
63 const { options } = setup();
64 showThreadUndoNotice(options);
65 await notice().undo();
66 expect(threadUndoClaimsShortcut()).toBe(false);
67 showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/expiring") });
68 vi.advanceTimersByTime(5_000);
69 expect(threadUndoClaimsShortcut()).toBe(false);
70 });
71
72 it("announces each new summary once, and cancels it when the notice clears", async () => {
73 const { options } = setup();
74 vi.mocked(announce).mockClear();
75 vi.mocked(cancelAnnouncement).mockClear();
76 showThreadUndoNotice(options);
77 expect(announce).toHaveBeenCalledExactlyOnceWith(
78 expect.stringMatching(/^Unpinned 1 thread, /),
79 "thread-undo-notice",
80 );
81 ThreadUndo.invalidate("pin", "env/other");
82 expect(announce).toHaveBeenCalledOnce();
83 showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/second") });
84 expect(announce).toHaveBeenLastCalledWith(
85 expect.stringMatching(/^Unpinned 2 threads, /),
86 "thread-undo-notice",
87 );
88 await notice().undo();
89 expect(cancelAnnouncement).toHaveBeenCalledWith("thread-undo-notice");
90 });
91});