// Tests for the walls of a map: straight segments drawn with the wall tool, stored with the map in // image pixels, in three kinds (normal, terrain, invisible). // // Same set-up as tests/map-tokens.test.js, which the repository root runs without installing // anything: the express router is loaded into `node:vm` with its models stubbed, and the last checks // read the viewer template, where the drawing wiring itself is what has to stay right. // // 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"); } // A mongoose query answers whatever `await query` and `query.lean()` answer with, and lets `select()` // and `sort()` sit in between. function mongooseQuery(value) { const result = { select: () => result, sort: () => result, lean: () => Promise.resolve(value), then: (onFulfilled, onRejected) => Promise.resolve(value).then(onFulfilled, onRejected), }; return result; } // Loads an express router file with every module it requires stubbed out. function loadRouter(relativeParts, stubs) { const routes = new Map(); const router = { get: (route, ...handlers) => routes.set(`GET ${route}`, handlers), post: (route, ...handlers) => routes.set(`POST ${route}`, handlers), }; const requireStub = (id) => { if (id === "express") return { Router: () => router }; if (id === "../services/storage") return { single: () => (req, res, next) => next() }; if (!(id in stubs)) throw new Error(`require("${id}") is not stubbed`); return stubs[id]; }; const moduleStub = { exports: {} }; vm.compileFunction(readRepoFile(...relativeParts), ["require", "module", "exports"])( requireStub, moduleStub, moduleStub.exports ); async function call(route, { body = {}, query = {}, params = {}, user = { id: "user-1" } } = {}) { const handlers = routes.get(route); assert.ok(handlers, `${route} is not mounted`); const res = { payload: null, json(payload) { this.payload = payload; } }; await handlers[handlers.length - 1]({ body, query, params, user }, res); return res.payload; } return { routes, call }; } function makeMap(walls = []) { return { _id: "map-1", campaign: "campaign-1", walls, save: async () => {} }; } function loadMapRoutes({ ownsCampaign = true, maps = [] } = {}) { return loadRouter(["backend", "src", "routes", "map.js"], { "../models/Campaign": { findOne: () => mongooseQuery(ownsCampaign ? { _id: "campaign-1" } : null) }, "../models/Map": { findById: (id) => mongooseQuery(maps.find((map) => map._id === id) ?? null) }, "../models/Note": { findOne: () => mongooseQuery(null) }, "../models/Actor": { findOne: () => mongooseQuery(null) }, "../models/Folder": { find: () => mongooseQuery([]), belongsToSection: async () => true }, }); } function wallRouteTests() { test("a wall is saved with its type and both of its spots in image pixels", async () => { const map = makeMap(); const api = loadMapRoutes({ maps: [map] }); const response = await api.call("POST /wall", { body: { id: "map-1", type: "terrain", x1: "10.4", y1: "-3", x2: "70.6", y2: "40" }, }); assert.equal(response.status, "ok"); assert.deepEqual(map.walls, [{ type: "terrain", x1: 10, y1: 0, x2: 71, y2: 40 }]); }); test("a wall without a known type is a normal wall", async () => { const map = makeMap(); const api = loadMapRoutes({ maps: [map] }); await api.call("POST /wall", { body: { id: "map-1", x1: 0, y1: 0, x2: 50, y2: 0 } }); await api.call("POST /wall", { body: { id: "map-1", type: "door", x1: 0, y1: 50, x2: 50, y2: 50 } }); assert.deepEqual(map.walls.map((wall) => wall.type), ["normal", "normal"]); }); test("the three kinds of wall are kept apart", async () => { const map = makeMap(); const api = loadMapRoutes({ maps: [map] }); for (const type of ["normal", "terrain", "invisible"]) { await api.call("POST /wall", { body: { id: "map-1", type, x1: 0, y1: 0, x2: 10, y2: 10 } }); } assert.deepEqual(map.walls.map((wall) => wall.type), ["normal", "terrain", "invisible"]); }); test("a wall needs its map and both of its ends", async () => { const map = makeMap(); const api = loadMapRoutes({ maps: [map] }); assert.equal((await api.call("POST /wall", { body: { x1: 0, y1: 0, x2: 10, y2: 10 } })).msg, "errors.missing-data"); assert.equal((await api.call("POST /wall", { body: { id: "map-1", x1: 0, y1: 0, y2: 10 } })).msg, "errors.missing-data"); assert.deepEqual(map.walls, []); }); test("a map of somebody else's campaign takes no wall", async () => { const map = makeMap(); const api = loadMapRoutes({ ownsCampaign: false, maps: [map] }); const response = await api.call("POST /wall", { body: { id: "map-1", type: "terrain", x1: 0, y1: 0, x2: 10, y2: 10 } }); assert.equal(response.msg, "unauthorized"); assert.deepEqual(map.walls, []); }); test("removing a wall takes the one it is given and keeps the others", async () => { const map = makeMap([ { _id: "wall-1", type: "normal", x1: 0, y1: 0, x2: 50, y2: 0 }, { _id: "wall-2", type: "invisible", x1: 50, y1: 0, x2: 50, y2: 50 }, ]); const api = loadMapRoutes({ maps: [map] }); const response = await api.call("POST /unwall", { body: { id: "map-1", wall: "wall-1" } }); assert.equal(response.status, "ok"); assert.deepEqual(map.walls, [{ _id: "wall-2", type: "invisible", x1: 50, y1: 0, x2: 50, y2: 50 }]); }); test("removing a wall of a map of somebody else keeps it", async () => { const map = makeMap([{ _id: "wall-1", type: "normal", x1: 0, y1: 0, x2: 50, y2: 0 }]); const api = loadMapRoutes({ ownsCampaign: false, maps: [map] }); const response = await api.call("POST /unwall", { body: { id: "map-1", wall: "wall-1" } }); assert.equal(response.msg, "unauthorized"); assert.equal(map.walls.length, 1); }); test("the map schema stores walls of the three kinds", () => { const model = readRepoFile("backend", "src", "models", "Map.js"); assert.match(model, /walls:\s*\[/); for (const type of ["normal", "terrain", "invisible"]) { assert.match(model, new RegExp(`['"]${type}['"]`), `the ${type} wall is missing from the schema`); } }); } function mapViewerWallWiringTests() { const pane = readRepoFile("frontend", "app", "components", "viewer", "map", "MapViewerPane.vue"); test("the wall tool is a toggle of the map toolbar", () => { assert.match(pane, /:class="\{ selected: wallMode \}"/); assert.match(pane, /@click="toggleWalls"/); assert.match(pane, /const WALL_TYPES = \['normal', 'terrain', 'invisible'\]/); assert.match(pane, /v-for="type in WALL_TYPES"/); }); test("a drag with the wall tool on draws a wall instead of panning the map", () => { assert.match(pane, /if \(wallMode\.value\) \{[^}]*imagePointOf\(event\)/); assert.match(pane, /drawingWall\.value = \{ x1: spot\.x, y1: spot\.y, x2: spot\.x, y2: spot\.y \}/); assert.match(pane, /['"]\/map\/wall['"]/); assert.match(pane, /['"]\/map\/unwall['"]/); }); test("a click that did not drag leaves no wall behind", () => { assert.match(pane, /Math\.hypot\(wall\.x2 - wall\.x1, wall\.y2 - wall\.y1\) >= 2\) await saveWall\(wall\)/); }); test("walls are drawn over the image and only catch the mouse with the tool on", () => { assert.match(pane, /class="map-walls"/); assert.match(pane, /\.map-walls\s*\{[^}]*pointer-events:\s*none/); assert.match(pane, /\.map-walls\.editing \.map-wall-hit\s*\{[^}]*pointer-events:\s*stroke/); assert.match(pane, /@contextmenu\.stop\.prevent="onWallContextMenu\(wall\)"/); }); test("invisible walls are hidden until the wall tool is on", () => { assert.match(pane, /function wallVisible\(wall\) \{\s*return wall\.type !== 'invisible' \|\| wallMode\.value;/); }); } wallRouteTests(); mapViewerWallWiringTests();