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 { showThreadUndoNotice, useThreadUndoNotice } from "./showThreadUndoNotice";
6import * as ThreadUndo from "./threadUndo";
7
8beforeEach(() => vi.useFakeTimers());
9afterEach(() => {
10 vi.runAllTimers();
11 vi.useRealTimers();
12 vi.restoreAllMocks();
13});
14
15function setup() {
16 const add = vi.spyOn(toastManager, "add").mockReturnValue("error-toast");
17 const undo = vi.fn(async () => AsyncResult.success(undefined));
18 const claim = ThreadUndo.begin("pin", "env/thread");
19 const options = { action: "Unpinned" as const, failureTitle: "Restore failed", undo, claim };
20 return { add, undo, claim, options };
21}
22
23function notice() {
24 const value = useThreadUndoNotice.getState().notice;
25 if (!value) throw new Error("Undo notice is missing");
26 return value;
27}
28
29describe("thread undo notice", () => {
30 it("drops invalidated claims immediately and rejects a captured stale undo", async () => {
31 const { undo, options } = setup();
32 showThreadUndoNotice(options);
33 const stale = notice();
34 ThreadUndo.invalidate("pin", "env/thread");
35 expect(useThreadUndoNotice.getState().notice).toBeNull();
36 showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/thread") });
37 await stale.undo();
38 expect(undo).not.toHaveBeenCalled();
39 await notice().undo();
40 expect(undo).toHaveBeenCalledOnce();
41 });
42});