2024-07-30 23:24:17 +08:00
|
|
|
import { beforeEach, describe, it } from "@std/testing/bdd";
|
2024-02-26 13:46:03 +08:00
|
|
|
import { buildQueryFunctions } from "$common/query_functions.ts";
|
|
|
|
import type { System } from "$lib/plugos/system.ts";
|
2024-07-30 23:24:17 +08:00
|
|
|
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
|
2024-02-26 13:46:03 +08:00
|
|
|
|
|
|
|
let functions: ReturnType<typeof buildQueryFunctions>;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
functions = buildQueryFunctions(
|
2024-05-28 02:33:41 +08:00
|
|
|
new Set(["page1.md"]),
|
2024-02-26 13:46:03 +08:00
|
|
|
{} as System<unknown>,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("pageExists", () => {
|
|
|
|
const invalidValues = [/hello/, 1, null, undefined, true, {}];
|
|
|
|
for (const value of invalidValues) {
|
|
|
|
it(`should throw if name is ${value}`, () => {
|
|
|
|
assertThrows(
|
|
|
|
() => functions.pageExists(value),
|
|
|
|
Error,
|
|
|
|
"pageExists(): name is not a string",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it("should return true if name starts with ! or {{", () => {
|
|
|
|
assertEquals(functions.pageExists("!invalid name"), true);
|
|
|
|
assertEquals(functions.pageExists("{{invalid name"), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return true if page exists", () => {
|
|
|
|
assertEquals(functions.pageExists("page1"), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return false if page doesn't exist", () => {
|
|
|
|
assertEquals(functions.pageExists("page2"), false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-07-23 20:53:36 +08:00
|
|
|
describe("rewriteRefsAndFederationLinks", () => {
|
2024-07-10 14:50:54 +08:00
|
|
|
it("should rewrite all task references to include a page ref", () => {
|
|
|
|
const template1 =
|
|
|
|
"* [ ] My task\n* [ ] [[other@2]] Ignore me\n* [ ] Rewrite me too [[other page]]\n";
|
|
|
|
assertEquals(
|
2024-07-23 20:56:07 +08:00
|
|
|
functions.rewriteRefsAndFederationLinks(template1, "page1"),
|
2024-07-10 14:50:54 +08:00
|
|
|
"* [ ] [[page1@2]] My task\n* [ ] [[other@2]] Ignore me\n* [ ] [[page1@44]] Rewrite me too [[other page]]\n",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-02-26 13:46:03 +08:00
|
|
|
describe("template", () => {
|
|
|
|
const invalidValues = [/hello/, 1, null, undefined, true, {}];
|
|
|
|
for (const value of invalidValues) {
|
|
|
|
it(`should throw if template is ${value}`, async () => {
|
|
|
|
await assertRejects(
|
|
|
|
() => functions.template(value),
|
|
|
|
Error,
|
|
|
|
"template(): template is not a string",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|