1
0
Fork 0
polaris-task-force/tests/int/npc-dialogue-condition-groups.int.spec.ts
Z8MB1E c299acbe4f feat(npc-dialogue): condition groups and ternary text tokens
Conditions become a discriminated union: the existing flag leaf plus a
single-level group combining leaves with AND / OR / NOR. The evaluator
recurses one level, and every flat-assuming site (choice gating, graph
validation, editor, canvas previews) is group-aware; DB entry gates stay
flat by design. The node editor gains a shared condition builder with a
combinator dropdown and repeatable rows for both branch nodes and gated
choices.

Display strings also gain ternaries: {{ key ? yes : no }}, {{ !key ? .. }},
and numeric compares like {{ rep >= 5 ? .. }}. A brace-depth scanner
rewrites ternaries over bounded passes so branches may nest flag tokens
and even nested ternaries, then plain interpolation runs its single
never-rescanned pass. Ternary keys join collectNodeTokens so the server
reads exactly the flags a node needs.
2026-10-14 16:21:16 -04:00

160 lines
5.2 KiB
TypeScript

import { getPayload, Payload } from "payload";
import config from "@/payload.config";
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 getPayload({ config });
}, 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");
});
});