sprite-namespace.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 SpriteDef } 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-sprite-"));
  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 SPROUT_DEF: SpriteDef = {
  63. id: "parsnip-sprout",
  64. frames: [{ name: "idle", durationTicks: 1, layers: [{ shape: "rect", x: 8, y: 8, w: 16, h: 16, fill: "#66bb6a" }] }],
  65. };
  66. describe("sprite namespace", () => {
  67. it("sprite.create writes content/sprites/<id>.yaml, registers live, and persists across restart", async () => {
  68. const { engine, gateway, tmp, close } = await setup();
  69. try {
  70. const created = await callText(gateway, "sprite.create", { def: SPROUT_DEF });
  71. expect(created.ok).toBe(true);
  72. expect(JSON.parse(created.text) as SpriteDef).toEqual(SPROUT_DEF);
  73. expect(engine.registry.get<SpriteDef>("sprite", "parsnip-sprout")).toEqual(SPROUT_DEF);
  74. const file = await readFile(join(tmp, "sprites", "parsnip-sprout.yaml"), "utf8");
  75. expect(file).toContain("id: parsnip-sprout");
  76. expect(file).toContain("shape: rect");
  77. await engine.stop();
  78. const reloaded = createEngine({ contentDir: tmp });
  79. await reloaded.start();
  80. try {
  81. expect(reloaded.registry.get<SpriteDef>("sprite", "parsnip-sprout")).toEqual(SPROUT_DEF);
  82. } finally {
  83. await reloaded.stop();
  84. }
  85. } finally {
  86. await close();
  87. }
  88. });
  89. it("sprite.validate accepts a valid def and rejects an invalid one", async () => {
  90. const { gateway, close } = await setup();
  91. try {
  92. const valid = await callText(gateway, "sprite.validate", { def: SPROUT_DEF });
  93. expect(valid.ok).toBe(true);
  94. expect(JSON.parse(valid.text)).toEqual(SPROUT_DEF);
  95. const invalid = await callText(gateway, "sprite.validate", {
  96. def: { id: "nope", frames: [{ layers: [{ shape: "rect", x: 0, y: 0, w: 4, h: 4, fill: "green" }] }] },
  97. });
  98. expect(invalid.ok).toBe(false);
  99. expect(invalid.text).toMatch(/fill/);
  100. const strictInvalid = await callText(gateway, "sprite.validate", {
  101. def: { id: "nope", frames: [{ layers: [{ shape: "rect", x: 0, y: 0, w: 4, h: 4, fill: "#ffffff", extra: 1 }] }] },
  102. });
  103. expect(strictInvalid.ok).toBe(false);
  104. expect(strictInvalid.text).toMatch(/Unrecognized key/);
  105. } finally {
  106. await close();
  107. }
  108. });
  109. it("sprite.create rejects an invalid def and does not write or register", async () => {
  110. const { engine, gateway, tmp, close } = await setup();
  111. try {
  112. const bad = await callText(gateway, "sprite.create", {
  113. def: { id: "broken", frames: [{ layers: [] }] },
  114. });
  115. expect(bad.ok).toBe(false);
  116. expect(engine.registry.get("sprite", "broken")).toBeUndefined();
  117. await expect(readFile(join(tmp, "sprites", "broken.yaml"))).rejects.toThrow();
  118. } finally {
  119. await close();
  120. }
  121. });
  122. it("duplicate sprite.create overwrites the existing definition", async () => {
  123. const { engine, gateway, tmp, close } = await setup();
  124. try {
  125. await callText(gateway, "sprite.create", { def: SPROUT_DEF });
  126. const recreated = await callText(gateway, "sprite.create", {
  127. def: { id: "parsnip-sprout", frames: [{ layers: [{ shape: "circle", x: 8, y: 8, r: 4, fill: "#4caf50" }] }] },
  128. });
  129. expect(recreated.ok).toBe(true);
  130. const current = engine.registry.get<SpriteDef>("sprite", "parsnip-sprout");
  131. expect(current?.frames[0]?.layers[0]?.shape).toBe("circle");
  132. const file = await readFile(join(tmp, "sprites", "parsnip-sprout.yaml"), "utf8");
  133. expect(file).toContain("shape: circle");
  134. } finally {
  135. await close();
  136. }
  137. });
  138. it("sprite.update merges a patch, validates, rewrites the file, and re-registers", async () => {
  139. const { engine, gateway, tmp, close } = await setup();
  140. try {
  141. await callText(gateway, "sprite.create", { def: SPROUT_DEF });
  142. const updated = await callText(gateway, "sprite.update", {
  143. id: "parsnip-sprout",
  144. patch: { frames: [{ layers: [{ shape: "circle", x: 8, y: 8, r: 6, fill: "#2e7d32" }] }] },
  145. });
  146. expect(updated.ok).toBe(true);
  147. const def = JSON.parse(updated.text) as SpriteDef;
  148. expect(def.frames[0]?.layers[0]).toEqual({ shape: "circle", x: 8, y: 8, r: 6, fill: "#2e7d32" });
  149. const file = await readFile(join(tmp, "sprites", "parsnip-sprout.yaml"), "utf8");
  150. expect(file).toContain("r: 6");
  151. const missing = await callText(gateway, "sprite.update", { id: "nope", patch: { id: "x" } });
  152. expect(missing.ok).toBe(false);
  153. expect(missing.text).toMatch(/sprite not found: nope/);
  154. const badPatch = await callText(gateway, "sprite.update", {
  155. id: "parsnip-sprout",
  156. patch: { frames: [{ layers: [] }] },
  157. });
  158. expect(badPatch.ok).toBe(false);
  159. expect(badPatch.text).toMatch(/frames/);
  160. expect(engine.registry.get<SpriteDef>("sprite", "parsnip-sprout")?.frames[0]?.layers[0]?.fill).toBe("#2e7d32");
  161. } finally {
  162. await close();
  163. }
  164. });
  165. it("sprite.list returns one-line summaries", async () => {
  166. const { gateway, close } = await setup();
  167. try {
  168. await callText(gateway, "sprite.create", { def: SPROUT_DEF });
  169. await callText(gateway, "sprite.create", {
  170. def: { id: "fence", frames: [{ layers: [{ shape: "rect", x: 0, y: 0, w: 4, h: 4, fill: "#8d6e63" }] }] },
  171. });
  172. const listed = await callText(gateway, "sprite.list");
  173. expect(listed.ok).toBe(true);
  174. expect(JSON.parse(listed.text)).toEqual({
  175. sprites: [
  176. { id: "parsnip-sprout", frameCount: 1 },
  177. { id: "fence", frameCount: 1 },
  178. ],
  179. });
  180. } finally {
  181. await close();
  182. }
  183. });
  184. it("sprite.get returns the full def; unknown id is an error result", async () => {
  185. const { gateway, close } = await setup();
  186. try {
  187. await callText(gateway, "sprite.create", { def: SPROUT_DEF });
  188. const got = await callText(gateway, "sprite.get", { spriteId: "parsnip-sprout" });
  189. expect(got.ok).toBe(true);
  190. expect(JSON.parse(got.text)).toEqual(SPROUT_DEF);
  191. const missing = await callText(gateway, "sprite.get", { spriteId: "nope" });
  192. expect(missing.ok).toBe(false);
  193. expect(missing.text).toMatch(/sprite not found: nope/);
  194. } finally {
  195. await close();
  196. }
  197. });
  198. it("resources: sprite://catalog and sprite://{id}; missing id rejects", async () => {
  199. const { gateway, close } = await setup();
  200. try {
  201. await callText(gateway, "sprite.create", { def: SPROUT_DEF });
  202. const catalog = await gateway.readResource("sprite://catalog");
  203. expect(JSON.parse(catalog.contents[0]?.text ?? "{}")).toEqual({ sprites: [SPROUT_DEF] });
  204. const single = await gateway.readResource("sprite://parsnip-sprout");
  205. expect(JSON.parse(single.contents[0]?.text ?? "{}")).toEqual(SPROUT_DEF);
  206. await expect(gateway.readResource("sprite://missing")).rejects.toThrow(/sprite not found: missing/);
  207. } finally {
  208. await close();
  209. }
  210. });
  211. it("serves the sprite.create-prompt prompt", async () => {
  212. const { gateway, close } = await setup();
  213. try {
  214. const prompt = await gateway.getPrompt("sprite.create-prompt", {});
  215. const text = prompt.messages[0]?.content.text ?? "";
  216. expect(text).toContain("frames");
  217. expect(text).toContain("sprite.validate");
  218. expect(text).toContain("content/sprites/<id>.yaml");
  219. } finally {
  220. await close();
  221. }
  222. });
  223. });