1
0
Fork 0
polaris-task-force/tests/int/npc-dialogue-unread.int.spec.ts
Z8MB1E 4dc82c0d5d feat(npc-dialogue): mark contacts whose conversation has unread content
The only unread signal was the NEW badge inside a conversation, so players had
no way to see who had something to say. The directory now carries that signal:
a cyan NEW chip on the card for any contact whose offered conversation has a node
this player has never read.

Unread is derived from the per-node read markers that already existed. Two gates
run first so the chip never lies: the contact must have canMessage true, and the
loader resolves the offered conversation with the same selectDialogueForUser gate
the contact page uses (testing conversations filtered by dialogueVisibleTo).

Cost is two queries for the whole directory rather than one per card. A branch
added after a player finished a conversation brings the chip back, which is the
point; a partially explored conversation stays marked until every branch is read.

Measured on the live directory: three contacts chipped (the two with canMessage
and unread flows, plus a seeded probe on a third), the probe's chip cleared after
all its nodes were read, and a canMessage:false contact never chipped.

Pure derivation covered by 6 tests.
2026-11-24 20:26:47 -05:00

51 lines
2.1 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it } from "vitest";
import { dialogueNodeIds, isDialogueUnread, unreadNodeIds } from "@/lib/npc-dialogue/unread";
/**
* "Unread" is derived from per-node read markers inside flowData JSON. These
* cover the shapes an author's canvas can actually produce, including a
* half-written flow, since the directory reads every conversation's flow.
*/
describe("conversation unread derivation", () => {
it("reads node ids out of a stored flow", () => {
const flow = {
nodes: [{ id: "n1" }, { id: "n2" }, { id: "" }, { notAnId: true }, null],
edges: [],
};
expect(dialogueNodeIds(flow)).toEqual(["n1", "n2"]);
});
it("treats a missing or broken flow as having no nodes", () => {
expect(dialogueNodeIds(null)).toEqual([]);
expect(dialogueNodeIds(undefined)).toEqual([]);
expect(dialogueNodeIds({})).toEqual([]);
expect(dialogueNodeIds({ nodes: "nope" })).toEqual([]);
});
it("reports the nodes this player has not read", () => {
const nodeIds = ["n1", "n2", "n3"];
expect(unreadNodeIds(nodeIds, new Set(["n1", "n3"]))).toEqual(["n2"]);
expect(unreadNodeIds(nodeIds, new Set(["n1", "n2", "n3"]))).toEqual([]);
expect(unreadNodeIds(nodeIds, new Set())).toEqual(["n1", "n2", "n3"]);
});
it("is unread while any node is unread, and clean once all are", () => {
const nodeIds = ["n1", "n2"];
expect(isDialogueUnread(nodeIds, new Set(["n1"]))).toBe(true);
expect(isDialogueUnread(nodeIds, new Set(["n1", "n2"]))).toBe(false);
});
it("never calls an empty conversation unread", () => {
expect(isDialogueUnread([], new Set())).toBe(false);
});
it("counts read markers for nodes that no longer exist as harmless", () => {
// An author deleted a branch after the player read it. The stale marker
// must not make the conversation look read, and must not make it look
// unread either: only node ids still in the flow are considered.
expect(isDialogueUnread(["n1"], new Set(["deleted-node"]))).toBe(true);
expect(isDialogueUnread(["n1"], new Set(["n1", "deleted-node"]))).toBe(false);
});
});