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 {
6 releaseThreadUndoShortcut,
7 showThreadUndoNotice,
8 threadUndoClaimsShortcut,
9 useThreadUndoNotice,
10} from "./showThreadUndoNotice";
11import * as ThreadUndo from "./threadUndo";
12
13beforeEach(() => vi.useFakeTimers());
14afterEach(() => {
15 vi.runAllTimers();
16 vi.useRealTimers();
17 vi.restoreAllMocks();
18});
19
20function setup() {
21 const add = vi.spyOn(toastManager, "add").mockReturnValue("error-toast");
22 const undo = vi.fn(async () => AsyncResult.success(undefined));
23 const claim = ThreadUndo.begin("pin", "env/thread");
24 const options = { action: "Unpinned" as const, failureTitle: "Restore failed", undo, claim };
25 return { add, undo, claim, options };
26}
27
28function notice() {
29 const value = useThreadUndoNotice.getState().notice;
30 if (!value) throw new Error("Undo notice is missing");
31 return value;
32}
33
34describe("thread undo notice", () => {
35 it("drops invalidated claims immediately and rejects a captured stale undo", async () => {
36 const { undo, options } = setup();
37 showThreadUndoNotice(options);
38 const stale = notice();
39 ThreadUndo.invalidate("pin", "env/thread");
40 expect(useThreadUndoNotice.getState().notice).toBeNull();
41 showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/thread") });
42 await stale.undo();
43 expect(undo).not.toHaveBeenCalled();
44 await notice().undo();
45 expect(undo).toHaveBeenCalledOnce();
46 });
47
48 it("claims mod+z from text fields until the user types, then again for a new notice", () => {
49 const { options } = setup();
50 expect(threadUndoClaimsShortcut()).toBe(false);
51 showThreadUndoNotice(options);
52 expect(threadUndoClaimsShortcut()).toBe(true);
53 releaseThreadUndoShortcut();
54 expect(threadUndoClaimsShortcut()).toBe(false);
55 showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/next") });
56 expect(threadUndoClaimsShortcut()).toBe(true);
57 });
58
59 it("stops claiming mod+z once nothing is left to undo", async () => {
60 const { options } = setup();
61 showThreadUndoNotice(options);
62 await notice().undo();
63 expect(threadUndoClaimsShortcut()).toBe(false);
64 showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/expiring") });
65 vi.advanceTimersByTime(5_000);
66 expect(threadUndoClaimsShortcut()).toBe(false);
67 });
68});