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
import { describe, expect, it, vi } from "vitest";
import type FNote from "../../entities/fnote";
import {
type PromotedAttributeSetting, resolvePromotedAttributes, storedPromotedAttributes,
visiblePromotedAttributeNames
} from "./promoted_attributes";
const mocks = vi.hoisted(() => ({ bulk: vi.fn(async () => {}) }));
vi.mock("../../services/bulk_action", () => ({ executeBulkActions: mocks.bulk }));
// i18next is never initialised under test, so `t` echoes the key with what it interpolates.
vi.mock("../../services/i18n", () => ({
t: (key: string, values?: { name?: string }) =>
values?.name ? `${key}:${values.name}` : key
}));
/** A definition as the collection note carries it, `label:dueDate` naming `dueDate`. */
function definition(name: string, {
alias, noteId = "board1", value = "promoted,single,text", isPromoted = true,
isInheritable = true, labelType, selectOptions
}: {
alias?: string, noteId?: string, value?: string, isPromoted?: boolean, isInheritable?: boolean,
labelType?: string, selectOptions?: string[]
} = {}) {
return {
name,
value,
noteId,
isInheritable,
getDefinition: () => ({ isPromoted, promotedAlias: alias, labelType, selectOptions })
};
}
/** A collection note offering the given definitions. */
function collection(definitions: ReturnType<typeof definition>[], noteId = "board1") {
return {
noteId,
getAttributeDefinitions: () => definitions
} as unknown as FNote;
}
const DEFINED = [
definition("label:dueDate", { alias: "Due" }),
definition("label:requiresResearch"),
definition("relation:owner")
];
describe("resolvePromotedAttributes", () => {
it("reads what the note defines, naming each by its alias or by its kind and name", () => {
const resolved = resolvePromotedAttributes(collection(DEFINED), undefined);
expect(resolved).toEqual([
{
name: "dueDate",
definitionName: "label:dueDate",
type: "label",
title: "Due",
promotedAlias: "Due",
hidden: false,
drawnByCollection: false,
definitionValue: "promoted,single,text",
isOwned: true,
isDefinedByItems: false,
isConflicting: false,
isInheritable: true
},
// No alias, so the name is prefixed by the kind that defines it.
expect.objectContaining({
name: "requiresResearch",
title: "promoted_attributes.label_name:requiresResearch",
promotedAlias: undefined
}),
expect.objectContaining({
name: "owner",
type: "relation",
title: "promoted_attributes.relation_name:owner"
})
]);
});
it("puts them in the order the settings give, and hides what they hide", () => {
const settings: PromotedAttributeSetting[] = [
{ name: "owner" },
{ name: "requiresResearch", hidden: true }
];
const resolved = resolvePromotedAttributes(collection(DEFINED), settings);
// What the settings arrange leads, in their order; what they say nothing of follows.
expect(resolved.map((attribute) => attribute.name))
.toEqual([ "owner", "requiresResearch", "dueDate" ]);
expect(resolved.map((attribute) => attribute.hidden)).toEqual([ false, true, false ]);
expect(visiblePromotedAttributeNames(resolved)).toEqual([ "owner", "dueDate" ]);
});
/** An attribute nobody has arranged is one the reader has yet to see. */
it("shows an attribute the settings have never named", () => {
const resolved = resolvePromotedAttributes(
collection(DEFINED), [ { name: "dueDate", hidden: true } ]);
expect(visiblePromotedAttributeNames(resolved)).toEqual([ "requiresResearch", "owner" ]);
});
it("hides an attribute the settings have never named, for a collection that opts in", () => {
const resolved = resolvePromotedAttributes(
collection(DEFINED), [ { name: "owner" } ], [], [], { unarrangedHidden: true });
expect(visiblePromotedAttributeNames(resolved)).toEqual([ "owner" ]);
// Still listed, after the arranged ones, so the reader can switch it on.
expect(resolved.map((attribute) => attribute.name))
.toEqual([ "owner", "dueDate", "requiresResearch" ]);
});
/** Writing the result back is what prunes the config. */
it("leaves out what the settings name and the note no longer defines", () => {
const settings: PromotedAttributeSetting[] = [
{ name: "gone", hidden: true },
{ name: "dueDate", hidden: true }
];
const resolved = resolvePromotedAttributes(
collection([ definition("label:dueDate") ]), settings);
expect(storedPromotedAttributes(resolved)).toEqual([ { name: "dueDate", hidden: true } ]);
});
it("stores a shown attribute as its name alone", () => {
const resolved = resolvePromotedAttributes(collection(DEFINED), undefined);
expect(storedPromotedAttributes(resolved))
.toEqual([ { name: "dueDate" }, { name: "requiresResearch" }, { name: "owner" } ]);
});
it("names an attribute once, whichever of the two settings arranged it", () => {
const resolved = resolvePromotedAttributes(
collection(DEFINED), [ { name: "owner" }, { name: "owner", hidden: true } ]);
expect(resolved.filter((attribute) => attribute.name === "owner").length).toBe(1);
expect(resolved[0].hidden).toBe(false);
});
/** A collection whose definitions are its template's or an ancestor's still offers them. */
it("marks a definition the collection does not carry itself", () => {
const inherited = definition("label:dueDate", { noteId: "template1" });
const [ resolved ] = resolvePromotedAttributes(collection([ inherited ]), undefined);
expect(resolved.isOwned).toBe(false);
});
it("answers with nothing for a note that defines none, and for no note at all", () => {
expect(resolvePromotedAttributes(collection([]), [ { name: "dueDate" } ])).toEqual([]);
expect(resolvePromotedAttributes(undefined, undefined)).toEqual([]);
});
it("preserves colons in the attribute name when the definition name contains multiple colons", () => {
const resolved = resolvePromotedAttributes(
collection([ definition("label:sometool:parameter") ]), undefined);
expect(resolved).toHaveLength(1);
expect(resolved[0].name).toBe("sometool:parameter");
expect(resolved[0].type).toBe("label");
});
/** A definition names its kind before the colon; anything else is not one to draw. */
it("passes over a definition of a kind it cannot draw", () => {
const resolved = resolvePromotedAttributes(
collection([ definition("dueDate"), definition("child:label:x") ]), undefined);
expect(resolved).toEqual([]);
});
/**
* An item draws every attribute it has a definition for, promoted or not, and a collection
* carrying `#hidePromotedAttributes` reports no promoted ones at all: reading the promoted
* subset would leave both out of the reader's hands.
*/
it("lists a definition that is not promoted", () => {
const resolved = resolvePromotedAttributes(collection([
definition("label:dueDate", { alias: "Due" }),
definition("label:internal", { isPromoted: false, value: "single,text" })
]), undefined);
expect(resolved.map((attribute) => attribute.name)).toEqual([ "dueDate", "internal" ]);
});
/** A definition that does not pass down describes the collection alone, never its items. */
it("leaves out a definition that is not inheritable", () => {
const resolved = resolvePromotedAttributes(collection([
definition("label:dueDate"),
definition("label:boardSetting", { isInheritable: false })
]), undefined);
expect(resolved.map((attribute) => attribute.name)).toEqual([ "dueDate" ]);
});
/**
* What the collection draws itself is listed and arranged with the rest, so it keeps its place
* among them, but it is marked, and nothing shows it on an item.
*/
it("marks what the caller names as its own rather than leaving it out", () => {
const resolved = resolvePromotedAttributes(
collection(DEFINED), [ { name: "owner" } ], [ "owner" ]);
expect(resolved.map((attribute) => attribute.name))
.toEqual([ "owner", "dueDate", "requiresResearch" ]);
expect(resolved.map((attribute) => attribute.drawnByCollection))
.toEqual([ true, false, false ]);
// However it is stored, it is never among what an item draws.
expect(visiblePromotedAttributeNames(resolved))
.toEqual([ "dueDate", "requiresResearch" ]);
});
/**
* The items' definitions follow the collection's, once per name, including non-inheritable
* ones.
*/
it("appends what only the items define, once per name", () => {
const resolved = resolvePromotedAttributes(collection(DEFINED), undefined, [], [
collection([ definition("label:project", { noteId: "template1" }) ], "task1"),
collection([
definition("label:project", { noteId: "template1" }),
definition("label:dueDate", { noteId: "template1", alias: "Deadline" }),
definition("label:estimate", { noteId: "task2", isInheritable: false })
], "task2")
]);
expect(resolved.map((attribute) => attribute.name))
.toEqual([ "dueDate", "requiresResearch", "owner", "project", "estimate" ]);
expect(resolved.map((attribute) => attribute.isDefinedByItems))
.toEqual([ false, false, false, true, true ]);
expect(resolved[0].title).toBe("Due");
});
/**
* Items that define one name with a different type or different options are marked as
* conflicting, and so is an item that overrides the collection's definition with its own.
*/
it("marks a name the items define in different ways", () => {
const item = (noteId: string, labelType?: string, selectOptions?: string[]) =>
collection([
definition("label:priority", { noteId: `tpl_${noteId}`, labelType, selectOptions })
], noteId);
const conflicting = (items: FNote[]) => resolvePromotedAttributes(
collection([ definition("label:dueDate") ]), undefined, [], items)
.map((attribute) => [ attribute.name, attribute.isConflicting ]);
expect(conflicting([ item("a", "select", [ "Low" ]), item("b", "select", [ "Low" ]) ]))
.toEqual([ [ "dueDate", false ], [ "priority", false ] ]);
expect(conflicting([ item("a", "select", [ "Low" ]), item("b", "number") ]))
.toEqual([ [ "dueDate", false ], [ "priority", true ] ]);
expect(conflicting([ item("a", "select", [ "Low" ]), item("b", "select", [ "High" ]) ]))
.toEqual([ [ "dueDate", false ], [ "priority", true ] ]);
// A definition naming no type is a text field.
expect(conflicting([ item("a", "text"), item("b") ]))
.toEqual([ [ "dueDate", false ], [ "priority", false ] ]);
// An item inheriting the collection's `dueDate` reads the same definition back.
const board = collection([ definition("label:dueDate", { labelType: "date" }) ]);
const inheriting = collection([ definition("label:dueDate", { labelType: "date" }) ], "c");
const overriding = collection([
definition("label:dueDate", { noteId: "d", labelType: "text" })
], "d");
const resolve = (items: FNote[]) =>
resolvePromotedAttributes(board, undefined, [], items)[0].isConflicting;
expect(resolve([ inheriting ])).toBe(false);
expect(resolve([ inheriting, overriding ])).toBe(true);
});
/** Two notes in the chain can define the same attribute; the nearer one is what applies. */
it("keeps the first of two definitions of one name", () => {
const resolved = resolvePromotedAttributes(collection([
definition("label:dueDate", { alias: "Due" }),
definition("label:dueDate", { alias: "Deadline", noteId: "template1" })
]), undefined);
expect(resolved.map((attribute) => attribute.title)).toEqual([ "Due" ]);
});
});