Files
dragonroll/tests/link-autocomplete.test.js
Aran Roig 1ec29121e1
Some checks failed
Test / test (push) Failing after 2s
Build and Deploy Nuxt / build (push) Successful in 1m51s
Deploy deepseek 2
2026-10-19 19:04:28 +02:00

154 lines
6.8 KiB
JavaScript

// Tests for the note editor's wiki-link autocomplete (type `[[` while editing a note and the
// campaign's notes, actors, items and maps are offered to link to). The rules live in
// `frontend/app/services/LinkAutocomplete.js`, which imports nothing, so it is loaded straight from
// source; the fetching side, `LinkTargets.js`, is checked by reading it, because it talks to the API
// the repository root does not stand up.
//
// Run with `npm test` from the repository root, or `node --test tests/`.
const { test } = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
function readRepoFile(...parts) {
return fs.readFileSync(path.join(__dirname, "..", ...parts), "utf8");
}
function loadLinkAutocomplete() {
const source = readRepoFile("frontend", "app", "services", "LinkAutocomplete.js");
return new Function(source.replace("export {", "return {"))();
}
const autocomplete = loadLinkAutocomplete();
// --- Finding the `[[target` the caret is in ---
test("typing after a [[ opens a trigger with what has been typed so far", () => {
assert.deepEqual(autocomplete.FindLinkTrigger("see [[The Wr", 12), { start: 4, query: "The Wr" });
assert.deepEqual(autocomplete.FindLinkTrigger("[[", 2), { start: 0, query: "" });
assert.deepEqual(autocomplete.FindLinkTrigger("a\n[[Mi", 6), { start: 2, query: "Mi" });
});
test("the trigger starts at the last [[ before the caret", () => {
assert.deepEqual(autocomplete.FindLinkTrigger("[[a [[b", 8), { start: 4, query: "b" });
});
test("a link is a single line, so a closed or newlined [[ opens nothing", () => {
// Caret after the closing brackets: the link is finished, not being typed.
assert.equal(autocomplete.FindLinkTrigger("[[The Inn]] ", 12), null);
assert.equal(autocomplete.FindLinkTrigger("[[The\nInn", 9), null);
assert.equal(autocomplete.FindLinkTrigger("no brackets here", 15), null);
assert.equal(autocomplete.FindLinkTrigger("", 0), null);
});
// --- The entries offered, from the campaign's four lists ---
test("every named note, actor, item and map becomes a suggestion, sorted by label", () => {
const suggestions = autocomplete.BuildLinkSuggestions({
notes: [{ _id: "n1", title: "The Wreck" }, { _id: "n2", title: " " }, { _id: "n3", title: "The Inn" }],
actors: [{ _id: "a1", name: "Mira" }],
items: [{ _id: "i1", name: "Rope" }],
maps: [{ _id: "m1", name: "Saltmarsh" }],
});
assert.deepEqual(suggestions, [
{ id: "a1", label: "Mira", type: "actor" },
{ id: "i1", label: "Rope", type: "item" },
{ id: "m1", label: "Saltmarsh", type: "map" },
{ id: "n3", label: "The Inn", type: "note" },
{ id: "n1", label: "The Wreck", type: "note" },
]);
});
test("entries without a name are left out, and a repeated label is offered once", () => {
const suggestions = autocomplete.BuildLinkSuggestions({
notes: [{ _id: "n1", title: "The Inn" }, { _id: "n2", title: "The Inn" }, { _id: "n3" }],
});
assert.deepEqual(suggestions, [{ id: "n1", label: "The Inn", type: "note" }]);
});
test("a campaign with no lists offers nothing", () => {
assert.deepEqual(autocomplete.BuildLinkSuggestions({}), []);
assert.deepEqual(autocomplete.BuildLinkSuggestions(undefined), []);
});
// --- Filtering what has been typed ---
test("the suggestions are filtered accent- and case-insensitively", () => {
const suggestions = [{ id: "n1", label: "Café", type: "note" }];
assert.equal(autocomplete.FilterLinkSuggestions(suggestions, "cafe").length, 1);
assert.equal(autocomplete.FilterLinkSuggestions(suggestions, "CAFÉ").length, 1);
assert.equal(autocomplete.FilterLinkSuggestions(suggestions, "tea").length, 0);
});
test("labels that start with what was typed come before the ones that only contain it", () => {
const suggestions = [
{ id: "n1", label: "The Inn", type: "note" },
{ id: "n2", label: "Innkeeper", type: "note" },
];
assert.deepEqual(
autocomplete.FilterLinkSuggestions(suggestions, "inn").map((s) => s.label),
["Innkeeper", "The Inn"]
);
});
test("an empty query offers the entries, capped so the menu stays short", () => {
const many = Array.from({ length: 60 }, (_, i) => ({ id: `n${i}`, label: `Note ${i}`, type: "note" }));
assert.equal(autocomplete.FilterLinkSuggestions(many, "").length, 50);
assert.equal(autocomplete.FilterLinkSuggestions([], "").length, 0);
});
// --- What is written when an entry is chosen ---
test("choosing an entry writes [[label]] and leaves the caret after the closing brackets", () => {
const trigger = autocomplete.FindLinkTrigger("see [[The Wr", 12);
const result = autocomplete.InsertLinkSuggestion("see [[The Wr", trigger, { label: "The Wreck" });
assert.equal(result.text, "see [[The Wreck]]");
assert.equal(result.caret, 17);
});
test("only the trigger is replaced, the note around it is kept", () => {
const trigger = autocomplete.FindLinkTrigger("a [[M b", 5);
const result = autocomplete.InsertLinkSuggestion("a [[M b", trigger, { label: "Mira" });
assert.equal(result.text, "a [[Mira]] b");
assert.equal(result.text.slice(0, result.caret) + "|" + result.text.slice(result.caret), "a [[Mira]]| b");
});
// --- The editor, the fetch and the menu are wired together ---
function wiringTests() {
const note = readRepoFile("frontend", "app", "components", "viewer", "content", "Note.vue");
const targets = readRepoFile("frontend", "app", "services", "LinkTargets.js");
test("the editor offers links through the service and the shared menu", () => {
assert.match(note, /from '~\/services\/LinkAutocomplete'/, "the editor reads the link autocomplete service");
assert.match(note, /from '~\/services\/LinkTargets'/, "…and the one that fetches the entries");
assert.match(note, /<SuggestionMenu/, "widgets and links share one menu");
assert.match(note, /@select="applySuggestion"/);
});
test("the entries a note can link to are the campaign's four lists", () => {
for (const path of ["/note/all", "/actor/list", "/item/list", "/map/list"]) {
assert.ok(targets.includes(path), `the link entries do not read ${path}`);
}
assert.match(targets, /CACHE_TTL/, "the four lists are cached instead of fetched per keystroke");
});
test("the link menu names its section in every locale", () => {
for (const locale of ["en", "es", "ca"]) {
const sections = JSON.parse(readRepoFile("frontend", "i18n", "locales", locale, "campaigns.json")).sections;
for (const type of ["note", "actor", "item", "map"]) {
assert.ok(sections[`${type}s`], `${locale} has no label for the ${type} section`);
}
}
});
}
wiringTests();