Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(tree): remove a moved note's node when froca already lost its branch (closes #7288)
Elian Doran99b342b961c8
diff --git a/apps/client/src/services/froca_updater.spec.ts b/apps/client/src/services/froca_updater.spec.tsindex 1371adf04b..7f9f67573c 100644--- a/apps/client/src/services/froca_updater.spec.ts+++ b/apps/client/src/services/froca_updater.spec.ts@@ -187,6 +187,28 @@ describe("froca_updater - branch changes", () => {expect(triggerSpy).toHaveBeenCalledTimes(1);});+ it("reports a deleted branch that froca already dropped while its notes are loaded", async () => {+ // `froca.addResp()` replaces a refreshed note's parent branches, so a subtree load can+ // remove the branch before the WebSocket update that deletes it arrives (#7288).+ const parent = buildNote({ title: "Main", children: [{ title: "Moved" }] });+ const childId = parent.children[0];+ const branchId = `${parent.noteId}_${childId}`;+ delete froca.branches[branchId];++ await process([ec({+ entityName: "branches",+ entityId: branchId,+ componentId: "comp-b",+ entity: { branchId, noteId: childId, parentNoteId: parent.noteId, isDeleted: true } as any+ })]);++ expect(triggerSpy).toHaveBeenCalledTimes(1);+ const { loadResults } = triggerSpy.mock.calls[0][1] as { loadResults: LoadResults };+ expect(loadResults.getBranchRows()).toEqual([+ expect.objectContaining({ branchId, noteId: childId, parentNoteId: parent.noteId, isDeleted: true })+ ]);+ });+it("returns early for a deleted branch that is not loaded", async () => {await process([ec({entityName: "branches",diff --git a/apps/client/src/services/froca_updater.ts b/apps/client/src/services/froca_updater.tsindex 48fdbbdc8f..06b12cc983 100644--- a/apps/client/src/services/froca_updater.ts+++ b/apps/client/src/services/froca_updater.ts@@ -134,7 +134,16 @@ async function processBranchChange(loadResults: LoadResults, ec: EntityChange) {let branch = froca.branches[ec.entityId];if (ec.isErased || ec.entity?.isDeleted) {- if (branch) {+ if (!branch) {+ // `froca.addResp()` can drop the branch before its deletion arrives here, but the tree+ // still shows it while either note is loaded.+ const branchRow = ec.entity as FBranchRow | undefined;+ const isShown = !!branchRow+ && (branchRow.noteId in froca.notes || branchRow.parentNoteId in froca.notes);+ if (ec.componentId && isShown) {+ loadResults.addBranch(ec.entityId, ec.componentId);+ }+ } else {const childNote = froca.notes[branch.noteId];const parentNote = froca.notes[branch.parentNoteId];diff --git a/packages/trilium-e2e/src/tree_move.spec.ts b/packages/trilium-e2e/src/tree_move.spec.tsnew file mode 100644index 0000000000..e9c6ae91e0--- /dev/null+++ b/packages/trilium-e2e/src/tree_move.spec.ts@@ -0,0 +1,85 @@+import test, { expect, Page } from "@playwright/test";++import App from "./support/app";++interface CreatedNote {+ noteId: string;+ branchId: string;+}++test("Moving several notes into a childless note leaves no ghost behind", async ({ page, context }) => {+ const app = new App(page, context);+ await app.goto();++ const suffix = Date.now().toString(36);+ const main = await createNote(page, "root", `Main ${suffix}`);+ const children: CreatedNote[] = [];+ for (const index of [1, 2, 3, 4]) {+ children.push(await createNote(page, main.noteId, `Child ${index} ${suffix}`));+ }+ const target = await createNote(page, main.noteId, `Target ${suffix}`);++ const tree = app.noteTree;+ await tree.locator(".fancytree-node", { hasText: `Main ${suffix}` })+ .locator(".fancytree-expander").click();+ await tree.getByText(`Child 1 ${suffix}`, { exact: true }).click();+ await tree.getByText(`Child 4 ${suffix}`, { exact: true }).click({ modifiers: ["Shift"] });+ await page.keyboard.press("ControlOrMeta+X");++ await tree.getByText(`Target ${suffix}`, { exact: true }).click();+ await page.keyboard.press("ControlOrMeta+V");++ // Wait for the moves to land in froca, then for the tree to settle.+ await expect.poll(() => getFrocaChildIds(page, main.noteId)).toEqual([target.noteId]);+ await expect.poll(() => getFrocaChildIds(page, target.noteId))+ .toEqual(children.map((child) => child.noteId));+ await page.waitForTimeout(500);++ expect(await getTreeChildIds(page, main.noteId)).toEqual([target.noteId]);+ expect(await getStaleTreeBranchIds(page)).toEqual([]);+});++async function createNote(page: Page, parentNoteId: string, title: string): Promise<CreatedNote> {+ return page.evaluate(async ({ parentNoteId, title }) => {+ const response = await fetch(`/api/notes/${parentNoteId}/children?target=into`, {+ method: "POST",+ headers: {+ "content-type": "application/json",+ "x-csrf-token": (window as any).glob.csrfToken+ },+ body: JSON.stringify({ title, content: "", type: "text" })+ });+ const { note, branch } = await response.json();+ return { noteId: note.noteId, branchId: branch.branchId };+ }, { parentNoteId, title });+}++function getFrocaChildIds(page: Page, noteId: string) {+ return page.evaluate((noteId) => {+ return (window as any).glob.froca.notes[noteId]?.children ?? [];+ }, noteId);+}++/** Note IDs of the tree nodes under every node of the note, in tree order. */+function getTreeChildIds(page: Page, noteId: string) {+ return page.evaluate((noteId) => {+ const tree = (window as any).$.ui.fancytree.getTree(".tree-wrapper .tree");+ const nodes = tree.getNodesByRef(noteId) ?? [];+ return nodes.flatMap((node: any) => (node.getChildren() ?? []).map((child: any) => child.data.noteId));+ }, noteId);+}++/** Branch IDs of tree nodes whose branch froca no longer knows. */+function getStaleTreeBranchIds(page: Page) {+ return page.evaluate(() => {+ const froca = (window as any).glob.froca;+ const tree = (window as any).$.ui.fancytree.getTree(".tree-wrapper .tree");+ const stale: string[] = [];+ tree.getRootNode().visit((node: any) => {+ if (node.data.branchId && !(node.data.branchId in froca.branches)) {+ stale.push(`${node.data.branchId} (${node.title})`);+ }+ });+ return stale;+ });+}