This commit is contained in:
@@ -55,7 +55,7 @@ function matches(document, filter = {}) {
|
||||
});
|
||||
}
|
||||
|
||||
function fakeModel(table, seed = []) {
|
||||
function fakeModel(table, seed = [], campaignId) {
|
||||
const rows = [];
|
||||
let counter = 0;
|
||||
|
||||
@@ -74,7 +74,9 @@ function fakeModel(table, seed = []) {
|
||||
Object.defineProperty(Document.prototype, "id", { get() { return this._id; } });
|
||||
|
||||
for (const data of seed) {
|
||||
const row = new Document({ [table === "folder" ? "target" : "name"]: undefined, ...data });
|
||||
// A seeded row belongs to the campaign the requests are made against unless the
|
||||
// seed says otherwise: the routes scope every lookup by campaign.
|
||||
const row = new Document({ campaign: campaignId, [table === "folder" ? "target" : "name"]: undefined, ...data });
|
||||
row.isNew = false;
|
||||
rows.push(row);
|
||||
}
|
||||
@@ -121,13 +123,16 @@ function loadSyncRoute({ seed = {}, worldId = "world-1" } = {}) {
|
||||
};
|
||||
const campaign = { _id: "campaign-1", id: "campaign-1", name: "Test Campaign", foundryWorldId: worldId };
|
||||
const models = {
|
||||
"../models/Actor": fakeModel("actor", seed.actors),
|
||||
"../models/Item": fakeModel("item", seed.items),
|
||||
"../models/Folder": fakeModel("folder", seed.folders),
|
||||
"../models/Actor": fakeModel("actor", seed.actors, campaign.id),
|
||||
"../models/Item": fakeModel("item", seed.items, campaign.id),
|
||||
"../models/Folder": fakeModel("folder", seed.folders, campaign.id),
|
||||
};
|
||||
|
||||
const requireStub = (id) => {
|
||||
if (id === "express") return { Router: () => router };
|
||||
// The route hashes the sync token with node's own crypto: the real module costs nothing
|
||||
// to hand it, and nothing here depends on what the hash looks like.
|
||||
if (id === "node:crypto") return require("node:crypto");
|
||||
if (id === "mongoose") return { isValidObjectId: (value) => typeof value === "string" && value.length > 20 };
|
||||
if (id === "../models/Campaign") return { findOne: async () => campaign };
|
||||
if (id === "../services/actor-foundry") return { foundryActorToCharacter: (actor) => ({ name: actor?.name ?? "" }) };
|
||||
@@ -298,9 +303,12 @@ test("every folder listing says which campaign list it belongs to", () => {
|
||||
// field existed carry none at all. tests/entry-folders.test.js runs these routes.
|
||||
for (const route of ["note.js", "folder.js"]) {
|
||||
const source = read("backend", "src", "routes", route);
|
||||
const listings = source.match(/Folder\.find\(/g) ?? [];
|
||||
// The listings that gather a campaign's folders are the ones shown as a section's
|
||||
// tree. A walk of one folder's own children (`parentFolder`) is inside a single
|
||||
// list already: folders are only ever nested within their own section.
|
||||
const listings = source.match(/Folder\.find\(\{\s*campaign/g) ?? [];
|
||||
assert.ok(listings.length, `${route} lists folders`);
|
||||
const filtered = source.match(/Folder\.find\(\{[^{]*Folder\.filterForTarget\((?:'note'|target)\)/g) ?? [];
|
||||
const filtered = source.match(/Folder\.find\(\{\s*campaign[^{]*Folder\.filterForTarget\((?:'note'|target)\)/g) ?? [];
|
||||
assert.equal(filtered.length, listings.length, `${route} has a folder listing that names no campaign list`);
|
||||
}
|
||||
});
|
||||
@@ -354,6 +362,10 @@ function makeWorld() {
|
||||
id: "actor-1", name: "Thalia", type: "character", documentName: "Actor", flags: {},
|
||||
items: { contents: [carriedItem] },
|
||||
getFlag() { return undefined; },
|
||||
// A pushed actor has its items marked as synced and itself mapped to the
|
||||
// campaign row: nothing here reads those flags back.
|
||||
async update() { return this; },
|
||||
async updateEmbeddedDocuments() { return []; },
|
||||
toObject() { return { _id: this.id, name: this.name, type: this.type, flags: this.flags }; },
|
||||
},
|
||||
];
|
||||
@@ -523,12 +535,12 @@ test("autosync off: folder changes stop syncing", async () => {
|
||||
|
||||
// --- The sidebar groups its items under those folders ----------------------------
|
||||
|
||||
async function loadItemFoldersService() {
|
||||
function loadItemFoldersService() {
|
||||
const source = read("frontend", "app", "services", "ItemFolders.js").replace("export { BuildItemRows };", "return { BuildItemRows };");
|
||||
return vm.compileFunction(source)();
|
||||
}
|
||||
|
||||
const { BuildItemRows } = await loadItemFoldersService();
|
||||
const { BuildItemRows } = loadItemFoldersService();
|
||||
|
||||
const SIDEBAR_FOLDERS = [
|
||||
{ _id: "f-melee", name: "Melee Weapons" },
|
||||
@@ -546,13 +558,15 @@ test("items are grouped under the folder they stand in, nested", () => {
|
||||
row.kind, row.kind === "folder" ? row.folder.name : row.item.name, row.depth,
|
||||
]);
|
||||
|
||||
// Every folder is followed by what stands in it, one step deeper, and the items that
|
||||
// stand at the root come last (see BuildItemRows).
|
||||
assert.deepEqual(shape, [
|
||||
["folder", "Melee Weapons", 0],
|
||||
["item", "Longsword", 1],
|
||||
["folder", "Ranged Weapons", 0],
|
||||
["folder", "Powder Weapons", 1],
|
||||
["item", "Pistol", 1],
|
||||
["item", "Pistol", 2],
|
||||
["item", "Torch", 0],
|
||||
["item", "Longsword", 1],
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -579,9 +593,9 @@ test("an item whose folder is gone, or a folder nested in itself, reads as a roo
|
||||
);
|
||||
|
||||
const shape = rows.map((row) => [row.kind, row.kind === "folder" ? row.folder.name : row.item.name]);
|
||||
assert.ok(shape.includes(["item", "Torch"]), "the item is not lost with the folder that was removed");
|
||||
assert.ok(shape.some(([kind, name]) => kind === "item" && name === "Torch"), "the item is not lost with the folder that was removed");
|
||||
assert.deepEqual(new Set(shape.filter(([kind]) => kind === "folder").map(([, name]) => name)), new Set(["A", "B"]));
|
||||
assert.equal(rows.filter(([kind]) => kind === "folder").length, 2, "a broken tree is walked once, not forever");
|
||||
assert.equal(shape.filter(([kind]) => kind === "folder").length, 2, "a broken tree is walked once, not forever");
|
||||
});
|
||||
|
||||
test("every locale labels the folder row", () => {
|
||||
|
||||
Reference in New Issue
Block a user