particle-namespace.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 ParticleDef } 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-particle-"));
  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 DUST_DEF: ParticleDef = {
  63. id: "dust",
  64. emitRate: 8,
  65. angle: -90,
  66. spread: 35,
  67. colors: ["#d7ccc8", "#a1887f"],
  68. };
  69. describe("particle namespace", () => {
  70. it("particle.create writes content/particles/<id>.yaml, registers live, and persists across restart", async () => {
  71. const { engine, gateway, tmp, close } = await setup();
  72. try {
  73. const created = await callText(gateway, "particle.create", { def: DUST_DEF });
  74. expect(created.ok).toBe(true);
  75. expect(JSON.parse(created.text) as ParticleDef).toEqual(DUST_DEF);
  76. expect(engine.registry.get<ParticleDef>("particle", "dust")).toEqual(DUST_DEF);
  77. const file = await readFile(join(tmp, "particles", "dust.yaml"), "utf8");
  78. expect(file).toContain("id: dust");
  79. expect(file).toContain("emitRate: 8");
  80. await engine.stop();
  81. const reloaded = createEngine({ contentDir: tmp });
  82. await reloaded.start();
  83. try {
  84. expect(reloaded.registry.get<ParticleDef>("particle", "dust")).toEqual(DUST_DEF);
  85. } finally {
  86. await reloaded.stop();
  87. }
  88. } finally {
  89. await close();
  90. }
  91. });
  92. it("particle.validate accepts a valid def and rejects an invalid one", async () => {
  93. const { gateway, close } = await setup();
  94. try {
  95. const valid = await callText(gateway, "particle.validate", { def: DUST_DEF });
  96. expect(valid.ok).toBe(true);
  97. const invalid = await callText(gateway, "particle.validate", {
  98. def: { id: "nope", colors: ["green"] },
  99. });
  100. expect(invalid.ok).toBe(false);
  101. expect(invalid.text).toMatch(/colors/);
  102. const strictInvalid = await callText(gateway, "particle.validate", {
  103. def: { id: "nope", extra: 1 },
  104. });
  105. expect(strictInvalid.ok).toBe(false);
  106. expect(strictInvalid.text).toMatch(/Unrecognized key/);
  107. } finally {
  108. await close();
  109. }
  110. });
  111. it("particle.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, "particle.create", {
  115. def: { id: "broken", emitRate: -1 },
  116. });
  117. expect(bad.ok).toBe(false);
  118. expect(engine.registry.get("particle", "broken")).toBeUndefined();
  119. await expect(readFile(join(tmp, "particles", "broken.yaml"))).rejects.toThrow();
  120. } finally {
  121. await close();
  122. }
  123. });
  124. it("particle.update merges a patch, validates, rewrites the file, and re-registers", async () => {
  125. const { engine, gateway, tmp, close } = await setup();
  126. try {
  127. await callText(gateway, "particle.create", { def: DUST_DEF });
  128. const updated = await callText(gateway, "particle.update", {
  129. id: "dust",
  130. patch: { emitRate: 16 },
  131. });
  132. expect(updated.ok).toBe(true);
  133. const def = JSON.parse(updated.text) as ParticleDef;
  134. expect(def.emitRate).toBe(16);
  135. const file = await readFile(join(tmp, "particles", "dust.yaml"), "utf8");
  136. expect(file).toContain("emitRate: 16");
  137. const missing = await callText(gateway, "particle.update", { id: "nope", patch: { id: "x" } });
  138. expect(missing.ok).toBe(false);
  139. expect(missing.text).toMatch(/particle not found: nope/);
  140. } finally {
  141. await close();
  142. }
  143. });
  144. it("particle.list and particle.get return summaries and defs; unknown id errors", async () => {
  145. const { gateway, close } = await setup();
  146. try {
  147. await callText(gateway, "particle.create", { def: DUST_DEF });
  148. await callText(gateway, "particle.create", { def: { id: "rain", emitRate: 24, colors: ["#64b5f6"] } });
  149. const listed = await callText(gateway, "particle.list");
  150. expect(JSON.parse(listed.text)).toEqual({
  151. particles: [
  152. { id: "dust", emitRate: 8 },
  153. { id: "rain", emitRate: 24 },
  154. ],
  155. });
  156. const got = await callText(gateway, "particle.get", { particleId: "dust" });
  157. expect(JSON.parse(got.text)).toEqual(DUST_DEF);
  158. const missing = await callText(gateway, "particle.get", { particleId: "nope" });
  159. expect(missing.ok).toBe(false);
  160. expect(missing.text).toMatch(/particle not found: nope/);
  161. } finally {
  162. await close();
  163. }
  164. });
  165. it("resources: particle://catalog and particle://{id}; missing id rejects", async () => {
  166. const { gateway, close } = await setup();
  167. try {
  168. await callText(gateway, "particle.create", { def: DUST_DEF });
  169. const catalog = await gateway.readResource("particle://catalog");
  170. expect(JSON.parse(catalog.contents[0]?.text ?? "{}")).toEqual({ particles: [DUST_DEF] });
  171. const single = await gateway.readResource("particle://dust");
  172. expect(JSON.parse(single.contents[0]?.text ?? "{}")).toEqual(DUST_DEF);
  173. await expect(gateway.readResource("particle://missing")).rejects.toThrow(/particle not found: missing/);
  174. } finally {
  175. await close();
  176. }
  177. });
  178. it("serves the particle.create-prompt prompt", async () => {
  179. const { gateway, close } = await setup();
  180. try {
  181. const prompt = await gateway.getPrompt("particle.create-prompt", {});
  182. const text = prompt.messages[0]?.content.text ?? "";
  183. expect(text).toContain("emitRate");
  184. expect(text).toContain("particle.validate");
  185. expect(text).toContain("content/particles/<id>.yaml");
  186. } finally {
  187. await close();
  188. }
  189. });
  190. });