import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { createEngine, type AnimationDef } from "@nengine/core"; import { createEngineNamespaces, McpGateway } from "../src/index.js"; const TIME_YAML = [ "id: default", "dayLengthTicks: 480", "seasons:", " - { name: spring, dayCount: 28 }", "start: { day: 1, season: spring }", "", ].join("\n"); const FARM_YAML = [ "id: farm", "width: 2", "height: 2", 'tilesetRef: "tiles"', "spawn: { x: 0, y: 0 }", "layers:", " - name: ground", " data:", " - [a, b]", " - [b, a]", "", ].join("\n"); interface Setup { engine: ReturnType; gateway: McpGateway; tmp: string; close: () => Promise; } async function setup(): Promise { const tmp = await mkdtemp(join(tmpdir(), "farm-animation-")); await mkdir(join(tmp, "maps"), { recursive: true }); await writeFile(join(tmp, "time.yaml"), TIME_YAML); await writeFile(join(tmp, "maps", "farm.yaml"), FARM_YAML); const engine = createEngine({ contentDir: tmp }); const gateway = new McpGateway(); for (const namespace of createEngineNamespaces(engine, { gateway })) { gateway.register(namespace); } await engine.start(); return { engine, gateway, tmp, close: async () => { await engine.stop(); await rm(tmp, { recursive: true, force: true }); }, }; } async function callText(gateway: McpGateway, name: string, args: Record = {}): Promise<{ ok: boolean; text: string; }> { const result = await gateway.callTool(name, args); return { ok: result.isError !== true, text: result.content[0]?.text ?? "" }; } const PLAYER_ANIM: AnimationDef = { id: "player", initial: "idle", states: [ { id: "idle", spriteRef: "player", loop: true }, { id: "walk", spriteRef: "player-walk", fps: 8, loop: true }, ], transitions: [ { from: "idle", to: "walk", on: "move" }, { from: "walk", to: "idle", on: "idle" }, ], }; describe("animation namespace", () => { it("animation.create writes content/animations/.yaml, registers live, and persists across restart", async () => { const { engine, gateway, tmp, close } = await setup(); try { const created = await callText(gateway, "animation.create", { def: PLAYER_ANIM }); expect(created.ok).toBe(true); expect(JSON.parse(created.text) as AnimationDef).toEqual(PLAYER_ANIM); expect(engine.registry.get("animation", "player")).toEqual(PLAYER_ANIM); const file = await readFile(join(tmp, "animations", "player.yaml"), "utf8"); expect(file).toContain("id: player"); expect(file).toContain("spriteRef: player-walk"); await engine.stop(); const reloaded = createEngine({ contentDir: tmp }); await reloaded.start(); try { expect(reloaded.registry.get("animation", "player")).toEqual(PLAYER_ANIM); } finally { await reloaded.stop(); } } finally { await close(); } }); it("animation.validate accepts a valid def and rejects an invalid one (bad initial / undeclared transition)", async () => { const { gateway, close } = await setup(); try { const valid = await callText(gateway, "animation.validate", { def: PLAYER_ANIM }); expect(valid.ok).toBe(true); expect(JSON.parse(valid.text)).toEqual(PLAYER_ANIM); const badInitial = await callText(gateway, "animation.validate", { def: { id: "x", initial: "nope", states: [{ id: "idle", spriteRef: "player" }] }, }); expect(badInitial.ok).toBe(false); expect(badInitial.text).toMatch(/initial state must be a declared state/); const badTransition = await callText(gateway, "animation.validate", { def: { id: "x", initial: "idle", states: [{ id: "idle", spriteRef: "player" }], transitions: [{ from: "idle", to: "dance" }] }, }); expect(badTransition.ok).toBe(false); expect(badTransition.text).toMatch(/transition to must be a declared state/); const strictInvalid = await callText(gateway, "animation.validate", { def: { id: "x", initial: "idle", states: [{ id: "idle", spriteRef: "player", extra: 1 }] }, }); expect(strictInvalid.ok).toBe(false); expect(strictInvalid.text).toMatch(/Unrecognized key/); } finally { await close(); } }); it("animation.create rejects an invalid def and does not write or register", async () => { const { engine, gateway, tmp, close } = await setup(); try { const bad = await callText(gateway, "animation.create", { def: { id: "broken", initial: "nope", states: [] }, }); expect(bad.ok).toBe(false); expect(engine.registry.get("animation", "broken")).toBeUndefined(); await expect(readFile(join(tmp, "animations", "broken.yaml"))).rejects.toThrow(); } finally { await close(); } }); it("duplicate animation.create overwrites the existing definition", async () => { const { engine, gateway, tmp, close } = await setup(); try { await callText(gateway, "animation.create", { def: PLAYER_ANIM }); const recreated = await callText(gateway, "animation.create", { def: { id: "player", initial: "idle", states: [{ id: "idle", spriteRef: "player" }] }, }); expect(recreated.ok).toBe(true); const current = engine.registry.get("animation", "player"); expect(current?.states).toHaveLength(1); const file = await readFile(join(tmp, "animations", "player.yaml"), "utf8"); expect(file).toContain("states:"); expect(file).not.toContain("player-walk"); } finally { await close(); } }); it("animation.update merges a patch, validates, rewrites the file, and re-registers", async () => { const { engine, gateway, tmp, close } = await setup(); try { await callText(gateway, "animation.create", { def: PLAYER_ANIM }); const updated = await callText(gateway, "animation.update", { id: "player", patch: { initial: "walk", states: [{ id: "walk", spriteRef: "player-walk", fps: 6 }], transitions: [] }, }); expect(updated.ok).toBe(true); const def = JSON.parse(updated.text) as AnimationDef; expect(def.initial).toBe("walk"); expect(def.states).toEqual([{ id: "walk", spriteRef: "player-walk", fps: 6 }]); const file = await readFile(join(tmp, "animations", "player.yaml"), "utf8"); expect(file).toContain("fps: 6"); const missing = await callText(gateway, "animation.update", { id: "nope", patch: { id: "x" } }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/animation not found: nope/); const badPatch = await callText(gateway, "animation.update", { id: "player", patch: { initial: "nope" }, }); expect(badPatch.ok).toBe(false); expect(badPatch.text).toMatch(/initial/); expect(engine.registry.get("animation", "player")?.initial).toBe("walk"); } finally { await close(); } }); it("animation.list returns one-line summaries", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "animation.create", { def: PLAYER_ANIM }); const listed = await callText(gateway, "animation.list"); expect(listed.ok).toBe(true); expect(JSON.parse(listed.text)).toEqual({ animations: [{ id: "player", stateCount: 2 }] }); } finally { await close(); } }); it("animation.get returns the full def; unknown id is an error result", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "animation.create", { def: PLAYER_ANIM }); const got = await callText(gateway, "animation.get", { animationId: "player" }); expect(got.ok).toBe(true); expect(JSON.parse(got.text)).toEqual(PLAYER_ANIM); const missing = await callText(gateway, "animation.get", { animationId: "nope" }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/animation not found: nope/); const blank = await callText(gateway, "animation.get", { animationId: "" }); expect(blank.ok).toBe(false); expect(blank.text).toMatch(/animation\.get: animationId must be a non-empty string/); } finally { await close(); } }); it("resources: animation://catalog and animation://{id}; missing id rejects", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "animation.create", { def: PLAYER_ANIM }); const catalog = await gateway.readResource("animation://catalog"); expect(JSON.parse(catalog.contents[0]?.text ?? "{}")).toEqual({ animations: [PLAYER_ANIM] }); const single = await gateway.readResource("animation://player"); expect(JSON.parse(single.contents[0]?.text ?? "{}")).toEqual(PLAYER_ANIM); await expect(gateway.readResource("animation://missing")).rejects.toThrow(/animation not found: missing/); } finally { await close(); } }); it("serves the animation.create-prompt prompt", async () => { const { gateway, close } = await setup(); try { const prompt = await gateway.getPrompt("animation.create-prompt", {}); const text = prompt.messages[0]?.content.text ?? ""; expect(text).toContain("states"); expect(text).toContain("animation.validate"); expect(text).toContain("content/animations/.yaml"); expect(text).toContain("set-state player.anim"); } finally { await close(); } }); });