From c9f648fcb8c497b16012a897a94d17c7fde45660 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 15 Nov 2025 22:27:02 +0200 Subject: [PATCH] fix(tree): #top/#bottom reversed in desc order (closes #7716) --- apps/server/src/services/tree.spec.ts | 40 +++++++++++++++++++++++++++ apps/server/src/services/tree.ts | 8 +++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/apps/server/src/services/tree.spec.ts b/apps/server/src/services/tree.spec.ts index e7538ce69..1efd9acbe 100644 --- a/apps/server/src/services/tree.spec.ts +++ b/apps/server/src/services/tree.spec.ts @@ -5,6 +5,7 @@ import BBranch from "../becca/entities/bbranch.js"; import BNote from "../becca/entities/bnote.js"; import tree from "./tree.js"; import cls from "./cls.js"; +import { buildNote } from "../test/becca_easy_mocking.js"; describe("Tree", () => { let rootNote!: NoteBuilder; @@ -73,4 +74,43 @@ describe("Tree", () => { expect(order).toStrictEqual(expectedOrder); } }); + + it("pins to the top and bottom", () => { + const note = buildNote({ + children: [ + { title: "bottom", "#bottom": "" }, + { title: "5" }, + { title: "3" }, + { title: "2" }, + { title: "1" }, + { title: "top", "#top": "" } + ], + "#sorted": "" + }); + cls.init(() => { + tree.sortNotesIfNeeded(note.noteId); + }); + const orderedTitles = note.children.map((child) => child.title); + expect(orderedTitles).toStrictEqual([ "top", "1", "2", "3", "5", "bottom" ]); + }); + + it("pins to the top and bottom in reverse order", () => { + const note = buildNote({ + children: [ + { title: "bottom", "#bottom": "" }, + { title: "1" }, + { title: "2" }, + { title: "3" }, + { title: "5" }, + { title: "top", "#top": "" } + ], + "#sorted": "", + "#sortDirection": "desc" + }); + cls.init(() => { + tree.sortNotesIfNeeded(note.noteId); + }); + const orderedTitles = note.children.map((child) => child.title); + expect(orderedTitles).toStrictEqual([ "top", "5", "3", "2", "1", "bottom" ]); + }); }); diff --git a/apps/server/src/services/tree.ts b/apps/server/src/services/tree.ts index 6211c5cd0..05c9ecdd9 100644 --- a/apps/server/src/services/tree.ts +++ b/apps/server/src/services/tree.ts @@ -136,8 +136,8 @@ function sortNotes(parentNoteId: string, customSortBy: string = "title", reverse const topBEl = fetchValue(b, "top"); if (topAEl !== topBEl) { - if (topAEl === null) return 1; - if (topBEl === null) return -1; + if (topAEl === null) return reverse ? -1 : 1; + if (topBEl === null) return reverse ? 1 : -1; // since "top" should not be reversible, we'll reverse it once more to nullify this effect return compare(topAEl, topBEl) * (reverse ? -1 : 1); @@ -147,8 +147,8 @@ function sortNotes(parentNoteId: string, customSortBy: string = "title", reverse const bottomBEl = fetchValue(b, "bottom"); if (bottomAEl !== bottomBEl) { - if (bottomAEl === null) return -1; - if (bottomBEl === null) return 1; + if (bottomAEl === null) return reverse ? 1 : -1; + if (bottomBEl === null) return reverse ? -1 : 1; // since "bottom" should not be reversible, we'll reverse it once more to nullify this effect return compare(bottomBEl, bottomAEl) * (reverse ? -1 : 1);