231 lines
8.6 KiB
JavaScript
231 lines
8.6 KiB
JavaScript
// Tests for the pairing status the Dragonroll Foundry module shows in its
|
|
// settings (Syndria98/dragonroll#17). 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).
|
|
//
|
|
// 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");
|
|
|
|
const MODULE_ID = "dragonroll-foundry";
|
|
const MODULE_SOURCE = fs.readFileSync(
|
|
path.join(__dirname, "..", "dragonroll-foundry", "scripts", "api.js"),
|
|
"utf8",
|
|
);
|
|
|
|
const LINKED_CAMPAIGN = { id: "campaign-1", name: "Curse of Strahd" };
|
|
|
|
// 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 = {} } = {}) {
|
|
const state = {
|
|
hooks: new Map(),
|
|
registered: new Map(),
|
|
requests: [],
|
|
notifications: [],
|
|
};
|
|
|
|
const settingsStore = {
|
|
autoSync: true,
|
|
syncToken,
|
|
linkCode,
|
|
dragonrollApiUrl: "http://backend.test:5000",
|
|
};
|
|
|
|
const sandbox = {
|
|
console: { log() {}, info() {}, warn() {}, error() {} },
|
|
URL,
|
|
CONFIG: {},
|
|
window: { location: { origin: "http://vtt.test" } },
|
|
ui: {
|
|
notifications: {
|
|
info: (text) => state.notifications.push(["info", text]),
|
|
warn: (text) => state.notifications.push(["warn", text]),
|
|
error: (text) => state.notifications.push(["error", text]),
|
|
},
|
|
},
|
|
Hooks: {
|
|
once(name, fn) { state.hooks.set(name, [fn]); },
|
|
on(name, fn) {
|
|
const handlers = state.hooks.get(name) || [];
|
|
handlers.push(fn);
|
|
state.hooks.set(name, handlers);
|
|
},
|
|
},
|
|
// 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
|
|
// snapshot and reconcile loops out of these tests.
|
|
setTimeout: () => 0,
|
|
clearTimeout: () => {},
|
|
setInterval: () => 0,
|
|
clearInterval: () => {},
|
|
fetch: async (url, options = {}) => {
|
|
const href = String(url);
|
|
state.requests.push({
|
|
href,
|
|
method: options.method,
|
|
authorization: options.headers?.Authorization || "",
|
|
});
|
|
const route = Object.keys(answers).find((fragment) => href.includes(fragment));
|
|
const payload = route ? answers[route] : { status: "ok" };
|
|
if (payload === null) {
|
|
return { ok: false, status: 404, json: async () => { throw new SyntaxError("Unexpected token < in JSON."); } };
|
|
}
|
|
return { ok: true, status: 200, json: async () => payload };
|
|
},
|
|
Actor: { async create() { return null; } },
|
|
Item: { async create() { return null; } },
|
|
};
|
|
|
|
sandbox.game = {
|
|
user: { isGM: true },
|
|
world: { id: "world-1", title: "Test World" },
|
|
system: { id: "dnd5e" },
|
|
modules: { get: () => ({}) },
|
|
actors: { contents: [], get: () => null },
|
|
items: { contents: [], get: () => null },
|
|
settings: {
|
|
register(module, key, definition) {
|
|
state.registered.set(key, definition);
|
|
if (!(key in settingsStore)) settingsStore[key] = definition.default;
|
|
},
|
|
get(module, key) { return settingsStore[key]; },
|
|
async set(module, key, value) {
|
|
settingsStore[key] = value;
|
|
await state.registered.get(key)?.onChange?.(value);
|
|
},
|
|
},
|
|
};
|
|
|
|
vm.createContext(sandbox);
|
|
vm.runInContext(MODULE_SOURCE, sandbox);
|
|
|
|
state.game = sandbox.game;
|
|
state.status = () => settingsStore.pairingStatus;
|
|
state.statusRequests = () => state.requests.filter((request) => request.href.includes("foundry/sync/status"));
|
|
|
|
// 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.
|
|
state.settle = async () => {
|
|
for (let round = 0; round < 10; round += 1) await new Promise((resolve) => setImmediate(resolve));
|
|
};
|
|
|
|
state.fire = async (name, ...args) => {
|
|
for (const handler of state.hooks.get(name) || []) await handler(...args);
|
|
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");
|
|
return state;
|
|
};
|
|
|
|
return state;
|
|
}
|
|
|
|
test("the pairing status is a setting the module configures", async () => {
|
|
const world = await makeStubWorld().start();
|
|
|
|
const setting = world.registered.get("pairingStatus");
|
|
assert.equal(setting.config, true, "the pairing status appears in the module settings");
|
|
assert.equal(setting.type.name, "String", "the pairing status is a sentence");
|
|
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();
|
|
|
|
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");
|
|
});
|
|
|
|
test("a paired world names the Dragonroll campaign it is connected to", 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.equal(world.statusRequests()[0].authorization, "Bearer sync-token-1", "the campaign is asked with the sync token");
|
|
});
|
|
|
|
test("a campaign that unlinked this world stops being reported as connected", async () => {
|
|
const world = await makeStubWorld({
|
|
answers: { "foundry/sync/status": { status: "error", msg: "unauthorized" } },
|
|
}).start();
|
|
|
|
assert.match(world.status(), /^Not connected/);
|
|
assert.match(world.status(), /no longer linked/);
|
|
});
|
|
|
|
test("an unreachable Dragonroll keeps the pairing this client already knows", async () => {
|
|
const world = await makeStubWorld({ answers: { "foundry/sync/status": null } }).start();
|
|
|
|
assert.equal(world.status(), "Connected to a Dragonroll campaign.",
|
|
"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 () => {
|
|
const world = await makeStubWorld({
|
|
syncToken: "",
|
|
answers: { "foundry/link/complete": { status: "error", msg: "foundry.errors.link-expired" } },
|
|
});
|
|
await world.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/);
|
|
});
|
|
|
|
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 () => {
|
|
const world = await makeStubWorld({
|
|
syncToken: "",
|
|
answers: {
|
|
"foundry/link/complete": { status: "ok", campaign: LINKED_CAMPAIGN, syncToken: "sync-token-2" },
|
|
},
|
|
});
|
|
await world.start();
|
|
|
|
await world.configure("linkCode", "A-FRESH-CODE");
|
|
|
|
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");
|
|
});
|
|
|
|
test("whatever is typed into the status is replaced by the real one", async () => {
|
|
const world = await makeStubWorld({
|
|
answers: { "foundry/sync/status": { status: "ok", campaign: LINKED_CAMPAIGN, worldId: "world-1" } },
|
|
});
|
|
await world.start();
|
|
|
|
await world.configure("pairingStatus", "My own status text");
|
|
|
|
assert.equal(world.status(), `Connected to the Dragonroll campaign "${LINKED_CAMPAIGN.name}".`);
|
|
});
|