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 SoundDef } 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-sound-")); 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 COIN_DEF: SoundDef = { id: "coin", mode: "sfx", synth: { wave: "sine", freq: 880, durationMs: 120, gain: 0.5, slideTo: 1320 } }; describe("sound namespace", () => { it("sound.create writes content/sounds/.yaml, registers live, and persists across restart", async () => { const { engine, gateway, tmp, close } = await setup(); try { const created = await callText(gateway, "sound.create", { def: COIN_DEF }); expect(created.ok).toBe(true); expect(JSON.parse(created.text) as SoundDef).toEqual(COIN_DEF); expect(engine.registry.get("sound", "coin")).toEqual(COIN_DEF); const file = await readFile(join(tmp, "sounds", "coin.yaml"), "utf8"); expect(file).toContain("id: coin"); expect(file).toContain("wave: sine"); await engine.stop(); const reloaded = createEngine({ contentDir: tmp }); await reloaded.start(); try { expect(reloaded.registry.get("sound", "coin")).toEqual(COIN_DEF); } finally { await reloaded.stop(); } } finally { await close(); } }); it("sound.validate accepts a valid def and rejects an invalid one", async () => { const { gateway, close } = await setup(); try { const valid = await callText(gateway, "sound.validate", { def: COIN_DEF }); expect(valid.ok).toBe(true); expect(JSON.parse(valid.text)).toEqual(COIN_DEF); const invalid = await callText(gateway, "sound.validate", { def: { id: "silent" } }); expect(invalid.ok).toBe(false); expect(invalid.text).toMatch(/at least one of synth or src/); const badWave = await callText(gateway, "sound.validate", { def: { id: "nope", synth: { wave: "noise", freq: 1, durationMs: 1 } }, }); expect(badWave.ok).toBe(false); expect(badWave.text).toMatch(/Invalid enum value/); } finally { await close(); } }); it("sound.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, "sound.create", { def: { id: "broken", synth: { wave: "x", freq: 1, durationMs: 1 } } }); expect(bad.ok).toBe(false); expect(engine.registry.get("sound", "broken")).toBeUndefined(); await expect(readFile(join(tmp, "sounds", "broken.yaml"))).rejects.toThrow(); } finally { await close(); } }); it("sound.update merges a patch, validates, rewrites the file, and re-registers", async () => { const { engine, gateway, tmp, close } = await setup(); try { await callText(gateway, "sound.create", { def: COIN_DEF }); const updated = await callText(gateway, "sound.update", { id: "coin", patch: { synth: { wave: "sine", freq: 880, durationMs: 120, gain: 0.2, slideTo: 1320 } }, }); expect(updated.ok).toBe(true); const def = JSON.parse(updated.text) as SoundDef; expect(def.synth).toEqual({ wave: "sine", freq: 880, durationMs: 120, gain: 0.2, slideTo: 1320 }); const file = await readFile(join(tmp, "sounds", "coin.yaml"), "utf8"); expect(file).toContain("gain: 0.2"); const missing = await callText(gateway, "sound.update", { id: "nope", patch: { id: "x" } }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/sound not found: nope/); } finally { await close(); } }); it("sound.list returns one-line summaries", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "sound.create", { def: COIN_DEF }); await callText(gateway, "sound.create", { def: { id: "water", synth: { wave: "sine", freq: 520, durationMs: 160 } } }); await callText(gateway, "sound.create", { def: { id: "file-only", src: "sfx/coin.ogg" } }); const listed = await callText(gateway, "sound.list"); expect(listed.ok).toBe(true); expect(JSON.parse(listed.text)).toEqual({ sounds: [ { id: "coin", hasSynth: true, hasSrc: false }, { id: "water", hasSynth: true, hasSrc: false }, { id: "file-only", hasSynth: false, hasSrc: true }, ], }); } finally { await close(); } }); it("sound.get returns the full def; unknown id is an error result", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "sound.create", { def: COIN_DEF }); const got = await callText(gateway, "sound.get", { soundId: "coin" }); expect(got.ok).toBe(true); expect(JSON.parse(got.text)).toEqual(COIN_DEF); const missing = await callText(gateway, "sound.get", { soundId: "nope" }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/sound not found: nope/); } finally { await close(); } }); it("resources: sound://catalog and sound://{id}; missing id rejects", async () => { const { gateway, close } = await setup(); try { await callText(gateway, "sound.create", { def: COIN_DEF }); const catalog = await gateway.readResource("sound://catalog"); expect(JSON.parse(catalog.contents[0]?.text ?? "{}")).toEqual({ sounds: [COIN_DEF] }); const single = await gateway.readResource("sound://coin"); expect(JSON.parse(single.contents[0]?.text ?? "{}")).toEqual(COIN_DEF); await expect(gateway.readResource("sound://missing")).rejects.toThrow(/sound not found: missing/); } finally { await close(); } }); it("serves the sound.create-prompt prompt", async () => { const { gateway, close } = await setup(); try { const prompt = await gateway.getPrompt("sound.create-prompt", {}); const text = prompt.messages[0]?.content.text ?? ""; expect(text).toContain("wave"); expect(text).toContain("sound.validate"); expect(text).toContain("content/sounds/.yaml"); } finally { await close(); } }); });