// Tests for the map viewer adjustments of Syndria98/dragonroll#15: // * the campaign workspace can show an anchored map where the open notes are, and the sidebar's // notes/maps buttons switch between the two without losing either, // * the create map window no longer snaps the scale to `min + n * step` values and insets its // content from the window edges. // // `frontend/app/services/Content.js` is a Vue module and the repository root installs no // dependencies, so it is loaded into a `node:vm` context with a minimal `ref` stub: the anchor and // pane rules are plain state transitions and stay fully observable that way. The create map window // checks read its template, because both requirements are about the input element itself. // // 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 vm = require("node:vm"); function readRepoFile(...parts) { return fs.readFileSync(path.join(__dirname, "..", ...parts), "utf8"); } function loadContentService() { const source = readRepoFile("frontend", "app", "services", "Content.js"); const body = source .replace(/import\s*\{[^}]*\}\s*from\s*['"]vue['"];?/, "") .replace(/export\s*\{/, "return {") .concat("\n;"); const context = vm.createContext({ ref: (value) => ({ value }) }); return vm.compileFunction(body, [], { parsingContext: context })(); } function anchoredMapTests() { test("the workspace starts on the open notes with no map anchored", () => { const service = loadContentService(); assert.equal(service.ActivePane.value, "notes"); assert.equal(service.AnchoredMap.value, null); }); test("anchoring a map shows it instead of the notes", () => { const service = loadContentService(); service.AnchorMap({ _id: "map-1", name: "Dungeon" }); assert.equal(service.ActivePane.value, "map"); assert.equal(service.AnchoredMap.value._id, "map-1"); }); test("switching back to the notes keeps the map anchored", () => { const service = loadContentService(); service.AnchorMap({ _id: "map-1" }); service.SetActivePane("notes"); assert.equal(service.ActivePane.value, "notes"); assert.equal(service.AnchoredMap.value._id, "map-1"); }); test("clearing the anchor shows the notes again", () => { const service = loadContentService(); service.AnchorMap({ _id: "map-1" }); service.ClearAnchoredMap(); assert.equal(service.AnchoredMap.value, null); assert.equal(service.ActivePane.value, "notes"); }); test("anchoring nothing falls back to the notes", () => { const service = loadContentService(); service.AnchorMap({ _id: "map-1" }); service.AnchorMap(null); assert.equal(service.AnchoredMap.value, null); assert.equal(service.ActivePane.value, "notes"); }); test("hiding a pane does not disturb the other one", () => { const service = loadContentService(); service.AnchorMap({ _id: "map-1" }); service.SetShowContent(false); assert.equal(service.ShowContent.value, false); assert.equal(service.ActivePane.value, "map"); }); } function createMapWindowTests() { const source = readRepoFile("frontend", "app", "components", "windows", "CreateMapWindow.vue"); const scaleInput = /]*v-model\.number="form\.scale"[^>]*>/.exec(source); test("the create map window has a scale input", () => { assert.ok(scaleInput, "expected a v-model.number=\"form.scale\" input"); }); test("the scale input accepts any decimal number", () => { const step = /step="([^"]*)"/.exec(scaleInput[0])?.[1]; // A number input only accepts `min + n * step`, so a 0.5 step on the 0.01 minimum only // ever offered 5.01, 5.51, 6.01... instead of the number that was typed. assert.ok( step === undefined || step === "any", `the scale input must not snap values, found step="${step}"` ); }); test("the scale input keeps the positive minimum shared with the backend", () => { const min = /min="([^"]*)"/.exec(scaleInput[0])?.[1]; assert.equal(min, "0.01"); }); test("the create map window insets its content with an outer margin", () => { const body = /\.body\s*\{[^}]*\}/.exec(source)?.[0]; assert.ok(body, "expected a .body style rule"); assert.match(body, /margin:\s*[^;]*\d+px/, ".body needs an outer margin"); // Without border-box the horizontal margin is added outside the full width and the // content overflows the window instead of being inset by it. assert.match(body, /box-sizing:\s*border-box/, ".body needs box-sizing: border-box"); }); } anchoredMapTests(); createMapWindowTests();