shader-namespace.test.ts 7.1 KB

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