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 ParticleDef } 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-particle-")); 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 DUST_DEF: ParticleDef = { id: "dust", emitRate: 8, angle: -90, spread: 35, colors: ["#d7ccc8", "#a1887f"], }; describe("particle namespace", () => { it("particle.create writes content/particles/.yaml, registers live, and persists across restart", async () => { const { engine, gateway, tmp, close } = await setup(); try { const created = await callText(gateway, "particle.create", { def: DUST_DEF }); expect(created.ok).toBe(true); expect(JSON.parse(created.text) as ParticleDef).toEqual(DUST_DEF); expect(engine.registry.get("particle", "dust")).toEqual(DUST_DEF); const file = await readFile(join(tmp, "particles", "dust.yaml"), "utf8"); expect(file).toContain("id: dust"); expect(file).toContain("emitRate: 8"); await engine.stop(); const reloaded = createEngine({ contentDir: tmp }); await reloaded.start(); try { expect(reloaded.registry.get("particle", "dust")).toEqual(DUST_DEF); } finally { await reloaded.stop(); } } finally { await close(); } }); it("particle.validate accepts a valid def and rejects an invalid one", async () => { const { gateway, close } = await setup(); try { const valid = await callText(gateway, "particle.validate", { def: DUST_DEF }); expect(valid.ok).toBe(true); const invalid = await callText(gateway, "particle.validate", { def: { id: "nope", colors: ["green"] }, }); expect(invalid.ok).toBe(false); expect(invalid.text).toMatch(/colors/); const strictInvalid = await callText(gateway, "particle.validate", { def: { id: "nope", extra: 1 }, }); expect(strictInvalid.ok).toBe(false); expect(strictInvalid.text).toMatch(/Unrecognized key/); } finally { await close(); } }); it("particle.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, "particle.create", { def: { id: "broken", emitRate: -1 }, }); expect(bad.ok).toBe(false); expect(engine.registry.get("particle", "broken")).toBeUndefined(); await expect(readFile(join(tmp, "particles", "broken.yaml"))).rejects.toThrow(); } finally { await close(); } }); it("particle.update merges a patch, validates, rewrites the file, and re-registers", async () => { const { engine, gateway, tmp, close } = await setup(); try { await callText(gateway, "particle.create", { def: DUST_DEF }); const updated = await callText(gateway, "particle.update", { id: "dust", patch: { emitRate: 16 }, }); expect(updated.ok).toBe(true); const def = JSON.parse(updated.text) as ParticleDef; expect(def.emitRate).toBe(16); const file = await readFile(join(tmp, "particles", "dust.yaml"), "utf8"); expect(file).toContain("emitRate: 16"); const missing = await callText(gateway, "particle.update", { id: "nope", patch: { id: "x" } }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/particle not found: nope/); } finally { await close(); } }); it("particle.list and particle.get return summaries and defs; unknown id errors", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "particle.create", { def: DUST_DEF }); await callText(gateway, "particle.create", { def: { id: "rain", emitRate: 24, colors: ["#64b5f6"] } }); const listed = await callText(gateway, "particle.list"); expect(JSON.parse(listed.text)).toEqual({ particles: [ { id: "dust", emitRate: 8 }, { id: "rain", emitRate: 24 }, ], }); const got = await callText(gateway, "particle.get", { particleId: "dust" }); expect(JSON.parse(got.text)).toEqual(DUST_DEF); const missing = await callText(gateway, "particle.get", { particleId: "nope" }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/particle not found: nope/); } finally { await close(); } }); it("resources: particle://catalog and particle://{id}; missing id rejects", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "particle.create", { def: DUST_DEF }); const catalog = await gateway.readResource("particle://catalog"); expect(JSON.parse(catalog.contents[0]?.text ?? "{}")).toEqual({ particles: [DUST_DEF] }); const single = await gateway.readResource("particle://dust"); expect(JSON.parse(single.contents[0]?.text ?? "{}")).toEqual(DUST_DEF); await expect(gateway.readResource("particle://missing")).rejects.toThrow(/particle not found: missing/); } finally { await close(); } }); it("serves the particle.create-prompt prompt", async () => { const { gateway, close } = await setup(); try { const prompt = await gateway.getPrompt("particle.create-prompt", {}); const text = prompt.messages[0]?.content.text ?? ""; expect(text).toContain("emitRate"); expect(text).toContain("particle.validate"); expect(text).toContain("content/particles/.yaml"); } finally { await close(); } }); });