Fix issue #65: Item selecting icon
This commit is contained in:
170
tests/foundry-icon-picker.test.js
Normal file
170
tests/foundry-icon-picker.test.js
Normal file
@@ -0,0 +1,170 @@
|
||||
// Tests for Syndria98/dragonroll#65: picking an item's icon opens a window that asks
|
||||
// where the icon comes from — upload one from this device, or load one from the icon
|
||||
// library the linked Foundry world ships in its data directory's `icons/` folder.
|
||||
// That library runs into the thousands, so it is listed one folder per request, with
|
||||
// its icons paged and its thumbnails lazy to load.
|
||||
//
|
||||
// The icon listing helpers are a pure CommonJS service, so they are required and called
|
||||
// directly. The windows and the bridge live 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 iconService = require(path.join(__dirname, "..", "backend", "src", "services", "foundry-icons"));
|
||||
|
||||
const windowDefsSource = read("frontend", "app", "services", "WindowDefinitions.js");
|
||||
const itemWindowSource = read("frontend", "app", "components", "windows", "ItemSheetWindow.vue");
|
||||
const sourceWindowSource = read("frontend", "app", "components", "windows", "ItemIconSourceWindow.vue");
|
||||
const foundryIconsSource = read("frontend", "app", "components", "windows", "FoundryIconPickerWindow.vue");
|
||||
const foundryRouteSource = read("backend", "src", "routes", "foundry.js");
|
||||
const bridgeSource = read("dragonroll-foundry", "bridge", "server.js");
|
||||
|
||||
// --- The listing service: what the browser may ask for, and what it gets back ------
|
||||
|
||||
test("icon listings always stay inside Foundry's icon root", () => {
|
||||
assert.equal(iconService.ICON_ROOT, "icons");
|
||||
assert.equal(iconService.normalizeIconDirectory(""), "icons");
|
||||
assert.equal(iconService.normalizeIconDirectory(undefined), "icons");
|
||||
assert.equal(iconService.normalizeIconDirectory("/icons/svg/"), "icons/svg");
|
||||
assert.equal(iconService.normalizeIconDirectory("icons/sfdcc/axes"), "icons/sfdcc/axes");
|
||||
assert.equal(iconService.normalizeIconDirectory("icons/./svg"), "icons/svg");
|
||||
// nothing that leaves `icons/` is a folder this route will list
|
||||
assert.equal(iconService.normalizeIconDirectory("icons/../secret"), null);
|
||||
assert.equal(iconService.normalizeIconDirectory("../etc"), null);
|
||||
assert.equal(iconService.normalizeIconDirectory("systems/dnd5e/icons"), null);
|
||||
assert.equal(iconService.parentIconDirectory("icons/svg/axes"), "icons/svg");
|
||||
assert.equal(iconService.parentIconDirectory("icons"), null);
|
||||
});
|
||||
|
||||
test("icons are listed a page at a time, bounded", () => {
|
||||
assert.deepEqual(iconService.readIconListingPage({}), { limit: iconService.DEFAULT_ICON_PAGE_SIZE, offset: 0 });
|
||||
assert.deepEqual(iconService.readIconListingPage({ limit: "20", offset: "240" }), { limit: 20, offset: 240 });
|
||||
// a caller cannot ask for the whole library at once, or for a page before the first
|
||||
assert.equal(iconService.readIconListingPage({ limit: "99999" }).limit, iconService.MAX_ICON_PAGE_SIZE);
|
||||
assert.equal(iconService.readIconListingPage({ offset: "-10" }).offset, 0);
|
||||
});
|
||||
|
||||
test("only image files become icons, and they load from the world's own address", () => {
|
||||
const listing = iconService.shapeIconListing({
|
||||
base: "https://vtt.example.com/",
|
||||
dir: "icons/svg",
|
||||
listing: {
|
||||
directories: ["icons/svg/creature", "icons/svg/creature", "icons/svg"],
|
||||
files: ["icons/svg/dice.svg", "icons/svg/sword.png", "icons/svg/readme.txt", "stray.md"],
|
||||
offset: 0,
|
||||
next: 120,
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(listing.dir, "icons/svg");
|
||||
assert.equal(listing.parent, "icons");
|
||||
assert.deepEqual(listing.folders.map((folder) => folder.path), ["icons/svg/creature"]);
|
||||
assert.deepEqual(listing.folders.map((folder) => folder.name), ["creature"]);
|
||||
assert.deepEqual(listing.files.map((icon) => icon.path), ["icons/svg/dice.svg", "icons/svg/sword.png"]);
|
||||
assert.equal(listing.files[0].name, "dice.svg");
|
||||
assert.equal(listing.files[0].url, "https://vtt.example.com/icons/svg/dice.svg");
|
||||
assert.equal(listing.next, 120);
|
||||
});
|
||||
|
||||
test("an icon path with spaces still gives the browser a loadable url", () => {
|
||||
assert.equal(
|
||||
iconService.joinFoundryAssetUrl("http://vtt.test:30000", "icons/sfdcc/axes/halberd sword.svg"),
|
||||
"http://vtt.test:30000/icons/sfdcc/axes/halberd%20sword.svg"
|
||||
);
|
||||
assert.equal(iconService.joinFoundryAssetUrl("", "icons/svg/dice.svg"), "");
|
||||
});
|
||||
|
||||
// --- The backend route ------------------------------------------------------------
|
||||
|
||||
test("the foundry proxy lists one icon folder for one of the caller's campaigns", () => {
|
||||
assert.match(foundryRouteSource, /router\.get\(["']\/icons["'], async \(req, res\)/);
|
||||
const route = /router\.get\(["']\/icons[\s\S]*?module\.exports/.exec(foundryRouteSource)?.[0] ?? foundryRouteSource;
|
||||
// the campaign is where the world address is kept, so it has to be the caller's own
|
||||
assert.match(route, /findOwnedCampaign\(req\.query\.campaign, req\.user\.id\)/);
|
||||
assert.match(foundryRouteSource, /Campaign\.findOne\(\{\s*_id:\s*campaignId,\s*createdBy:\s*userId\s*\}\)/);
|
||||
assert.match(route, /campaign\.settings\?\.integrations\?\.foundryUrl/);
|
||||
assert.match(route, /requestFoundry\(`api\/icons\?\$\{query\}`\)/);
|
||||
// a folder outside the icon root is refused before it reaches the Foundry host
|
||||
assert.match(route, /const dir = normalizeIconDirectory\(req\.query\.dir\);/);
|
||||
});
|
||||
|
||||
// --- The bridge on the Foundry host ------------------------------------------------
|
||||
|
||||
test("the bridge lists a core icon folder and never leaves the icon root", () => {
|
||||
assert.match(bridgeSource, /const dataPath = \(process\.env\.DRAGONROLL_FOUNDRY_DATA_PATH \|\| ""\)\.trim\(\);/);
|
||||
assert.match(bridgeSource, /if \(pathname === "\/api\/icons"\) return sendIconListing\(request, response\);/);
|
||||
assert.match(bridgeSource, /if \(!dataPath\) return sendJson\(response, 503, \{ status: "error", msg: "foundry\.errors\.icons-unavailable" \}\);/);
|
||||
const normalize = /function normalizeIconDirectory\(input\) \{[\s\S]*?\n\}/.exec(bridgeSource)?.[0] ?? "";
|
||||
assert.match(normalize, /segments\.includes\("\.\."\) \|\| segments\[0\] !== iconRoot/);
|
||||
// the answer is one folder plus the offset of the next page of its icons
|
||||
assert.match(bridgeSource, /directories\.sort\(\(a, b\) => a\.localeCompare\(b\)\);/);
|
||||
assert.match(bridgeSource, /const page = files\.slice\(offset, offset \+ limit\);/);
|
||||
assert.match(bridgeSource, /next: offset \+ page\.length < files\.length \? offset \+ page\.length : null,/);
|
||||
});
|
||||
|
||||
// --- The windows -------------------------------------------------------------------
|
||||
|
||||
test("choosing an icon opens its own window from the item sheet", () => {
|
||||
assert.match(windowDefsSource, /item_icon_source:\s*{\s*title:[^}]*ItemIconSourceWindow\.vue/s);
|
||||
assert.match(windowDefsSource, /foundry_icons:\s*{\s*title:[^}]*FoundryIconPickerWindow\.vue/s);
|
||||
// the item window asks where the icon comes from instead of opening a file dialog
|
||||
assert.match(itemWindowSource, /CreateChildWindow\(id, ['"]item_icon_source['"]/);
|
||||
assert.doesNotMatch(itemWindowSource, /type="file"/);
|
||||
});
|
||||
|
||||
test("the icon source window offers uploading and Foundry, and uploads the local file", () => {
|
||||
assert.match(sourceWindowSource, /\$t\(['"]campaigns\.item\.icon-picker\.upload['"]\)/);
|
||||
assert.match(sourceWindowSource, /\$t\(['"]campaigns\.item\.icon-picker\.foundry['"]\)/);
|
||||
// upload: a local file, sent through the item route like the character sheet does its photo
|
||||
assert.match(sourceWindowSource, /type="file" accept="image\/\*"/);
|
||||
assert.match(sourceWindowSource, /\/item\/upload-image\?item=\$\{itemId\}/);
|
||||
assert.match(sourceWindowSource, /formData\.append\(['"]image['"], file\)/);
|
||||
// and it hands the item over to the icon browser, as its child window
|
||||
assert.match(sourceWindowSource, /CreateChildWindow\(id, ['"]foundry_icons['"]/);
|
||||
});
|
||||
|
||||
test("the icon browser browses the world's icons folder by folder", () => {
|
||||
assert.match(foundryIconsSource, /\/foundry\/icons\?\$\{params\.toString\(\)\}/);
|
||||
// one folder per request: navigating a folder asks for it, and going up asks for its parent
|
||||
assert.match(foundryIconsSource, /Load\(ICON_ROOT\)/);
|
||||
assert.match(foundryIconsSource, /@click="Load\(folder\.path\)"/);
|
||||
assert.match(foundryIconsSource, /Load\(listing\.value\.parent \|\| ICON_ROOT\)/);
|
||||
// lazy in every way that matters: a page of icons at a time, thumbnails fetched when seen
|
||||
assert.match(foundryIconsSource, /if \(offset\) params\.set\(['"]offset['"], String\(offset\)\)/);
|
||||
assert.match(foundryIconsSource, /@click="Load\(listing\.dir, listing\.next\)"/);
|
||||
assert.match(foundryIconsSource, /loading="lazy"/);
|
||||
// picking an icon stores the world's url and tells the item window about it
|
||||
assert.match(foundryIconsSource, /Server\(\)\.post\(['"]\/item\/update['"], \{ id: itemId, image: icon\.url \}\)/);
|
||||
assert.match(foundryIconsSource, /emitter\.emit\(['"]campaign-entry-updated['"], \{ entityType: ['"]item['"], entry: response\.data\.item \}\)/);
|
||||
assert.match(foundryIconsSource, /ClearWindow\(\{ type: ['"]item_icon_source['"] \}\)/);
|
||||
});
|
||||
|
||||
// --- Strings -----------------------------------------------------------------------
|
||||
|
||||
test("every locale names the icon windows and their controls", () => {
|
||||
for (const locale of ["en", "es", "ca"]) {
|
||||
const windows = JSON.parse(read("frontend", "i18n", "locales", locale, "windows.json"));
|
||||
for (const key of ["icon-source", "foundry-icons"]) {
|
||||
assert.ok(windows[key], `${locale} is missing the ${key} window title`);
|
||||
}
|
||||
|
||||
const campaigns = JSON.parse(read("frontend", "i18n", "locales", locale, "campaigns.json"));
|
||||
for (const key of ["intro", "upload", "upload-hint", "foundry", "foundry-hint"]) {
|
||||
assert.ok(campaigns.item?.["icon-picker"]?.[key], `${locale} is missing campaigns.item.icon-picker.${key}`);
|
||||
}
|
||||
|
||||
const foundry = JSON.parse(read("frontend", "i18n", "locales", locale, "foundry.json"));
|
||||
for (const key of ["loading", "empty", "load-more", "up"]) {
|
||||
assert.ok(foundry.icons?.[key], `${locale} is missing foundry.icons.${key}`);
|
||||
}
|
||||
for (const key of ["icons-unavailable", "icons-no-world-url"]) {
|
||||
assert.ok(foundry.errors?.[key], `${locale} is missing foundry.errors.${key}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -78,9 +78,10 @@ test("the item window shows the info and offers both icon sources", () => {
|
||||
for (const key of ["form.name", "form.category", "form.rarity", "form.weight", "form.properties", "form.description"]) {
|
||||
assert.ok(itemWindowSource.includes(key), `expected the window to render ${key}`);
|
||||
}
|
||||
assert.doesNotMatch(itemWindowSource, /quantity/, "the item window must not list the quantity");
|
||||
// own icon: upload through the item route like the character sheet does its photo
|
||||
assert.match(itemWindowSource, /\/item\/upload-image\?item=/);
|
||||
// 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 \}\)/);
|
||||
|
||||
Reference in New Issue
Block a user