// Tests for the `[[…]]` links a note shows in display mode: the link carries the entry's name, the // note resolves it against the campaign's lists, and a character or item link previews its sheet // before opening it. The label lookup is the pure part of `LinkAutocomplete.js`, loaded straight // from source; the rest is checked by reading the files that wire it together. // // 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 entry a written link points at --- test("a link resolves to the entry whose label it carries", () => { const targets = [ { id: "a1", label: "Mira", type: "actor" }, { id: "i1", label: "Rope of Climbing", type: "item" }, ]; assert.deepEqual(autocomplete.FindLinkTarget(targets, "Mira"), targets[0]); assert.deepEqual(autocomplete.FindLinkTarget(targets, "Rope of Climbing"), targets[1]); }); test("the lookup forgives case, accents and surrounding spaces", () => { const targets = [{ id: "a1", label: "Café", type: "actor" }]; assert.equal(autocomplete.FindLinkTarget(targets, "cafe")?.id, "a1"); assert.equal(autocomplete.FindLinkTarget(targets, " CAFÉ ")?.id, "a1"); }); test("a link that no longer matches anything resolves to null", () => { const targets = [{ id: "a1", label: "Mira", type: "actor" }]; assert.equal(autocomplete.FindLinkTarget(targets, "Mira the Bold"), null); assert.equal(autocomplete.FindLinkTarget(targets, " "), null); assert.equal(autocomplete.FindLinkTarget(undefined, "Mira"), null); }); // --- The rendered link, the resolver and the preview are wired together --- test("the markdown renders the link's label instead of a placeholder", () => { const marker = readRepoFile("frontend", "app", "services", "Marker.js"); const link = marker.slice(marker.indexOf("const linkExtension"), marker.indexOf("marker.use")); assert.match(link, /class="vue-link"/, "the link keeps the class the note binds to"); assert.match(link, /data-href=/, "…and the label it points at"); assert.match(link, /token\.link/, "the label is the link's text"); assert.doesNotMatch(link, />Link { const targets = readRepoFile("frontend", "app", "services", "LinkTargets.js"); for (const type of ["note", "actor", "item", "map"]) { assert.ok(targets.includes(`${type}:`), `the link targets do not cover ${type}s`); } assert.match(targets, /LoadLinkEntry/, "the preview needs the entry itself, not only its label"); assert.match(targets, /CACHE_TTL/, "the four lists are still cached"); }); test("the note binds its rendered links to the preview and the sheet windows", () => { const note = readRepoFile("frontend", "app", "components", "viewer", "content", "Note.vue"); assert.match(note, /FindLinkTarget/, "the note resolves the label it renders"); assert.match(note, /LoadLinkEntry/, "…and asks for the entry to preview"); assert.match(note, /classList\.add\('is-resolved'\)/, "resolved links are marked"); assert.match(note, /classList\.add\('is-unresolved'\)/, "a link to a deleted entry is marked too"); assert.match(note, / { const preview = readRepoFile("frontend", "app", "components", "viewer", "content", "LinkPreview.vue"); assert.match(preview, /target\.value\.type === 'actor'/); assert.match(preview, /target\.value\.type === 'item'/); assert.match(preview, /campaigns\.link-preview\.open-character/); assert.match(preview, /campaigns\.link-preview\.open-item/); assert.match(preview, /armorClass/, "the character preview reads the sheet's vitals"); }); // --- `![[…]]`: the entry itself, drawn in the note --- test("the editor's link menu also completes an embed", () => { // The trigger starts at the `[[`, so the bang typed before it is kept. const trigger = autocomplete.FindLinkTrigger("see ![[Mir", 10); assert.deepEqual(trigger, { start: 5, query: "Mir" }); const result = autocomplete.InsertLinkSuggestion("see ![[Mir", trigger, { label: "Mira" }); assert.equal(result.text, "see ![[Mira]]"); }); test("the bang writes an embed token, and the embed is tried before the plain link", () => { const marker = readRepoFile("frontend", "app", "services", "Marker.js"); const embed = marker.slice(marker.indexOf("const embedExtension"), marker.indexOf("const linkExtension")); assert.match(embed, /!\\\[\\\[/, "the token starts with a bang"); assert.match(embed, /class="vue-embed"/, "the note mounts something other than a link"); assert.match(embed, /data-href=/, "…for the label that was written"); assert.doesNotMatch(embed, /class="vue-link"/); const registration = marker.slice(marker.indexOf("marker.use")); assert.ok( registration.indexOf("embedExtension") < registration.indexOf("linkExtension"), "the embed tokenizer has to run before the link one" ); }); test("the note mounts an embedded entry the way it mounts a widget", () => { const note = readRepoFile("frontend", "app", "components", "viewer", "content", "Note.vue"); assert.match(note, /import EmbeddedEntry from '\.\/EmbeddedEntry\.vue'/); assert.match(note, /querySelectorAll\('\.vue-embed'\)/, "the placeholders Marker left are found"); assert.match(note, /createApp\(EmbeddedEntry,\s*\{\s*href:/, "each one becomes a card of its own"); assert.match(note, /mountEmbeds\(\)/, "…and is mounted with the note"); assert.match(note, /mountedApps\.push\(app\)/, "so it is unmounted when the note re-renders"); }); test("an embed draws the sheet inline and opens the full one", () => { const embed = readRepoFile("frontend", "app", "components", "viewer", "content", "EmbeddedEntry.vue"); assert.match(embed, /FindLinkTarget/, "the label is resolved exactly like a link"); assert.match(embed, /LoadLinkEntry/, "…and its entry read from the campaign lists"); assert.match(embed, /target\.value\?\.type === 'actor'/, "a character gets its sheet"); assert.match(embed, /target\.value\?\.type === 'item'/, "an item gets its description"); assert.match(embed, /armorClass/, "the character card carries the sheet's vitals"); assert.match(embed, /abilityScores/, "…and the ability scores"); assert.match(embed, /entry\.description/, "the item card carries the description"); assert.match(embed, /CreateWindow\(windowType/, "the full sheet opens from the card"); }); test("the preview strings exist in every locale", () => { for (const locale of ["en", "es", "ca"]) { const strings = JSON.parse(readRepoFile("frontend", "i18n", "locales", locale, "campaigns.json")); const preview = strings["link-preview"]; assert.ok(preview, `${locale} has no link preview strings`); for (const key of ["open-character", "open-item", "no-description", "not-found", "close"]) { assert.ok(preview[key], `${locale} has no link-preview.${key}`); } } });