149 lines
7.0 KiB
JavaScript
149 lines
7.0 KiB
JavaScript
// Tests for the note editor's widget autocomplete (type `@` while editing a note and the widgets of
|
|
// Marker.js are offered to insert). The rules live in `frontend/app/services/WidgetAutocomplete.js`,
|
|
// which imports nothing, so it is loaded straight from source the way the other tests load the
|
|
// frontend services; the repository root installs no dependencies, so `new Function` is enough.
|
|
//
|
|
// The last checks read the editor and the dropdown, because there the wiring itself is what has to
|
|
// stay right: the textarea asks the service, the registry comes from Marker.js, and the two strings
|
|
// the dropdown shows exist in every locale.
|
|
//
|
|
// 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 loadWidgetAutocomplete() {
|
|
const source = readRepoFile("frontend", "app", "services", "WidgetAutocomplete.js");
|
|
return new Function(source.replace("export {", "return {"))();
|
|
}
|
|
|
|
const autocomplete = loadWidgetAutocomplete();
|
|
|
|
// --- Finding the `@name` the caret is in ---
|
|
|
|
test("typing after an @ opens a trigger with what has been typed so far", () => {
|
|
assert.deepEqual(autocomplete.FindWidgetTrigger("hello @ro", 9), { start: 6, query: "ro" });
|
|
assert.deepEqual(autocomplete.FindWidgetTrigger("@", 1), { start: 0, query: "" });
|
|
assert.deepEqual(autocomplete.FindWidgetTrigger("@roll", 5), { start: 0, query: "roll" });
|
|
assert.deepEqual(autocomplete.FindWidgetTrigger("a\n@r", 4), { start: 2, query: "r" });
|
|
});
|
|
|
|
test("the trigger is the token under the caret, not the rest of the note", () => {
|
|
// Caret sits after `@ro`, with more text after it: only the token is replaced later.
|
|
assert.deepEqual(autocomplete.FindWidgetTrigger("@ro b", 3), { start: 0, query: "ro" });
|
|
});
|
|
|
|
test("an @ inside a word does not open the menu", () => {
|
|
// An email address should never pop the widget list open.
|
|
assert.equal(autocomplete.FindWidgetTrigger("user@example", 12), null);
|
|
assert.equal(autocomplete.FindWidgetTrigger("x@ro", 4), null);
|
|
});
|
|
|
|
test("a caret away from any @ finds no trigger", () => {
|
|
assert.equal(autocomplete.FindWidgetTrigger("just some text", 5), null);
|
|
assert.equal(autocomplete.FindWidgetTrigger("", 0), null);
|
|
});
|
|
|
|
// --- The widgets offered, from Marker's registry ---
|
|
|
|
test("every registered widget is offered, once per way of writing it", () => {
|
|
const registry = { inline: ["roll"], display: ["roll"] };
|
|
assert.deepEqual(autocomplete.BuildWidgetSuggestions(registry), [
|
|
{ name: "roll", type: "inline" },
|
|
{ name: "roll", type: "display" },
|
|
]);
|
|
});
|
|
|
|
test("a registry with no widgets offers nothing", () => {
|
|
assert.deepEqual(autocomplete.BuildWidgetSuggestions({}), []);
|
|
assert.deepEqual(autocomplete.BuildWidgetSuggestions(undefined), []);
|
|
});
|
|
|
|
test("the suggestions are filtered by the prefix that has been typed", () => {
|
|
const suggestions = [
|
|
{ name: "roll", type: "inline" },
|
|
{ name: "roll", type: "display" },
|
|
{ name: "table", type: "display" },
|
|
];
|
|
|
|
assert.equal(autocomplete.FilterWidgetSuggestions(suggestions, "").length, 3);
|
|
assert.deepEqual(autocomplete.FilterWidgetSuggestions(suggestions, "ro"), [
|
|
{ name: "roll", type: "inline" },
|
|
{ name: "roll", type: "display" },
|
|
]);
|
|
assert.deepEqual(autocomplete.FilterWidgetSuggestions(suggestions, "zz"), []);
|
|
});
|
|
|
|
// --- What is written when a widget is chosen ---
|
|
|
|
test("choosing an inline widget writes it with the caret between the brackets", () => {
|
|
// "hit @ro" (caret at the end) becomes "hit @roll[]" with the caret before "]".
|
|
const trigger = autocomplete.FindWidgetTrigger("hit @ro", 7);
|
|
const result = autocomplete.InsertWidgetSuggestion("hit @ro", trigger, { name: "roll", type: "inline" });
|
|
|
|
assert.equal(result.text, "hit @roll[]");
|
|
assert.equal(result.caret, 10);
|
|
assert.equal(result.text.slice(0, result.caret) + "|" + result.text.slice(result.caret), "hit @roll[|]");
|
|
});
|
|
|
|
test("choosing a block widget writes it with the caret on the blank line", () => {
|
|
const trigger = autocomplete.FindWidgetTrigger("@ro", 3);
|
|
const result = autocomplete.InsertWidgetSuggestion("@ro", trigger, { name: "roll", type: "display" });
|
|
|
|
assert.equal(result.text, "@roll\n\n@end");
|
|
assert.equal(result.caret, 6);
|
|
assert.equal(result.text.slice(0, result.caret) + "|" + result.text.slice(result.caret), "@roll\n|\n@end");
|
|
});
|
|
|
|
test("only the trigger is replaced, the note around it is kept", () => {
|
|
const trigger = autocomplete.FindWidgetTrigger("a @ro b", 5);
|
|
const result = autocomplete.InsertWidgetSuggestion("a @ro b", trigger, { name: "roll", type: "inline" });
|
|
|
|
assert.equal(result.text, "a @roll[] b");
|
|
});
|
|
|
|
// --- The editor and the dropdown are wired together ---
|
|
|
|
function wiringTests() {
|
|
const note = readRepoFile("frontend", "app", "components", "viewer", "content", "Note.vue");
|
|
const menu = readRepoFile("frontend", "app", "components", "viewer", "content", "SuggestionMenu.vue");
|
|
const marker = readRepoFile("frontend", "app", "services", "Marker.js");
|
|
const textarea = /<textarea[^>]*class="full-editor"[^>]*>/.exec(note)?.[0];
|
|
|
|
test("the editing textarea drives the suggestions from its own text and caret", () => {
|
|
assert.ok(textarea, "expected the note editor's textarea");
|
|
assert.match(textarea, /@input="onEditorInput"/);
|
|
assert.match(textarea, /@keydown="onEditorKeydown"/);
|
|
assert.match(note, /from '~\/services\/WidgetAutocomplete'/, "the editor reads the autocomplete service");
|
|
});
|
|
|
|
test("the suggestion menu is drawn while editing", () => {
|
|
assert.match(note, /<SuggestionMenu[\s\S]*v-if="suggestions\.length"/, "the menu is shown only when there is something to offer");
|
|
assert.match(note, /@select="applySuggestion"/, "choosing an entry inserts it");
|
|
assert.match(menu, /@mousedown\.prevent="emit\('select', suggestion\)"/, "a click does not blur the textarea before it lands");
|
|
});
|
|
|
|
test("the widgets shown come from Marker's registry, not a second hand-kept list", () => {
|
|
assert.match(marker, /const GetWidgetRegistry/, "Marker exposes the widget registry");
|
|
assert.match(marker, /export \{ ParseMarkdown, GetWidget, GetWidgetRegistry \}/);
|
|
assert.match(note, /BuildWidgetSuggestions\(GetWidgetRegistry\(\)\)/, "the editor builds its menu from the registry");
|
|
});
|
|
|
|
test("the dropdown's two labels exist in every locale", () => {
|
|
for (const locale of ["en", "es", "ca"]) {
|
|
const strings = JSON.parse(readRepoFile("frontend", "i18n", "locales", locale, "general.json"));
|
|
for (const type of ["inline", "display"]) {
|
|
assert.ok(strings.widgets?.[type], `${locale} has no label for a ${type} widget`);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
wiringTests();
|