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 SpriteDef } 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-sprite-")); 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 SPROUT_DEF: SpriteDef = { id: "parsnip-sprout", frames: [{ name: "idle", durationTicks: 1, layers: [{ shape: "rect", x: 8, y: 8, w: 16, h: 16, fill: "#66bb6a" }] }], }; describe("sprite namespace", () => { it("sprite.create writes content/sprites/.yaml, registers live, and persists across restart", async () => { const { engine, gateway, tmp, close } = await setup(); try { const created = await callText(gateway, "sprite.create", { def: SPROUT_DEF }); expect(created.ok).toBe(true); expect(JSON.parse(created.text) as SpriteDef).toEqual(SPROUT_DEF); expect(engine.registry.get("sprite", "parsnip-sprout")).toEqual(SPROUT_DEF); const file = await readFile(join(tmp, "sprites", "parsnip-sprout.yaml"), "utf8"); expect(file).toContain("id: parsnip-sprout"); expect(file).toContain("shape: rect"); await engine.stop(); const reloaded = createEngine({ contentDir: tmp }); await reloaded.start(); try { expect(reloaded.registry.get("sprite", "parsnip-sprout")).toEqual(SPROUT_DEF); } finally { await reloaded.stop(); } } finally { await close(); } }); it("sprite.validate accepts a valid def and rejects an invalid one", async () => { const { gateway, close } = await setup(); try { const valid = await callText(gateway, "sprite.validate", { def: SPROUT_DEF }); expect(valid.ok).toBe(true); expect(JSON.parse(valid.text)).toEqual(SPROUT_DEF); const invalid = await callText(gateway, "sprite.validate", { def: { id: "nope", frames: [{ layers: [{ shape: "rect", x: 0, y: 0, w: 4, h: 4, fill: "green" }] }] }, }); expect(invalid.ok).toBe(false); expect(invalid.text).toMatch(/fill/); const strictInvalid = await callText(gateway, "sprite.validate", { def: { id: "nope", frames: [{ layers: [{ shape: "rect", x: 0, y: 0, w: 4, h: 4, fill: "#ffffff", extra: 1 }] }] }, }); expect(strictInvalid.ok).toBe(false); expect(strictInvalid.text).toMatch(/Unrecognized key/); } finally { await close(); } }); it("sprite.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, "sprite.create", { def: { id: "broken", frames: [{ layers: [] }] }, }); expect(bad.ok).toBe(false); expect(engine.registry.get("sprite", "broken")).toBeUndefined(); await expect(readFile(join(tmp, "sprites", "broken.yaml"))).rejects.toThrow(); } finally { await close(); } }); it("duplicate sprite.create overwrites the existing definition", async () => { const { engine, gateway, tmp, close } = await setup(); try { await callText(gateway, "sprite.create", { def: SPROUT_DEF }); const recreated = await callText(gateway, "sprite.create", { def: { id: "parsnip-sprout", frames: [{ layers: [{ shape: "circle", x: 8, y: 8, r: 4, fill: "#4caf50" }] }] }, }); expect(recreated.ok).toBe(true); const current = engine.registry.get("sprite", "parsnip-sprout"); expect(current?.frames[0]?.layers[0]?.shape).toBe("circle"); const file = await readFile(join(tmp, "sprites", "parsnip-sprout.yaml"), "utf8"); expect(file).toContain("shape: circle"); } finally { await close(); } }); it("sprite.update merges a patch, validates, rewrites the file, and re-registers", async () => { const { engine, gateway, tmp, close } = await setup(); try { await callText(gateway, "sprite.create", { def: SPROUT_DEF }); const updated = await callText(gateway, "sprite.update", { id: "parsnip-sprout", patch: { frames: [{ layers: [{ shape: "circle", x: 8, y: 8, r: 6, fill: "#2e7d32" }] }] }, }); expect(updated.ok).toBe(true); const def = JSON.parse(updated.text) as SpriteDef; expect(def.frames[0]?.layers[0]).toEqual({ shape: "circle", x: 8, y: 8, r: 6, fill: "#2e7d32" }); const file = await readFile(join(tmp, "sprites", "parsnip-sprout.yaml"), "utf8"); expect(file).toContain("r: 6"); const missing = await callText(gateway, "sprite.update", { id: "nope", patch: { id: "x" } }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/sprite not found: nope/); const badPatch = await callText(gateway, "sprite.update", { id: "parsnip-sprout", patch: { frames: [{ layers: [] }] }, }); expect(badPatch.ok).toBe(false); expect(badPatch.text).toMatch(/frames/); expect(engine.registry.get("sprite", "parsnip-sprout")?.frames[0]?.layers[0]?.fill).toBe("#2e7d32"); } finally { await close(); } }); it("sprite.list returns one-line summaries", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "sprite.create", { def: SPROUT_DEF }); await callText(gateway, "sprite.create", { def: { id: "fence", frames: [{ layers: [{ shape: "rect", x: 0, y: 0, w: 4, h: 4, fill: "#8d6e63" }] }] }, }); const listed = await callText(gateway, "sprite.list"); expect(listed.ok).toBe(true); expect(JSON.parse(listed.text)).toEqual({ sprites: [ { id: "parsnip-sprout", frameCount: 1 }, { id: "fence", frameCount: 1 }, ], }); } finally { await close(); } }); it("sprite.get returns the full def; unknown id is an error result", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "sprite.create", { def: SPROUT_DEF }); const got = await callText(gateway, "sprite.get", { spriteId: "parsnip-sprout" }); expect(got.ok).toBe(true); expect(JSON.parse(got.text)).toEqual(SPROUT_DEF); const missing = await callText(gateway, "sprite.get", { spriteId: "nope" }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/sprite not found: nope/); } finally { await close(); } }); it("resources: sprite://catalog and sprite://{id}; missing id rejects", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "sprite.create", { def: SPROUT_DEF }); const catalog = await gateway.readResource("sprite://catalog"); expect(JSON.parse(catalog.contents[0]?.text ?? "{}")).toEqual({ sprites: [SPROUT_DEF] }); const single = await gateway.readResource("sprite://parsnip-sprout"); expect(JSON.parse(single.contents[0]?.text ?? "{}")).toEqual(SPROUT_DEF); await expect(gateway.readResource("sprite://missing")).rejects.toThrow(/sprite not found: missing/); } finally { await close(); } }); it("serves the sprite.create-prompt prompt", async () => { const { gateway, close } = await setup(); try { const prompt = await gateway.getPrompt("sprite.create-prompt", {}); const text = prompt.messages[0]?.content.text ?? ""; expect(text).toContain("frames"); expect(text).toContain("sprite.validate"); expect(text).toContain("content/sprites/.yaml"); } finally { await close(); } }); });