Merge pull request 'Fix #74: Foundry's icons are not available: its bridge is not set up, or it cannot be reached right now.' (#82) from ai/issue-74 into master
Some checks failed
Test / test (push) Successful in 2s
Build and Deploy Nuxt / build (push) Has been cancelled

Reviewed-on: #82
This commit was merged in pull request #82.
This commit is contained in:
2026-10-15 19:31:20 +00:00
3 changed files with 82 additions and 2 deletions

View File

@@ -1,5 +1,11 @@
async function requestFoundry(path) {
const baseUrl = process.env.FOUNDRY_API_URL;
// The bridge a campaign's icon and world reads go to is the very one that
// world already pushes its snapshot to. It is often configured under
// FOUNDRY_API_CLIENT_URL alone — the address the Foundry client reaches —
// while FOUNDRY_API_URL is the optional private one the backend prefers.
// Falling back to it keeps a connected, syncing world's reads working
// instead of reporting its bridge as unset (Syndria98/dragonroll#74).
const baseUrl = process.env.FOUNDRY_API_URL || process.env.FOUNDRY_API_CLIENT_URL;
const token = process.env.FOUNDRY_API_TOKEN;
if (!baseUrl || !token) {

View File

@@ -62,7 +62,12 @@ async function Load(dir, offset = 0) {
? { ...page, files: [...listing.value.files, ...page.files] }
: page;
} catch (requestError) {
error.value = t('foundry.errors.icons-unavailable');
// The route answers every real failure with an i18n key in `msg`, but
// axios rejects a non-2xx response, so this catch would otherwise blame
// the bridge for anything at all — a campaign without a world address,
// a folder that went missing. Read the reason off the response and say
// that instead (Syndria98/dragonroll#74).
error.value = t(requestError.response?.data?.msg || 'foundry.errors.icons-unavailable');
} finally {
loading.value = false;
}

View File

@@ -17,6 +17,8 @@ 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 foundryService = require(path.join(__dirname, "..", "backend", "src", "services", "foundry"));
const http = require("node:http");
const windowDefsSource = read("frontend", "app", "services", "WindowDefinitions.js");
const itemWindowSource = read("frontend", "app", "components", "windows", "ItemSheetWindow.vue");
@@ -145,6 +147,73 @@ test("the icon browser browses the world's icons folder by folder", () => {
assert.match(foundryIconsSource, /ClearWindow\(\{ type: ['"]item_icon_source['"] \}\)/);
});
// --- The bridge the backend reads the icon library through (issue #74) --------
//
// The icon window reaches the world's `icons/` folder through the backend, which
// in turn reads the bridge that the world already pushes its snapshot to. When
// that push works the campaign reads as connected, so a read that still reports
// the bridge as unset is the bug this guards against.
// Set the bridge environment for one call and put it back exactly as it was.
function withBridgeEnv(env, run) {
const keys = ["FOUNDRY_API_URL", "FOUNDRY_API_CLIENT_URL", "FOUNDRY_API_TOKEN"];
const saved = Object.fromEntries(keys.map((key) => [key, process.env[key]]));
for (const key of keys) {
if (env[key] === undefined) delete process.env[key];
else process.env[key] = env[key];
}
return Promise.resolve()
.then(run)
.finally(() => {
for (const key of keys) {
if (saved[key] === undefined) delete process.env[key];
else process.env[key] = saved[key];
}
});
}
test("a connected world answers icon reads through the bridge set under the client URL", async () => {
// A world that connects and syncs is often configured with FOUNDRY_API_CLIENT_URL
// alone — the address its own client reaches. The backend read falls back to it,
// so the icon window is not told its bridge is unset while the world is right there.
const answers = [];
const bridge = http.createServer((request, response) => {
answers.push(request.url);
response.writeHead(200, { "Content-Type": "application/json" });
response.end(JSON.stringify({ status: "ok", dir: "icons", directories: ["icons/svg"], files: [], offset: 0, next: null }));
});
await new Promise((resolve) => bridge.listen(0, "127.0.0.1", resolve));
const clientUrl = `http://127.0.0.1:${bridge.address().port}/`;
try {
const result = await withBridgeEnv(
{ FOUNDRY_API_URL: undefined, FOUNDRY_API_CLIENT_URL: clientUrl, FOUNDRY_API_TOKEN: "read-token" },
() => foundryService.requestFoundry("api/icons?dir=icons&offset=0&limit=120"),
);
assert.equal(result.status, 200);
assert.equal(result.data.status, "ok");
assert.equal(answers.length, 1);
assert.match(answers[0], /^\/api\/icons\?/);
} finally {
await new Promise((resolve) => bridge.close(resolve));
}
});
test("without a bridge under either name a read says it is not configured", async () => {
const error = await withBridgeEnv(
{ FOUNDRY_API_URL: undefined, FOUNDRY_API_CLIENT_URL: undefined, FOUNDRY_API_TOKEN: undefined },
() => foundryService.requestFoundry("api/icons?dir=icons").then(() => null, (err) => err),
);
assert.equal(error?.code, "FOUNDRY_NOT_CONFIGURED");
});
test("the icon window says what actually failed instead of always blaming the bridge", () => {
// axios rejects a non-2xx answer, so the catch reads the route's own i18n key
// off the response: a campaign with no world address is told as much rather
// than as a bridge that is not set up.
assert.match(foundryIconsSource, /requestError\.response\?\.data\?\.msg/);
assert.match(foundryIconsSource, /requestError\.response\?\.data\?\.msg \|\| ['"]foundry\.errors\.icons-unavailable['"]/);
});
// --- Strings -----------------------------------------------------------------------
test("every locale names the icon windows and their controls", () => {