1
0
Fork 0
polaris-task-force/tests/int/faction-discovery.int.spec.ts
Z8MB1E 9e6cd55fe6 refactor(intelligence): drop the factions.relationships array in favour of the ledger
Nothing stores a pairwise score on a faction any more, so remove the array
field, its admin UI and the masking line that blanked it for undiscovered
factions. The disposal-mask doc comment now points at the ledger, which
applies the same rule by filtering entries that name an undiscovered faction.

The second migration converts every non-zero legacy value into an
opening-balance entry (custom delta, dated at the owning faction's last update
so it does not read as a fresh event, legacy note preserved) and then drops
the table. Zero rows are skipped because a stored 0 and no entries mean the
same thing.

Two integration specs that created or asserted `relationships` now use the
ledger instead.
2026-11-18 14:13:29 -05:00

268 lines
8.1 KiB
TypeScript

import { getTestPayload } from "../helpers/get-test-payload";
import type { Payload } from "payload";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import type { Faction, GameNpc, Role, User } from "@/payload-types";
import { invalidatePermissionCache } from "@/utils/access-control/loadUserPermissions";
import {
UNDISCOVERED_FACTION_NAME,
maskUndiscoveredFaction,
} from "@/lib/intelligence/factionDiscovery";
let payload: Payload;
const RUN = `factiondisc-${Date.now().toString(36)}`;
const TIMEOUT = 30_000;
const DESCRIPTION = {
root: {
type: "root",
direction: null,
format: "" as const,
indent: 0,
version: 1,
children: [
{
type: "paragraph",
version: 1,
direction: null,
format: "" as const,
indent: 0,
children: [{ type: "text", version: 1, text: "Covert logistics and deniable assets." }],
},
],
},
};
describe("Faction discovery masking", () => {
const roleIds: number[] = [];
const userIds: number[] = [];
const factionIds: number[] = [];
const npcIds: number[] = [];
let reader: User;
let revealer: User;
let superUser: User;
let covertId: number;
let overtId: number;
let npcId: number;
const makeRole = async (label: string, extra: Partial<Role> = {}): Promise<Role> => {
const role = (await payload.create({
collection: "roles",
data: { name: `${RUN}-${label}`, slug: `${RUN}-${label}`, ...extra },
overrideAccess: true,
depth: 0,
})) as unknown as Role;
roleIds.push(role.id);
return role;
};
const makeUser = async (label: string, roleId: number): Promise<User> => {
const user = (await payload.create({
collection: "users",
data: {
username: `${RUN}-${label}`,
discordUsername: `${RUN}-${label}`,
displayName: label.toUpperCase(),
steamId: `7656119${Math.floor(Math.random() * 1e9)}`,
password: "Test123",
roleDocs: [roleId],
},
overrideAccess: true,
depth: 0,
})) as unknown as User;
userIds.push(user.id);
return user;
};
/** Local-API read as a real user, so collection access and afterRead hooks apply. */
const readAs = async (user: User | undefined, id: number): Promise<Faction | null> => {
const res = await payload.find({
collection: "factions",
where: { id: { equals: id } },
limit: 1,
depth: 1,
user: user as never,
overrideAccess: false,
});
return (res.docs[0] as Faction | undefined) ?? null;
};
beforeAll(async () => {
payload = await getTestPayload();
invalidatePermissionCache();
const readerRole = await makeRole("reader", { permissions: ["factions:read"] });
const revealerRole = await makeRole("revealer", {
permissions: ["factions:read", "factions:view-undiscovered"],
});
const superRole = await makeRole("super", { isSuperuser: true });
reader = await makeUser("reader", readerRole.id);
revealer = await makeUser("revealer", revealerRole.id);
superUser = await makeUser("super", superRole.id);
const overt = (await payload.create({
collection: "factions",
data: {
name: `${RUN}-Overt Combine`,
type: "corporation",
reputation: 24,
description: DESCRIPTION,
},
overrideAccess: true,
depth: 0,
})) as unknown as Faction;
factionIds.push(overt.id);
overtId = overt.id;
const covert = (await payload.create({
collection: "factions",
data: {
name: `${RUN}-Covert Directorate`,
type: "minorFaction",
discovered: false,
reputation: 64,
description: DESCRIPTION,
playableFaction: true,
startingFaction: true,
associatedCampaigns: [],
},
overrideAccess: true,
depth: 0,
})) as unknown as Faction;
factionIds.push(covert.id);
covertId = covert.id;
const npc = (await payload.create({
collection: "game-npcs",
data: { name: `${RUN}-Courier`, faction: covertId },
overrideAccess: true,
depth: 0,
})) as unknown as GameNpc;
npcIds.push(npc.id);
npcId = npc.id;
invalidatePermissionCache();
}, TIMEOUT);
afterAll(async () => {
if (!payload) return;
for (const id of npcIds) {
await payload.delete({ collection: "game-npcs", id, overrideAccess: true }).catch(() => {});
}
for (const id of factionIds) {
await payload.delete({ collection: "factions", id, overrideAccess: true }).catch(() => {});
}
for (const id of userIds) {
await payload.delete({ collection: "users", id, overrideAccess: true }).catch(() => {});
}
for (const id of roleIds) {
await payload.delete({ collection: "roles", id, overrideAccess: true }).catch(() => {});
}
invalidatePermissionCache();
}, TIMEOUT);
it("masks an undiscovered faction for a plain reader", async () => {
const faction = await readAs(reader, covertId);
expect(faction).not.toBeNull();
const masked = faction as Faction;
expect(masked.name).toBe(UNDISCOVERED_FACTION_NAME);
expect(masked.description).toBeNull();
expect(masked.reputation).toBeNull();
expect(masked.playableFaction).toBe(false);
expect(masked.startingFaction).toBe(false);
expect(masked.coverImage ?? null).toBeNull();
// The type badge and the discovery flag survive so cards still render.
expect(masked.type).toBe("minorFaction");
expect(masked.discovered).toBe(false);
});
it("leaves a discovered faction untouched for the same reader", async () => {
const faction = (await readAs(reader, overtId)) as Faction;
expect(faction.name).toBe(`${RUN}-Overt Combine`);
expect(faction.reputation).toBe(24);
expect(faction.description).not.toBeNull();
});
it("shows the real record to readers holding factions:view-undiscovered", async () => {
const faction = (await readAs(revealer, covertId)) as Faction;
expect(faction.name).toBe(`${RUN}-Covert Directorate`);
expect(faction.reputation).toBe(64);
expect(faction.description).not.toBeNull();
expect(faction.playableFaction).toBe(true);
});
it("shows the real record to superusers", async () => {
const faction = (await readAs(superUser, covertId)) as Faction;
expect(faction.name).toBe(`${RUN}-Covert Directorate`);
expect(faction.reputation).toBe(64);
});
it("keeps the faction unreadable to anonymous requests", async () => {
await expect(
payload.find({
collection: "factions",
where: { id: { equals: covertId } },
limit: 1,
depth: 0,
overrideAccess: false,
}),
).rejects.toThrow(/not allowed/i);
});
it("masks the faction when it is populated through a relationship", async () => {
const asReader = await payload.find({
collection: "game-npcs",
where: { id: { equals: npcId } },
limit: 1,
depth: 1,
user: reader as never,
overrideAccess: false,
});
const readerNpc = asReader.docs[0] as GameNpc;
expect(typeof readerNpc.faction).toBe("object");
expect((readerNpc.faction as Faction).name).toBe(UNDISCOVERED_FACTION_NAME);
const asRevealer = await payload.find({
collection: "game-npcs",
where: { id: { equals: npcId } },
limit: 1,
depth: 1,
user: revealer as never,
overrideAccess: false,
});
const revealerNpc = asRevealer.docs[0] as GameNpc;
expect((revealerNpc.faction as Faction).name).toBe(`${RUN}-Covert Directorate`);
});
it("clears every identifying field in the masking helper", () => {
const masked = maskUndiscoveredFaction({
id: 42,
name: "Real Name",
type: "majorFaction",
discovered: false,
description: DESCRIPTION,
reputation: 90,
associatedCampaigns: [1, 2],
playableFaction: true,
startingFaction: true,
coverImage: 9,
});
expect(masked).toMatchObject({
id: 42,
name: UNDISCOVERED_FACTION_NAME,
type: "majorFaction",
discovered: false,
description: null,
reputation: null,
associatedCampaigns: [],
playableFaction: false,
startingFaction: false,
coverImage: null,
});
});
});