Skip to content

Build your personal knowledge base with Trilium Notes

29.6 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";

// branches.ts registers two ws.subscribeToMessages callbacks at import time. branches.ts is loaded
// transitively (via app_context -> main_tree_executors) before any top-level spec code runs, so we
// must capture those subscribers through a hoisted ws mock. We still provide
// waitForMaxKnownEntityChangeId so froca keeps working.
const { wsCallbacks } = vi.hoisted(() => ({ wsCallbacks: [] as ((message: any) => Promise<void>)[] }));

vi.mock("./ws.js", () => {
    const logError = (message: string) => console.error(message);
    const logInfo = (message: string) => console.log(message);
    // ws.ts sets these window globals as a side effect; froca.ts relies on the global logError.
    (window as any).logError = logError;
    (window as any).logInfo = logInfo;
    const subscribeToMessages = (cb: (message: any) => Promise<void>) => {
        wsCallbacks.push(cb);
    };
    return {
        default: {
            subscribeToMessages,
            waitForMaxKnownEntityChangeId: async () => {},
            getMaxKnownEntityChangeSyncId: () => 0,
            logError
        },
        subscribeToMessages,
        logError,
        logInfo,
        throwError: (message: string) => {
            throw new Error(message);
        }
    };
});

/** Dispatch a websocket message to every captured subscriber (branches filters by taskType). */
async function dispatchWs(message: any) {
    for (const cb of wsCallbacks) {
        await cb(message);
    }
}

import appContext from "../components/app_context.js";
import FBranch from "../entities/fbranch.js";
import { buildNote } from "../test/easy-froca.js";
import branches from "./branches.js";
import froca from "./froca.js";
import hoistedNoteService from "./hoisted_note.js";
import server from "./server.js";
import toastService from "./toast.js";
import utils from "./utils.js";

// Seed the "root" note so branches whose parent/target is "root" resolve from the froca cache
// instead of triggering the throwing tree/load POST.
buildNote({ id: "root", title: "root" });

/** Register a branch directly in froca with full control over its ids. */
function makeBranch(branchId: string, noteId: string, parentNoteId = "root") {
    const branch = new FBranch(froca, {
        branchId,
        noteId,
        parentNoteId,
        notePosition: 0,
        fromSearchNote: false
    });
    froca.branches[branchId] = branch;
    return branch;
}

beforeEach(() => {
    vi.clearAllMocks();
    // Reset commonly-overridden collaborators back to safe defaults.
    server.put = vi.fn(async () => ({ success: true, message: "" })) as typeof server.put;
    server.post = vi.fn(async () => ({})) as typeof server.post;
    server.remove = vi.fn(async () => ({})) as typeof server.remove;
    toastService.showError = vi.fn();
    toastService.showPersistent = vi.fn();
    toastService.closePersistent = vi.fn();
    hoistedNoteService.getHoistedNoteId = vi.fn(() => "root");
    appContext.tabManager = { getActiveContext: () => undefined } as any;
});

describe("moveBeforeBranch", () => {
    it("filters root/search branches, moves valid ones and bails on server failure", async () => {
        const targetNote = buildNote({ title: "Target" });
        const beforeBranch = makeBranch("before1", targetNote.noteId);
        const a = makeBranch("a1", buildNote({ title: "A" }).noteId);

        // virt- branches and root/hoisted branches are filtered out before the loop.
        makeBranch("rootBranch", "root");

        await expect(branches.moveBeforeBranch(["virt-x", "rootBranch", "a1"], "before1")).resolves.toBe(true);
        expect(server.put).toHaveBeenCalledTimes(1);
        expect(server.put).toHaveBeenCalledWith(`branches/a1/move-before/before1`);
        expect(beforeBranch.noteId).toBe(targetNote.noteId);
        expect(a).toBeDefined();

        // server failure -> error toast and early return
        server.put = vi.fn(async () => ({ success: false, message: "nope" })) as typeof server.put;
        await expect(branches.moveBeforeBranch(["a1"], "before1")).resolves.toBe(false);
        expect(toastService.showError).toHaveBeenCalledWith("nope");
    });

    it("returns early when before-branch is missing", async () => {
        await expect(branches.moveBeforeBranch(["a1"], "does-not-exist")).resolves.toBe(false);
        expect(server.put).not.toHaveBeenCalled();
    });

    it("rejects moving before root or launch bar config", async () => {
        makeBranch("rootDest", "root");
        await expect(branches.moveBeforeBranch(["a1"], "rootDest")).resolves.toBe(false);
        expect(toastService.showError).toHaveBeenCalledTimes(1);

        const lbSpy = vi.spyOn(utils, "isLaunchBarConfig").mockReturnValue(true);
        makeBranch("lbDest", "_lbVisibleLaunchers");
        await expect(branches.moveBeforeBranch(["a1"], "lbDest")).resolves.toBe(false);
        expect(toastService.showError).toHaveBeenCalledTimes(2);
        lbSpy.mockRestore();
    });
});

describe("moveAfterBranch", () => {
    it("moves valid branches in reverse and bails on server failure", async () => {
        const dest = buildNote({ title: "Dest" });
        makeBranch("afterDest", dest.noteId);
        const n1 = buildNote({ title: "N1" });
        const n2 = buildNote({ title: "N2" });
        makeBranch("m1", n1.noteId);
        makeBranch("m2", n2.noteId);

        await expect(branches.moveAfterBranch(["m1", "m2"], "afterDest")).resolves.toBe(true);
        expect(server.put).toHaveBeenCalledTimes(2);
        // reversed order: m2 first, then m1
        expect((server.put as any).mock.calls[0][0]).toBe("branches/m2/move-after/afterDest");
        expect((server.put as any).mock.calls[1][0]).toBe("branches/m1/move-after/afterDest");

        server.put = vi.fn(async () => ({ success: false, message: "fail" })) as typeof server.put;
        await expect(branches.moveAfterBranch(["m1"], "afterDest")).resolves.toBe(false);
        expect(toastService.showError).toHaveBeenCalledWith("fail");
    });

    it("returns early when after-note is missing", async () => {
        await expect(branches.moveAfterBranch(["m1"], "missing")).resolves.toBe(false);
        expect(server.put).not.toHaveBeenCalled();
    });

    it("rejects moving after a forbidden destination note", async () => {
        // dest note id "root" is in the forbidden list
        makeBranch("forbiddenDest", "root");
        await expect(branches.moveAfterBranch(["m1"], "forbiddenDest")).resolves.toBe(false);
        expect(toastService.showError).toHaveBeenCalledTimes(1);
        expect(server.put).not.toHaveBeenCalled();
    });
});

describe("moveToParentNote", () => {
    it("returns early when new parent branch is missing", async () => {
        await expect(branches.moveToParentNote(["m1"], "no-parent")).resolves.toBe(false);
        expect(server.put).not.toHaveBeenCalled();
    });

    it("rejects moving into _lbRoot", async () => {
        makeBranch("lbRootBranch", "_lbRoot");
        await expect(branches.moveToParentNote(["m1"], "lbRootBranch")).resolves.toBe(false);
        expect(toastService.showError).toHaveBeenCalledTimes(1);
        expect(server.put).not.toHaveBeenCalled();
    });

    it("skips missing/hoisted/search-parent branches and moves the valid ones in one request", async () => {
        const parentNote = buildNote({ title: "Parent" });
        makeBranch("destParent", parentNote.noteId);

        // a regular note under root
        const regular = buildNote({ title: "Regular" });
        makeBranch("regularBranch", regular.noteId, "root");
        const regular2 = buildNote({ title: "Regular too" });
        makeBranch("regularBranch2", regular2.noteId, "root");

        // a note whose parent is a search note -> skipped
        const searchParent = buildNote({ title: "Search", type: "search" });
        const childOfSearch = buildNote({ title: "Child" });
        makeBranch("searchChildBranch", childOfSearch.noteId, searchParent.noteId);

        // a hoisted note branch -> skipped
        hoistedNoteService.getHoistedNoteId = vi.fn(() => regular.noteId === "x" ? "" : "hoistedId");
        const hoisted = buildNote({ id: "hoistedId", title: "Hoisted" });
        makeBranch("hoistedBranch", hoisted.noteId, "root");

        await expect(branches.moveToParentNote(["missingBranch", "hoistedBranch", "regularBranch", "searchChildBranch", "regularBranch2"], "destParent", "comp-1"))
            .resolves.toBe(true);

        expect(server.put).toHaveBeenCalledTimes(1);
        expect(server.put).toHaveBeenCalledWith("branches/move-to/destParent",
            { branchIds: [ "regularBranch", "regularBranch2" ] }, "comp-1");
    });

    it("sends nothing when no branch can be moved", async () => {
        const parentNote = buildNote({ title: "Parent3" });
        makeBranch("destParent3", parentNote.noteId);

        await expect(branches.moveToParentNote(["missingBranch"], "destParent3")).resolves.toBe(false);
        expect(server.put).not.toHaveBeenCalled();
    });

    it("shows error and bails when the move fails", async () => {
        const parentNote = buildNote({ title: "Parent2" });
        makeBranch("destParent2", parentNote.noteId);
        const regular = buildNote({ title: "Regular2" });
        makeBranch("regularBranch2", regular.noteId, "root");

        server.put = vi.fn(async () => ({ success: false, message: "boom" })) as typeof server.put;
        await expect(branches.moveToParentNote(["regularBranch2"], "destParent2")).resolves.toBe(false);
        expect(toastService.showError).toHaveBeenCalledWith("boom");
    });
});

describe("deleteNotes", () => {
    it("returns false when nothing remains after filtering root notes", async () => {
        makeBranch("onlyRoot", "root");
        const result = await branches.deleteNotes(["onlyRoot"]);
        expect(result).toBe(false);
    });

    it("returns false when the user cancels the dialog", async () => {
        const note = buildNote({ title: "Del" });
        makeBranch("delBranch", note.noteId, "root");
        appContext.triggerCommand = vi.fn((_name: any, data: any) => {
            data.callback({ proceed: false });
        }) as any;
        const result = await branches.deleteNotes(["delBranch"]);
        expect(result).toBe(false);
    });

    it("deletes a single branch, navigates to parent, and returns true", async () => {
        const note = buildNote({ title: "Del2" });
        makeBranch("delBranch2", note.noteId, "root");
        const note2 = buildNote({ title: "Del2b" });
        makeBranch("delBranch2b", note2.noteId, "root");

        const setNote = vi.fn(async () => {});
        appContext.tabManager = {
            getActiveContext: () => ({ notePathArray: ["root", note.noteId], setNote })
        } as any;
        appContext.triggerCommand = vi.fn((_name: any, data: any) => {
            data.callback({
                proceed: true, deleteAllClones: false, eraseNotes: false, noteCountToDelete: 7
            });
        }) as any;

        // Two branches, one request: the backend deletes the selection in a single transaction.
        const result = await branches.deleteNotes(["delBranch2", "delBranch2b"]);
        expect(result).toBe(true);
        expect(server.post).toHaveBeenCalledTimes(1);
        const [url, body] = (server.post as any).mock.calls[0];
        expect(url).toBe("delete-notes");
        expect(body).toMatchObject({
            branchIdsToDelete: ["delBranch2", "delBranch2b"],
            deleteAllClones: false,
            eraseNotes: false,
            // The dialog's own preview count, so the toast counts towards the number it showed.
            totalCount: 7,
            taskId: expect.any(String)
        });
        // root has no children registered here, so the parent path ("root") is the fallback
        expect(setNote).toHaveBeenCalledWith("root");
    });

    it("asks for all clones, erases without reloading, and tolerates navigation errors", async () => {
        const note = buildNote({ title: "Del3" });
        makeBranch("delBranch3", note.noteId, "root");

        appContext.tabManager = {
            getActiveContext: () => {
                throw new Error("navigation blew up");
            }
        } as any;
        appContext.triggerCommand = vi.fn((_name: any, data: any) => {
            data.callback({ proceed: true, deleteAllClones: true, eraseNotes: true });
        }) as any;
        const reloadSpy = vi.spyOn(utils, "reloadFrontendApp").mockImplementation(() => {});
        const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});

        const result = await branches.deleteNotes(["delBranch3"], false, true, "comp-9");
        expect(result).toBe(true);
        const [, body, componentId] = (server.post as any).mock.calls[0];
        expect(body).toMatchObject({ deleteAllClones: true, eraseNotes: true });
        expect(componentId).toBe("comp-9");
        expect(reloadSpy).not.toHaveBeenCalled();
        expect(errSpy).toHaveBeenCalled();
        reloadSpy.mockRestore();
        errSpy.mockRestore();
    });

    it("skips parent navigation when moveToParent is false", async () => {
        const note = buildNote({ title: "Del4" });
        makeBranch("delBranch4", note.noteId, "root");
        const getActiveContext = vi.fn(() => undefined);
        appContext.tabManager = { getActiveContext } as any;
        appContext.triggerCommand = vi.fn((_name: any, data: any) => {
            data.callback({ proceed: true, deleteAllClones: false, eraseNotes: false });
        }) as any;

        const result = await branches.deleteNotes(["delBranch4"], false, false);
        expect(result).toBe(true);
        // With moveToParent off, the active tab is left alone: the collection views that pass it
        // delete a child of the note they show, so there is nothing to navigate away from.
        expect(getActiveContext).not.toHaveBeenCalled();
    });

});

describe("activateNeighbouringNotePath (via deleteNotes navigation)", () => {
    /** Confirms the delete dialog with the given options and captures where the tab navigates. */
    function confirmDeletion(notePathArray: string[], deleteAllClones = false) {
        const setNote = vi.fn(async () => {});
        appContext.tabManager = {
            getActiveContext: () => ({ notePathArray, setNote })
        } as any;
        appContext.triggerCommand = vi.fn((_name: any, data: any) => {
            data.callback({ proceed: true, deleteAllClones, eraseNotes: false });
        }) as any;
        return setNote;
    }

    it("activates the next sibling, or the previous one for the last note", async () => {
        const parent = buildNote({ title: "P", children: [
            { id: "sibA", title: "A" },
            { id: "sibB", title: "B" },
            { id: "sibC", title: "C" },
            { id: "sibD", title: "D" }
        ] });
        const p = parent.noteId;

        let setNote = confirmDeletion(["root", p, "sibB"]);
        await branches.deleteNotes([`${p}_sibB`]);
        expect(setNote).toHaveBeenCalledWith(`root/${p}/sibC`);

        setNote = confirmDeletion(["root", p, "sibD"]);
        await branches.deleteNotes([`${p}_sibD`]);
        expect(setNote).toHaveBeenCalledWith(`root/${p}/sibC`);

        // The deleted note is an ancestor of the active one: its own siblings are what count.
        buildNote({ id: "grandchild", title: "G" });
        setNote = confirmDeletion(["root", p, "sibA", "grandchild"]);
        await branches.deleteNotes([`${p}_sibA`]);
        expect(setNote).toHaveBeenCalledWith(`root/${p}/sibB`);
    });

    it("skips removed and archived siblings, falling back to the parent", async () => {
        const parent = buildNote({ title: "P2", children: [
            { id: "arcA", title: "A", "#archived": "" },
            { id: "selB", title: "B" },
            { id: "selC", title: "C" }
        ] });
        const p = parent.noteId;

        // B and C are both selected, A is archived: nothing survives, so the parent is the target.
        let setNote = confirmDeletion(["root", p, "selB"]);
        await branches.deleteNotes([`${p}_selB`, `${p}_selC`]);
        expect(setNote).toHaveBeenCalledWith(`root/${p}`);

        // Only B is selected, so C survives.
        setNote = confirmDeletion(["root", p, "selB"]);
        await branches.deleteNotes([`${p}_selB`]);
        expect(setNote).toHaveBeenCalledWith(`root/${p}/selC`);
    });

    it("treats a clone of a deleted note as gone only when all clones are deleted", async () => {
        // X hangs both under P3 (right after B) and under Q; only its Q branch is selected.
        const parent = buildNote({ title: "P3", children: [
            { id: "clB", title: "B" }, { id: "clX", title: "X" }, { id: "clC", title: "C" }
        ] });
        const other = buildNote({ title: "Q" });
        const p = parent.noteId;
        makeBranch("Q_clX", "clX", other.noteId);
        other.addChild("clX", "Q_clX", false);
        froca.getNoteFromCache("clX")?.addParent(other.noteId, "Q_clX", false);

        let setNote = confirmDeletion(["root", p, "clB"], false);
        await branches.deleteNotes([`${p}_clB`, "Q_clX"]);
        expect(setNote).toHaveBeenCalledWith(`root/${p}/clX`);

        setNote = confirmDeletion(["root", p, "clB"], true);
        await branches.deleteNotes([`${p}_clB`, "Q_clX"], true);
        expect(setNote).toHaveBeenCalledWith(`root/${p}/clC`);
    });

    it("never activates the hidden subtree when deleting the last child of root", async () => {
        const root = froca.getNoteFromCache("root");
        for (const noteId of ["rootFirst", "rootLast", "_hidden"]) {
            buildNote({ id: noteId, title: noteId });
            makeBranch(`root_${noteId}`, noteId, "root");
            root?.addChild(noteId, `root_${noteId}`, false);
            froca.getNoteFromCache(noteId)?.addParent("root", `root_${noteId}`, false);
        }
        expect(root?.getChildNoteIds()).toEqual(["rootFirst", "rootLast", "_hidden"]);

        const setNote = confirmDeletion(["root", "rootLast"]);
        await branches.deleteNotes(["root_rootLast"]);
        expect(setNote).toHaveBeenCalledWith("root/rootFirst");
    });

    it("falls back to the parent when its children are not loaded in froca", async () => {
        const note = buildNote({ title: "Lonely" });
        makeBranch("lonelyBranch", note.noteId, "root");
        const setNote = confirmDeletion(["root", note.noteId]);
        await branches.deleteNotes(["lonelyBranch"]);
        expect(setNote).toHaveBeenCalledWith("root");
    });

    it("does not navigate when the deleted note is not on the active path", async () => {
        const note = buildNote({ title: "Off" });
        makeBranch("offBranch", note.noteId, "root");
        const setNote = vi.fn(async () => {});
        appContext.tabManager = {
            getActiveContext: () => ({ notePathArray: ["someOtherNote"], setNote })
        } as any;
        appContext.triggerCommand = vi.fn((_name: any, data: any) => {
            data.callback({ proceed: true, deleteAllClones: false, eraseNotes: false });
        }) as any;

        await branches.deleteNotes(["offBranch"]);
        expect(setNote).not.toHaveBeenCalled();
    });

    it("does not navigate when the deleted note is the path root (no parent path)", async () => {
        const note = buildNote({ title: "Top" });
        makeBranch("topBranch", note.noteId, "root");
        const setNote = vi.fn(async () => {});
        appContext.tabManager = {
            getActiveContext: () => ({ notePathArray: [note.noteId, "child"], setNote })
        } as any;
        appContext.triggerCommand = vi.fn((_name: any, data: any) => {
            data.callback({ proceed: true, deleteAllClones: false, eraseNotes: false });
        }) as any;

        await branches.deleteNotes(["topBranch"]);
        // earliestIndex is 0 -> parentPath is empty -> no setNote
        expect(setNote).not.toHaveBeenCalled();
    });

    it("ignores deleted-then-missing branches and a context without a note path", async () => {
        const noteA = buildNote({ title: "Ancestor" });
        makeBranch("ancBranch", noteA.noteId, "root");
        const setNote = vi.fn(async () => {});
        // Active context exists but has no notePathArray -> the `?? []` fallback is exercised.
        appContext.tabManager = {
            getActiveContext: () => ({ notePathArray: undefined, setNote })
        } as any;
        appContext.triggerCommand = vi.fn((_name: any, data: any) => {
            // Remove the branch after filtering but before navigation, so that
            // activateNeighbouringNotePath's `froca.getBranch(...)` returns undefined and the
            // `if (branch)` false arm is taken.
            delete froca.branches["ancBranch"];
            data.callback({ proceed: true, deleteAllClones: false, eraseNotes: false });
        }) as any;

        const result = await branches.deleteNotes(["ancBranch"]);
        expect(result).toBe(true);
        // empty note path + missing branch -> no navigation
        expect(setNote).not.toHaveBeenCalled();
    });
});

describe("moveNodeUpInHierarchy", () => {
    function fakeNode(opts: { parentNoteType?: string; parentBranchId?: string; branchId?: string }) {
        const parent: any = {
            data: { noteType: opts.parentNoteType, branchId: opts.parentBranchId },
            getParent: () => null
        };
        return {
            data: { branchId: opts.branchId },
            getParent: () => parent
        } as unknown as Fancytree.FancytreeNode;
    }

    it("returns early for hoisted, top-level or search-parent nodes", async () => {
        // hoisted node
        hoistedNoteService.isHoistedNode = vi.fn(() => true);
        hoistedNoteService.isTopLevelNode = vi.fn(() => false);
        await branches.moveNodeUpInHierarchy(fakeNode({ parentNoteType: "text" }));
        expect(server.put).not.toHaveBeenCalled();

        // top-level node
        hoistedNoteService.isHoistedNode = vi.fn(() => false);
        hoistedNoteService.isTopLevelNode = vi.fn(() => true);
        await branches.moveNodeUpInHierarchy(fakeNode({ parentNoteType: "text" }));
        expect(server.put).not.toHaveBeenCalled();

        // search-parent node
        hoistedNoteService.isTopLevelNode = vi.fn(() => false);
        await branches.moveNodeUpInHierarchy(fakeNode({ parentNoteType: "search" }));
        expect(server.put).not.toHaveBeenCalled();
    });

    it("moves the node after its parent's branch and shows error on failure", async () => {
        hoistedNoteService.isHoistedNode = vi.fn(() => false);
        hoistedNoteService.isTopLevelNode = vi.fn(() => false);

        await branches.moveNodeUpInHierarchy(fakeNode({ parentNoteType: "text", parentBranchId: "pb", branchId: "cb" }));
        expect(server.put).toHaveBeenCalledWith("branches/cb/move-after/pb");

        server.put = vi.fn(async () => ({ success: false, message: "denied" })) as typeof server.put;
        await branches.moveNodeUpInHierarchy(fakeNode({ parentNoteType: "text", parentBranchId: "pb", branchId: "cb" }));
        expect(toastService.showError).toHaveBeenCalledWith("denied");
    });
});

describe("clone helpers", () => {
    it("cloneNoteToBranch calls server and surfaces errors", async () => {
        await branches.cloneNoteToBranch("childN", "parentB", "px");
        expect(server.put).toHaveBeenCalledWith("notes/childN/clone-to-branch/parentB", { prefix: "px" });

        server.put = vi.fn(async () => ({ success: false, message: "e1" })) as typeof server.put;
        await branches.cloneNoteToBranch("childN", "parentB");
        expect(toastService.showError).toHaveBeenCalledWith("e1");
    });

    it("cloneNoteToParentNote calls server and surfaces errors", async () => {
        await branches.cloneNoteToParentNote("childN", "parentN", "py");
        expect(server.put).toHaveBeenCalledWith("notes/childN/clone-to-note/parentN", { prefix: "py" });

        server.put = vi.fn(async () => ({ success: false, message: "e2" })) as typeof server.put;
        await branches.cloneNoteToParentNote("childN", "parentN");
        expect(toastService.showError).toHaveBeenCalledWith("e2");
    });

    it("cloneNoteAfter calls server and surfaces errors", async () => {
        await branches.cloneNoteAfter("noteN", "afterB");
        expect(server.put).toHaveBeenCalledWith("notes/noteN/clone-after/afterB");

        server.put = vi.fn(async () => ({ success: false, message: "e3" })) as typeof server.put;
        await branches.cloneNoteAfter("noteN", "afterB");
        expect(toastService.showError).toHaveBeenCalledWith("e3");
    });

    it("cloneNoteBefore calls server and surfaces errors", async () => {
        await branches.cloneNoteBefore("noteN", "beforeB");
        expect(server.put).toHaveBeenCalledWith("notes/noteN/clone-before/beforeB");

        server.put = vi.fn(async () => ({ success: false, message: "e4" })) as typeof server.put;
        await branches.cloneNoteBefore("noteN", "beforeB");
        expect(toastService.showError).toHaveBeenCalledWith("e4");
    });
});

describe("ws task-message subscribers", () => {
    it("registered at least the two branch task subscribers at import time", () => {
        // Other modules also subscribe; branches.ts contributes two, identified by their taskType filter.
        expect(wsCallbacks.length).toBeGreaterThanOrEqual(2);
    });

    it("deleteNotes subscriber reacts to error/progress/success and ignores other task types", async () => {
        // Messages without a matching taskType (or without taskType at all) -> ignored by branch subscribers.
        await dispatchWs({ type: "taskProgressCount", taskId: "t", progressCount: 1 });
        await dispatchWs({ taskType: "deleteNotes" }); // matching taskType but no recognized type -> no branch taken
        expect(toastService.showError).not.toHaveBeenCalled();
        expect(toastService.showPersistent).not.toHaveBeenCalled();

        await dispatchWs({ taskType: "deleteNotes", type: "taskError", taskId: "e1", message: "boom" });
        expect(toastService.closePersistent).toHaveBeenCalledWith("e1");
        expect(toastService.showError).toHaveBeenCalledWith("boom");

        await dispatchWs({ taskType: "deleteNotes", type: "taskProgressCount", taskId: "p1", progressCount: 3 });
        expect(toastService.showPersistent).toHaveBeenCalledTimes(1);
        expect((toastService.showPersistent as any).mock.calls[0][0].id).toBe("p1");

        await dispatchWs({ taskType: "deleteNotes", type: "taskSucceeded", taskId: "s1" });
        const succeededToast = (toastService.showPersistent as any).mock.calls[1][0];
        expect(succeededToast.id).toBe("s1");
        expect(succeededToast.timeout).toBe(5000);
        expect(succeededToast.icon).toBe("trash");
        // The finished toast replaces the in-progress one by id, and showPersistent merges fields.
        expect(succeededToast.progress).toBeUndefined();
        expect(succeededToast.dismissible).toBe(true);
    });

    it("shows a delete progress bar only with a total, and drops it while erasing", async () => {
        await dispatchWs({
            taskType: "deleteNotes", type: "taskProgressCount",
            taskId: "b1", progressCount: 3, totalCount: 12
        });
        const withTotal = (toastService.showPersistent as any).mock.calls[0][0];
        expect(withTotal.progress).toBeCloseTo(3 / 12);
        expect(withTotal.dismissible).toBe(false);

        // The count walks branches while the total counts notes, so a surviving clone can push the
        // count past the total. The bar must not overflow.
        await dispatchWs({
            taskType: "deleteNotes", type: "taskProgressCount",
            taskId: "b2", progressCount: 20, totalCount: 12
        });
        expect((toastService.showPersistent as any).mock.calls[1][0].progress).toBe(1);

        // No total (a caller that never opened the dialog) -> a running count, no bar.
        await dispatchWs({
            taskType: "deleteNotes", type: "taskProgressCount", taskId: "b3", progressCount: 4
        });
        expect((toastService.showPersistent as any).mock.calls[2][0].progress).toBeUndefined();

        // Erasing is bulk SQL with nothing to count, so the bar goes even though a total is known.
        await dispatchWs({
            taskType: "deleteNotes", type: "taskProgressCount",
            taskId: "b4", progressCount: 12, totalCount: 12, phase: "erasing"
        });
        const erasing = (toastService.showPersistent as any).mock.calls[3][0];
        expect(erasing.progress).toBeUndefined();
        expect(erasing.dismissible).toBe(false);
    });

    it("undeleteNotes subscriber reacts to error/progress/success and ignores other task types", async () => {
        // undelete message with no recognized type -> falls through every else-if (no toast).
        await dispatchWs({ taskType: "undeleteNotes" });
        expect(toastService.showError).not.toHaveBeenCalled();
        expect(toastService.showPersistent).not.toHaveBeenCalled();

        // a deleteNotes message must NOT be handled by the undelete subscriber as an undelete,
        // but it IS handled by the delete subscriber. Assert the undelete-specific outcome instead.
        await dispatchWs({ taskType: "undeleteNotes", type: "taskError", taskId: "ue1", message: "undel-boom" });
        expect(toastService.closePersistent).toHaveBeenCalledWith("ue1");
        expect(toastService.showError).toHaveBeenCalledWith("undel-boom");

        await dispatchWs({ taskType: "undeleteNotes", type: "taskProgressCount", taskId: "ue2", progressCount: 7 });
        expect(toastService.showPersistent).toHaveBeenCalledTimes(1);
        expect((toastService.showPersistent as any).mock.calls[0][0].id).toBe("ue2");

        await dispatchWs({ taskType: "undeleteNotes", type: "taskSucceeded", taskId: "ue3" });
        const succeededToast = (toastService.showPersistent as any).mock.calls[1][0];
        expect(succeededToast.id).toBe("ue3");
        expect(succeededToast.timeout).toBe(5000);
    });
});