is_true/is_false test truthiness, so a flag explicitly set to false reads the same as a flag that was never recorded. is_defined/is_undefined test existence instead, which lets a branch fire on "has this happened yet". A pending-activation flag counts as defined (recorded, not yet in effect) and an expired personal entry now falls through to the global entry, which is what readFlag's contract already promised. NUMERIC_CONDITION_OPS becomes the shared list of ops that take an amount, so the editor, the entryGate field and conditionSummary agree. Adding to CONDITION_OPS also alters enum_npc_dialogues_entry_gate_op, so the migration is an ALTER TYPE ADD VALUE, applied to dev.
232 lines
7.7 KiB
TypeScript
232 lines
7.7 KiB
TypeScript
import { getTestPayload } from "../helpers/get-test-payload";
|
|
import { Payload } from "payload";
|
|
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
|
|
import type { DialogueCondition, DialogueFlagCondition } from "@/lib/npc-dialogue/dialogue";
|
|
import { applyFlagEffect, evaluateCondition } from "@/lib/npc-dialogue/flags";
|
|
import { conditionSummary, hasEmptyConditionKey, validateGraph } from "@/lib/npc-dialogue/dialogue";
|
|
|
|
let payload: Payload;
|
|
|
|
const RUN = `grp_${Date.now().toString(36)}`;
|
|
const TIMEOUT = 30_000;
|
|
|
|
const userIds: number[] = [];
|
|
|
|
async function createUser(username: string): Promise<number> {
|
|
const user = (await payload.create({
|
|
collection: "users",
|
|
data: {
|
|
username,
|
|
discordUsername: username,
|
|
displayName: `GRP ${username}`,
|
|
steamId: `7656119${Math.floor(Math.random() * 1e9)}`,
|
|
password: "Test123",
|
|
roles: ["user"],
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as { id: number };
|
|
userIds.push(user.id);
|
|
return user.id;
|
|
}
|
|
|
|
describe("condition groups (and / or / nor over leaf conditions)", () => {
|
|
beforeAll(async () => {
|
|
payload = await getTestPayload();
|
|
}, TIMEOUT);
|
|
|
|
afterAll(async () => {
|
|
const flags = await payload.find({
|
|
collection: "player-flags",
|
|
where: { key: { like: `${RUN}_%` } },
|
|
limit: 200,
|
|
overrideAccess: true,
|
|
});
|
|
for (const doc of flags.docs) {
|
|
await payload.delete({ collection: "player-flags", id: doc.id, overrideAccess: true });
|
|
}
|
|
for (const id of userIds) {
|
|
await payload.delete({ collection: "users", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
});
|
|
|
|
it(
|
|
"combines leaves with and / or / nor against live flags",
|
|
async () => {
|
|
const user = await createUser(`${RUN}_combo`);
|
|
await applyFlagEffect(payload, user, `${RUN}_a`, "set_true", 0);
|
|
await applyFlagEffect(payload, user, `${RUN}_rep`, "set_number", 7);
|
|
|
|
const leafA: DialogueFlagCondition = { kind: "flag", key: `${RUN}_a`, op: "is_true" };
|
|
const leafMissing: DialogueFlagCondition = {
|
|
kind: "flag",
|
|
key: `${RUN}_nope`,
|
|
op: "is_true",
|
|
};
|
|
const leafRep: DialogueFlagCondition = { kind: "flag", key: `${RUN}_rep`, op: "gte", amount: 5 };
|
|
const leafRepFail: DialogueFlagCondition = {
|
|
kind: "flag",
|
|
key: `${RUN}_rep`,
|
|
op: "gte",
|
|
amount: 99,
|
|
};
|
|
|
|
const evalGroup = (op: "and" | "or" | "nor", children: DialogueFlagCondition[]) =>
|
|
evaluateCondition(payload, user, { kind: "group", op, children });
|
|
|
|
expect(await evalGroup("and", [leafA, leafRep])).toBe(true);
|
|
expect(await evalGroup("and", [leafA, leafMissing])).toBe(false);
|
|
expect(await evalGroup("or", [leafMissing, leafRep])).toBe(true);
|
|
expect(await evalGroup("or", [leafMissing, leafRepFail])).toBe(false);
|
|
expect(await evalGroup("nor", [leafMissing, leafRepFail])).toBe(true);
|
|
expect(await evalGroup("nor", [leafA, leafRepFail])).toBe(false);
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it(
|
|
"treats empty groups as vacuous (and/nor pass, or fails) and leaves still work",
|
|
async () => {
|
|
const user = await createUser(`${RUN}_empty`);
|
|
expect(await evaluateCondition(payload, user, { kind: "group", op: "and", children: [] })).toBe(
|
|
true,
|
|
);
|
|
expect(await evaluateCondition(payload, user, { kind: "group", op: "nor", children: [] })).toBe(
|
|
true,
|
|
);
|
|
expect(await evaluateCondition(payload, user, { kind: "group", op: "or", children: [] })).toBe(
|
|
false,
|
|
);
|
|
// A plain leaf keeps its old semantics.
|
|
expect(
|
|
await evaluateCondition(payload, user, { kind: "flag", key: `${RUN}_x`, op: "is_false" }),
|
|
).toBe(true);
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it("summarises and validates groups for the editor", () => {
|
|
const group: DialogueCondition = {
|
|
kind: "group",
|
|
op: "or",
|
|
children: [
|
|
{ kind: "flag", key: "met_informant", op: "is_true" },
|
|
{ kind: "flag", key: "rep", op: "gte", amount: 5 },
|
|
],
|
|
};
|
|
expect(conditionSummary(group)).toBe('"met_informant" is true or "rep" gte 5');
|
|
expect(hasEmptyConditionKey(group)).toBe(false);
|
|
expect(
|
|
hasEmptyConditionKey({
|
|
kind: "group",
|
|
op: "and",
|
|
children: [{ kind: "flag", key: " ", op: "is_true" }],
|
|
}),
|
|
).toBe(true);
|
|
expect(hasEmptyConditionKey({ kind: "group", op: "and", children: [] })).toBe(true);
|
|
|
|
const nodes = [
|
|
{
|
|
id: "start",
|
|
type: "dialogue" as const,
|
|
position: { x: 0, y: 0 },
|
|
data: { isStart: true, body: "hi", next: { target: "c1" } },
|
|
},
|
|
{
|
|
id: "c1",
|
|
type: "choice" as const,
|
|
position: { x: 0, y: 0 },
|
|
data: {
|
|
choices: [
|
|
{
|
|
label: "Ask",
|
|
target: "",
|
|
condition: {
|
|
kind: "group" as const,
|
|
op: "and" as const,
|
|
children: [{ kind: "flag" as const, key: " ", op: "is_true" as const }],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
];
|
|
const validation = validateGraph(nodes);
|
|
expect(validation.valid).toBe(false);
|
|
expect(validation.errors.join(" ")).toContain("needs a flag key");
|
|
});
|
|
|
|
it(
|
|
"tells an absent flag apart from one set to false, and counts a pending flag as defined",
|
|
async () => {
|
|
const user = await createUser(`${RUN}_defined`);
|
|
const key = `${RUN}_reported`;
|
|
await applyFlagEffect(payload, user, key, "set_false", 0);
|
|
await applyFlagEffect(payload, user, `${RUN}_later`, "set_true", 0, undefined, 60);
|
|
|
|
const check = (k: string, op: DialogueFlagCondition["op"]) =>
|
|
evaluateCondition(payload, user, { kind: "flag", key: k, op });
|
|
|
|
// A recorded false is defined; a truthiness test cannot express this.
|
|
expect(await check(key, "is_defined")).toBe(true);
|
|
expect(await check(key, "is_undefined")).toBe(false);
|
|
expect(await check(key, "is_true")).toBe(false);
|
|
expect(await check(key, "is_false")).toBe(true);
|
|
|
|
// Nothing recorded at all.
|
|
expect(await check(`${RUN}_never`, "is_defined")).toBe(false);
|
|
expect(await check(`${RUN}_never`, "is_undefined")).toBe(true);
|
|
|
|
// Scheduled for later: recorded, so defined, but not yet in effect.
|
|
expect(await check(`${RUN}_later`, "is_defined")).toBe(true);
|
|
expect(await check(`${RUN}_later`, "is_true")).toBe(false);
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it(
|
|
"falls back to the global entry once a personal flag expires",
|
|
async () => {
|
|
const user = await createUser(`${RUN}_expiry`);
|
|
const key = `${RUN}_shared`;
|
|
// Personal entry already expired; global entry still active.
|
|
await payload.create({
|
|
collection: "player-flags",
|
|
data: {
|
|
user,
|
|
key,
|
|
flagType: "boolean",
|
|
boolValue: false,
|
|
expiresAt: new Date(Date.now() - 60_000).toISOString(),
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
await applyFlagEffect(payload, null, key, "set_true", 0);
|
|
|
|
const leaf = (op: DialogueFlagCondition["op"]): DialogueFlagCondition => ({
|
|
kind: "flag",
|
|
key,
|
|
op,
|
|
});
|
|
expect(await evaluateCondition(payload, user, leaf("is_true"))).toBe(true);
|
|
expect(await evaluateCondition(payload, user, leaf("is_defined"))).toBe(true);
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it("summarises defined checks without a stray amount", () => {
|
|
expect(
|
|
conditionSummary({
|
|
kind: "flag",
|
|
key: "met_informant",
|
|
op: "is_defined",
|
|
amount: 3,
|
|
}),
|
|
).toBe('"met_informant" is defined');
|
|
expect(
|
|
conditionSummary({ kind: "flag", key: "met_informant", op: "is_undefined" }),
|
|
).toBe('"met_informant" is undefined');
|
|
});
|
|
});
|