Files
dragonroll/tests/search-everywhere.test.js
Gitea OpenCode Agent c6656fad3a
All checks were successful
Test / test (push) Successful in 2s
Fix issue #42: Improve search
2026-10-14 22:39:24 +00:00

98 lines
5.4 KiB
JavaScript

// Tests for Syndria98/dragonroll#42: the Ctrl+K search only looked at notes; it now searches the
// whole campaign — notes (nested in folders included), actors, items and maps — and selecting a
// result opens that entry the same way the campaign sidebar opens it.
//
// The repository root installs no dependencies, so these checks read the search component's
// source, like tests/map-viewer.test.js reads its window.
//
// 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");
const read = (...parts) => fs.readFileSync(path.join(__dirname, "..", ...parts), "utf8");
const searchSource = read("frontend", "app", "components", "viewer", "topbar", "TopSearchBar.vue");
const windowDefsSource = read("frontend", "app", "services", "WindowDefinitions.js");
test("the search fetches notes, actors, items and maps of the campaign", () => {
for (const [route, key] of [
["/note/all", "notes"],
["/actor/list", "actors"],
["/item/list", "items"],
["/map/list", "maps"],
]) {
assert.ok(searchSource.includes(`'${route}'`), `expected the search to fetch ${route}`);
assert.ok(searchSource.includes(`'${key}'`), `expected the ${route} response key '${key}' to be read`);
}
// every list is fetched scoped to the current campaign
assert.match(searchSource, /params:\s*\{\s*campaign:\s*campaignId\.value\s*\}/);
});
test("every entry list is matched against the query, accent- and case-insensitively", () => {
// one shared normalisation like the action registry's own filter uses
assert.match(searchSource, /normalize\("NFD"\)\.replace\(\/\[\\u0300-\\u036f\]\/g,\s*""\)\.toLowerCase\(\)/);
// notes by title, actors / items / maps by name
for (const call of [
String.raw`pushMatches\(notes\.value,\s*'note',\s*n => n\.title\)`,
String.raw`pushMatches\(actors\.value,\s*'actor',\s*a => a\.name\)`,
String.raw`pushMatches\(items\.value,\s*'item',\s*i => i\.name\)`,
String.raw`pushMatches\(maps\.value,\s*'map',\s*m => m\.name\)`,
]) {
assert.match(searchSource, new RegExp(call));
}
// command actions keep matching through the registry
assert.match(searchSource, /searchActions\(query \?\? ''\)/);
});
test("selecting a result opens each entry type the way the sidebar opens it", () => {
// a note opens in the note column, with its content retrieved (/note/all never carries it)
assert.match(searchSource, /get\(`\/note\/retrieve\/\$\{note\.key\}`\)/);
assert.match(searchSource, /emitter\.emit\('push-note',\s*\{ key: full\._id, title: full\.title, text: full\.content \?\? ''/);
// a character replaces the open sheet window
assert.match(searchSource, /GetFirstWindowId\('character_sheet'\)/);
assert.match(searchSource, /CreateWindow\('character_sheet',\s*\{ actor \}\)/);
// an item replaces the open item window
assert.match(searchSource, /GetFirstWindowId\('item_sheet'\)/);
assert.match(searchSource, /CreateWindow\('item_sheet',\s*\{ item \}\)/);
// a map anchors on the note container, clearing a floating viewer first
assert.match(searchSource, /GetFirstWindowId\('map_viewer'\)/);
assert.match(searchSource, /AnchorMap\(map\)/);
// a command action still executes
assert.match(searchSource, /executeAction\(item\.data\.id\)/);
// the windows these opens use are all registered
for (const type of ["character_sheet", "item_sheet"]) {
assert.match(windowDefsSource, new RegExp(`${type}:\\s*\\{`));
}
});
test("keyboard Enter and clicking share one dispatcher over the combined results", () => {
// a single openResult handles every type, keyed off the result's type tag
assert.match(searchSource, /function openResult\(item\)[\s\S]*item\.type === 'note'[\s\S]*item\.type === 'actor'[\s\S]*item\.type === 'item'[\s\S]*item\.type === 'map'/);
// Enter on the selected row and a click on a row both go through it
assert.match(searchSource, /if \(event\.key === 'Enter'\)[\s\S]*?openResult\(combined\[selectedIndex\.value\]\)/);
assert.match(searchSource, /@click\.stop\.prevent="openResult\(item\)"/);
});
test("the palette shows what each result is and stays fresh", () => {
// every result carries its section's icon and label
for (const icon of ["book.svg", "user.svg", "box.svg", "map.svg"]) {
assert.ok(searchSource.includes(`/icons/iconoir/regular/${icon}`), `expected an icon per entry type (${icon})`);
}
assert.match(searchSource, /t\(`campaigns\.sections\.\$\{item\.type\}s`\)/);
// the section labels come from i18n: they exist in every locale
for (const locale of ["en", "es", "ca"]) {
const campaigns = JSON.parse(read("frontend", "i18n", "locales", locale, "campaigns.json"));
for (const key of ["notes", "actors", "items", "maps"]) {
assert.ok(campaigns.sections?.[key], `${locale} is missing campaigns.sections.${key}`);
}
}
// refetched when the palette opens (Foundry syncs entries in the background)...
assert.match(searchSource, /function onFocus\(\)[\s\S]*?fetchSearchEntries\(\)/);
// ...and dropped when another campaign becomes the current one
assert.match(searchSource, /watch\(campaignId,\s*\(\)\s*=>\s*\{[^}]*notes\.value = \[\][^}]*actors\.value = \[\][^}]*items\.value = \[\][^}]*maps\.value = \[\]/);
});