2024-07-03 13:50:26 +08:00
|
|
|
import { encodePageRef, parsePageRef, validatePageName } from "./page_ref.ts";
|
2024-07-13 19:51:49 +08:00
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
AssertionError,
|
|
|
|
assertThrows,
|
|
|
|
} from "$std/testing/asserts.ts";
|
2024-01-24 18:58:33 +08:00
|
|
|
|
|
|
|
Deno.test("Page utility functions", () => {
|
|
|
|
// Base cases
|
|
|
|
assertEquals(parsePageRef("foo"), { page: "foo" });
|
|
|
|
assertEquals(parsePageRef("[[foo]]"), { page: "foo" });
|
|
|
|
assertEquals(parsePageRef("foo@1"), { page: "foo", pos: 1 });
|
|
|
|
assertEquals(parsePageRef("foo$bar"), { page: "foo", anchor: "bar" });
|
2024-01-25 21:51:40 +08:00
|
|
|
assertEquals(parsePageRef("foo#My header"), {
|
|
|
|
page: "foo",
|
|
|
|
header: "My header",
|
|
|
|
});
|
2024-01-24 18:58:33 +08:00
|
|
|
assertEquals(parsePageRef("foo$bar@1"), {
|
|
|
|
page: "foo",
|
|
|
|
anchor: "bar",
|
|
|
|
pos: 1,
|
|
|
|
});
|
|
|
|
|
2024-07-17 23:03:25 +08:00
|
|
|
// Meta page
|
|
|
|
assertEquals(parsePageRef("^foo"), { page: "foo", meta: true });
|
|
|
|
|
2024-01-24 18:58:33 +08:00
|
|
|
// Edge cases
|
|
|
|
assertEquals(parsePageRef(""), { page: "" });
|
|
|
|
assertEquals(parsePageRef("user@domain.com"), { page: "user@domain.com" });
|
|
|
|
|
|
|
|
// Encoding
|
|
|
|
assertEquals(encodePageRef({ page: "foo" }), "foo");
|
|
|
|
assertEquals(encodePageRef({ page: "foo", pos: 10 }), "foo@10");
|
|
|
|
assertEquals(encodePageRef({ page: "foo", anchor: "bar" }), "foo$bar");
|
2024-01-25 21:51:40 +08:00
|
|
|
assertEquals(encodePageRef({ page: "foo", header: "bar" }), "foo#bar");
|
2024-07-03 13:50:26 +08:00
|
|
|
|
|
|
|
// Page name validation
|
|
|
|
|
|
|
|
try {
|
|
|
|
validatePageName("perfectly fine page name");
|
2024-07-13 19:51:49 +08:00
|
|
|
validatePageName("this is special case of a.conflicted.1234");
|
2024-07-03 13:50:26 +08:00
|
|
|
} catch (error) {
|
2024-07-13 19:51:49 +08:00
|
|
|
throw new AssertionError(
|
|
|
|
`Something is very wrong with the validatePageName function: ${error}`,
|
|
|
|
);
|
2024-07-03 13:50:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assertThrows(() => validatePageName(""), Error);
|
|
|
|
assertThrows(() => validatePageName(".hidden"), Error);
|
|
|
|
assertThrows(() => validatePageName(".."), Error);
|
|
|
|
|
|
|
|
for (const extension of ["md", "txt", "exe", "cc", "ts"]) {
|
2024-07-13 19:51:49 +08:00
|
|
|
assertThrows(
|
|
|
|
() => validatePageName(`extensions-are-not-welcome.${extension}`),
|
|
|
|
Error,
|
|
|
|
);
|
2024-07-03 13:50:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const extension of ["db2", "woff2", "sqlite3", "42", "0"]) {
|
2024-07-13 19:51:49 +08:00
|
|
|
assertThrows(
|
|
|
|
() => validatePageName(`extensions-can-contain-numbers-too.${extension}`),
|
|
|
|
Error,
|
|
|
|
);
|
2024-07-03 13:50:26 +08:00
|
|
|
}
|
2024-01-24 18:58:33 +08:00
|
|
|
});
|