| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join, resolve } from "node:path";
- import { describe, expect, it } from "vitest";
- import { createEngine, type ItemDef } from "@nengine/core";
- import { createEngineNamespaces, McpGateway } from "../src/index.js";
- const TIME_YAML = [
- "id: default",
- "dayLengthTicks: 480",
- "seasons:",
- " - { name: spring, dayCount: 28 }",
- "start: { day: 1, season: spring }",
- "",
- ].join("\n");
- const FARM_YAML = [
- "id: farm",
- "width: 2",
- "height: 2",
- 'tilesetRef: "tiles"',
- "spawn: { x: 0, y: 0 }",
- "layers:",
- " - name: ground",
- " data:",
- " - [a, b]",
- " - [b, a]",
- "",
- ].join("\n");
- interface Setup {
- engine: ReturnType<typeof createEngine>;
- gateway: McpGateway;
- tmp: string;
- close: () => Promise<void>;
- }
- async function setup(): Promise<Setup> {
- const tmp = await mkdtemp(join(tmpdir(), "farm-item-"));
- await mkdir(join(tmp, "maps"), { recursive: true });
- await writeFile(join(tmp, "time.yaml"), TIME_YAML);
- await writeFile(join(tmp, "maps", "farm.yaml"), FARM_YAML);
- const engine = createEngine({ contentDir: tmp });
- const gateway = new McpGateway();
- for (const namespace of createEngineNamespaces(engine, { gateway })) {
- gateway.register(namespace);
- }
- await engine.start();
- return {
- engine,
- gateway,
- tmp,
- close: async () => {
- await engine.stop();
- await rm(tmp, { recursive: true, force: true });
- },
- };
- }
- async function callText(gateway: McpGateway, name: string, args: Record<string, unknown> = {}): Promise<{
- ok: boolean;
- text: string;
- }> {
- const result = await gateway.callTool(name, args);
- return { ok: result.isError !== true, text: result.content[0]?.text ?? "" };
- }
- const HOE_DEF: ItemDef = { id: "hoe", name: "Hoe", kind: "tool", stackable: false, rarity: "common" };
- describe("item namespace", () => {
- it("item.create writes content/items/<id>.yaml, registers live, and persists across restart", async () => {
- const { engine, gateway, tmp, close } = await setup();
- try {
- const created = await callText(gateway, "item.create", { def: HOE_DEF });
- expect(created.ok).toBe(true);
- expect(JSON.parse(created.text) as ItemDef).toEqual(HOE_DEF);
- expect(engine.registry.get<ItemDef>("item", "hoe")).toEqual(HOE_DEF);
- const file = await readFile(join(tmp, "items", "hoe.yaml"), "utf8");
- expect(file).toContain("id: hoe");
- expect(file).toContain("kind: tool");
- await engine.stop();
- const reloaded = createEngine({ contentDir: tmp });
- await reloaded.start();
- try {
- expect(reloaded.registry.get<ItemDef>("item", "hoe")).toEqual(HOE_DEF);
- } finally {
- await reloaded.stop();
- }
- } finally {
- await close();
- }
- });
- it("item.validate accepts a valid def and rejects an invalid one", async () => {
- const { gateway, close } = await setup();
- try {
- const valid = await callText(gateway, "item.validate", { def: HOE_DEF });
- expect(valid.ok).toBe(true);
- expect(JSON.parse(valid.text)).toEqual(HOE_DEF);
- const invalid = await callText(gateway, "item.validate", {
- def: { id: "nope", name: "Nope", kind: "not-a-kind", stackable: false },
- });
- expect(invalid.ok).toBe(false);
- expect(invalid.text).toMatch(/Invalid enum value/);
- expect(invalid.text).toMatch(/kind/);
- } finally {
- await close();
- }
- });
- it("item.create rejects an invalid def and does not write or register", async () => {
- const { engine, gateway, tmp, close } = await setup();
- try {
- const bad = await callText(gateway, "item.create", {
- def: { id: "broken", name: "Broken", kind: "tool", stackable: "yes" },
- });
- expect(bad.ok).toBe(false);
- expect(engine.registry.get("item", "broken")).toBeUndefined();
- await expect(readFile(join(tmp, "items", "broken.yaml"))).rejects.toThrow();
- } finally {
- await close();
- }
- });
- it("duplicate item.create overwrites the existing definition", async () => {
- const { engine, gateway, tmp, close } = await setup();
- try {
- await callText(gateway, "item.create", { def: HOE_DEF });
- const recreated = await callText(gateway, "item.create", {
- def: { id: "hoe", name: "Copper Hoe", kind: "tool", stackable: false, price: 800, rarity: "uncommon" },
- });
- expect(recreated.ok).toBe(true);
- const current = engine.registry.get<ItemDef>("item", "hoe");
- expect(current?.name).toBe("Copper Hoe");
- expect(current?.price).toBe(800);
- const file = await readFile(join(tmp, "items", "hoe.yaml"), "utf8");
- expect(file).toContain("name: Copper Hoe");
- } finally {
- await close();
- }
- });
- it("item.update merges a patch, validates, rewrites the file, and re-registers", async () => {
- const { engine, gateway, tmp, close } = await setup();
- try {
- await callText(gateway, "item.create", { def: HOE_DEF });
- const updated = await callText(gateway, "item.update", { id: "hoe", patch: { price: 700 } });
- expect(updated.ok).toBe(true);
- const def = JSON.parse(updated.text) as ItemDef;
- expect(def).toEqual({ id: "hoe", name: "Hoe", kind: "tool", stackable: false, rarity: "common", price: 700 });
- const file = await readFile(join(tmp, "items", "hoe.yaml"), "utf8");
- expect(file).toContain("price: 700");
- const missing = await callText(gateway, "item.update", { id: "nope", patch: { price: 1 } });
- expect(missing.ok).toBe(false);
- expect(missing.text).toMatch(/item not found: nope/);
- const badPatch = await callText(gateway, "item.update", { id: "hoe", patch: { kind: "bogus" } });
- expect(badPatch.ok).toBe(false);
- expect(badPatch.text).toMatch(/Invalid enum value/);
- expect(engine.registry.get<ItemDef>("item", "hoe")?.price).toBe(700);
- } finally {
- await close();
- }
- });
- it("item.list returns one-line summaries", async () => {
- const { gateway, close } = await setup();
- try {
- await callText(gateway, "item.create", { def: HOE_DEF });
- await callText(gateway, "item.create", {
- def: { id: "parsnip-seed", name: "Parsnip Seed", kind: "seed", stackable: true },
- });
- const listed = await callText(gateway, "item.list");
- expect(listed.ok).toBe(true);
- expect(JSON.parse(listed.text)).toEqual({
- items: [
- { id: "hoe", name: "Hoe", kind: "tool" },
- { id: "parsnip-seed", name: "Parsnip Seed", kind: "seed" },
- ],
- });
- } finally {
- await close();
- }
- });
- it("item.get returns the full def; unknown id is an error result", async () => {
- const { gateway, close } = await setup();
- try {
- await callText(gateway, "item.create", { def: HOE_DEF });
- const got = await callText(gateway, "item.get", { itemId: "hoe" });
- expect(got.ok).toBe(true);
- expect(JSON.parse(got.text)).toEqual(HOE_DEF);
- const missing = await callText(gateway, "item.get", { itemId: "nope" });
- expect(missing.ok).toBe(false);
- expect(missing.text).toMatch(/item not found: nope/);
- } finally {
- await close();
- }
- });
- it("item.query_inventory and item.give read and mutate the player inventory", async () => {
- const { engine, gateway, close } = await setup();
- try {
- await callText(gateway, "item.create", { def: HOE_DEF });
- const empty = await callText(gateway, "item.query_inventory");
- expect(JSON.parse(empty.text)).toEqual({ inventory: {} });
- const gave = await callText(gateway, "item.give", { itemId: "hoe", qty: 3 });
- expect(JSON.parse(gave.text)).toEqual({ itemId: "hoe", qty: 3, total: 3 });
- const after = await callText(gateway, "item.query_inventory");
- expect(JSON.parse(after.text)).toEqual({ inventory: { hoe: 3 } });
- const second = await callText(gateway, "item.give", { itemId: "hoe", qty: 2 });
- expect(JSON.parse(second.text)).toEqual({ itemId: "hoe", qty: 2, total: 5 });
- expect(engine.world.get("player")?.inventory).toEqual({ hoe: 5 });
- const unknown = await callText(gateway, "item.give", { itemId: "nope", qty: 1 });
- expect(unknown.ok).toBe(false);
- expect(unknown.text).toMatch(/unknown item: nope/);
- const badQty = await callText(gateway, "item.give", { itemId: "hoe", qty: 0 });
- expect(badQty.ok).toBe(false);
- expect(badQty.text).toMatch(/positive integer/);
- } finally {
- await close();
- }
- });
- it("resources: item://catalog and item://{id}; missing id rejects", async () => {
- const { engine, gateway, close } = await setup();
- try {
- await callText(gateway, "item.create", { def: HOE_DEF });
- const catalog = await gateway.readResource("item://catalog");
- expect(JSON.parse(catalog.contents[0]?.text ?? "{}")).toEqual({ items: [HOE_DEF] });
- const single = await gateway.readResource("item://hoe");
- expect(JSON.parse(single.contents[0]?.text ?? "{}")).toEqual(HOE_DEF);
- await expect(gateway.readResource("item://missing")).rejects.toThrow(/item not found: missing/);
- expect(engine.registry.get("item", "missing")).toBeUndefined();
- } finally {
- await close();
- }
- });
- it("serves the item.create-prompt prompt", async () => {
- const { gateway, close } = await setup();
- try {
- const prompt = await gateway.getPrompt("item.create-prompt", {});
- const text = prompt.messages[0]?.content.text ?? "";
- expect(text).toContain("kind");
- expect(text).toContain("stackable");
- expect(text).toContain("item.validate");
- expect(text).toContain("content/items/<id>.yaml");
- } finally {
- await close();
- }
- });
- });
|