Merge branch 'master' into ai/issue-20

This commit is contained in:
2026-10-14 16:30:48 +00:00
24 changed files with 1565 additions and 925 deletions

View File

@@ -1,8 +1,12 @@
// Tests for the pairing status the Dragonroll Foundry module shows in its
// settings (Syndria98/dragonroll#17). Like foundry-sync-toggle.test.js, the module
// Tests for how the Dragonroll Foundry module connects a world to a campaign
// (Syndria98/dragonroll#17 and #19). Like foundry-sync-toggle.test.js, the module
// script runs inside a `node:vm` context of stubbed Foundry globals; here each
// test also decides what Dragonroll answers, because the status is that answer
// plus what this client holds (its sync token and any pairing code).
// test also decides what Dragonroll answers, because the pairing status is that
// answer plus what this client holds (its sync token and its sign-up link).
//
// The world no longer redeems a code typed into its settings: it asks Dragonroll
// for the same sign-up link every service gets, shows it, and picks its token up
// once a user has answered that link.
//
// Run with `npm test` from the repository root, or `node --test tests/`.
@@ -19,12 +23,19 @@ const MODULE_SOURCE = fs.readFileSync(
);
const LINKED_CAMPAIGN = { id: "campaign-1", name: "Curse of Strahd" };
const SIGN_UP_LINK = "http://frontend.test/link/authorize?service=foundry#code=link-code-1";
// The stub starts as a world that finished pairing (`syncToken: ""` is one that
// never did). `answers` maps a route fragment to the JSON Dragonroll replies
// with; a route that is not listed answers `{status:"ok"}`, and one mapped to
// null answers HTML, like a Dragonroll old enough not to have that route at all.
function makeStubWorld({ syncToken = "sync-token-1", linkCode = "", answers = {} } = {}) {
// never did), optionally holding a sign-up link it is waiting on. `answers` maps
// a route fragment to the JSON Dragonroll replies with; a route that is not
// listed answers `{status:"ok"}`, and one mapped to null answers HTML, like a
// Dragonroll old enough not to have that route at all.
function makeStubWorld({
syncToken = "sync-token-1",
linkCode = "",
linkUrl = "",
answers = {},
} = {}) {
const state = {
hooks: new Map(),
registered: new Map(),
@@ -36,6 +47,7 @@ function makeStubWorld({ syncToken = "sync-token-1", linkCode = "", answers = {}
autoSync: true,
syncToken,
linkCode,
linkUrl,
dragonrollApiUrl: "http://backend.test:5000",
};
@@ -60,7 +72,7 @@ function makeStubWorld({ syncToken = "sync-token-1", linkCode = "", answers = {}
},
},
// The pairing status never waits on a timer: it is written when the world is
// ready and whenever a pairing attempt answers, so dropping these keeps the
// ready and whenever a link check answers, so dropping these keeps the
// snapshot and reconcile loops out of these tests.
setTimeout: () => 0,
clearTimeout: () => {},
@@ -71,6 +83,7 @@ function makeStubWorld({ syncToken = "sync-token-1", linkCode = "", answers = {}
state.requests.push({
href,
method: options.method,
body: options.body ? JSON.parse(options.body) : null,
authorization: options.headers?.Authorization || "",
});
const route = Object.keys(answers).find((fragment) => href.includes(fragment));
@@ -85,7 +98,7 @@ function makeStubWorld({ syncToken = "sync-token-1", linkCode = "", answers = {}
};
sandbox.game = {
user: { isGM: true },
user: { isGM: true, name: "Test GM" },
world: { id: "world-1", title: "Test World" },
system: { id: "dnd5e" },
modules: { get: () => ({}) },
@@ -108,8 +121,11 @@ function makeStubWorld({ syncToken = "sync-token-1", linkCode = "", answers = {}
vm.runInContext(MODULE_SOURCE, sandbox);
state.game = sandbox.game;
state.setting = (key) => settingsStore[key];
state.status = () => settingsStore.pairingStatus;
state.statusRequests = () => state.requests.filter((request) => request.href.includes("foundry/sync/status"));
state.linkStarts = () => state.requests.filter((request) => request.href.includes("link/start"));
state.linkPickups = () => state.requests.filter((request) => request.href.includes("link/pickup"));
// The module's own awaits run in the vm's promise intrinsics, so a few turns of
// the event loop are what it takes for a written status to be observable.
@@ -122,11 +138,6 @@ function makeStubWorld({ syncToken = "sync-token-1", linkCode = "", answers = {}
await state.settle();
};
state.configure = async (key, value) => {
await sandbox.game.settings.set(MODULE_ID, key, value);
await state.settle();
};
state.start = async () => {
await state.fire("init");
await state.fire("ready");
@@ -145,20 +156,31 @@ test("the pairing status is a setting the module configures", async () => {
assert.equal(setting.scope, "client", "the pairing status belongs to this client, like the token it comes from");
});
test("a world that was never paired says it is not connected", async () => {
const world = await makeStubWorld({ syncToken: "" }).start();
test("an unpaired world asks Dragonroll for a sign-up link and shows it", async () => {
const world = await makeStubWorld({
syncToken: "",
answers: { "link/start": { status: "ok", code: "link-code-1", linkUrl: SIGN_UP_LINK } },
}).start();
assert.match(world.status(), /^Not connected/, "an unpaired world says so plainly");
assert.match(world.status(), /Dragonroll campaign/, "and names what it is not connected to");
assert.deepEqual(world.statusRequests(), [], "there is nothing to verify without a sync token");
const started = world.linkStarts()[0];
assert.ok(started, "the world asks for a link of its own");
assert.equal(started.body.service, "foundry", "and says which service it is");
assert.equal(started.body.target.id, "world-1", "the world tells Dragonroll who it is");
assert.equal(started.body.target.name, "Test World");
assert.equal(started.body.target.meta.system, "dnd5e");
assert.match(world.status(), /^Not connected yet/);
assert.ok(world.status().includes(SIGN_UP_LINK), "the status is the link the GM has to open");
assert.equal(world.setting("linkCode"), "link-code-1", "the code behind the link is kept to ask with");
});
test("a paired world names the Dragonroll campaign it is connected to", async () => {
test("a world that holds a token neither asks for a link nor waits on one", async () => {
const world = await makeStubWorld({
answers: { "foundry/sync/status": { status: "ok", campaign: LINKED_CAMPAIGN, worldId: "world-1" } },
}).start();
assert.equal(world.status(), `Connected to the Dragonroll campaign "${LINKED_CAMPAIGN.name}".`);
assert.deepEqual(world.linkStarts(), [], "a paired world has nothing to link");
assert.equal(world.statusRequests()[0].authorization, "Bearer sync-token-1", "the campaign is asked with the sync token");
});
@@ -169,6 +191,7 @@ test("a campaign that unlinked this world stops being reported as connected", as
assert.match(world.status(), /^Not connected/);
assert.match(world.status(), /no longer linked/);
assert.equal(world.setting("syncToken"), "", "the dead token is dropped, so nothing keeps pushing to that campaign");
});
test("an unreachable Dragonroll keeps the pairing this client already knows", async () => {
@@ -178,44 +201,58 @@ test("an unreachable Dragonroll keeps the pairing this client already knows", as
"a Dragonroll without the status route says nothing, so the token speaks");
});
test("a code that cannot be redeemed explains why the world is not connected", async () => {
test("a link still waiting on its GM says the world is not connected yet", async () => {
const world = await makeStubWorld({
syncToken: "",
answers: { "foundry/link/complete": { status: "error", msg: "foundry.errors.link-expired" } },
});
await world.start();
linkCode: "link-code-1",
linkUrl: SIGN_UP_LINK,
answers: { "link/pickup": { status: "ok", state: "pending" } },
}).start();
await world.configure("linkCode", "AN-EXPIRED-CODE");
assert.match(world.status(), /^Not connected/);
assert.match(world.status(), /pairing code is invalid, expired, or already used/);
assert.equal(world.linkPickups()[0].body.code, "link-code-1", "the world asks with the code it was given");
assert.deepEqual(world.linkStarts(), [], "an unanswered link is kept, not replaced");
assert.match(world.status(), /^Not connected yet/);
assert.ok(world.status().includes(SIGN_UP_LINK), "and it is still the link to open");
});
test("a code still waiting on Dragonroll says the pairing is not finished", async () => {
const world = await makeStubWorld({
syncToken: "",
answers: { "foundry/link/complete": { status: "error", msg: "errors.internal" } },
});
await world.start();
await world.configure("linkCode", "A-CODE-TO-RETRY");
assert.equal(world.status(), "Not connected yet: waiting for Dragonroll to accept this pairing code.");
});
test("redeeming a pairing code connects the world to its campaign", async () => {
test("a picked-up link connects the world, hands over its token and is dropped", async () => {
const world = await makeStubWorld({
syncToken: "",
linkCode: "link-code-1",
linkUrl: SIGN_UP_LINK,
answers: {
"foundry/link/complete": { status: "ok", campaign: LINKED_CAMPAIGN, syncToken: "sync-token-2" },
"link/pickup": {
status: "ok",
state: "linked",
campaign: LINKED_CAMPAIGN,
syncToken: "sync-token-2",
bridgeConfig: { url: "http://bridge.test", pushToken: "push-1" },
},
// The token this link handed over is the one the campaign is asked with.
"foundry/sync/status": { status: "ok", campaign: LINKED_CAMPAIGN, worldId: "world-1" },
},
});
await world.start();
await world.configure("linkCode", "A-FRESH-CODE");
}).start();
assert.equal(world.status(), `Connected to the Dragonroll campaign "${LINKED_CAMPAIGN.name}".`);
assert.equal(world.game.settings.get(MODULE_ID, "linkCode"), "", "a redeemed code is not kept");
assert.equal(world.setting("syncToken"), "sync-token-2", "the token the link came with is what sync runs on");
assert.deepEqual(world.setting("bridgeConfig"), { url: "http://bridge.test", pushToken: "push-1" });
assert.equal(world.setting("linkCode"), "", "a link that delivered its token is not asked about again");
assert.equal(world.setting("linkUrl"), "");
});
test("an expired link is replaced by one the GM can still open", async () => {
const world = await makeStubWorld({
syncToken: "",
linkCode: "link-code-1",
linkUrl: SIGN_UP_LINK,
answers: {
"link/pickup": { status: "ok", state: "expired" },
"link/start": { status: "ok", code: "link-code-2", linkUrl: "http://frontend.test/link/authorize?service=foundry#code=link-code-2" },
},
}).start();
assert.equal(world.setting("linkCode"), "link-code-2", "the world keeps a link nobody has to guess at");
assert.match(world.status(), /link-code-2/, "and shows the new one");
});
test("whatever is typed into the status is replaced by the real one", async () => {
@@ -224,7 +261,8 @@ test("whatever is typed into the status is replaced by the real one", async () =
});
await world.start();
await world.configure("pairingStatus", "My own status text");
await world.game.settings.set(MODULE_ID, "pairingStatus", "My own status text");
await world.settle();
assert.equal(world.status(), `Connected to the Dragonroll campaign "${LINKED_CAMPAIGN.name}".`);
});

View File

@@ -0,0 +1,88 @@
// Tests for the entity-entry sizing problem of Syndria98/dragonroll#21: the actor / item / map rows
// in the campaign sidebar were wider than the sidebar itself, so their hover border and the anchored
// state of a map poked outside of it.
//
// The repository root installs no dependencies, so the checks read the sidebar component the same way
// `tests/map-viewer.test.js` reads its window: the rules are about the box of the elements themselves.
//
// 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 source = fs.readFileSync(
path.join(__dirname, "..", "frontend", "app", "components", "partials", "ContentSidebar.vue"),
"utf8"
);
// `selector` is inserted verbatim, so escape the `.` of a class combination (`entity-entry\\.anchored`).
// A trailing `\.entity-entry\s*\{` cannot match `.entity-entry:hover` or `.entity-entry-name`, so the
// plain rule is always the one that comes back. Comments are dropped: they explain a rule, they are
// not part of it.
function ruleFor(selector) {
const rule = new RegExp(`\\.${selector}\\s*\\{([^}]*)\\}`).exec(source)?.[0];
return rule?.replace(/\/\*[\s\S]*?\*\//g, "");
}
function declaration(rule, property) {
return new RegExp(`${property}:\\s*([^;]*)`).exec(rule)?.[1].trim();
}
test("the sidebar list the entity entries live in is border-box sized", () => {
const wrap = ruleFor("sidebar-list-drop-wrap");
assert.ok(wrap, "expected a .sidebar-list-drop-wrap style rule");
// Its fixed width already includes the padding the entries sit in; the entries' 100% resolves
// against that padded content box.
assert.match(wrap, /box-sizing:\s*border-box/);
assert.match(wrap, /padding:/);
});
test("every section renders its entries inside the sidebar list", () => {
const list = /class="sidebar-list-drop-wrap"[\s\S]*?\n {12}<\/div>\n {8}<\/aside>/.exec(source);
assert.ok(list, "expected the sidebar list wrapper to be followed by the closing aside");
// actors, items and maps all use the same row
const rendered = list[0].match(/class="entity-entry"/g) || [];
assert.ok(rendered.length >= 3, `expected every section to render entity entries, found ${rendered.length}`);
});
test("an entity entry measures its padding and border inside the sidebar width", () => {
const entry = ruleFor("entity-entry");
assert.ok(entry, "expected a .entity-entry style rule");
const width = declaration(entry, "width");
const padding = declaration(entry, "padding");
const border = declaration(entry, "border");
assert.ok(width, "the entry must fill the sidebar list");
assert.ok(padding && border, "the entry has inner padding and a border to fit");
// These rows are divs and the app ships no global box-sizing reset, so with the default
// content-box the padding and the border were added on top of the 100% width and the box — border
// and all — overflowed the sidebar.
assert.equal(
declaration(entry, "box-sizing"),
"border-box",
"an entry sized at 100% with padding and a border needs box-sizing: border-box"
);
});
test("the hovered and anchored states keep the entry the same size", () => {
const entry = ruleFor("entity-entry");
// The resting border is transparent instead of `none`, so showing it on hover or when a map is
// anchored only recolours it and never grows the row.
assert.match(declaration(entry, "border"), /transparent/);
for (const selector of ["entity-entry:hover", "entity-entry\\.anchored"]) {
const rule = ruleFor(selector);
assert.ok(rule, `expected a .${selector} style rule`);
assert.doesNotMatch(rule, /border(-width)?\s*:\s*[^;]*\d+px/, `.${selector} must not widen the border`);
assert.match(rule, /border-color:/, `.${selector} is a state that shows the border`);
}
});
test("a long entry name wraps instead of pushing the entry out of the sidebar", () => {
const name = ruleFor("entity-entry-name");
assert.ok(name, "expected a .entity-entry-name style rule");
assert.match(name, /(word-break|overflow-wrap):/, "an unbreakable name has to wrap inside the entry");
});