import { describe, expect, it } from "vitest"; import { reorderChoice } from "@/lib/npc-dialogue/reorder"; import type { DialogueNext } from "@/lib/npc-dialogue/dialogue"; /** Build a choice with a unique target so reordering is observable. */ const choice = (label: string): DialogueNext => ({ label, target: `node-${label}` }); describe("reorderChoice (editor choice ordering)", () => { const choices = [choice("a"), choice("b"), choice("c")]; it("swaps a choice with its neighbour", () => { expect(reorderChoice(choices, 0, 1)).toEqual([choice("b"), choice("a"), choice("c")]); // direction -1 from index 1 lands on the same swap expect(reorderChoice(choices, 1, -1)).toEqual([choice("b"), choice("a"), choice("c")]); expect(reorderChoice(choices, 2, -1)).toEqual([choice("a"), choice("c"), choice("b")]); }); it("returns null at the edges so nothing moves", () => { expect(reorderChoice(choices, 0, -1)).toBeNull(); expect(reorderChoice(choices, 2, 1)).toBeNull(); // A single-item list is an edge in every direction. expect(reorderChoice([choice("only")], 0, 1)).toBeNull(); expect(reorderChoice([choice("only")], 0, -1)).toBeNull(); }); it("does not mutate the input array", () => { const moved = reorderChoice(choices, 1, 1); expect(moved).not.toBe(choices); expect(choices).toEqual([choice("a"), choice("b"), choice("c")]); }); });