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
client: fix an issue
Adorian Doranaed12903d8ae
diff --git a/apps/client/src/services/attributes.spec.ts b/apps/client/src/services/attributes.spec.tsindex 92c1979ea7..8652556a77 100644--- a/apps/client/src/services/attributes.spec.ts+++ b/apps/client/src/services/attributes.spec.ts@@ -168,6 +168,46 @@ describe("Set boolean with inheritance", () => {expect(server.remove).not.toHaveBeenCalled();});+ it("removes every owned duplicate to restore the inherited value", async () => {+ buildNote({+ title: "Parent note",+ "#foo(inheritable)": "",+ "children": [ { id: "twoFalse", title: "Owns false twice", "#foo": [ "false", "false" ] } ]+ });+ const note = froca.notes["twoFalse"];+ const [ first, second ] = note.getOwnedLabels("foo");++ await setBooleanWithInheritance(note, "foo", true);+ expect(server.remove).toHaveBeenCalledTimes(2);+ expect(server.remove).toHaveBeenCalledWith(`notes/twoFalse/attributes/${first.attributeId}`);+ expect(server.remove).toHaveBeenCalledWith(`notes/twoFalse/attributes/${second.attributeId}`);+ expect(server.put).not.toHaveBeenCalled();+ });++ it("removes the owned duplicates before overriding the inherited value", async () => {+ buildNote({+ title: "Parent note",+ "#foo(inheritable)": "",+ "children": [ { id: "twoTrue", title: "Owns true twice", "#foo": [ "", "" ] } ]+ });+ const note = froca.notes["twoTrue"];+ const [ , second ] = note.getOwnedLabels("foo");++ await setBooleanWithInheritance(note, "foo", false);+ expect(server.remove).toHaveBeenCalledExactlyOnceWith(+ `notes/twoTrue/attributes/${second.attributeId}`+ );+ expect(server.put).toHaveBeenCalledExactlyOnceWith(`notes/twoTrue/set-attribute`, {+ type: "label",+ name: "foo",+ value: "false",+ isInheritable: false+ }, undefined);+ // `set-attribute` updates whichever owned label it finds, so only one can be left by then.+ expect(vi.mocked(server.remove).mock.invocationCallOrder[0])+ .toBeLessThan(vi.mocked(server.put).mock.invocationCallOrder[0]);+ });+it("waits for the label write and surfaces its failure", async () => {const note = buildNote({ title: "New note" });vi.mocked(server.put).mockRejectedValueOnce(new Error("offline"));diff --git a/apps/client/src/services/attributes.ts b/apps/client/src/services/attributes.tsindex e3c0d395de..86fe1c061f 100644--- a/apps/client/src/services/attributes.ts+++ b/apps/client/src/services/attributes.ts@@ -51,10 +51,15 @@ export async function setBooleanWithInheritance(note: FNote, labelName: string,const inheritedValue = !!inheritedLabel && inheritedLabel.value !== "false";if (value === inheritedValue) {- await removeOwnedLabelByName(note, labelName);- } else {- await setLabel(note.noteId, labelName, value ? "" : "false");+ await removeOwnedAttributesByNameOrType(note, "label", labelName);+ return;+ }++ // `set-attribute` updates one owned label and leaves its duplicates in effect.+ for (const duplicate of note.getOwnedLabels(labelName).slice(1)) {+ await removeAttributeById(note.noteId, duplicate.attributeId);}+ await setLabel(note.noteId, labelName, value ? "" : "false");}async function removeAttributeById(noteId: string, attributeId: string) {