// Tests for Syndria98/dragonroll#35: clicking an item in the campaign sidebar opens a window with // the item's info, and that window can pick the item's icon — Foundry's own when the item is synced, // or an uploaded one. // // The mapper from a Foundry item to Dragonroll item fields is a pure CommonJS service, so it is // required and called directly. The window wiring lives in files this repository installs no // dependencies for, so those checks read the sources, like tests/map-viewer.test.js does. // // 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 { foundryItemToItemData } = require(path.join(__dirname, "..", "backend", "src", "services", "item-foundry")); const sidebarSource = read("frontend", "app", "components", "partials", "ContentSidebar.vue"); const windowDefsSource = read("frontend", "app", "services", "WindowDefinitions.js"); const itemWindowSource = read("frontend", "app", "components", "windows", "ItemSheetWindow.vue"); const itemRouteSource = read("backend", "src", "routes", "item.js"); const itemModelSource = read("backend", "src", "models", "Item.js"); const foundryLinkSource = read("backend", "src", "routes", "foundry-link.js"); const moduleSource = read("dragonroll-foundry", "scripts", "api.js"); test("a synced Foundry item carries its icon over to the Dragonroll item", () => { const fields = foundryItemToItemData({ name: "Longsword", type: "weapon", img: "http://vtt.test/icons/svg/sword.svg", system: { quantity: 1, weight: 3, rarity: "common" }, }); assert.equal(fields.image, "http://vtt.test/icons/svg/sword.svg"); }); test("a Foundry item without an icon maps to an empty one", () => { const fields = foundryItemToItemData({ name: "Torch", type: "loot", system: { quantity: 1 } }); assert.equal(fields.image, ""); }); test("the Item model stores the icon and the icon Foundry last sent", () => { assert.match(itemModelSource, /image:\s*{\s*type:\s*String/); assert.match(itemModelSource, /foundryImage:\s*{\s*type:\s*String/); }); test("Foundry sync keeps foundryImage even when the save preserves Dragonroll edits", () => { // The preserve-edits rule must not cover foundryImage: it is Foundry's state, // and the item window offers it back after the user picked their own icon. const keep = /if \(!fullSync \|\| item\.isNew\) Object\.assign\(item, fields\);([\s\S]{0,220}?)if \(!item\.foundryNames/.exec(foundryLinkSource); assert.ok(keep, "expected the foundryImage keep between the guarded assign and the name bookkeeping"); assert.match(keep[1], /if \(fields\.image\) item\.foundryImage = fields\.image;/); }); test("the item routes accept the icon and upload one", () => { assert.match(itemRouteSource, /router\.post\(["']\/upload-image["'], upload\.single\(["']image["']\)/); assert.match(itemRouteSource, /req\.query\.item \|\| req\.body\.item/); assert.match(itemRouteSource, /item\.image = req\.file\.filename/); // the upload checks ownership like every other item handler const uploadRoute = /router\.post\(["']\/upload-image[\s\S]*?module\.exports/.exec(itemRouteSource)?.[0] ?? itemRouteSource; assert.match(uploadRoute, /userOwnsCampaign\(item\.campaign, req\.user\.id\)/); // /update accepts the icon so the window can switch between own and Foundry's assert.match(itemRouteSource, /if \(image !== undefined\) item\.image = String\(image\);/); }); test("the item info window is registered and opens on a sidebar click", () => { assert.match(windowDefsSource, /item_sheet:\s*{\s*title:[^}]*ItemSheetWindow\.vue/s); // clicking (or Enter/Space on) an item row opens the window instead of expanding inline. // Since #44 the rows come from the folder tree the world synced, so the row holds the item. assert.match(sidebarSource, /v-for="row in itemRows"[\s\S]*?@click="openItemSheet\(row\.item\)"/); assert.match(sidebarSource, /CreateWindow\(['"]item_sheet['"],\s*\{ item \}\)/); assert.doesNotMatch(sidebarSource, /toggleEntryDetails/); }); test("the item window shows the info and offers both icon sources", () => { // info: name, category, rarity, quantity, weight, properties, description for (const key of ["form.name", "form.category", "form.rarity", "form.quantity", "form.weight", "form.properties", "form.description"]) { assert.ok(itemWindowSource.includes(key), `expected the window to render ${key}`); } // own icon: #65 moved picking one into a window of its own, which uploads through // the item route (see tests/foundry-icon-picker.test.js); the sheet opens it and // follows whatever the choice leaves behind. assert.match(itemWindowSource, /CreateChildWindow\(id, ['"]item_icon_source['"]/); // Foundry icon: switch the item image back to the one the world sent assert.match(itemWindowSource, /form\.value\.foundryImage/); assert.match(itemWindowSource, /updateItem\(\{ image: form\.value\.foundryImage \}\)/); // uploads resolve as backend /public files, world URLs load as they are assert.match(itemWindowSource, /\$\{getBaseUrl\(\)\}\/public\//); }); test("the Foundry module sends item icons as URLs Dragonroll can load", () => { // actor-embedded items go through the same serializer as world items assert.match(moduleSource, /items:\s*\(actor\.items\?\.contents\?\.map\(serializeFoundryItem\)/); // the icon chosen in Dragonroll travels back into the linked world assert.match(moduleSource, /const image = getDragonrollImageUrl\(entry\.image\);\s*if \(image\) updates\.img = image;/); assert.match(moduleSource, /img: getDragonrollImageUrl\(entry\.image\) \|\| undefined,\s*system:/); }); test("every locale names the item window and its icon controls", () => { for (const locale of ["en", "es", "ca"]) { const windows = JSON.parse(read("frontend", "i18n", "locales", locale, "windows.json")); assert.ok(windows["item-sheet"], `${locale} is missing the item window title`); const campaigns = JSON.parse(read("frontend", "i18n", "locales", locale, "campaigns.json")); for (const key of ["choose-icon", "change-icon", "foundry-icon", "remove-icon", "upload-error"]) { assert.ok(campaigns.item?.sheet?.[key], `${locale} is missing campaigns.item.sheet.${key}`); } } });