Skip to content

Build your personal knowledge base with Trilium Notes

client/content manager: improve test coverage

Adorian Doranb9626162154f
diff --git a/apps/client/src/services/content_manager.spec.ts b/apps/client/src/services/content_manager.spec.ts
index 0804f5ec34..e90071f126 100644
--- a/apps/client/src/services/content_manager.spec.ts
+++ b/apps/client/src/services/content_manager.spec.ts
@@ -1,15 +1,21 @@
-import { describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { buildNote } from "../test/easy-froca";
+import attributeService from "./attributes";
import {
buildCategoryQuery,
CONTENT_CATEGORIES,
type ContentCategory,
+ findCategoryNotes,
isCategoryEnabled,
isUserContent,
parseFilterTriggers,
- resolveProperties
+ resolveProperties,
+ setCategoryEnabled
} from "./content_manager";
+import searchService from "./search";
+
+afterEach(() => vi.restoreAllMocks());
function categoryById(id: string) {
const category = CONTENT_CATEGORIES.find((candidate) => candidate.id === id);
@@ -98,6 +104,10 @@ describe("parseFilterTriggers", () => {
]);
});
+ it("returns nothing for a filter naming no attributes", () => {
+ expect(parseFilterTriggers("")).toEqual([]);
+ });
+
it("finds the triggers of every real category", () => {
expect(parseFilterTriggers(categoryById("snippets").filter)).toEqual([
{ type: "label", name: "snippet" },
@@ -253,11 +263,99 @@ describe("resolveProperties", () => {
]);
});
+ it("skips a value whose condition names neither a label nor a relation", () => {
+ const category: ContentCategory = {
+ id: "test",
+ titleKey: "unused",
+ filter: "#anything",
+ properties: [ { titleKey: "prop.k", values: [ { titleKey: "v", condition: {} } ] } ]
+ };
+
+ expect(resolveProperties(buildNote({ title: "X", "#anything": "" }), category)).toEqual([]);
+ });
+
+ it("ignores a value that specifies neither a title nor a label to read", () => {
+ const category: ContentCategory = {
+ id: "test",
+ titleKey: "unused",
+ filter: "#anything",
+ properties: [ { titleKey: "prop.k", values: [ {} ] } ]
+ };
+
+ expect(resolveProperties(buildNote({ title: "X", "#anything": "" }), category)).toEqual([]);
+ });
+
it("returns nothing for a category that declares no properties", () => {
expect(resolveProperties(buildNote({ title: "X", "#appCss": "" }), categoryById("customCss"))).toEqual([]);
});
});
+describe("setCategoryEnabled", () => {
+ function spyOnToggle() {
+ return vi.spyOn(attributeService, "toggleDangerousAttribute").mockResolvedValue(undefined);
+ }
+
+ it("renames only the triggers of the category, leaving the note's other capabilities alone", async () => {
+ // The whole point of toggling per category: this note is both a widget and a backend script.
+ const toggle = spyOnToggle();
+ const note = buildNote({ title: "Both", "#widget": "", "#run": "daily" });
+
+ await setCategoryEnabled(note, categoryById("widgets"), false);
+
+ expect(toggle).toHaveBeenCalledTimes(1);
+ expect(toggle).toHaveBeenCalledWith(note, "label", "widget", false);
+ });
+
+ it("toggles relation triggers as relations", async () => {
+ const toggle = spyOnToggle();
+ const note = buildNote({ title: "Render", "~renderNote": "abc123" });
+
+ await setCategoryEnabled(note, categoryById("renderNotes"), false);
+
+ expect(toggle).toHaveBeenCalledWith(note, "relation", "renderNote", false);
+ });
+
+ it("writes back the canonical name when re-enabling a disabled trigger", async () => {
+ const toggle = spyOnToggle();
+ const note = buildNote({ title: "Paused", "#disabled:widget": "" });
+
+ await setCategoryEnabled(note, categoryById("widgets"), true);
+
+ expect(toggle).toHaveBeenCalledWith(note, "label", "widget", true);
+ });
+
+ it("toggles each distinct trigger once, even with several attributes of the same name", async () => {
+ const toggle = spyOnToggle();
+ const note = buildNote({ title: "Two triggers", "#run": "hourly" });
+
+ await setCategoryEnabled(note, categoryById("backendScripts"), false);
+
+ expect(toggle).toHaveBeenCalledTimes(1);
+ });
+
+ it("does nothing when the note carries none of the category's triggers", async () => {
+ const toggle = spyOnToggle();
+
+ await setCategoryEnabled(buildNote({ title: "Plain" }), categoryById("widgets"), false);
+
+ expect(toggle).not.toHaveBeenCalled();
+ });
+});
+
+describe("findCategoryNotes", () => {
+ it("runs the category's ordered query and drops built-in notes", async () => {
+ const mine = buildNote({ id: "mine123", title: "My theme" });
+ const builtIn = buildNote({ id: "_builtInTheme", title: "Built-in" });
+ const search = vi.spyOn(searchService, "searchForNotes").mockResolvedValue([ mine, builtIn ]);
+ const category = categoryById("themes");
+
+ const notes = await findCategoryNotes(category, "title");
+
+ expect(search).toHaveBeenCalledWith(buildCategoryQuery(category.filter, "title"));
+ expect(notes).toEqual([ mine ]);
+ });
+});
+
describe("isUserContent", () => {
it("accepts notes the user created", () => {
expect(isUserContent(buildNote({ id: "abc123", title: "My script" }))).toBe(true);
diff --git a/apps/client/src/services/content_manager.ts b/apps/client/src/services/content_manager.ts
index cae5c2d507..10fd6ed666 100644
--- a/apps/client/src/services/content_manager.ts
+++ b/apps/client/src/services/content_manager.ts
@@ -344,10 +344,6 @@ function getCategoryTriggers(note: FNote, category: ContentCategory) {
const found = new Map<string, ContentTrigger & { enabled: boolean }>();
for (const attribute of note.getOwnedAttributes()) {
- if (attribute.type !== "label" && attribute.type !== "relation") {
- continue;
- }
-
const trigger: ContentTrigger = {
type: attribute.type,
name: attributes.getNameWithoutDangerousPrefix(attribute.name)