| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { describe, expect, it } from "vitest";
- import { createEngine, type BuildingDef } 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: 5",
- "height: 5",
- 'tilesetRef: "tiles"',
- "spawn: { x: 0, y: 0 }",
- "layers:",
- " - name: ground",
- " data:",
- " - [grass, grass, water, water, water]",
- " - [grass, grass, water, water, water]",
- " - [grass, grass, grass, grass, grass]",
- " - [tilled, tilled, grass, grass, grass]",
- " - [tilled, tilled, grass, grass, grass]",
- "",
- ].join("\n");
- const SHED_YAML = [
- "id: shed",
- "name: Shed",
- "footprint: { w: 2, h: 2 }",
- "buildable: map",
- "blocksMovement: true",
- 'description: "Stores tools and harvests."',
- "",
- ].join("\n");
- const FENCE_YAML = [
- "id: fence",
- "name: Fence",
- "footprint: { w: 1, h: 1 }",
- "buildable: { tiles: [grass] }",
- "blocksMovement: true",
- "",
- ].join("\n");
- const BARN_DEF = {
- id: "barn",
- name: "Barn",
- footprint: { w: 3, h: 2 },
- buildable: { tiles: ["grass"] },
- blocksMovement: true,
- description: "Houses animals.",
- };
- 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-placement-ns-"));
- await mkdir(join(tmp, "maps"), { recursive: true });
- await mkdir(join(tmp, "buildings"), { recursive: true });
- await writeFile(join(tmp, "time.yaml"), TIME_YAML);
- await writeFile(join(tmp, "maps", "farm.yaml"), FARM_YAML);
- await writeFile(join(tmp, "buildings", "shed.yaml"), SHED_YAML);
- await writeFile(join(tmp, "buildings", "fence.yaml"), FENCE_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 ?? "" };
- }
- describe("placement namespace", () => {
- it("building.create writes content/buildings/<id>.yaml, registers live, and persists across restart", async () => {
- const { engine, gateway, tmp, close } = await setup();
- try {
- const created = await callText(gateway, "placement.building.create", { def: BARN_DEF });
- expect(created.ok).toBe(true);
- expect(JSON.parse(created.text) as BuildingDef).toEqual(BARN_DEF);
- expect(engine.registry.get<BuildingDef>("building", "barn")).toEqual(BARN_DEF);
- const file = await readFile(join(tmp, "buildings", "barn.yaml"), "utf8");
- expect(file).toContain("id: barn");
- expect(file).toContain("blocksMovement: true");
- await engine.stop();
- const reloaded = createEngine({ contentDir: tmp });
- await reloaded.start();
- try {
- expect(reloaded.registry.get<BuildingDef>("building", "barn")).toEqual(BARN_DEF);
- } finally {
- await reloaded.stop();
- }
- } finally {
- await close();
- }
- });
- it("building.validate accepts a valid def and rejects an invalid one", async () => {
- const { gateway, close } = await setup();
- try {
- const valid = await callText(gateway, "placement.building.validate", { def: BARN_DEF });
- expect(valid.ok).toBe(true);
- expect(JSON.parse(valid.text)).toEqual(BARN_DEF);
- const invalid = await callText(gateway, "placement.building.validate", {
- def: { id: "nope", footprint: { w: 0, h: 1 }, buildable: "map", blocksMovement: true },
- });
- expect(invalid.ok).toBe(false);
- expect(invalid.text).toMatch(/footprint/);
- } finally {
- await close();
- }
- });
- it("placement.building.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, "placement.building.create", {
- def: { id: "broken", footprint: { w: 1, h: 1 }, buildable: { tiles: [] }, blocksMovement: true },
- });
- expect(bad.ok).toBe(false);
- expect(engine.registry.get("building", "broken")).toBeUndefined();
- await expect(readFile(join(tmp, "buildings", "broken.yaml"))).rejects.toThrow();
- } finally {
- await close();
- }
- });
- it("building.update merges a patch, validates, rewrites the file, and re-registers", async () => {
- const { engine, gateway, tmp, close } = await setup();
- try {
- const updated = await callText(gateway, "placement.building.update", {
- id: "fence",
- patch: { description: "A sturdy fence post." },
- });
- expect(updated.ok).toBe(true);
- const def = JSON.parse(updated.text) as BuildingDef;
- expect(def.description).toBe("A sturdy fence post.");
- expect(def.blocksMovement).toBe(true);
- const file = await readFile(join(tmp, "buildings", "fence.yaml"), "utf8");
- expect(file).toContain("A sturdy fence post.");
- const missing = await callText(gateway, "placement.building.update", {
- id: "nope",
- patch: { blocksMovement: false },
- });
- expect(missing.ok).toBe(false);
- expect(missing.text).toMatch(/building not found: nope/);
- const badPatch = await callText(gateway, "placement.building.update", {
- id: "fence",
- patch: { footprint: { w: 0, h: 1 } },
- });
- expect(badPatch.ok).toBe(false);
- expect(badPatch.text).toMatch(/footprint/);
- expect(engine.registry.get<BuildingDef>("building", "fence")?.description).toBe("A sturdy fence post.");
- } finally {
- await close();
- }
- });
- it("building.list returns one-line summaries and building.get the full def", async () => {
- const { gateway, close } = await setup();
- try {
- const listed = await callText(gateway, "placement.building.list");
- expect(listed.ok).toBe(true);
- expect(JSON.parse(listed.text)).toEqual({
- buildings: [
- { id: "fence", name: "Fence", w: 1, h: 1, blocksMovement: true },
- { id: "shed", name: "Shed", w: 2, h: 2, blocksMovement: true },
- ],
- });
- const got = await callText(gateway, "placement.building.get", { buildingId: "shed" });
- expect(got.ok).toBe(true);
- expect(JSON.parse(got.text)).toMatchObject({ id: "shed", footprint: { w: 2, h: 2 } });
- const missing = await callText(gateway, "placement.building.get", { buildingId: "nope" });
- expect(missing.ok).toBe(false);
- expect(missing.text).toMatch(/building not found: nope/);
- } finally {
- await close();
- }
- });
- it("placement.place creates a placed entity and placement.list/occupies/remove roundtrip", async () => {
- const { gateway, close } = await setup();
- try {
- const placed = await callText(gateway, "placement.place", { buildingId: "shed", x: 3, y: 3 });
- expect(placed.ok).toBe(true);
- expect(JSON.parse(placed.text)).toEqual({
- id: "building:shed:3:3",
- type: "building",
- buildingId: "shed",
- x: 3,
- y: 3,
- w: 2,
- h: 2,
- blocksMovement: true,
- });
- const listed = await callText(gateway, "placement.list");
- expect(JSON.parse(listed.text)).toEqual({
- buildings: [{ id: "building:shed:3:3", type: "building", buildingId: "shed", x: 3, y: 3, w: 2, h: 2, blocksMovement: true }],
- });
- const inside = await callText(gateway, "placement.occupies", { x: 4, y: 4 });
- expect(JSON.parse(inside.text)).toMatchObject({ id: "building:shed:3:3" });
- const outside = await callText(gateway, "placement.occupies", { x: 0, y: 0 });
- expect(JSON.parse(outside.text)).toBeNull();
- const removed = await callText(gateway, "placement.remove", { x: 3, y: 3 });
- expect(JSON.parse(removed.text)).toMatchObject({ id: "building:shed:3:3" });
- const empty = await callText(gateway, "placement.list");
- expect(JSON.parse(empty.text)).toEqual({ buildings: [] });
- } finally {
- await close();
- }
- });
- it("placement.place error paths: unknown building, out of bounds, unbuildable tile, overlap", async () => {
- const { gateway, close } = await setup();
- try {
- const unknown = await callText(gateway, "placement.place", { buildingId: "barn", x: 0, y: 0 });
- expect(unknown.ok).toBe(false);
- expect(unknown.text).toMatch(/unknown building: barn/);
- const bounds = await callText(gateway, "placement.place", { buildingId: "shed", x: 4, y: 4 });
- expect(bounds.ok).toBe(false);
- expect(bounds.text).toMatch(/footprint out of bounds/);
- const tile = await callText(gateway, "placement.place", { buildingId: "fence", x: 2, y: 0 });
- expect(tile.ok).toBe(false);
- expect(tile.text).toMatch(/tile \(2,0\) is not buildable for fence/);
- await callText(gateway, "placement.place", { buildingId: "fence", x: 0, y: 0 });
- const overlap = await callText(gateway, "placement.place", { buildingId: "fence", x: 0, y: 0 });
- expect(overlap.ok).toBe(false);
- expect(overlap.text).toMatch(/overlaps existing building/);
- const badCoord = await callText(gateway, "placement.place", { buildingId: "fence", x: -1, y: 0 });
- expect(badCoord.ok).toBe(false);
- expect(badCoord.text).toMatch(/non-negative integers/);
- } finally {
- await close();
- }
- });
- it("resources: placement://buildings, placement://{id}, placement://placed", async () => {
- const { gateway, close } = await setup();
- try {
- const catalog = await gateway.readResource("placement://buildings");
- const defs = JSON.parse(catalog.contents[0]?.text ?? "{}");
- expect(defs.buildings.map((building: { id: string }) => building.id)).toEqual(["fence", "shed"]);
- const single = await gateway.readResource("placement://shed");
- expect(JSON.parse(single.contents[0]?.text ?? "{}")).toMatchObject({ id: "shed" });
- await callText(gateway, "placement.place", { buildingId: "fence", x: 0, y: 0 });
- const placed = await gateway.readResource("placement://placed");
- expect(JSON.parse(placed.contents[0]?.text ?? "{}")).toEqual({
- buildings: [{ id: "building:fence:0:0", type: "building", buildingId: "fence", x: 0, y: 0, w: 1, h: 1, blocksMovement: true }],
- });
- await expect(gateway.readResource("placement://missing")).rejects.toThrow(/building not found: missing/);
- } finally {
- await close();
- }
- });
- it("serves the placement.create-prompt prompt", async () => {
- const { gateway, close } = await setup();
- try {
- const prompt = await gateway.getPrompt("placement.create-prompt", {});
- const text = prompt.messages[0]?.content.text ?? "";
- expect(text).toContain("footprint");
- expect(text).toContain("buildable");
- expect(text).toContain("blocksMovement");
- expect(text).toContain("placement.building.validate");
- expect(text).toContain("content/buildings/<id>.yaml");
- } finally {
- await close();
- }
- });
- });
|