Merge remote-tracking branch 'origin/develop' into test_simplify-data-dir

This commit is contained in:
Elian Doran
2025-01-19 23:13:56 +02:00
23 changed files with 95 additions and 489 deletions

View File

@@ -1528,7 +1528,9 @@ class BNote extends AbstractBeccaEntity<BNote> {
}
isLaunchBarConfig() {
return this.type === "launcher" || ["_lbRoot", "_lbAvailableLaunchers", "_lbVisibleLaunchers"].includes(this.noteId);
return this.type === "launcher"
|| ["_lbRoot", "_lbAvailableLaunchers", "_lbVisibleLaunchers"].includes(this.noteId)
|| ["_lbMobileRoot", "_lbMobileAvailableLaunchers", "_lbMobileVisibleLaunchers"].includes(this.noteId);
}
isOptions() {

View File

@@ -34,8 +34,8 @@ export default class LauncherContextMenu implements SelectMenuItemEventListener<
const isVisibleRoot = note?.noteId === "_lbVisibleLaunchers";
const isAvailableRoot = note?.noteId === "_lbAvailableLaunchers";
const isVisibleItem = parentNoteId === "_lbVisibleLaunchers";
const isAvailableItem = parentNoteId === "_lbAvailableLaunchers";
const isVisibleItem = (parentNoteId === "_lbVisibleLaunchers" || parentNoteId === "_lbMobileVisibleLaunchers");
const isAvailableItem = (parentNoteId === "_lbAvailableLaunchers" || parentNoteId === "_lbMobileAvailableLaunchers");
const isItem = isVisibleItem || isAvailableItem;
const canBeDeleted = !note?.noteId.startsWith("_"); // fixed notes can't be deleted
const canBeReset = !canBeDeleted && note?.isLaunchBarConfig();

View File

@@ -0,0 +1,101 @@
import { describe, it, expect } from "vitest";
import attributeParser from "./attribute_parser.js";
describe("Lexing", () => {
it("simple label", () => {
expect(attributeParser.lex("#label").map((t: any) => t.text)).toEqual(["#label"]);
});
it("simple label with trailing spaces", () => {
expect(attributeParser.lex(" #label ").map((t: any) => t.text)).toEqual(["#label"]);
});
it("inherited label", () => {
expect(attributeParser.lex("#label(inheritable)").map((t: any) => t.text)).toEqual(["#label", "(", "inheritable", ")"]);
expect(attributeParser.lex("#label ( inheritable ) ").map((t: any) => t.text)).toEqual(["#label", "(", "inheritable", ")"]);
});
it("label with value", () => {
expect(attributeParser.lex("#label=Hallo").map((t: any) => t.text)).toEqual(["#label", "=", "Hallo"]);
});
it("label with value", () => {
const tokens = attributeParser.lex("#label=Hallo");
expect(tokens[0].startIndex).toEqual(0);
expect(tokens[0].endIndex).toEqual(5);
});
it("relation with value", () => {
expect(attributeParser.lex("~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM").map((t: any) => t.text)).toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]);
});
it("use quotes to define value", () => {
expect(attributeParser.lex("#'label a'='hello\"` world'").map((t: any) => t.text)).toEqual(["#label a", "=", 'hello"` world']);
expect(attributeParser.lex('#"label a" = "hello\'` world"').map((t: any) => t.text)).toEqual(["#label a", "=", "hello'` world"]);
expect(attributeParser.lex("#`label a` = `hello'\" world`").map((t: any) => t.text)).toEqual(["#label a", "=", "hello'\" world"]);
});
});
describe.todo("Parser", () => {
/* #TODO
it("simple label", () => {
const attrs = attributeParser.parse(["#token"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual("label");
expect(attrs[0].name).toEqual("token");
expect(attrs[0].isInheritable).toBeFalsy();
expect(attrs[0].value).toBeFalsy();
});
it("inherited label", () => {
const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual("label");
expect(attrs[0].name).toEqual("token");
expect(attrs[0].isInheritable).toBeTruthy();
expect(attrs[0].value).toBeFalsy();
});
it("label with value", () => {
const attrs = attributeParser.parse(["#token", "=", "val"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual("label");
expect(attrs[0].name).toEqual("token");
expect(attrs[0].value).toEqual("val");
});
it("relation", () => {
let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual("relation");
expect(attrs[0].name).toEqual("token");
expect(attrs[0].value).toEqual("NFi2gL4xtPxM");
attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual("relation");
expect(attrs[0].name).toEqual("token");
expect(attrs[0].value).toEqual("NFi2gL4xtPxM");
});
*/
});
describe("error cases", () => {
it("error cases", () => {
expect(() => attributeParser.lexAndParse("~token")).toThrow('Relation "~token" in "~token" should point to a note.');
expect(() => attributeParser.lexAndParse("#a&b/s")).toThrow(`Attribute name "a&b/s" contains disallowed characters, only alphanumeric characters, colon and underscore are allowed.`);
expect(() => attributeParser.lexAndParse("#")).toThrow(`Attribute name is empty, please fill the name.`);
});
});

View File

@@ -308,7 +308,9 @@ function dynamicRequire(moduleName: string) {
if (typeof __non_webpack_require__ !== "undefined") {
return __non_webpack_require__(moduleName);
} else {
return require(moduleName);
// explicitly pass as string and not as expression to suppress webpack warning
// 'Critical dependency: the request of a dependency is an expression'
return require(`${moduleName}`);
}
}

View File

@@ -1636,12 +1636,24 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
}
}
moveShortcutToVisibleCommand({ node, selectedOrActiveBranchIds }) {
branchService.moveToParentNote(selectedOrActiveBranchIds, "_lbVisibleLaunchers");
moveLauncherToVisibleCommand({ selectedOrActiveBranchIds }) {
this.#moveLaunchers(selectedOrActiveBranchIds, "_lbVisibleLaunchers", "_lbMobileVisibleLaunchers");
}
moveShortcutToAvailableCommand({ node, selectedOrActiveBranchIds }) {
branchService.moveToParentNote(selectedOrActiveBranchIds, "_lbAvailableLaunchers");
moveLauncherToAvailableCommand({ selectedOrActiveBranchIds }) {
this.#moveLaunchers(selectedOrActiveBranchIds, "_lbAvailableLaunchers", "_lbMobileAvailableLaunchers");
}
#moveLaunchers(selectedOrActiveBranchIds, desktopParent, mobileParent) {
const desktopLaunchersToMove = selectedOrActiveBranchIds.filter((branchId) => !branchId.startsWith("_lbMobile"));
if (desktopLaunchersToMove) {
branchService.moveToParentNote(desktopLaunchersToMove, "_lbRoot_" + desktopParent);
}
const mobileLaunchersToMove = selectedOrActiveBranchIds.filter((branchId) => branchId.startsWith("_lbMobile"));
if (mobileLaunchersToMove) {
branchService.moveToParentNote(mobileLaunchersToMove, "_lbMobileRoot_" + mobileParent);
}
}
addNoteLauncherCommand({ node }) {

View File

@@ -0,0 +1,346 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import type { getTriliumDataDir as getTriliumDataDirType, getDataDirs as getDataDirsType, getPlatformAppDataDir as getPlatformAppDataDirType } from "./data_dir.js";
describe("data_dir.ts unit tests", async () => {
let getTriliumDataDir: typeof getTriliumDataDirType;
let getPlatformAppDataDir: typeof getPlatformAppDataDirType;
let getDataDirs: typeof getDataDirsType;
const mockFn = {
existsSyncMock: vi.fn(),
mkdirSyncMock: vi.fn(),
osHomedirMock: vi.fn(),
osPlatformMock: vi.fn(),
pathJoinMock: vi.fn()
};
// using doMock, to avoid hoisting, so that we can use the mockFn object
// to collect all mocked Fns
vi.doMock("node:fs", () => {
return {
default: {
existsSync: mockFn.existsSyncMock,
mkdirSync: mockFn.mkdirSyncMock
}
};
});
vi.doMock("node:os", () => {
return {
default: {
homedir: mockFn.osHomedirMock,
platform: mockFn.osPlatformMock
}
};
});
vi.doMock("path", () => {
return {
join: mockFn.pathJoinMock
};
});
// import function to test now, after creating the mocks
({ getTriliumDataDir } = await import("./data_dir.js"));
({ getPlatformAppDataDir } = await import("./data_dir.js"));
({ getDataDirs } = await import("./data_dir.js"));
// helper to reset call counts
const resetAllMocks = () => {
Object.values(mockFn).forEach((mockedFn) => {
mockedFn.mockReset();
});
};
// helper to set mocked Platform
const setMockPlatform = (osPlatform: string, homedir: string, pathJoin: string) => {
mockFn.osPlatformMock.mockImplementation(() => osPlatform);
mockFn.osHomedirMock.mockImplementation(() => homedir);
mockFn.pathJoinMock.mockImplementation(() => pathJoin);
};
describe("#getPlatformAppDataDir()", () => {
type TestCaseGetPlatformAppDataDir = [description: string, fnValue: Parameters<typeof getPlatformAppDataDir>, expectedValue: string | null, osHomedirMockValue: string | null];
const testCases: TestCaseGetPlatformAppDataDir[] = [
["w/ unsupported OS it should return 'null'", ["aix", undefined], null, null],
["w/ win32 and no APPDATA set it should return 'null'", ["win32", undefined], null, null],
["w/ win32 and set APPDATA it should return set 'APPDATA'", ["win32", "AppData"], "AppData", null],
["w/ linux it should return '~/.local/share'", ["linux", undefined], "/home/mock/.local/share", "/home/mock"],
["w/ linux and wrongly set APPDATA it should ignore APPDATA and return '~/.local/share'", ["linux", "FakeAppData"], "/home/mock/.local/share", "/home/mock"],
["w/ darwin it should return '~/Library/Application Support'", ["darwin", undefined], "/Users/mock/Library/Application Support", "/Users/mock"]
];
testCases.forEach((testCase) => {
const [testDescription, fnValues, expected, osHomedirMockValue] = testCase;
return it(testDescription, () => {
mockFn.osHomedirMock.mockReturnValue(osHomedirMockValue);
const actual = getPlatformAppDataDir(...fnValues);
expect(actual).toEqual(expected);
});
});
});
describe("#getTriliumDataDir", async () => {
beforeEach(() => {
// make sure these are not set
delete process.env.TRILIUM_DATA_DIR;
delete process.env.APPDATA;
resetAllMocks();
});
/**
* case A process.env.TRILIUM_DATA_DIR is set
* case B process.env.TRILIUM_DATA_DIR is not set and Trilium folder is existing in platform
* case C process.env.TRILIUM_DATA_DIR is not set and Trilium folder is not existing in platform's home dir
* case D fallback to creating Trilium folder in home dir
*/
describe("case A", () => {
it("when folder exists it should return the path, without attempting to create the folder", async () => {
const mockTriliumDataPath = "/home/mock/trilium-data-ENV-A1";
process.env.TRILIUM_DATA_DIR = mockTriliumDataPath;
// set fs.existsSync to true, i.e. the folder does exist
mockFn.existsSyncMock.mockImplementation(() => true);
const result = getTriliumDataDir("trilium-data");
// createDirIfNotExisting should call existsync 1 time and mkdirSync 0 times -> as it does not need to create the folder
// and return value should be TRILIUM_DATA_DIR value from process.env
expect(mockFn.existsSyncMock).toHaveBeenCalledTimes(1);
expect(mockFn.mkdirSyncMock).toHaveBeenCalledTimes(0);
expect(result).toEqual(process.env.TRILIUM_DATA_DIR);
});
it("when folder does not exist it should attempt to create the folder and return the path", async () => {
const mockTriliumDataPath = "/home/mock/trilium-data-ENV-A2";
process.env.TRILIUM_DATA_DIR = mockTriliumDataPath;
// set fs.existsSync mock to return false, i.e. the folder does not exist
mockFn.existsSyncMock.mockImplementation(() => false);
const result = getTriliumDataDir("trilium-data");
// createDirIfNotExisting should call existsync 1 time and mkdirSync 1 times -> as it has to create the folder
// and return value should be TRILIUM_DATA_DIR value from process.env
expect(mockFn.existsSyncMock).toHaveBeenCalledTimes(1);
expect(mockFn.mkdirSyncMock).toHaveBeenCalledTimes(1);
expect(result).toEqual(process.env.TRILIUM_DATA_DIR);
});
});
describe("case B", () => {
it("it should check if folder exists and return it", async () => {
const homedir = "/home/mock";
const dataDirName = "trilium-data";
const mockTriliumDataPath = `${homedir}/${dataDirName}`;
mockFn.pathJoinMock.mockImplementation(() => mockTriliumDataPath);
// set fs.existsSync to true, i.e. the folder does exist
mockFn.existsSyncMock.mockImplementation(() => true);
const result = getTriliumDataDir(dataDirName);
expect(mockFn.existsSyncMock).toHaveBeenCalledTimes(1);
expect(result).toEqual(mockTriliumDataPath);
});
});
describe("case C", () => {
it("w/ Platform 'Linux', an existing App Data Folder (~/.local/share) but non-existing Trilium dir (~/.local/share/trilium-data) it should attempt to create the dir", async () => {
const homedir = "/home/mock";
const dataDirName = "trilium-data";
const mockPlatformDataPath = `${homedir}/.local/share/${dataDirName}`;
// mock set: os.platform, os.homedir and pathJoin return values
setMockPlatform("linux", homedir, mockPlatformDataPath);
// use Generator to precisely control order of fs.existSync return values
const existsSyncMockGen = (function* () {
// 1) fs.existSync -> case B
yield false;
// 2) fs.existSync -> case C -> checking if default OS PlatformAppDataDir exists
yield true;
// 3) fs.existSync -> case C -> checking if Trilium Data folder exists
yield false;
})();
mockFn.existsSyncMock.mockImplementation(() => existsSyncMockGen.next().value);
const result = getTriliumDataDir(dataDirName);
expect(mockFn.existsSyncMock).toHaveBeenCalledTimes(3);
expect(mockFn.mkdirSyncMock).toHaveBeenCalledTimes(1);
expect(result).toEqual(mockPlatformDataPath);
});
it("w/ Platform Linux, an existing App Data Folder (~/.local/share) AND an existing Trilium Data dir it should return path to the dir", async () => {
const homedir = "/home/mock";
const dataDirName = "trilium-data";
const mockPlatformDataPath = `${homedir}/.local/share/${dataDirName}`;
// mock set: os.platform, os.homedir and pathJoin return values
setMockPlatform("linux", homedir, mockPlatformDataPath);
// use Generator to precisely control order of fs.existSync return values
const existsSyncMockGen = (function* () {
// 1) fs.existSync -> case B
yield false;
// 2) fs.existSync -> case C -> checking if default OS PlatformAppDataDir exists
yield true;
// 3) fs.existSync -> case C -> checking if Trilium Data folder exists
yield true;
})();
mockFn.existsSyncMock.mockImplementation(() => existsSyncMockGen.next().value);
const result = getTriliumDataDir(dataDirName);
expect(result).toEqual(mockPlatformDataPath);
expect(mockFn.existsSyncMock).toHaveBeenCalledTimes(3);
expect(mockFn.mkdirSyncMock).toHaveBeenCalledTimes(0);
});
it("w/ Platform 'win32' and set process.env.APPDATA behaviour", async () => {
const homedir = "C:\\Users\\mock";
const dataDirName = "trilium-data";
const appDataDir = `${homedir}\\AppData\\Roaming`;
const mockPlatformDataPath = `${appDataDir}\\${dataDirName}`;
process.env.APPDATA = `${appDataDir}`;
// mock set: os.platform, os.homedir and pathJoin return values
setMockPlatform("win32", homedir, mockPlatformDataPath);
// use Generator to precisely control order of fs.existSync return values
const existsSyncMockGen = (function* () {
// 1) fs.existSync -> case B
yield false;
// 2) fs.existSync -> case C -> checking if default OS PlatformAppDataDir exists
yield true;
// 3) fs.existSync -> case C -> checking if Trilium Data folder exists
yield false;
})();
mockFn.existsSyncMock.mockImplementation(() => existsSyncMockGen.next().value);
const result = getTriliumDataDir(dataDirName);
expect(result).toEqual(mockPlatformDataPath);
expect(mockFn.existsSyncMock).toHaveBeenCalledTimes(3);
expect(mockFn.mkdirSyncMock).toHaveBeenCalledTimes(1);
});
});
describe("case D", () => {
it("w/ unknown PlatformAppDataDir it should attempt to create the folder in the homefolder", async () => {
const homedir = "/home/mock";
const dataDirName = "trilium-data";
const mockPlatformDataPath = `${homedir}/${dataDirName}`;
setMockPlatform("aix", homedir, mockPlatformDataPath);
const existsSyncMockGen = (function* () {
// first fs.existSync -> case B -> checking if folder exists in home folder
yield false;
// second fs.existSync -> case D -> triggered by createDirIfNotExisting
yield false;
})();
mockFn.existsSyncMock.mockImplementation(() => existsSyncMockGen.next().value);
const result = getTriliumDataDir(dataDirName);
expect(result).toEqual(mockPlatformDataPath);
expect(mockFn.existsSyncMock).toHaveBeenCalledTimes(2);
expect(mockFn.mkdirSyncMock).toHaveBeenCalledTimes(1);
});
});
});
describe("#getDataDirs()", () => {
const envKeys: Omit<keyof ReturnType<typeof getDataDirs>, "TRILIUM_DATA_DIR">[] = ["DOCUMENT_PATH", "BACKUP_DIR", "LOG_DIR", "ANONYMIZED_DB_DIR", "CONFIG_INI_PATH"];
const setMockedEnv = (prefix: string | null) => {
envKeys.forEach((key) => {
if (prefix) {
process.env[`TRILIUM_${key}`] = `${prefix}_${key}`;
} else {
delete process.env[`TRILIUM_${key}`];
}
});
};
it("w/ process.env values present, it should return an object using values from process.env", () => {
// set mocked values
const mockValuePrefix = "MOCK";
setMockedEnv(mockValuePrefix);
// get result
const result = getDataDirs(`${mockValuePrefix}_TRILIUM_DATA_DIR`);
for (const key in result) {
expect(result[key as keyof typeof result]).toEqual(`${mockValuePrefix}_${key}`);
}
});
it("w/ NO process.env values present, it should return an object using supplied TRILIUM_DATA_DIR as base", () => {
// make sure values are undefined
setMockedEnv(null);
// mock pathJoin implementation to just return mockDataDir
const mockDataDir = "/home/test/MOCK_TRILIUM_DATA_DIR";
mockFn.pathJoinMock.mockImplementation(() => mockDataDir);
const result = getDataDirs(mockDataDir);
for (const key in result) {
expect(result[key as keyof typeof result].startsWith(mockDataDir)).toBeTruthy();
}
mockFn.pathJoinMock.mockReset();
});
it("should ignore attempts to change a property on the returned object", () => {
// make sure values are undefined
setMockedEnv(null);
const mockDataDirBase = "/home/test/MOCK_TRILIUM_DATA_DIR";
const result = getDataDirs(mockDataDirBase);
// as per MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#description
// Any attempt to change a frozen object will, either silently be ignored or
// throw a TypeError exception (most commonly, but not exclusively, when in strict mode).
// so be safe and check for both, even though it looks weird
const getChangeAttemptResult = () => {
try {
//@ts-expect-error - attempt to change value of readonly property
result.BACKUP_DIR = "attempt to change";
return result.BACKUP_DIR;
} catch (error) {
return error;
}
};
const changeAttemptResult = getChangeAttemptResult();
if (typeof changeAttemptResult === "string") {
// if it didn't throw above: assert that it did not change the value of it or any other keys of the object
for (const key in result) {
expect(result[key as keyof typeof result].startsWith(mockDataDirBase)).toBeTruthy();
}
} else {
expect(changeAttemptResult).toBeInstanceOf(TypeError);
}
});
});
});

View File

@@ -63,6 +63,7 @@ async function initNotSyncedOptions(initialized: boolean, opts: NotSyncedOpts =
optionService.createOption("lastSyncedPush", "0", false);
optionService.createOption("theme", "next", false);
optionService.createOption("textNoteEditorType", "ckeditor-classic", true);
optionService.createOption("syncServerHost", opts.syncServerHost || "", false);
optionService.createOption("syncServerTimeout", "120000", false);

View File

@@ -0,0 +1,37 @@
import { expect, describe, it } from "vitest";
import sanitizeAttributeName from "./sanitize_attribute_name.js";
// fn value, expected value
const testCases: [fnValue: string, expectedValue: string][] = [
["testName", "testName"],
["test_name", "test_name"],
["test with space", "test_with_space"],
["test:with:colon", "test:with:colon"],
// numbers
["123456", "123456"],
["123:456", "123:456"],
["123456 abc", "123456_abc"],
// non-latin characters
["ε", "ε"],
["attribute ε", "attribute_ε"],
// special characters
["test/name", "test_name"],
["test%name", "test_name"],
["\/", "_"],
// empty string
["", "unnamed"]
];
describe("sanitizeAttributeName unit tests", () => {
testCases.forEach((testCase) => {
return it(`'${testCase[0]}' should return '${testCase[1]}'`, () => {
const [value, expected] = testCase;
const actual = sanitizeAttributeName(value);
expect(actual).toStrictEqual(expected);
});
});
});

View File

@@ -0,0 +1,13 @@
import { describe, it, expect } from "vitest";
import handleParens from "./handle_parens.js";
import type { TokenStructure } from "./types.js";
describe("Parens handler", () => {
it("handles parens", () => {
const input = ["(", "hello", ")", "and", "(", "(", "pick", "one", ")", "and", "another", ")"].map((token) => ({ token }));
const actual: TokenStructure = [[{ token: "hello" }], { token: "and" }, [[{ token: "pick" }, { token: "one" }], { token: "and" }, { token: "another" }]];
expect(handleParens(input)).toEqual(actual);
});
});

View File

@@ -0,0 +1,163 @@
import { describe, it, expect } from "vitest";
import lex from "./lex.js";
describe("Lexer fulltext", () => {
it("simple lexing", () => {
expect(lex("hello world").fulltextTokens.map((t) => t.token)).toEqual(["hello", "world"]);
expect(lex("hello, world").fulltextTokens.map((t) => t.token)).toEqual(["hello", "world"]);
});
it("use quotes to keep words together", () => {
expect(lex("'hello world' my friend").fulltextTokens.map((t) => t.token)).toEqual(["hello world", "my", "friend"]);
expect(lex('"hello world" my friend').fulltextTokens.map((t) => t.token)).toEqual(["hello world", "my", "friend"]);
expect(lex("`hello world` my friend").fulltextTokens.map((t) => t.token)).toEqual(["hello world", "my", "friend"]);
});
it("you can use different quotes and other special characters inside quotes", () => {
expect(lex("'i can use \" or ` or #~=*' without problem").fulltextTokens.map((t) => t.token)).toEqual(['i can use " or ` or #~=*', "without", "problem"]);
});
it("I can use backslash to escape quotes", () => {
expect(lex('hello \\"world\\"').fulltextTokens.map((t) => t.token)).toEqual(["hello", '"world"']);
expect(lex("hello \\'world\\'").fulltextTokens.map((t) => t.token)).toEqual(["hello", "'world'"]);
expect(lex("hello \\`world\\`").fulltextTokens.map((t) => t.token)).toEqual(["hello", "`world`"]);
expect(lex('"hello \\"world\\"').fulltextTokens.map((t) => t.token)).toEqual(['hello "world"']);
expect(lex("'hello \\'world\\''").fulltextTokens.map((t) => t.token)).toEqual(["hello 'world'"]);
expect(lex("`hello \\`world\\``").fulltextTokens.map((t) => t.token)).toEqual(["hello `world`"]);
expect(lex("\\#token").fulltextTokens.map((t) => t.token)).toEqual(["#token"]);
});
it("quote inside a word does not have a special meaning", () => {
const lexResult = lex("d'Artagnan is dead #hero = d'Artagnan");
expect(lexResult.fulltextTokens.map((t) => t.token)).toEqual(["d'artagnan", "is", "dead"]);
expect(lexResult.expressionTokens.map((t) => t.token)).toEqual(["#hero", "=", "d'artagnan"]);
});
it("if quote is not ended then it's just one long token", () => {
expect(lex("'unfinished quote").fulltextTokens.map((t) => t.token)).toEqual(["unfinished quote"]);
});
it("parenthesis and symbols in fulltext section are just normal characters", () => {
expect(lex("what's u=p <b(r*t)h>").fulltextTokens.map((t) => t.token)).toEqual(["what's", "u=p", "<b(r*t)h>"]);
});
it("operator characters in expressions are separate tokens", () => {
expect(lex("# abc+=-def**-+d").expressionTokens.map((t) => t.token)).toEqual(["#", "abc", "+=-", "def", "**-+", "d"]);
});
it("escaping special characters", () => {
expect(lex("hello \\#\\~\\'").fulltextTokens.map((t) => t.token)).toEqual(["hello", "#~'"]);
});
});
describe("Lexer expression", () => {
it("simple attribute existence", () => {
expect(lex("#label ~relation").expressionTokens.map((t) => t.token)).toEqual(["#label", "~relation"]);
});
it("simple label operators", () => {
expect(lex("#label*=*text").expressionTokens.map((t) => t.token)).toEqual(["#label", "*=*", "text"]);
});
it("simple label operator with in quotes", () => {
expect(lex("#label*=*'text'").expressionTokens).toEqual([
{ token: "#label", inQuotes: false, startIndex: 0, endIndex: 5 },
{ token: "*=*", inQuotes: false, startIndex: 6, endIndex: 8 },
{ token: "text", inQuotes: true, startIndex: 10, endIndex: 13 }
]);
});
it("simple label operator with param without quotes", () => {
expect(lex("#label*=*text").expressionTokens).toEqual([
{ token: "#label", inQuotes: false, startIndex: 0, endIndex: 5 },
{ token: "*=*", inQuotes: false, startIndex: 6, endIndex: 8 },
{ token: "text", inQuotes: false, startIndex: 9, endIndex: 12 }
]);
});
it("simple label operator with empty string param", () => {
expect(lex("#label = ''").expressionTokens).toEqual([
{ token: "#label", inQuotes: false, startIndex: 0, endIndex: 5 },
{ token: "=", inQuotes: false, startIndex: 7, endIndex: 7 },
// weird case for empty strings which ends up with endIndex < startIndex :-(
{ token: "", inQuotes: true, startIndex: 10, endIndex: 9 }
]);
});
it("note. prefix also separates fulltext from expression", () => {
expect(lex(`hello fulltext note.labels.capital = Prague`).expressionTokens.map((t) => t.token)).toEqual(["note", ".", "labels", ".", "capital", "=", "prague"]);
});
it("note. prefix in quotes will note start expression", () => {
expect(lex(`hello fulltext "note.txt"`).expressionTokens.map((t) => t.token)).toEqual([]);
expect(lex(`hello fulltext "note.txt"`).fulltextTokens.map((t) => t.token)).toEqual(["hello", "fulltext", "note.txt"]);
});
it("complex expressions with and, or and parenthesis", () => {
expect(lex(`# (#label=text OR #second=text) AND ~relation`).expressionTokens.map((t) => t.token)).toEqual([
"#",
"(",
"#label",
"=",
"text",
"or",
"#second",
"=",
"text",
")",
"and",
"~relation"
]);
});
it("dot separated properties", () => {
expect(lex(`# ~author.title = 'Hugh Howey' AND note.'book title' = 'Silo'`).expressionTokens.map((t) => t.token)).toEqual([
"#",
"~author",
".",
"title",
"=",
"hugh howey",
"and",
"note",
".",
"book title",
"=",
"silo"
]);
});
it("negation of label and relation", () => {
expect(lex(`#!capital ~!neighbor`).expressionTokens.map((t) => t.token)).toEqual(["#!capital", "~!neighbor"]);
});
it("negation of sub-expression", () => {
expect(lex(`# not(#capital) and note.noteId != "root"`).expressionTokens.map((t) => t.token)).toEqual(["#", "not", "(", "#capital", ")", "and", "note", ".", "noteid", "!=", "root"]);
});
it("order by multiple labels", () => {
expect(lex(`# orderby #a,#b`).expressionTokens.map((t) => t.token)).toEqual(["#", "orderby", "#a", ",", "#b"]);
});
});
describe("Lexer invalid queries and edge cases", () => {
it("concatenated attributes", () => {
expect(lex("#label~relation").expressionTokens.map((t) => t.token)).toEqual(["#label", "~relation"]);
});
it("trailing escape \\", () => {
expect(lex("abc \\").fulltextTokens.map((t) => t.token)).toEqual(["abc", "\\"]);
});
});

View File

@@ -0,0 +1,356 @@
import { describe, it, expect } from "vitest";
import AndExp from "../../search/expressions/and.js";
import AttributeExistsExp from "../../search/expressions/attribute_exists.js";
import type Expression from "../../search/expressions/expression.js";
import LabelComparisonExp from "../../search/expressions/label_comparison.js";
import NotExp from "../../search/expressions/not.js";
import NoteContentFulltextExp from "../../search/expressions/note_content_fulltext.js";
import NoteFlatTextExp from "../../search/expressions/note_flat_text.js";
import OrExp from "../../search/expressions/or.js";
import OrderByAndLimitExp from "../../search/expressions/order_by_and_limit.js";
import PropertyComparisonExp from "../../search/expressions/property_comparison.js";
import SearchContext from "../../search/search_context.js";
import { default as parseInternal, type ParseOpts } from "./parse.js";
describe("Parser", () => {
it("fulltext parser without content", () => {
const rootExp = parse({
fulltextTokens: tokens(["hello", "hi"]),
expressionTokens: [],
searchContext: new SearchContext()
}, AndExp);
expectExpression(rootExp.subExpressions[0], PropertyComparisonExp);
const orExp = expectExpression(rootExp.subExpressions[2], OrExp);
const flatTextExp = expectExpression(orExp.subExpressions[0], NoteFlatTextExp);
expect(flatTextExp.tokens).toEqual(["hello", "hi"]);
});
it("fulltext parser with content", () => {
const rootExp = parse({
fulltextTokens: tokens(["hello", "hi"]),
expressionTokens: [],
searchContext: new SearchContext()
}, AndExp);
assertIsArchived(rootExp.subExpressions[0]);
const orExp = expectExpression(rootExp.subExpressions[2], OrExp);
const firstSub = expectExpression(orExp.subExpressions[0], NoteFlatTextExp);
expect(firstSub.tokens).toEqual(["hello", "hi"]);
const secondSub = expectExpression(orExp.subExpressions[1], NoteContentFulltextExp);
expect(secondSub.tokens).toEqual(["hello", "hi"]);
});
it("simple label comparison", () => {
const rootExp = parse({
fulltextTokens: [],
expressionTokens: tokens(["#mylabel", "=", "text"]),
searchContext: new SearchContext()
}, AndExp);
assertIsArchived(rootExp.subExpressions[0]);
const labelComparisonExp = expectExpression(rootExp.subExpressions[2], LabelComparisonExp);
expect(labelComparisonExp.attributeType).toEqual("label");
expect(labelComparisonExp.attributeName).toEqual("mylabel");
expect(labelComparisonExp.comparator).toBeTruthy();
});
it("simple attribute negation", () => {
let rootExp = parse({
fulltextTokens: [],
expressionTokens: tokens(["#!mylabel"]),
searchContext: new SearchContext()
}, AndExp);
assertIsArchived(rootExp.subExpressions[0]);
let notExp = expectExpression(rootExp.subExpressions[2], NotExp);
let attributeExistsExp = expectExpression(notExp.subExpression, AttributeExistsExp);
expect(attributeExistsExp.attributeType).toEqual("label");
expect(attributeExistsExp.attributeName).toEqual("mylabel");
rootExp = parse({
fulltextTokens: [],
expressionTokens: tokens(["~!myrelation"]),
searchContext: new SearchContext()
}, AndExp);
assertIsArchived(rootExp.subExpressions[0]);
notExp = expectExpression(rootExp.subExpressions[2], NotExp);
attributeExistsExp = expectExpression(notExp.subExpression, AttributeExistsExp);
expect(attributeExistsExp.attributeType).toEqual("relation");
expect(attributeExistsExp.attributeName).toEqual("myrelation");
});
it("simple label AND", () => {
const rootExp = parse({
fulltextTokens: [],
expressionTokens: tokens(["#first", "=", "text", "and", "#second", "=", "text"]),
searchContext: new SearchContext()
}, AndExp);
assertIsArchived(rootExp.subExpressions[0]);
const andExp = expectExpression(rootExp.subExpressions[2], AndExp);
const [firstSub, secondSub] = expectSubexpressions(andExp, LabelComparisonExp, LabelComparisonExp);
expect(firstSub.attributeName).toEqual("first");
expect(secondSub.attributeName).toEqual("second");
});
it("simple label AND without explicit AND", () => {
const rootExp = parse({
fulltextTokens: [],
expressionTokens: tokens(["#first", "=", "text", "#second", "=", "text"]),
searchContext: new SearchContext()
}, AndExp);
assertIsArchived(rootExp.subExpressions[0]);
const andExp = expectExpression(rootExp.subExpressions[2], AndExp);
const [firstSub, secondSub] = expectSubexpressions(andExp, LabelComparisonExp, LabelComparisonExp);
expect(firstSub.attributeName).toEqual("first");
expect(secondSub.attributeName).toEqual("second");
});
it("simple label OR", () => {
const rootExp = parse({
fulltextTokens: [],
expressionTokens: tokens(["#first", "=", "text", "or", "#second", "=", "text"]),
searchContext: new SearchContext()
}, AndExp);
assertIsArchived(rootExp.subExpressions[0]);
const orExp = expectExpression(rootExp.subExpressions[2], OrExp);
const [firstSub, secondSub] = expectSubexpressions(orExp, LabelComparisonExp, LabelComparisonExp);
expect(firstSub.attributeName).toEqual("first");
expect(secondSub.attributeName).toEqual("second");
});
it("fulltext and simple label", () => {
const rootExp = parse({
fulltextTokens: tokens(["hello"]),
expressionTokens: tokens(["#mylabel", "=", "text"]),
searchContext: new SearchContext()
}, AndExp);
const [firstSub, _, thirdSub, fourth] = expectSubexpressions(rootExp, PropertyComparisonExp, undefined, OrExp, LabelComparisonExp);
expect(firstSub.propertyName).toEqual("isArchived");
const noteFlatTextExp = expectExpression(thirdSub.subExpressions[0], NoteFlatTextExp);
expect(noteFlatTextExp.tokens).toEqual(["hello"]);
expect(fourth.attributeName).toEqual("mylabel");
});
it("label sub-expression", () => {
const rootExp = parse({
fulltextTokens: [],
expressionTokens: tokens(["#first", "=", "text", "or", ["#second", "=", "text", "and", "#third", "=", "text"]]),
searchContext: new SearchContext()
}, AndExp);
assertIsArchived(rootExp.subExpressions[0]);
const orExp = expectExpression(rootExp.subExpressions[2], OrExp);
const [firstSub, secondSub] = expectSubexpressions(orExp, LabelComparisonExp, AndExp);
expect(firstSub.attributeName).toEqual("first");
const [firstSubSub, secondSubSub] = expectSubexpressions(secondSub, LabelComparisonExp, LabelComparisonExp);
expect(firstSubSub.attributeName).toEqual("second");
expect(secondSubSub.attributeName).toEqual("third");
});
it("label sub-expression without explicit operator", () => {
const rootExp = parse({
fulltextTokens: [],
expressionTokens: tokens(["#first", ["#second", "or", "#third"], "#fourth"]),
searchContext: new SearchContext()
}, AndExp);
assertIsArchived(rootExp.subExpressions[0]);
const andExp = expectExpression(rootExp.subExpressions[2], AndExp);
const [firstSub, secondSub, thirdSub] = expectSubexpressions(andExp, AttributeExistsExp, OrExp, AttributeExistsExp);
expect(firstSub.attributeName).toEqual("first");
const [firstSubSub, secondSubSub] = expectSubexpressions(secondSub, AttributeExistsExp, AttributeExistsExp);
expect(firstSubSub.attributeName).toEqual("second");
expect(secondSubSub.attributeName).toEqual("third");
expect(thirdSub.attributeName).toEqual("fourth");
});
it("parses limit without order by", () => {
const rootExp = parse({
fulltextTokens: tokens(["hello", "hi"]),
expressionTokens: [],
searchContext: new SearchContext({ limit: 2 })
}, OrderByAndLimitExp);
expect(rootExp.limit).toBe(2);
expect(rootExp.subExpression).toBeInstanceOf(AndExp);
});
});
describe("Invalid expressions", () => {
it("incomplete comparison", () => {
const searchContext = new SearchContext();
parseInternal({
fulltextTokens: [],
expressionTokens: tokens(["#first", "="]),
searchContext
});
expect(searchContext.error).toEqual('Misplaced or incomplete expression "="');
});
it("comparison between labels is impossible", () => {
let searchContext = new SearchContext();
searchContext.originalQuery = "#first = #second";
parseInternal({
fulltextTokens: [],
expressionTokens: tokens(["#first", "=", "#second"]),
searchContext
});
expect(searchContext.error).toEqual(`Error near token "#second" in "#first = #second", it's possible to compare with constant only.`);
searchContext = new SearchContext();
searchContext.originalQuery = "#first = note.relations.second";
parseInternal({
fulltextTokens: [],
expressionTokens: tokens(["#first", "=", "note", ".", "relations", "second"]),
searchContext
});
expect(searchContext.error).toEqual(`Error near token "note" in "#first = note.relations.second", it's possible to compare with constant only.`);
const rootExp = parse({
fulltextTokens: [],
expressionTokens: [
{ token: "#first", inQuotes: false },
{ token: "=", inQuotes: false },
{ token: "#second", inQuotes: true }
],
searchContext: new SearchContext()
}, AndExp);
assertIsArchived(rootExp.subExpressions[0]);
const labelComparisonExp = expectExpression(rootExp.subExpressions[2], LabelComparisonExp);
expect(labelComparisonExp.attributeType).toEqual("label");
expect(labelComparisonExp.attributeName).toEqual("first");
expect(labelComparisonExp.comparator).toBeTruthy();
});
it("searching by relation without note property", () => {
const searchContext = new SearchContext();
parseInternal({
fulltextTokens: [],
expressionTokens: tokens(["~first", "=", "text", "-", "abc"]),
searchContext
});
expect(searchContext.error).toEqual('Relation can be compared only with property, e.g. ~relation.title=hello in ""');
});
});
type ClassType<T extends Expression> = new (...args: any[]) => T;
function tokens(toks: (string | string[])[], cur = 0): Array<any> {
return toks.map((arg) => {
if (Array.isArray(arg)) {
return tokens(arg, cur);
} else {
cur += arg.length;
return {
token: arg,
inQuotes: false,
startIndex: cur - arg.length,
endIndex: cur - 1
};
}
});
}
function assertIsArchived(_exp: Expression) {
const exp = expectExpression(_exp, PropertyComparisonExp);
expect(exp.propertyName).toEqual("isArchived");
expect(exp.operator).toEqual("=");
expect(exp.comparedValue).toEqual("false");
}
/**
* Parses the corresponding {@link Expression} from plain text, while also expecting the resulting expression to be of the given type.
*
* @param opts the options for parsing.
* @param type the expected type of the expression.
* @returns the expression typecasted to the expected type.
*/
function parse<T extends Expression>(opts: ParseOpts, type: ClassType<T>) {
return expectExpression(parseInternal(opts), type);
}
/**
* Expects the given {@link Expression} to be of the given type.
*
* @param exp an instance of an {@link Expression}.
* @param type a type class such as {@link AndExp}, {@link OrExp}, etc.
* @returns the same expression typecasted to the expected type.
*/
function expectExpression<T extends Expression>(exp: Expression, type: ClassType<T>) {
expect(exp).toBeInstanceOf(type);
return exp as T;
}
/**
* For an {@link AndExp}, it goes through all its subexpressions (up to fourth) and checks their type and returns them as a typecasted array.
* Each subexpression can have their own type.
*
* @param exp the expression containing one or more subexpressions.
* @param firstType the type of the first subexpression.
* @param secondType the type of the second subexpression.
* @param thirdType the type of the third subexpression.
* @param fourthType the type of the fourth subexpression.
* @returns an array of all the subexpressions (in order) typecasted to their expected type.
*/
function expectSubexpressions<FirstT extends Expression,
SecondT extends Expression,
ThirdT extends Expression,
FourthT extends Expression>(
exp: AndExp,
firstType: ClassType<FirstT>,
secondType?: ClassType<SecondT>,
thirdType?: ClassType<ThirdT>,
fourthType?: ClassType<FourthT>): [ FirstT, SecondT, ThirdT, FourthT ]
{
expectExpression(exp.subExpressions[0], firstType);
if (secondType) {
expectExpression(exp.subExpressions[1], secondType);
}
if (thirdType) {
expectExpression(exp.subExpressions[2], thirdType);
}
if (fourthType) {
expectExpression(exp.subExpressions[3], fourthType);
}
return [
exp.subExpressions[0] as FirstT,
exp.subExpressions[1] as SecondT,
exp.subExpressions[2] as ThirdT,
exp.subExpressions[3] as FourthT
]
}

View File

@@ -0,0 +1,601 @@
import { describe, it, expect, beforeEach, } from "vitest";
import searchService from "./search.js";
import BNote from "../../../becca/entities/bnote.js";
import BBranch from "../../../becca/entities/bbranch.js";
import SearchContext from "../search_context.js";
import dateUtils from "../../date_utils.js";
import becca from "../../../becca/becca.js";
import becca_mocking from "../../../../spec/support/becca_mocking.js";
describe("Search", () => {
let rootNote: any;
beforeEach(() => {
becca.reset();
rootNote = new becca_mocking.NoteBuilder(new BNote({ noteId: "root", title: "root", type: "text" }));
new BBranch({
branchId: "none_root",
noteId: "root",
parentNoteId: "none",
notePosition: 10
});
});
it.skip("simple path match", () => {
rootNote.child(becca_mocking.note("Europe").child(becca_mocking.note("Austria")));
const searchContext = new SearchContext();
const searchResults = searchService.findResultsWithQuery("europe austria", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
it.skip("normal search looks also at attributes", () => {
const austria = becca_mocking.note("Austria");
const vienna = becca_mocking.note("Vienna");
rootNote.child(austria.relation("capital", vienna.note)).child(vienna.label("inhabitants", "1888776"));
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("capital", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("inhabitants", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Vienna")).toBeTruthy();
});
it.skip("normal search looks also at type and mime", () => {
rootNote.child(becca_mocking.note("Effective Java", { type: "book", mime: "" })).child(becca_mocking.note("Hello World.java", { type: "code", mime: "text/x-java" }));
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("book", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Effective Java")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("text", searchContext); // should match mime
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Hello World.java")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("java", searchContext);
expect(searchResults.length).toEqual(2);
});
it.skip("only end leafs are results", () => {
rootNote.child(becca_mocking.note("Europe").child(becca_mocking.note("Austria")));
const searchContext = new SearchContext();
const searchResults = searchService.findResultsWithQuery("europe", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Europe")).toBeTruthy();
});
it.skip("only end leafs are results", () => {
rootNote.child(becca_mocking.note("Europe").child(becca_mocking.note("Austria").label("capital", "Vienna")));
const searchContext = new SearchContext();
const searchResults = searchService.findResultsWithQuery("Vienna", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
it("label comparison with short syntax", () => {
rootNote.child(becca_mocking.note("Europe").child(becca_mocking.note("Austria").label("capital", "Vienna")).child(becca_mocking.note("Czech Republic").label("capital", "Prague")));
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("#capital=Vienna", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
// case sensitivity:
searchResults = searchService.findResultsWithQuery("#CAPITAL=VIENNA", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("#caPItal=vienNa", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
it("label comparison with full syntax", () => {
rootNote.child(becca_mocking.note("Europe").child(becca_mocking.note("Austria").label("capital", "Vienna")).child(becca_mocking.note("Czech Republic").label("capital", "Prague")));
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("# note.labels.capital=Prague", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
it("numeric label comparison", () => {
rootNote.child(
becca_mocking
.note("Europe")
.label("country", "", true)
.child(becca_mocking.note("Austria").label("population", "8859000"))
.child(becca_mocking.note("Czech Republic").label("population", "10650000"))
);
const searchContext = new SearchContext();
const searchResults = searchService.findResultsWithQuery("#country #population >= 10000000", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
it.skip("inherited label comparison", () => {
rootNote.child(becca_mocking.note("Europe").label("country", "", true).child(becca_mocking.note("Austria")).child(becca_mocking.note("Czech Republic")));
const searchContext = new SearchContext();
const searchResults = searchService.findResultsWithQuery("austria #country", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
it("numeric label comparison fallback to string comparison", () => {
// dates should not be coerced into numbers which would then give wrong numbers
rootNote.child(
becca_mocking
.note("Europe")
.label("country", "", true)
.child(becca_mocking.note("Austria").label("established", "1955-07-27"))
.child(becca_mocking.note("Czech Republic").label("established", "1993-01-01"))
.child(becca_mocking.note("Hungary").label("established", "1920-06-04"))
);
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery('#established <= "1955-01-01"', searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Hungary")).toBeTruthy();
searchResults = searchService.findResultsWithQuery('#established > "1955-01-01"', searchContext);
expect(searchResults.length).toEqual(2);
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
it("smart date comparisons", () => {
// dates should not be coerced into numbers which would then give wrong numbers
rootNote.child(
becca_mocking
.note("My note", { dateCreated: dateUtils.localNowDateTime() })
.label("year", new Date().getFullYear().toString())
.label("month", dateUtils.localNowDate().substr(0, 7))
.label("date", dateUtils.localNowDate())
.label("dateTime", dateUtils.localNowDateTime())
);
const searchContext = new SearchContext();
function test(query: string, expectedResultCount: number) {
const searchResults = searchService.findResultsWithQuery(query, searchContext);
expect(searchResults.length, `Searching for '${query}' unexpectedly returned ${Number(searchResults?.length)} instead of ${expectedResultCount} results. SearchResult: '${JSON.stringify(searchResults)}'`)
.toEqual(expectedResultCount);
if (expectedResultCount === 1) {
expect(becca_mocking.findNoteByTitle(searchResults, "My note")).toBeTruthy();
}
}
test("#year = YEAR", 1);
test("#year = 'YEAR'", 0);
test("#year >= YEAR", 1);
test("#year <= YEAR", 1);
test("#year < YEAR+1", 1);
test("#year < YEAR + 1", 1);
test("#year < year + 1", 1);
test("#year > YEAR+1", 0);
test("#month = MONTH", 1);
test("#month = month", 1);
test("#month = 'MONTH'", 0);
test("note.dateCreated =* month", 2);
test("#date = TODAY", 1);
test("#date = today", 1);
test("#date = 'today'", 0);
test("#date > TODAY", 0);
test("#date > TODAY-1", 1);
test("#date > TODAY - 1", 1);
test("#date < TODAY+1", 1);
test("#date < TODAY + 1", 1);
test("#date < 'TODAY + 1'", 1);
test("#dateTime <= NOW+10", 1);
test("#dateTime <= NOW + 10", 1);
test("#dateTime < NOW-10", 0);
test("#dateTime >= NOW-10", 1);
test("#dateTime < NOW-10", 0);
});
it("logical or", () => {
rootNote.child(
becca_mocking
.note("Europe")
.label("country", "", true)
.child(becca_mocking.note("Austria").label("languageFamily", "germanic"))
.child(becca_mocking.note("Czech Republic").label("languageFamily", "slavic"))
.child(becca_mocking.note("Hungary").label("languageFamily", "finnougric"))
);
const searchContext = new SearchContext();
const searchResults = searchService.findResultsWithQuery("#languageFamily = slavic OR #languageFamily = germanic", searchContext);
expect(searchResults.length).toEqual(2);
expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
it("fuzzy attribute search", () => {
rootNote.child(
becca_mocking
.note("Europe")
.label("country", "", true)
.child(becca_mocking.note("Austria").label("languageFamily", "germanic"))
.child(becca_mocking.note("Czech Republic").label("languageFamily", "slavic"))
);
let searchContext = new SearchContext({ fuzzyAttributeSearch: false });
let searchResults = searchService.findResultsWithQuery("#language", searchContext);
expect(searchResults.length).toEqual(0);
searchResults = searchService.findResultsWithQuery("#languageFamily=ger", searchContext);
expect(searchResults.length).toEqual(0);
searchContext = new SearchContext({ fuzzyAttributeSearch: true });
searchResults = searchService.findResultsWithQuery("#language", searchContext);
expect(searchResults.length).toEqual(2);
searchResults = searchService.findResultsWithQuery("#languageFamily=ger", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
it("filter by note property", () => {
rootNote.child(becca_mocking.note("Europe").child(becca_mocking.note("Austria")).child(becca_mocking.note("Czech Republic")));
const searchContext = new SearchContext();
const searchResults = searchService.findResultsWithQuery("# note.title =* czech", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
it("filter by note's parent", () => {
rootNote
.child(
becca_mocking
.note("Europe")
.child(becca_mocking.note("Austria"))
.child(becca_mocking.note("Czech Republic").child(becca_mocking.note("Prague")))
)
.child(becca_mocking.note("Asia").child(becca_mocking.note("Taiwan")));
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("# note.parents.title = Europe", searchContext);
expect(searchResults.length).toEqual(2);
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("# note.parents.title = Asia", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Taiwan")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("# note.parents.parents.title = Europe", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Prague")).toBeTruthy();
});
it("filter by note's ancestor", () => {
rootNote
.child(
becca_mocking
.note("Europe")
.child(becca_mocking.note("Austria"))
.child(becca_mocking.note("Czech Republic").child(becca_mocking.note("Prague").label("city")))
)
.child(becca_mocking.note("Asia").child(becca_mocking.note("Taiwan").child(becca_mocking.note("Taipei").label("city"))));
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("#city AND note.ancestors.title = Europe", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Prague")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("#city AND note.ancestors.title = Asia", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Taipei")).toBeTruthy();
});
it("filter by note's child", () => {
rootNote
.child(
becca_mocking
.note("Europe")
.child(becca_mocking.note("Austria").child(becca_mocking.note("Vienna")))
.child(becca_mocking.note("Czech Republic").child(becca_mocking.note("Prague")))
)
.child(becca_mocking.note("Oceania").child(becca_mocking.note("Australia")));
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("# note.children.title =* Aust", searchContext);
expect(searchResults.length).toEqual(2);
expect(becca_mocking.findNoteByTitle(searchResults, "Europe")).toBeTruthy();
expect(becca_mocking.findNoteByTitle(searchResults, "Oceania")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("# note.children.title =* Aust AND note.children.title *= republic", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Europe")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("# note.children.children.title = Prague", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Europe")).toBeTruthy();
});
it("filter by relation's note properties using short syntax", () => {
const austria = becca_mocking.note("Austria");
const portugal = becca_mocking.note("Portugal");
rootNote.child(
becca_mocking
.note("Europe")
.child(austria)
.child(becca_mocking.note("Czech Republic").relation("neighbor", austria.note))
.child(portugal)
.child(becca_mocking.note("Spain").relation("neighbor", portugal.note))
);
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("# ~neighbor.title = Austria", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("# ~neighbor.title = Portugal", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Spain")).toBeTruthy();
});
it("filter by relation's note properties using long syntax", () => {
const austria = becca_mocking.note("Austria");
const portugal = becca_mocking.note("Portugal");
rootNote.child(
becca_mocking
.note("Europe")
.child(austria)
.child(becca_mocking.note("Czech Republic").relation("neighbor", austria.note))
.child(portugal)
.child(becca_mocking.note("Spain").relation("neighbor", portugal.note))
);
const searchContext = new SearchContext();
const searchResults = searchService.findResultsWithQuery("# note.relations.neighbor.title = Austria", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
it("filter by multiple level relation", () => {
const austria = becca_mocking.note("Austria");
const slovakia = becca_mocking.note("Slovakia");
const italy = becca_mocking.note("Italy");
const ukraine = becca_mocking.note("Ukraine");
rootNote.child(
becca_mocking
.note("Europe")
.child(austria.relation("neighbor", italy.note).relation("neighbor", slovakia.note))
.child(becca_mocking.note("Czech Republic").relation("neighbor", austria.note).relation("neighbor", slovakia.note))
.child(slovakia.relation("neighbor", ukraine.note))
.child(ukraine)
);
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("# note.relations.neighbor.relations.neighbor.title = Italy", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
searchResults = searchService.findResultsWithQuery("# note.relations.neighbor.relations.neighbor.title = Ukraine", searchContext);
expect(searchResults.length).toEqual(2);
expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
it("test note properties", () => {
const austria = becca_mocking.note("Austria");
austria.relation("myself", austria.note);
austria.label("capital", "Vienna");
austria.label("population", "8859000");
rootNote
.child(becca_mocking.note("Asia"))
.child(becca_mocking.note("Europe").child(austria.child(becca_mocking.note("Vienna")).child(becca_mocking.note("Sebastian Kurz"))))
.child(becca_mocking.note("Mozart").child(austria));
austria.note.isProtected = false;
austria.note.dateCreated = "2020-05-14 12:11:42.001+0200";
austria.note.dateModified = "2020-05-14 13:11:42.001+0200";
austria.note.utcDateCreated = "2020-05-14 10:11:42.001Z";
austria.note.utcDateModified = "2020-05-14 11:11:42.001Z";
// austria.note.contentLength = 1001;
const searchContext = new SearchContext();
function test(propertyName: string, value: string, expectedResultCount: number) {
const searchResults = searchService.findResultsWithQuery(`# note.${propertyName} = ${value}`, searchContext);
expect(searchResults.length).toEqual(expectedResultCount);
}
test("type", "text", 7);
test("TYPE", "TEXT", 7);
test("type", "code", 0);
test("mime", "text/html", 6);
test("mime", "application/json", 0);
test("isProtected", "false", 7);
test("isProtected", "FALSE", 7);
test("isProtected", "true", 0);
test("isProtected", "TRUE", 0);
test("dateCreated", "'2020-05-14 12:11:42.001+0200'", 1);
test("dateCreated", "wrong", 0);
test("dateModified", "'2020-05-14 13:11:42.001+0200'", 1);
test("dateModified", "wrong", 0);
test("utcDateCreated", "'2020-05-14 10:11:42.001Z'", 1);
test("utcDateCreated", "wrong", 0);
test("utcDateModified", "'2020-05-14 11:11:42.001Z'", 1);
test("utcDateModified", "wrong", 0);
test("parentCount", "2", 1);
test("parentCount", "3", 0);
test("childrenCount", "2", 1);
test("childrenCount", "10", 0);
test("attributeCount", "3", 1);
test("attributeCount", "4", 0);
test("labelCount", "2", 1);
test("labelCount", "3", 0);
test("relationCount", "1", 1);
test("relationCount", "2", 0);
});
it("test order by", () => {
const italy = becca_mocking.note("Italy").label("capital", "Rome");
const slovakia = becca_mocking.note("Slovakia").label("capital", "Bratislava");
const austria = becca_mocking.note("Austria").label("capital", "Vienna");
const ukraine = becca_mocking.note("Ukraine").label("capital", "Kiev");
rootNote.child(becca_mocking.note("Europe").child(ukraine).child(slovakia).child(austria).child(italy));
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("# note.parents.title = Europe orderBy note.title", searchContext);
expect(searchResults.length).toEqual(4);
expect(becca.notes[searchResults[0].noteId].title).toEqual("Austria");
expect(becca.notes[searchResults[1].noteId].title).toEqual("Italy");
expect(becca.notes[searchResults[2].noteId].title).toEqual("Slovakia");
expect(becca.notes[searchResults[3].noteId].title).toEqual("Ukraine");
searchResults = searchService.findResultsWithQuery("# note.parents.title = Europe orderBy note.labels.capital", searchContext);
expect(searchResults.length).toEqual(4);
expect(becca.notes[searchResults[0].noteId].title).toEqual("Slovakia");
expect(becca.notes[searchResults[1].noteId].title).toEqual("Ukraine");
expect(becca.notes[searchResults[2].noteId].title).toEqual("Italy");
expect(becca.notes[searchResults[3].noteId].title).toEqual("Austria");
searchResults = searchService.findResultsWithQuery("# note.parents.title = Europe orderBy note.labels.capital DESC", searchContext);
expect(searchResults.length).toEqual(4);
expect(becca.notes[searchResults[0].noteId].title).toEqual("Austria");
expect(becca.notes[searchResults[1].noteId].title).toEqual("Italy");
expect(becca.notes[searchResults[2].noteId].title).toEqual("Ukraine");
expect(becca.notes[searchResults[3].noteId].title).toEqual("Slovakia");
searchResults = searchService.findResultsWithQuery("# note.parents.title = Europe orderBy note.labels.capital DESC limit 2", searchContext);
expect(searchResults.length).toEqual(2);
expect(becca.notes[searchResults[0].noteId].title).toEqual("Austria");
expect(becca.notes[searchResults[1].noteId].title).toEqual("Italy");
searchResults = searchService.findResultsWithQuery("# note.parents.title = Europe orderBy #capital DESC limit 1", searchContext);
expect(searchResults.length).toEqual(1);
searchResults = searchService.findResultsWithQuery("# note.parents.title = Europe orderBy #capital DESC limit 1000", searchContext);
expect(searchResults.length).toEqual(4);
});
it("test not(...)", () => {
const italy = becca_mocking.note("Italy").label("capital", "Rome");
const slovakia = becca_mocking.note("Slovakia").label("capital", "Bratislava");
rootNote.child(becca_mocking.note("Europe").child(slovakia).child(italy));
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("# not(#capital) and note.noteId != root", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca.notes[searchResults[0].noteId].title).toEqual("Europe");
searchResults = searchService.findResultsWithQuery("#!capital and note.noteId != root", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca.notes[searchResults[0].noteId].title).toEqual("Europe");
});
it.skip("test note.text *=* something", () => {
const italy = becca_mocking.note("Italy").label("capital", "Rome");
const slovakia = becca_mocking.note("Slovakia").label("capital", "Bratislava");
rootNote.child(becca_mocking.note("Europe").child(slovakia).child(italy));
const searchContext = new SearchContext();
let searchResults = searchService.findResultsWithQuery("# note.text *=* vaki and note.noteId != root", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca.notes[searchResults[0].noteId].title).toEqual("Slovakia");
});
it.skip("test that fulltext does not match archived notes", () => {
const italy = becca_mocking.note("Italy").label("capital", "Rome");
const slovakia = becca_mocking.note("Slovakia").label("capital", "Bratislava");
rootNote.child(becca_mocking.note("Reddit").label("archived", "", true).child(becca_mocking.note("Post X")).child(becca_mocking.note("Post Y"))).child(becca_mocking.note("Reddit is bad"));
const searchContext = new SearchContext({ includeArchivedNotes: false });
let searchResults = searchService.findResultsWithQuery("reddit", searchContext);
expect(searchResults.length).toEqual(1);
expect(becca.notes[searchResults[0].noteId].title).toEqual("Reddit is bad");
});
// FIXME: test what happens when we order without any filter criteria
// it("comparison between labels", () => {
// rootNote
// .child(becca_mocking.note("Europe")
// .child(becca_mocking.note("Austria")
// .label('capital', 'Vienna')
// .label('largestCity', 'Vienna'))
// .child(becca_mocking.note("Canada")
// .label('capital', 'Ottawa')
// .label('largestCity', 'Toronto'))
// .child(becca_mocking.note("Czech Republic")
// .label('capital', 'Prague')
// .label('largestCity', 'Prague'))
// );
//
// const searchContext = new SearchContext();
//
// const searchResults = searchService.findResultsWithQuery('#capital = #largestCity', searchContext);
// expect(searchResults.length).toEqual(2);
// expect(becca_mocking.findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
// expect(becca_mocking.findNoteByTitle(searchResults, "Austria")).toBeTruthy();
// })
});

View File

@@ -0,0 +1,79 @@
import { describe, it, expect, beforeEach } from "vitest";
import becca_mocking from "../../../spec/support/becca_mocking.js";
import ValueExtractor from "./value_extractor.js";
import becca from "../../becca/becca.js";
import SearchContext from "./search_context.js";
const dsc = new SearchContext();
describe("Value extractor", () => {
beforeEach(() => {
becca.reset();
});
it("simple title extraction", async () => {
const europe = becca_mocking.note("Europe").note;
const valueExtractor = new ValueExtractor(dsc, ["note", "title"]);
expect(valueExtractor.validate()).toBeFalsy();
expect(valueExtractor.extract(europe)).toEqual("Europe");
});
it("label extraction", async () => {
const austria = becca_mocking.note("Austria").label("Capital", "Vienna").note;
let valueExtractor = new ValueExtractor(dsc, ["note", "labels", "capital"]);
expect(valueExtractor.validate()).toBeFalsy();
expect(valueExtractor.extract(austria)).toEqual("Vienna");
valueExtractor = new ValueExtractor(dsc, ["#capital"]);
expect(valueExtractor.validate()).toBeFalsy();
expect(valueExtractor.extract(austria)).toEqual("Vienna");
});
it("parent/child property extraction", async () => {
const vienna = becca_mocking.note("Vienna");
const europe = becca_mocking.note("Europe").child(becca_mocking.note("Austria").child(vienna));
let valueExtractor = new ValueExtractor(dsc, ["note", "children", "children", "title"]);
expect(valueExtractor.validate()).toBeFalsy();
expect(valueExtractor.extract(europe.note)).toEqual("Vienna");
valueExtractor = new ValueExtractor(dsc, ["note", "parents", "parents", "title"]);
expect(valueExtractor.validate()).toBeFalsy();
expect(valueExtractor.extract(vienna.note)).toEqual("Europe");
});
it("extract through relation", async () => {
const czechRepublic = becca_mocking.note("Czech Republic").label("capital", "Prague");
const slovakia = becca_mocking.note("Slovakia").label("capital", "Bratislava");
const austria = becca_mocking.note("Austria").relation("neighbor", czechRepublic.note).relation("neighbor", slovakia.note);
let valueExtractor = new ValueExtractor(dsc, ["note", "relations", "neighbor", "labels", "capital"]);
expect(valueExtractor.validate()).toBeFalsy();
expect(valueExtractor.extract(austria.note)).toEqual("Prague");
valueExtractor = new ValueExtractor(dsc, ["~neighbor", "labels", "capital"]);
expect(valueExtractor.validate()).toBeFalsy();
expect(valueExtractor.extract(austria.note)).toEqual("Prague");
});
});
describe("Invalid value extractor property path", () => {
it('each path must start with "note" (or label/relation)', () => expect(new ValueExtractor(dsc, ["neighbor"]).validate()).toBeTruthy());
it("extra path element after terminal label", () => expect(new ValueExtractor(dsc, ["~neighbor", "labels", "capital", "noteId"]).validate()).toBeTruthy());
it("extra path element after terminal title", () => expect(new ValueExtractor(dsc, ["note", "title", "isProtected"]).validate()).toBeTruthy());
it("relation name and note property is missing", () => expect(new ValueExtractor(dsc, ["note", "relations"]).validate()).toBeTruthy());
it("relation is specified but target note property is not specified", () => expect(new ValueExtractor(dsc, ["note", "relations", "myrel"]).validate()).toBeTruthy());
});

View File

@@ -213,7 +213,7 @@ function resetLauncher(noteId: string) {
if (note?.isLaunchBarConfig()) {
if (note) {
if (noteId === "_lbRoot") {
if (noteId === "_lbRoot" || noteId === "_lbMobileRoot") {
// deleting hoisted notes are not allowed, so we just reset the children
for (const childNote of note.getChildNotes()) {
childNote.deleteNote();

View File

@@ -0,0 +1,61 @@
import { expect, describe, it } from "vitest";
import { formatDownloadTitle } from "./utils.js";
const testCases: [fnValue: Parameters<typeof formatDownloadTitle>, expectedValue: ReturnType<typeof formatDownloadTitle>][] = [
// empty fileName tests
[["", "text", ""], "untitled.html"],
[["", "canvas", ""], "untitled.json"],
[["", null, ""], "untitled"],
// json extension from type tests
[["test_file", "canvas", ""], "test_file.json"],
[["test_file", "relationMap", ""], "test_file.json"],
[["test_file", "search", ""], "test_file.json"],
// extension based on mime type
[["test_file", null, "text/csv"], "test_file.csv"],
[["test_file_wo_ext", "image", "image/svg+xml"], "test_file_wo_ext.svg"],
[["test_file_wo_ext", "file", "application/json"], "test_file_wo_ext.json"],
[["test_file_w_fake_ext.ext", "image", "image/svg+xml"], "test_file_w_fake_ext.ext.svg"],
[["test_file_w_correct_ext.svg", "image", "image/svg+xml"], "test_file_w_correct_ext.svg"],
[["test_file_w_correct_ext.svgz", "image", "image/svg+xml"], "test_file_w_correct_ext.svgz"],
[["test_file.zip", "file", "application/zip"], "test_file.zip"],
[["test_file", "file", "application/zip"], "test_file.zip"],
// application/octet-stream tests
[["test_file", "file", "application/octet-stream"], "test_file"],
[["test_file.zip", "file", "application/octet-stream"], "test_file.zip"],
[["test_file.unknown", null, "application/octet-stream"], "test_file.unknown"],
// sanitized filename tests
[["test/file", null, "application/octet-stream"], "testfile"],
[["test:file.zip", "file", "application/zip"], "testfile.zip"],
[[":::", "file", "application/zip"], ".zip"],
[[":::a", "file", "application/zip"], "a.zip"]
];
describe("utils/formatDownloadTitle unit tests", () => {
testCases.forEach((testCase) => {
return it(`With args '${JSON.stringify(testCase[0])}' it should return '${testCase[1]}'`, () => {
const [value, expected] = testCase;
const actual = formatDownloadTitle(...value);
expect(actual).toStrictEqual(expected);
});
});
});