namespaces.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { resolve } from "node:path";
  2. import { describe, expect, it } from "vitest";
  3. import { createEngine } from "@nengine/core";
  4. import { createEngineNamespaces, McpGateway } from "../src/index.js";
  5. import type { McpNamespace } from "../src/namespace.js";
  6. const repoRoot = resolve(process.cwd(), "../..");
  7. const contentDir = resolve(repoRoot, "content");
  8. const modsDir = resolve(repoRoot, "mods");
  9. interface Setup {
  10. engine: ReturnType<typeof createEngine>;
  11. gateway: McpGateway;
  12. }
  13. async function setup(): Promise<Setup> {
  14. const engine = createEngine({ contentDir, modsDir });
  15. const gateway = new McpGateway();
  16. for (const namespace of createEngineNamespaces(engine, { gateway })) {
  17. gateway.register(namespace);
  18. }
  19. await engine.start();
  20. return { engine, gateway };
  21. }
  22. async function callText(gateway: McpGateway, name: string, args: Record<string, unknown> = {}): Promise<{
  23. ok: boolean;
  24. text: string;
  25. }> {
  26. const result = await gateway.callTool(name, args);
  27. return { ok: result.isError !== true, text: result.content[0]?.text ?? "" };
  28. }
  29. describe("engine namespaces", () => {
  30. it("server.info reports engine identity, content stats, subsystems, and time", async () => {
  31. const { engine, gateway } = await setup();
  32. try {
  33. const result = await callText(gateway, "server.info");
  34. expect(result.ok).toBe(true);
  35. const info = JSON.parse(result.text) as {
  36. engine: string;
  37. content: Record<string, string[]>;
  38. subsystems: { name: string; enabled: boolean }[];
  39. time: { day: number; season: string; tickInDay: number };
  40. };
  41. expect(info.engine).toBe("nengine");
  42. expect(info.content["map"]).toContain("farm");
  43. expect(info.content["time"]).toContain("default");
  44. expect(info.subsystems.map((s) => s.name).sort()).toEqual(["animal", "animation", "behavior", "craft", "crop", "dialogue", "item", "map", "mod", "npc", "particle", "placement", "player", "rng", "save", "scene", "server", "shader", "shop", "sound", "sprite", "time", "weather"]);
  45. expect(info.time).toEqual({ day: 1, season: "spring", tickInDay: 0 });
  46. expect(engine.registry.ids("map")).toEqual(["farm"]);
  47. } finally {
  48. await engine.stop();
  49. }
  50. });
  51. it("map.list and map.get return the normalized map plus player spawn", async () => {
  52. const { engine, gateway } = await setup();
  53. try {
  54. const listed = await callText(gateway, "map.list");
  55. const maps = (JSON.parse(listed.text) as { maps: { id: string; width: number; height: number }[] }).maps;
  56. expect(maps).toEqual([{ id: "farm", width: 12, height: 12, tilesetRef: "farm-tiles" }]);
  57. const got = await callText(gateway, "map.get");
  58. const map = JSON.parse(got.text) as { id: string; layers: unknown[]; spawn: { x: number; y: number }; player: { x: number; y: number } };
  59. expect(map.id).toBe("farm");
  60. expect(map.layers.length).toBe(1);
  61. expect(map.spawn).toEqual({ x: 2, y: 2 });
  62. expect(map.player).toMatchObject({ id: "player", type: "player", x: 2, y: 2 });
  63. const missing = await callText(gateway, "map.get", { mapId: "nope" });
  64. expect(missing.ok).toBe(false);
  65. expect(missing.text).toMatch(/map not found: nope/);
  66. } finally {
  67. await engine.stop();
  68. }
  69. });
  70. it("time.get, time.advance, and time.set read and mutate the TimeSystem", async () => {
  71. const { engine, gateway } = await setup();
  72. try {
  73. const initial = await callText(gateway, "time.get");
  74. expect(JSON.parse(initial.text)).toEqual({ day: 1, season: "spring", tickInDay: 0 });
  75. const advanced = await callText(gateway, "time.advance", { ticks: 480 });
  76. expect(JSON.parse(advanced.text)).toEqual({ day: 2, season: "spring", tickInDay: 0 });
  77. const engineTime = engine.systems.get("time");
  78. expect(engineTime?.get()).toEqual({ day: 2, season: "spring", tickInDay: 0 });
  79. const set = await callText(gateway, "time.set", { day: 12, season: "summer" });
  80. expect(JSON.parse(set.text)).toEqual({ day: 12, season: "summer", tickInDay: 0 });
  81. const invalid = await callText(gateway, "time.advance", { ticks: -1 });
  82. expect(invalid.ok).toBe(false);
  83. expect(invalid.text).toMatch(/positive integer/);
  84. } finally {
  85. await engine.stop();
  86. }
  87. });
  88. it("reads server, time, and template map resources", async () => {
  89. const { engine, gateway } = await setup();
  90. try {
  91. const status = await gateway.readResource("server://status");
  92. expect(JSON.parse(status.contents[0]?.text ?? "{}")).toMatchObject({ engine: "nengine" });
  93. const timeState = await gateway.readResource("time://state");
  94. expect(JSON.parse(timeState.contents[0]?.text ?? "{}")).toEqual({ day: 1, season: "spring", tickInDay: 0 });
  95. const mapResource = await gateway.readResource("map://farm");
  96. expect(JSON.parse(mapResource.contents[0]?.text ?? "{}")).toMatchObject({ id: "farm" });
  97. } finally {
  98. await engine.stop();
  99. }
  100. });
  101. it("serves the server.orient prompt", async () => {
  102. const { engine, gateway } = await setup();
  103. try {
  104. const prompt = await gateway.getPrompt("server.orient", {});
  105. const text = prompt.messages[0]?.content.text ?? "";
  106. expect(text).toContain("nEngine MCP server");
  107. expect(text).toContain("time.advance");
  108. expect(text).toContain("content/maps/<id>.yaml");
  109. } finally {
  110. await engine.stop();
  111. }
  112. });
  113. it("disabled namespaces vanish from listings and reject calls", async () => {
  114. const { engine, gateway } = await setup();
  115. try {
  116. gateway.disable("map");
  117. expect(gateway.listTools().map((tool) => tool.name)).not.toContain("map.list");
  118. expect(gateway.listTools().map((tool) => tool.name)).toContain("time.get");
  119. const rejected = await callText(gateway, "map.list");
  120. expect(rejected.ok).toBe(false);
  121. expect(rejected.text).toMatch(/namespace "map" is disabled/);
  122. gateway.enable("map");
  123. const restored = await callText(gateway, "map.list");
  124. expect(restored.ok).toBe(true);
  125. } finally {
  126. await engine.stop();
  127. }
  128. });
  129. });