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 ShaderDef } 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-shader-")); 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 RAIN_TINT_DEF: ShaderDef = { id: "rain-tint", filter: "grayscale", params: { factor: 0.7 }, }; describe("shader namespace", () => { it("shader.create writes content/shaders/.yaml, registers live, and persists across restart", async () => { const { engine, gateway, tmp, close } = await setup(); try { const created = await callText(gateway, "shader.create", { def: RAIN_TINT_DEF }); expect(created.ok).toBe(true); expect(JSON.parse(created.text) as ShaderDef).toEqual(RAIN_TINT_DEF); expect(engine.registry.get("shader", "rain-tint")).toEqual(RAIN_TINT_DEF); const file = await readFile(join(tmp, "shaders", "rain-tint.yaml"), "utf8"); expect(file).toContain("id: rain-tint"); expect(file).toContain("filter: grayscale"); await engine.stop(); const reloaded = createEngine({ contentDir: tmp }); await reloaded.start(); try { expect(reloaded.registry.get("shader", "rain-tint")).toEqual(RAIN_TINT_DEF); } finally { await reloaded.stop(); } } finally { await close(); } }); it("shader.validate accepts a valid def and rejects an invalid one", async () => { const { gateway, close } = await setup(); try { const valid = await callText(gateway, "shader.validate", { def: RAIN_TINT_DEF }); expect(valid.ok).toBe(true); const invalid = await callText(gateway, "shader.validate", { def: { id: "nope", filter: "vignette" }, }); expect(invalid.ok).toBe(false); expect(invalid.text).toMatch(/filter/); const strictInvalid = await callText(gateway, "shader.validate", { def: { id: "nope", filter: "blur", extra: 1 }, }); expect(strictInvalid.ok).toBe(false); expect(strictInvalid.text).toMatch(/Unrecognized key/); } finally { await close(); } }); it("shader.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, "shader.create", { def: { id: "broken", filter: "nope" }, }); expect(bad.ok).toBe(false); expect(engine.registry.get("shader", "broken")).toBeUndefined(); await expect(readFile(join(tmp, "shaders", "broken.yaml"))).rejects.toThrow(); } finally { await close(); } }); it("shader.update merges a patch, validates, rewrites the file, and re-registers", async () => { const { engine, gateway, tmp, close } = await setup(); try { await callText(gateway, "shader.create", { def: RAIN_TINT_DEF }); const updated = await callText(gateway, "shader.update", { id: "rain-tint", patch: { params: { factor: 0.5 } }, }); expect(updated.ok).toBe(true); const def = JSON.parse(updated.text) as ShaderDef; expect(def.params).toEqual({ factor: 0.5 }); const file = await readFile(join(tmp, "shaders", "rain-tint.yaml"), "utf8"); expect(file).toContain("factor: 0.5"); const missing = await callText(gateway, "shader.update", { id: "nope", patch: { id: "x" } }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/shader not found: nope/); } finally { await close(); } }); it("shader.list and shader.get return summaries and defs; unknown id errors", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "shader.create", { def: RAIN_TINT_DEF }); await callText(gateway, "shader.create", { def: { id: "sparkle-glow", filter: "brightness", params: { factor: 1.2 } } }); const listed = await callText(gateway, "shader.list"); expect(JSON.parse(listed.text)).toEqual({ shaders: [ { id: "rain-tint", filter: "grayscale" }, { id: "sparkle-glow", filter: "brightness" }, ], }); const got = await callText(gateway, "shader.get", { shaderId: "rain-tint" }); expect(JSON.parse(got.text)).toEqual(RAIN_TINT_DEF); const missing = await callText(gateway, "shader.get", { shaderId: "nope" }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/shader not found: nope/); } finally { await close(); } }); it("resources: shader://catalog and shader://{id}; missing id rejects", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "shader.create", { def: RAIN_TINT_DEF }); const catalog = await gateway.readResource("shader://catalog"); expect(JSON.parse(catalog.contents[0]?.text ?? "{}")).toEqual({ shaders: [RAIN_TINT_DEF] }); const single = await gateway.readResource("shader://rain-tint"); expect(JSON.parse(single.contents[0]?.text ?? "{}")).toEqual(RAIN_TINT_DEF); await expect(gateway.readResource("shader://missing")).rejects.toThrow(/shader not found: missing/); } finally { await close(); } }); it("serves the shader.create-prompt prompt", async () => { const { gateway, close } = await setup(); try { const prompt = await gateway.getPrompt("shader.create-prompt", {}); const text = prompt.messages[0]?.content.text ?? ""; expect(text).toContain("filter"); expect(text).toContain("shader.validate"); expect(text).toContain("content/shaders/.yaml"); } finally { await close(); } }); });