import { resolve } from "node:path"; import { describe, expect, it } from "vitest"; import { createEngine } from "@nengine/core"; import { createEngineNamespaces, McpGateway } from "../src/index.js"; import type { McpNamespace } from "../src/namespace.js"; const repoRoot = resolve(process.cwd(), "../.."); const contentDir = resolve(repoRoot, "content"); const modsDir = resolve(repoRoot, "mods"); interface Setup { engine: ReturnType; gateway: McpGateway; } async function setup(): Promise { const engine = createEngine({ contentDir, modsDir }); const gateway = new McpGateway(); for (const namespace of createEngineNamespaces(engine, { gateway })) { gateway.register(namespace); } await engine.start(); return { engine, gateway }; } 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 ?? "" }; } describe("engine namespaces", () => { it("server.info reports engine identity, content stats, subsystems, and time", async () => { const { engine, gateway } = await setup(); try { const result = await callText(gateway, "server.info"); expect(result.ok).toBe(true); const info = JSON.parse(result.text) as { engine: string; content: Record; subsystems: { name: string; enabled: boolean }[]; time: { day: number; season: string; tickInDay: number }; }; expect(info.engine).toBe("nengine"); expect(info.content["map"]).toContain("farm"); expect(info.content["time"]).toContain("default"); expect(info.subsystems.map((s) => s.name).sort()).toEqual(["animal", "animation", "behavior", "craft", "crop", "dialogue", "item", "map", "mod", "npc", "particle", "placement", "player", "rng", "save", "scene", "server", "shader", "shop", "sound", "sprite", "time", "weather"]); expect(info.time).toEqual({ day: 1, season: "spring", tickInDay: 0 }); expect(engine.registry.ids("map")).toEqual(["farm"]); } finally { await engine.stop(); } }); it("map.list and map.get return the normalized map plus player spawn", async () => { const { engine, gateway } = await setup(); try { const listed = await callText(gateway, "map.list"); const maps = (JSON.parse(listed.text) as { maps: { id: string; width: number; height: number }[] }).maps; expect(maps).toEqual([{ id: "farm", width: 12, height: 12, tilesetRef: "farm-tiles" }]); const got = await callText(gateway, "map.get"); const map = JSON.parse(got.text) as { id: string; layers: unknown[]; spawn: { x: number; y: number }; player: { x: number; y: number } }; expect(map.id).toBe("farm"); expect(map.layers.length).toBe(1); expect(map.spawn).toEqual({ x: 2, y: 2 }); expect(map.player).toMatchObject({ id: "player", type: "player", x: 2, y: 2 }); const missing = await callText(gateway, "map.get", { mapId: "nope" }); expect(missing.ok).toBe(false); expect(missing.text).toMatch(/map not found: nope/); } finally { await engine.stop(); } }); it("time.get, time.advance, and time.set read and mutate the TimeSystem", async () => { const { engine, gateway } = await setup(); try { const initial = await callText(gateway, "time.get"); expect(JSON.parse(initial.text)).toEqual({ day: 1, season: "spring", tickInDay: 0 }); const advanced = await callText(gateway, "time.advance", { ticks: 480 }); expect(JSON.parse(advanced.text)).toEqual({ day: 2, season: "spring", tickInDay: 0 }); const engineTime = engine.systems.get("time"); expect(engineTime?.get()).toEqual({ day: 2, season: "spring", tickInDay: 0 }); const set = await callText(gateway, "time.set", { day: 12, season: "summer" }); expect(JSON.parse(set.text)).toEqual({ day: 12, season: "summer", tickInDay: 0 }); const invalid = await callText(gateway, "time.advance", { ticks: -1 }); expect(invalid.ok).toBe(false); expect(invalid.text).toMatch(/positive integer/); } finally { await engine.stop(); } }); it("reads server, time, and template map resources", async () => { const { engine, gateway } = await setup(); try { const status = await gateway.readResource("server://status"); expect(JSON.parse(status.contents[0]?.text ?? "{}")).toMatchObject({ engine: "nengine" }); const timeState = await gateway.readResource("time://state"); expect(JSON.parse(timeState.contents[0]?.text ?? "{}")).toEqual({ day: 1, season: "spring", tickInDay: 0 }); const mapResource = await gateway.readResource("map://farm"); expect(JSON.parse(mapResource.contents[0]?.text ?? "{}")).toMatchObject({ id: "farm" }); } finally { await engine.stop(); } }); it("serves the server.orient prompt", async () => { const { engine, gateway } = await setup(); try { const prompt = await gateway.getPrompt("server.orient", {}); const text = prompt.messages[0]?.content.text ?? ""; expect(text).toContain("nEngine MCP server"); expect(text).toContain("time.advance"); expect(text).toContain("content/maps/.yaml"); } finally { await engine.stop(); } }); it("disabled namespaces vanish from listings and reject calls", async () => { const { engine, gateway } = await setup(); try { gateway.disable("map"); expect(gateway.listTools().map((tool) => tool.name)).not.toContain("map.list"); expect(gateway.listTools().map((tool) => tool.name)).toContain("time.get"); const rejected = await callText(gateway, "map.list"); expect(rejected.ok).toBe(false); expect(rejected.text).toMatch(/namespace "map" is disabled/); gateway.enable("map"); const restored = await callText(gateway, "map.list"); expect(restored.ok).toBe(true); } finally { await engine.stop(); } }); });