jdkjskdj
This commit is contained in:
@@ -148,6 +148,7 @@ function loadFoundryWriters({ seed = {}, settings } = {}) {
|
||||
if (id === "../services/item-foundry") return repoRequire(path.join("backend", "src", "services", "item-foundry.js"));
|
||||
if (id === "../services/foundry-icons") return repoRequire(path.join("backend", "src", "services", "foundry-icons.js"));
|
||||
if (id === "../services/map-foundry") return repoRequire(path.join("backend", "src", "services", "map-foundry.js"));
|
||||
if (id === "../services/service-links") return { unlinkFoundryWorld: async () => {} };
|
||||
if (models[id]) return models[id];
|
||||
throw new Error(`require("${id}") is not stubbed`);
|
||||
};
|
||||
|
||||
@@ -284,6 +284,7 @@ function loadFoundryWriters({ seed = {} } = {}) {
|
||||
if (id === "../services/item-foundry") return repoRequire(path.join("backend", "src", "services", "item-foundry.js"));
|
||||
if (id === "../services/foundry-icons") return repoRequire(path.join("backend", "src", "services", "foundry-icons.js"));
|
||||
if (id === "../services/map-foundry") return repoRequire(path.join("backend", "src", "services", "map-foundry.js"));
|
||||
if (id === "../services/service-links") return { unlinkFoundryWorld: async () => {} };
|
||||
if (models[id]) return models[id];
|
||||
throw new Error(`require("${id}") is not stubbed`);
|
||||
};
|
||||
|
||||
@@ -133,6 +133,7 @@ function loadSyncRoute({ seed = {}, worldId = "world-1" } = {}) {
|
||||
if (id === "../services/item-foundry") return repoRequire(path.join("backend", "src", "services", "item-foundry.js"));
|
||||
if (id === "../services/foundry-icons") return repoRequire(path.join("backend", "src", "services", "foundry-icons.js"));
|
||||
if (id === "../services/map-foundry") return repoRequire(path.join("backend", "src", "services", "map-foundry.js"));
|
||||
if (id === "../services/service-links") return { unlinkFoundryWorld: async () => {} };
|
||||
if (models[id]) return models[id];
|
||||
throw new Error(`require("${id}") is not stubbed`);
|
||||
};
|
||||
|
||||
@@ -40,8 +40,10 @@ function makeStubWorld({
|
||||
const state = {
|
||||
hooks: new Map(),
|
||||
registered: new Map(),
|
||||
menus: new Map(),
|
||||
requests: [],
|
||||
notifications: [],
|
||||
copied: [],
|
||||
};
|
||||
|
||||
const settingsStore = {
|
||||
@@ -57,6 +59,10 @@ function makeStubWorld({
|
||||
URL,
|
||||
CONFIG: {},
|
||||
window: { location: { origin: "http://vtt.test" } },
|
||||
// The module registers a menu for its connection actions: the window needs
|
||||
// ApplicationV2 to exist, not to render.
|
||||
foundry: { applications: { api: { ApplicationV2: class { render() {} } } } },
|
||||
navigator: { clipboard: { writeText: async (text) => state.copied.push(text) } },
|
||||
ui: {
|
||||
notifications: {
|
||||
info: (text) => state.notifications.push(["info", text]),
|
||||
@@ -110,6 +116,7 @@ function makeStubWorld({
|
||||
state.registered.set(key, definition);
|
||||
if (!(key in settingsStore)) settingsStore[key] = definition.default;
|
||||
},
|
||||
registerMenu(module, key, definition) { state.menus.set(key, definition); },
|
||||
get(module, key) { return settingsStore[key]; },
|
||||
async set(module, key, value) {
|
||||
settingsStore[key] = value;
|
||||
@@ -267,6 +274,43 @@ test("an expired link is replaced by one the GM can still open", async () => {
|
||||
assert.match(world.status(), /link-code-2/, "and shows the new one");
|
||||
});
|
||||
|
||||
test("the connection window copies the sign-up link", async () => {
|
||||
const world = await makeStubWorld({
|
||||
syncToken: "",
|
||||
linkCode: "link-code-1",
|
||||
linkUrl: SIGN_UP_LINK,
|
||||
answers: { "link/pickup": { status: "ok", state: "pending" } },
|
||||
}).start();
|
||||
|
||||
const menu = world.menus.get("pairingMenu");
|
||||
assert.ok(menu, "the module settings open the connection window");
|
||||
await menu.type.DEFAULT_OPTIONS.actions.copyLink.call(new menu.type());
|
||||
|
||||
assert.deepEqual(world.copied, [SIGN_UP_LINK], "the link waiting for its GM reaches the clipboard");
|
||||
});
|
||||
|
||||
test("the connection window unlinks the campaign and asks for a fresh link", async () => {
|
||||
const world = await makeStubWorld({
|
||||
answers: {
|
||||
"foundry/sync/status": { status: "ok", campaign: LINKED_CAMPAIGN, worldId: "world-1" },
|
||||
"foundry/sync/unlink": { status: "ok" },
|
||||
"link/start": { status: "ok", code: "link-code-2", linkUrl: SIGN_UP_LINK },
|
||||
},
|
||||
}).start();
|
||||
assert.equal(world.status(), `Connected to the Dragonroll campaign "${LINKED_CAMPAIGN.name}".`);
|
||||
|
||||
const menu = world.menus.get("pairingMenu");
|
||||
await menu.type.DEFAULT_OPTIONS.actions.unlink.call(new menu.type());
|
||||
|
||||
const unlink = world.requests.find((request) => request.href.includes("foundry/sync/unlink"));
|
||||
assert.ok(unlink, "Dragonroll is told to forget this world");
|
||||
assert.equal(unlink.body.worldId, "world-1");
|
||||
assert.equal(unlink.authorization, "Bearer sync-token-1", "the world unlinks with the token it holds");
|
||||
assert.equal(world.setting("syncToken"), "", "the token is dropped on this client too");
|
||||
assert.equal(world.setting("linkCode"), "link-code-2", "a fresh link is kept ready to pair again");
|
||||
assert.match(world.status(), /^Not connected yet/);
|
||||
});
|
||||
|
||||
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" } },
|
||||
|
||||
@@ -145,6 +145,7 @@ function loadSyncRoute({ seed = {}, worldId = "world-1" } = {}) {
|
||||
if (id === "../services/item-foundry") return repoRequire(path.join("backend", "src", "services", "item-foundry.js"));
|
||||
if (id === "../services/foundry-icons") return repoRequire(path.join("backend", "src", "services", "foundry-icons.js"));
|
||||
if (id === "../services/map-foundry") return repoRequire(path.join("backend", "src", "services", "map-foundry.js"));
|
||||
if (id === "../services/service-links") return { unlinkFoundryWorld: async () => {} };
|
||||
if (models[id]) return models[id];
|
||||
throw new Error(`require("${id}") is not stubbed`);
|
||||
};
|
||||
|
||||
@@ -125,6 +125,7 @@ function loadSyncRoute({ seed = {}, worldId = "world-1", settings } = {}) {
|
||||
if (id === "../services/item-foundry") return { foundryItemToItemData: () => null };
|
||||
if (id === "../services/foundry-icons") return repoRequire(path.join("backend", "src", "services", "foundry-icons.js"));
|
||||
if (id === "../services/map-foundry") return repoRequire(path.join("backend", "src", "services", "map-foundry.js"));
|
||||
if (id === "../services/service-links") return { unlinkFoundryWorld: async () => {} };
|
||||
if (models[id]) return models[id];
|
||||
throw new Error(`require("${id}") is not stubbed`);
|
||||
};
|
||||
|
||||
@@ -6,6 +6,58 @@ const { test } = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
|
||||
const serviceLinks = require("../backend/src/services/service-links");
|
||||
const ServiceLinkRequest = require("../backend/src/models/ServiceLinkRequest");
|
||||
|
||||
test("a link found by its code can be answered and saved", async () => {
|
||||
const original = ServiceLinkRequest.findOne;
|
||||
const document = { _id: "link-1", state: "pending", save: async () => {} };
|
||||
const query = {
|
||||
select: () => query,
|
||||
lean: () => Promise.resolve({ _id: "link-1", state: "pending" }),
|
||||
then: (resolve, reject) => Promise.resolve(document).then(resolve, reject),
|
||||
};
|
||||
ServiceLinkRequest.findOne = () => query;
|
||||
try {
|
||||
const request = await serviceLinks.findServiceLinkByCode("code-1");
|
||||
assert.equal(typeof request?.save, "function",
|
||||
"the complete route saves the answered link, so it cannot be a plain lean object");
|
||||
} finally {
|
||||
ServiceLinkRequest.findOne = original;
|
||||
}
|
||||
});
|
||||
|
||||
test("unlinking a Foundry world forgets it and revokes its link", async () => {
|
||||
const original = ServiceLinkRequest.updateMany;
|
||||
const revocations = [];
|
||||
ServiceLinkRequest.updateMany = async (filter, update) => {
|
||||
revocations.push({ filter, update });
|
||||
return { matchedCount: 1 };
|
||||
};
|
||||
try {
|
||||
const campaign = {
|
||||
_id: "campaign-1",
|
||||
foundryWorldId: "world-1",
|
||||
foundryWorldTitle: "Test World",
|
||||
foundrySystem: "dnd5e",
|
||||
foundrySyncTokenHash: "hash",
|
||||
set(key, value) { this[key] = value; },
|
||||
async save() { campaign.saved = true; },
|
||||
};
|
||||
|
||||
await serviceLinks.unlinkFoundryWorld(campaign);
|
||||
|
||||
assert.equal(campaign.saved, true);
|
||||
assert.equal(campaign.foundryWorldId, undefined);
|
||||
assert.equal(campaign.foundryWorldTitle, undefined);
|
||||
assert.equal(campaign.foundrySystem, undefined);
|
||||
assert.equal(campaign.foundrySyncTokenHash, undefined);
|
||||
assert.equal(revocations[0].filter.service, "foundry");
|
||||
assert.equal(revocations[0].filter.campaign, "campaign-1");
|
||||
assert.equal(revocations[0].update.$set.state, "revoked");
|
||||
} finally {
|
||||
ServiceLinkRequest.updateMany = original;
|
||||
}
|
||||
});
|
||||
|
||||
test("a link request keeps the service metadata short and plain", () => {
|
||||
const target = serviceLinks.readTarget({
|
||||
|
||||
Reference in New Issue
Block a user