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
test(commons): cover the search syntax the client reads
Elian Doranb21e7b62a5a4
diff --git a/packages/commons/src/lib/search_syntax.spec.ts b/packages/commons/src/lib/search_syntax.spec.tsnew file mode 100644index 0000000000..6e5d3ba5e6--- /dev/null+++ b/packages/commons/src/lib/search_syntax.spec.ts@@ -0,0 +1,57 @@+import { describe, expect, it } from "vitest";++import {+ allowedSearchOperators, CONTENT_SEARCH_OPERATORS, NO_SEARCH_OPERATORS, SEARCH_NOTE_PATH,+ SEARCH_NOTE_PATH_SEGMENTS, SEARCH_OPERATORS, TEXT_SEARCH_OPERATORS+} from "./search_syntax.js";++describe("SEARCH_NOTE_PATH_SEGMENTS", () => {+ it("holds every segment of the path, and names each one once", () => {+ const { properties, contentProperties, traversals, attributeSegments } = SEARCH_NOTE_PATH;++ expect(SEARCH_NOTE_PATH_SEGMENTS).toEqual([+ ...properties, ...contentProperties, ...traversals, ...attributeSegments+ ]);+ expect(new Set(SEARCH_NOTE_PATH_SEGMENTS).size).toBe(SEARCH_NOTE_PATH_SEGMENTS.length);+ });+});++describe("allowedSearchOperators", () => {+ it("restricts nothing for a label, a property or a traversal", () => {+ expect(allowedSearchOperators("#book")).toBeUndefined();+ expect(allowedSearchOperators("note.title")).toBeUndefined();+ expect(allowedSearchOperators("note.parents.title")).toBeUndefined();+ });++ it("restricts nothing after `labels.`, where the name is the user's own", () => {+ // `note.labels.text` names a label called `text`, not the note's own text.+ expect(allowedSearchOperators("note.labels.text")).toBeUndefined();+ expect(allowedSearchOperators("note.labels.content")).toBeUndefined();+ });++ it("allows no operator on a relation, which only a further property continues", () => {+ expect(allowedSearchOperators("~author")).toBe(NO_SEARCH_OPERATORS);+ expect(allowedSearchOperators("note.relations.author")).toBe(NO_SEARCH_OPERATORS);+ // Walking into the note the relation names lands on an ordinary property again.+ expect(allowedSearchOperators("~author.title")).toBeUndefined();+ });++ it("matches text with `*=*` alone", () => {+ expect(allowedSearchOperators("note.text")).toBe(TEXT_SEARCH_OPERATORS);+ expect([ ...TEXT_SEARCH_OPERATORS ]).toEqual([ "*=*" ]);+ });++ it("matches content with every operator but the ordering ones", () => {+ expect(allowedSearchOperators("note.content")).toBe(CONTENT_SEARCH_OPERATORS);+ expect(allowedSearchOperators("note.rawContent")).toBe(CONTENT_SEARCH_OPERATORS);+ expect(CONTENT_SEARCH_OPERATORS).toEqual(+ new Set(SEARCH_OPERATORS.filter((operator) => ![ ">", ">=", "<", "<=" ].includes(operator)))+ );+ });++ it("reads a property whatever it is spelled in, as the parser does", () => {+ expect(allowedSearchOperators("note.TEXT")).toBe(TEXT_SEARCH_OPERATORS);+ expect(allowedSearchOperators("NOTE.RawContent")).toBe(CONTENT_SEARCH_OPERATORS);+ expect(allowedSearchOperators("note.LABELS.text")).toBeUndefined();+ });+});diff --git a/packages/commons/src/lib/search_syntax.ts b/packages/commons/src/lib/search_syntax.tsindex 6fe00a0675..c331311e4e 100644--- a/packages/commons/src/lib/search_syntax.ts+++ b/packages/commons/src/lib/search_syntax.ts@@ -72,7 +72,8 @@ export const CONTENT_SEARCH_OPERATORS: ReadonlySet<string> = new Set([*/export function allowedSearchOperators(operand: string): ReadonlySet<string> | undefined {const segments = operand.split(".");- const property = (segments[segments.length - 1] ?? "").toLowerCase();+ // `split` always yields a last segment, where a path of one segment yields no previous one.+ const property = segments[segments.length - 1].toLowerCase();const previous = (segments[segments.length - 2] ?? "").toLowerCase();// The name after `labels.` is the user's own, and says nothing about the comparison.