| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- 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 MapDef } 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<typeof createEngine>;
- gateway: McpGateway;
- tmp: string;
- close: () => Promise<void>;
- }
- async function setup(): Promise<Setup> {
- const tmp = await mkdtemp(join(tmpdir(), "farm-map-"));
- 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<string, unknown> = {}): Promise<{
- ok: boolean;
- text: string;
- }> {
- const result = await gateway.callTool(name, args);
- return { ok: result.isError !== true, text: result.content[0]?.text ?? "" };
- }
- const MEADOW_DEF: MapDef = {
- id: "meadow",
- width: 2,
- height: 2,
- tilesetRef: "tiles",
- spawn: { x: 0, y: 0 },
- layers: [{ name: "ground", data: [["grass", "grass"], ["grass", "grass"]] }],
- };
- describe("map namespace", () => {
- it("map.create writes content/maps/<id>.yaml, registers live, and persists across restart", async () => {
- const { engine, gateway, tmp, close } = await setup();
- try {
- const created = await callText(gateway, "map.create", { def: MEADOW_DEF });
- expect(created.ok).toBe(true);
- expect(JSON.parse(created.text) as MapDef).toEqual(MEADOW_DEF);
- expect(engine.registry.get<MapDef>("map", "meadow")).toEqual(MEADOW_DEF);
- const file = await readFile(join(tmp, "maps", "meadow.yaml"), "utf8");
- expect(file).toContain("id: meadow");
- expect(file).toContain("grass");
- await engine.stop();
- const reloaded = createEngine({ contentDir: tmp });
- await reloaded.start();
- try {
- expect(reloaded.registry.get<MapDef>("map", "meadow")).toEqual(MEADOW_DEF);
- } finally {
- await reloaded.stop();
- }
- } finally {
- await close();
- }
- });
- it("map.validate accepts a valid def and rejects an invalid one", async () => {
- const { gateway, close } = await setup();
- try {
- const valid = await callText(gateway, "map.validate", { def: MEADOW_DEF });
- expect(valid.ok).toBe(true);
- expect(JSON.parse(valid.text)).toEqual(MEADOW_DEF);
- const invalid = await callText(gateway, "map.validate", {
- def: { ...MEADOW_DEF, layers: [{ name: "ground", data: [["grass"]] }] },
- });
- expect(invalid.ok).toBe(false);
- expect(invalid.text).toMatch(/expected 2/);
- const badWidth = await callText(gateway, "map.validate", { def: { ...MEADOW_DEF, width: 0 } });
- expect(badWidth.ok).toBe(false);
- expect(badWidth.text).toMatch(/width/);
- } finally {
- await close();
- }
- });
- it("map.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, "map.create", {
- def: { ...MEADOW_DEF, layers: [{ name: "ground", data: [["grass"]] }] },
- });
- expect(bad.ok).toBe(false);
- expect(engine.registry.get("map", "meadow")).toBeUndefined();
- await expect(readFile(join(tmp, "maps", "meadow.yaml"))).rejects.toThrow();
- } finally {
- await close();
- }
- });
- it("duplicate map.create overwrites the existing definition", async () => {
- const { engine, gateway, tmp, close } = await setup();
- try {
- await callText(gateway, "map.create", { def: MEADOW_DEF });
- const recreated = await callText(gateway, "map.create", {
- def: { ...MEADOW_DEF, tilesetRef: "meadow-tiles", walkable: { water: false } },
- });
- expect(recreated.ok).toBe(true);
- const current = engine.registry.get<MapDef>("map", "meadow");
- expect(current?.tilesetRef).toBe("meadow-tiles");
- expect(current?.walkable).toEqual({ water: false });
- const file = await readFile(join(tmp, "maps", "meadow.yaml"), "utf8");
- expect(file).toContain("meadow-tiles");
- } finally {
- await close();
- }
- });
- it("map.update merges a patch, validates, rewrites the file, and re-registers", async () => {
- const { engine, gateway, tmp, close } = await setup();
- try {
- await callText(gateway, "map.create", { def: MEADOW_DEF });
- const updated = await callText(gateway, "map.update", {
- id: "meadow",
- patch: { tilesetRef: "renamed", spawn: { x: 1, y: 1 } },
- });
- expect(updated.ok).toBe(true);
- const def = JSON.parse(updated.text) as MapDef;
- expect(def.tilesetRef).toBe("renamed");
- expect(def.spawn).toEqual({ x: 1, y: 1 });
- expect(def.width).toBe(2);
- const file = await readFile(join(tmp, "maps", "meadow.yaml"), "utf8");
- expect(file).toContain("renamed");
- const missing = await callText(gateway, "map.update", { id: "nope", patch: { tilesetRef: "x" } });
- expect(missing.ok).toBe(false);
- expect(missing.text).toMatch(/map not found: nope/);
- const badPatch = await callText(gateway, "map.update", { id: "meadow", patch: { layers: [] } });
- expect(badPatch.ok).toBe(false);
- expect(badPatch.text).toMatch(/layers/);
- expect(engine.registry.get<MapDef>("map", "meadow")?.tilesetRef).toBe("renamed");
- } finally {
- await close();
- }
- });
- it("map.list returns one-line summaries including created maps", async () => {
- const { gateway, close } = await setup();
- try {
- await callText(gateway, "map.create", { def: MEADOW_DEF });
- const listed = await callText(gateway, "map.list");
- expect(listed.ok).toBe(true);
- expect(JSON.parse(listed.text)).toEqual({
- maps: [
- { id: "farm", width: 2, height: 2, tilesetRef: "tiles" },
- { id: "meadow", width: 2, height: 2, tilesetRef: "tiles" },
- ],
- });
- } finally {
- await close();
- }
- });
- it("map.get returns the full def plus the player entity; unknown id is an error result", async () => {
- const { gateway, close } = await setup();
- try {
- await callText(gateway, "map.create", { def: MEADOW_DEF });
- const got = await callText(gateway, "map.get", { mapId: "meadow" });
- expect(got.ok).toBe(true);
- const map = JSON.parse(got.text) as Record<string, unknown>;
- expect(map.id).toBe("meadow");
- expect(map.width).toBe(2);
- expect(map.player).toMatchObject({ id: "player", type: "player", x: 0, y: 0 });
- const missing = await callText(gateway, "map.get", { mapId: "nope" });
- expect(missing.ok).toBe(false);
- expect(missing.text).toMatch(/map not found: nope/);
- } finally {
- await close();
- }
- });
- it("map://{mapId} resource stays readable; missing id rejects", async () => {
- const { gateway, close } = await setup();
- try {
- await callText(gateway, "map.create", { def: MEADOW_DEF });
- const single = await gateway.readResource("map://meadow");
- expect(JSON.parse(single.contents[0]?.text ?? "{}")).toMatchObject({ id: "meadow", width: 2 });
- await expect(gateway.readResource("map://missing")).rejects.toThrow(/map not found: missing/);
- } finally {
- await close();
- }
- });
- it("serves the map.create-prompt prompt", async () => {
- const { gateway, close } = await setup();
- try {
- const prompt = await gateway.getPrompt("map.create-prompt", {});
- const text = prompt.messages[0]?.content.text ?? "";
- expect(text).toContain("layers");
- expect(text).toContain("spawn");
- expect(text).toContain("map.validate");
- expect(text).toContain("content/maps/<id>.yaml");
- } finally {
- await close();
- }
- });
- });
|