// @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); }); });