animation-namespace.test.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 AnimationDef } 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-animation-"));
  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 PLAYER_ANIM: AnimationDef = {
  63. id: "player",
  64. initial: "idle",
  65. states: [
  66. { id: "idle", spriteRef: "player", loop: true },
  67. { id: "walk", spriteRef: "player-walk", fps: 8, loop: true },
  68. ],
  69. transitions: [
  70. { from: "idle", to: "walk", on: "move" },
  71. { from: "walk", to: "idle", on: "idle" },
  72. ],
  73. };
  74. describe("animation namespace", () => {
  75. it("animation.create writes content/animations/<id>.yaml, registers live, and persists across restart", async () => {
  76. const { engine, gateway, tmp, close } = await setup();
  77. try {
  78. const created = await callText(gateway, "animation.create", { def: PLAYER_ANIM });
  79. expect(created.ok).toBe(true);
  80. expect(JSON.parse(created.text) as AnimationDef).toEqual(PLAYER_ANIM);
  81. expect(engine.registry.get<AnimationDef>("animation", "player")).toEqual(PLAYER_ANIM);
  82. const file = await readFile(join(tmp, "animations", "player.yaml"), "utf8");
  83. expect(file).toContain("id: player");
  84. expect(file).toContain("spriteRef: player-walk");
  85. await engine.stop();
  86. const reloaded = createEngine({ contentDir: tmp });
  87. await reloaded.start();
  88. try {
  89. expect(reloaded.registry.get<AnimationDef>("animation", "player")).toEqual(PLAYER_ANIM);
  90. } finally {
  91. await reloaded.stop();
  92. }
  93. } finally {
  94. await close();
  95. }
  96. });
  97. it("animation.validate accepts a valid def and rejects an invalid one (bad initial / undeclared transition)", async () => {
  98. const { gateway, close } = await setup();
  99. try {
  100. const valid = await callText(gateway, "animation.validate", { def: PLAYER_ANIM });
  101. expect(valid.ok).toBe(true);
  102. expect(JSON.parse(valid.text)).toEqual(PLAYER_ANIM);
  103. const badInitial = await callText(gateway, "animation.validate", {
  104. def: { id: "x", initial: "nope", states: [{ id: "idle", spriteRef: "player" }] },
  105. });
  106. expect(badInitial.ok).toBe(false);
  107. expect(badInitial.text).toMatch(/initial state must be a declared state/);
  108. const badTransition = await callText(gateway, "animation.validate", {
  109. def: { id: "x", initial: "idle", states: [{ id: "idle", spriteRef: "player" }], transitions: [{ from: "idle", to: "dance" }] },
  110. });
  111. expect(badTransition.ok).toBe(false);
  112. expect(badTransition.text).toMatch(/transition to must be a declared state/);
  113. const strictInvalid = await callText(gateway, "animation.validate", {
  114. def: { id: "x", initial: "idle", states: [{ id: "idle", spriteRef: "player", extra: 1 }] },
  115. });
  116. expect(strictInvalid.ok).toBe(false);
  117. expect(strictInvalid.text).toMatch(/Unrecognized key/);
  118. } finally {
  119. await close();
  120. }
  121. });
  122. it("animation.create rejects an invalid def and does not write or register", async () => {
  123. const { engine, gateway, tmp, close } = await setup();
  124. try {
  125. const bad = await callText(gateway, "animation.create", {
  126. def: { id: "broken", initial: "nope", states: [] },
  127. });
  128. expect(bad.ok).toBe(false);
  129. expect(engine.registry.get("animation", "broken")).toBeUndefined();
  130. await expect(readFile(join(tmp, "animations", "broken.yaml"))).rejects.toThrow();
  131. } finally {
  132. await close();
  133. }
  134. });
  135. it("duplicate animation.create overwrites the existing definition", async () => {
  136. const { engine, gateway, tmp, close } = await setup();
  137. try {
  138. await callText(gateway, "animation.create", { def: PLAYER_ANIM });
  139. const recreated = await callText(gateway, "animation.create", {
  140. def: { id: "player", initial: "idle", states: [{ id: "idle", spriteRef: "player" }] },
  141. });
  142. expect(recreated.ok).toBe(true);
  143. const current = engine.registry.get<AnimationDef>("animation", "player");
  144. expect(current?.states).toHaveLength(1);
  145. const file = await readFile(join(tmp, "animations", "player.yaml"), "utf8");
  146. expect(file).toContain("states:");
  147. expect(file).not.toContain("player-walk");
  148. } finally {
  149. await close();
  150. }
  151. });
  152. it("animation.update merges a patch, validates, rewrites the file, and re-registers", async () => {
  153. const { engine, gateway, tmp, close } = await setup();
  154. try {
  155. await callText(gateway, "animation.create", { def: PLAYER_ANIM });
  156. const updated = await callText(gateway, "animation.update", {
  157. id: "player",
  158. patch: { initial: "walk", states: [{ id: "walk", spriteRef: "player-walk", fps: 6 }], transitions: [] },
  159. });
  160. expect(updated.ok).toBe(true);
  161. const def = JSON.parse(updated.text) as AnimationDef;
  162. expect(def.initial).toBe("walk");
  163. expect(def.states).toEqual([{ id: "walk", spriteRef: "player-walk", fps: 6 }]);
  164. const file = await readFile(join(tmp, "animations", "player.yaml"), "utf8");
  165. expect(file).toContain("fps: 6");
  166. const missing = await callText(gateway, "animation.update", { id: "nope", patch: { id: "x" } });
  167. expect(missing.ok).toBe(false);
  168. expect(missing.text).toMatch(/animation not found: nope/);
  169. const badPatch = await callText(gateway, "animation.update", {
  170. id: "player",
  171. patch: { initial: "nope" },
  172. });
  173. expect(badPatch.ok).toBe(false);
  174. expect(badPatch.text).toMatch(/initial/);
  175. expect(engine.registry.get<AnimationDef>("animation", "player")?.initial).toBe("walk");
  176. } finally {
  177. await close();
  178. }
  179. });
  180. it("animation.list returns one-line summaries", async () => {
  181. const { gateway, close } = await setup();
  182. try {
  183. await callText(gateway, "animation.create", { def: PLAYER_ANIM });
  184. const listed = await callText(gateway, "animation.list");
  185. expect(listed.ok).toBe(true);
  186. expect(JSON.parse(listed.text)).toEqual({ animations: [{ id: "player", stateCount: 2 }] });
  187. } finally {
  188. await close();
  189. }
  190. });
  191. it("animation.get returns the full def; unknown id is an error result", async () => {
  192. const { gateway, close } = await setup();
  193. try {
  194. await callText(gateway, "animation.create", { def: PLAYER_ANIM });
  195. const got = await callText(gateway, "animation.get", { animationId: "player" });
  196. expect(got.ok).toBe(true);
  197. expect(JSON.parse(got.text)).toEqual(PLAYER_ANIM);
  198. const missing = await callText(gateway, "animation.get", { animationId: "nope" });
  199. expect(missing.ok).toBe(false);
  200. expect(missing.text).toMatch(/animation not found: nope/);
  201. const blank = await callText(gateway, "animation.get", { animationId: "" });
  202. expect(blank.ok).toBe(false);
  203. expect(blank.text).toMatch(/animation\.get: animationId must be a non-empty string/);
  204. } finally {
  205. await close();
  206. }
  207. });
  208. it("resources: animation://catalog and animation://{id}; missing id rejects", async () => {
  209. const { gateway, close } = await setup();
  210. try {
  211. await callText(gateway, "animation.create", { def: PLAYER_ANIM });
  212. const catalog = await gateway.readResource("animation://catalog");
  213. expect(JSON.parse(catalog.contents[0]?.text ?? "{}")).toEqual({ animations: [PLAYER_ANIM] });
  214. const single = await gateway.readResource("animation://player");
  215. expect(JSON.parse(single.contents[0]?.text ?? "{}")).toEqual(PLAYER_ANIM);
  216. await expect(gateway.readResource("animation://missing")).rejects.toThrow(/animation not found: missing/);
  217. } finally {
  218. await close();
  219. }
  220. });
  221. it("serves the animation.create-prompt prompt", async () => {
  222. const { gateway, close } = await setup();
  223. try {
  224. const prompt = await gateway.getPrompt("animation.create-prompt", {});
  225. const text = prompt.messages[0]?.content.text ?? "";
  226. expect(text).toContain("states");
  227. expect(text).toContain("animation.validate");
  228. expect(text).toContain("content/animations/<id>.yaml");
  229. expect(text).toContain("set-state player.anim");
  230. } finally {
  231. await close();
  232. }
  233. });
  234. });