map-namespace.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import { createEngine, type MapDef } from "@nengine/core";
  6. import { createEngineNamespaces, McpGateway } from "../src/index.js";
  7. const TIME_YAML = [
  8. "id: default",
  9. "dayLengthTicks: 480",
  10. "seasons:",
  11. " - { name: spring, dayCount: 28 }",
  12. "start: { day: 1, season: spring }",
  13. "",
  14. ].join("\n");
  15. const FARM_YAML = [
  16. "id: farm",
  17. "width: 2",
  18. "height: 2",
  19. 'tilesetRef: "tiles"',
  20. "spawn: { x: 0, y: 0 }",
  21. "layers:",
  22. " - name: ground",
  23. " data:",
  24. " - [a, b]",
  25. " - [b, a]",
  26. "",
  27. ].join("\n");
  28. interface Setup {
  29. engine: ReturnType<typeof createEngine>;
  30. gateway: McpGateway;
  31. tmp: string;
  32. close: () => Promise<void>;
  33. }
  34. async function setup(): Promise<Setup> {
  35. const tmp = await mkdtemp(join(tmpdir(), "farm-map-"));
  36. await mkdir(join(tmp, "maps"), { recursive: true });
  37. await writeFile(join(tmp, "time.yaml"), TIME_YAML);
  38. await writeFile(join(tmp, "maps", "farm.yaml"), FARM_YAML);
  39. const engine = createEngine({ contentDir: tmp });
  40. const gateway = new McpGateway();
  41. for (const namespace of createEngineNamespaces(engine, { gateway })) {
  42. gateway.register(namespace);
  43. }
  44. await engine.start();
  45. return {
  46. engine,
  47. gateway,
  48. tmp,
  49. close: async () => {
  50. await engine.stop();
  51. await rm(tmp, { recursive: true, force: true });
  52. },
  53. };
  54. }
  55. async function callText(gateway: McpGateway, name: string, args: Record<string, unknown> = {}): Promise<{
  56. ok: boolean;
  57. text: string;
  58. }> {
  59. const result = await gateway.callTool(name, args);
  60. return { ok: result.isError !== true, text: result.content[0]?.text ?? "" };
  61. }
  62. const MEADOW_DEF: MapDef = {
  63. id: "meadow",
  64. width: 2,
  65. height: 2,
  66. tilesetRef: "tiles",
  67. spawn: { x: 0, y: 0 },
  68. layers: [{ name: "ground", data: [["grass", "grass"], ["grass", "grass"]] }],
  69. };
  70. describe("map namespace", () => {
  71. it("map.create writes content/maps/<id>.yaml, registers live, and persists across restart", async () => {
  72. const { engine, gateway, tmp, close } = await setup();
  73. try {
  74. const created = await callText(gateway, "map.create", { def: MEADOW_DEF });
  75. expect(created.ok).toBe(true);
  76. expect(JSON.parse(created.text) as MapDef).toEqual(MEADOW_DEF);
  77. expect(engine.registry.get<MapDef>("map", "meadow")).toEqual(MEADOW_DEF);
  78. const file = await readFile(join(tmp, "maps", "meadow.yaml"), "utf8");
  79. expect(file).toContain("id: meadow");
  80. expect(file).toContain("grass");
  81. await engine.stop();
  82. const reloaded = createEngine({ contentDir: tmp });
  83. await reloaded.start();
  84. try {
  85. expect(reloaded.registry.get<MapDef>("map", "meadow")).toEqual(MEADOW_DEF);
  86. } finally {
  87. await reloaded.stop();
  88. }
  89. } finally {
  90. await close();
  91. }
  92. });
  93. it("map.validate accepts a valid def and rejects an invalid one", async () => {
  94. const { gateway, close } = await setup();
  95. try {
  96. const valid = await callText(gateway, "map.validate", { def: MEADOW_DEF });
  97. expect(valid.ok).toBe(true);
  98. expect(JSON.parse(valid.text)).toEqual(MEADOW_DEF);
  99. const invalid = await callText(gateway, "map.validate", {
  100. def: { ...MEADOW_DEF, layers: [{ name: "ground", data: [["grass"]] }] },
  101. });
  102. expect(invalid.ok).toBe(false);
  103. expect(invalid.text).toMatch(/expected 2/);
  104. const badWidth = await callText(gateway, "map.validate", { def: { ...MEADOW_DEF, width: 0 } });
  105. expect(badWidth.ok).toBe(false);
  106. expect(badWidth.text).toMatch(/width/);
  107. } finally {
  108. await close();
  109. }
  110. });
  111. it("map.create rejects an invalid def and does not write or register", async () => {
  112. const { engine, gateway, tmp, close } = await setup();
  113. try {
  114. const bad = await callText(gateway, "map.create", {
  115. def: { ...MEADOW_DEF, layers: [{ name: "ground", data: [["grass"]] }] },
  116. });
  117. expect(bad.ok).toBe(false);
  118. expect(engine.registry.get("map", "meadow")).toBeUndefined();
  119. await expect(readFile(join(tmp, "maps", "meadow.yaml"))).rejects.toThrow();
  120. } finally {
  121. await close();
  122. }
  123. });
  124. it("duplicate map.create overwrites the existing definition", async () => {
  125. const { engine, gateway, tmp, close } = await setup();
  126. try {
  127. await callText(gateway, "map.create", { def: MEADOW_DEF });
  128. const recreated = await callText(gateway, "map.create", {
  129. def: { ...MEADOW_DEF, tilesetRef: "meadow-tiles", walkable: { water: false } },
  130. });
  131. expect(recreated.ok).toBe(true);
  132. const current = engine.registry.get<MapDef>("map", "meadow");
  133. expect(current?.tilesetRef).toBe("meadow-tiles");
  134. expect(current?.walkable).toEqual({ water: false });
  135. const file = await readFile(join(tmp, "maps", "meadow.yaml"), "utf8");
  136. expect(file).toContain("meadow-tiles");
  137. } finally {
  138. await close();
  139. }
  140. });
  141. it("map.update merges a patch, validates, rewrites the file, and re-registers", async () => {
  142. const { engine, gateway, tmp, close } = await setup();
  143. try {
  144. await callText(gateway, "map.create", { def: MEADOW_DEF });
  145. const updated = await callText(gateway, "map.update", {
  146. id: "meadow",
  147. patch: { tilesetRef: "renamed", spawn: { x: 1, y: 1 } },
  148. });
  149. expect(updated.ok).toBe(true);
  150. const def = JSON.parse(updated.text) as MapDef;
  151. expect(def.tilesetRef).toBe("renamed");
  152. expect(def.spawn).toEqual({ x: 1, y: 1 });
  153. expect(def.width).toBe(2);
  154. const file = await readFile(join(tmp, "maps", "meadow.yaml"), "utf8");
  155. expect(file).toContain("renamed");
  156. const missing = await callText(gateway, "map.update", { id: "nope", patch: { tilesetRef: "x" } });
  157. expect(missing.ok).toBe(false);
  158. expect(missing.text).toMatch(/map not found: nope/);
  159. const badPatch = await callText(gateway, "map.update", { id: "meadow", patch: { layers: [] } });
  160. expect(badPatch.ok).toBe(false);
  161. expect(badPatch.text).toMatch(/layers/);
  162. expect(engine.registry.get<MapDef>("map", "meadow")?.tilesetRef).toBe("renamed");
  163. } finally {
  164. await close();
  165. }
  166. });
  167. it("map.list returns one-line summaries including created maps", async () => {
  168. const { gateway, close } = await setup();
  169. try {
  170. await callText(gateway, "map.create", { def: MEADOW_DEF });
  171. const listed = await callText(gateway, "map.list");
  172. expect(listed.ok).toBe(true);
  173. expect(JSON.parse(listed.text)).toEqual({
  174. maps: [
  175. { id: "farm", width: 2, height: 2, tilesetRef: "tiles" },
  176. { id: "meadow", width: 2, height: 2, tilesetRef: "tiles" },
  177. ],
  178. });
  179. } finally {
  180. await close();
  181. }
  182. });
  183. it("map.get returns the full def plus the player entity; unknown id is an error result", async () => {
  184. const { gateway, close } = await setup();
  185. try {
  186. await callText(gateway, "map.create", { def: MEADOW_DEF });
  187. const got = await callText(gateway, "map.get", { mapId: "meadow" });
  188. expect(got.ok).toBe(true);
  189. const map = JSON.parse(got.text) as Record<string, unknown>;
  190. expect(map.id).toBe("meadow");
  191. expect(map.width).toBe(2);
  192. expect(map.player).toMatchObject({ id: "player", type: "player", x: 0, y: 0 });
  193. const missing = await callText(gateway, "map.get", { mapId: "nope" });
  194. expect(missing.ok).toBe(false);
  195. expect(missing.text).toMatch(/map not found: nope/);
  196. } finally {
  197. await close();
  198. }
  199. });
  200. it("map://{mapId} resource stays readable; missing id rejects", async () => {
  201. const { gateway, close } = await setup();
  202. try {
  203. await callText(gateway, "map.create", { def: MEADOW_DEF });
  204. const single = await gateway.readResource("map://meadow");
  205. expect(JSON.parse(single.contents[0]?.text ?? "{}")).toMatchObject({ id: "meadow", width: 2 });
  206. await expect(gateway.readResource("map://missing")).rejects.toThrow(/map not found: missing/);
  207. } finally {
  208. await close();
  209. }
  210. });
  211. it("serves the map.create-prompt prompt", async () => {
  212. const { gateway, close } = await setup();
  213. try {
  214. const prompt = await gateway.getPrompt("map.create-prompt", {});
  215. const text = prompt.messages[0]?.content.text ?? "";
  216. expect(text).toContain("layers");
  217. expect(text).toContain("spawn");
  218. expect(text).toContain("map.validate");
  219. expect(text).toContain("content/maps/<id>.yaml");
  220. } finally {
  221. await close();
  222. }
  223. });
  224. });