|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { buildServer } from "./server"; |
| 3 | +import { buildEditDesignResult, createDraft } from "./tools"; |
| 4 | +import { markAppAttached, resolveEditReceipt } from "./appBridge"; |
| 5 | +import { connect, spyStdout } from "./testFixtures"; |
| 6 | + |
| 7 | +const designAfter = () => { |
| 8 | + const created = createDraft({ |
| 9 | + widthMm: 60, heightMm: 40, dpmm: 8, |
| 10 | + objects: [{ type: "text", id: "t1", x: 10, y: 10, props: { content: "edited" } }, { type: "text", x: 10, y: 60, props: { content: "new" } }], |
| 11 | + }); |
| 12 | + if (!created.ok) throw new Error("fixture"); |
| 13 | + return created.designFile as unknown as Record<string, unknown>; |
| 14 | +}; |
| 15 | + |
| 16 | +type ToolReply = Promise<{ content: { type: string; text: string }[] }>; |
| 17 | + |
| 18 | +describe("edit_design", () => { |
| 19 | + it("is absent from the stdio build's tool list", async () => { |
| 20 | + const client = await connect(buildServer()); |
| 21 | + const { tools } = await client.listTools(); |
| 22 | + expect(tools.map((t) => t.name)).not.toContain("edit_design"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("sends the ops only and answers with the report the app measured, never an envelope", async () => { |
| 26 | + markAppAttached("session-test"); |
| 27 | + const { writes, restore } = spyStdout(); |
| 28 | + try { |
| 29 | + const client = await connect(buildServer({ hosted: true })); |
| 30 | + const operations = [ |
| 31 | + { op: "update", id: "t1", props: { content: "edited" } }, |
| 32 | + { op: "add", object: { type: "text", x: 10, y: 60, props: { content: "new" } } }, |
| 33 | + ]; |
| 34 | + const call = client.callTool({ name: "edit_design", arguments: { operations } }) as ToolReply; |
| 35 | + const line = await vi.waitUntil(() => writes[0]); |
| 36 | + const parsed = JSON.parse(line.trim()) as { zplabEvent: string; id: number; operations: unknown[]; designFile?: unknown }; |
| 37 | + expect(parsed.zplabEvent).toBe("editRequest"); |
| 38 | + expect(parsed.operations).toEqual(operations); |
| 39 | + expect(parsed.designFile).toBeUndefined(); |
| 40 | + expect(resolveEditReceipt({ id: parsed.id, ok: true, designFile: designAfter(), assignedIds: { 1: "text-2" }, capturesLost: [], capturesAtRisk: [] })).toBe(true); |
| 41 | + |
| 42 | + const res = JSON.parse((await call).content[0]?.text ?? "{}") as Record<string, unknown>; |
| 43 | + expect(res).toMatchObject({ ok: true, applied: 2, added: [{ opIndex: 1, id: "text-2" }] }); |
| 44 | + expect(res.designFile).toBeUndefined(); |
| 45 | + expect(Array.isArray(res.bounds)).toBe(true); |
| 46 | + } finally { |
| 47 | + restore(); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it("hands the app's refusal back with the op it named", async () => { |
| 52 | + markAppAttached("session-test"); |
| 53 | + const { writes, restore } = spyStdout(); |
| 54 | + try { |
| 55 | + const client = await connect(buildServer({ hosted: true })); |
| 56 | + const call = client.callTool({ name: "edit_design", arguments: { operations: [{ op: "remove", id: "ghost" }] } }) as ToolReply; |
| 57 | + const line = await vi.waitUntil(() => writes[0]); |
| 58 | + const { id } = JSON.parse(line.trim()) as { id: number }; |
| 59 | + resolveEditReceipt({ id, ok: false, errors: ["No object with id ghost"], opIndex: 0 }); |
| 60 | + expect(JSON.parse((await call).content[0]?.text ?? "{}")).toEqual({ ok: false, errors: ["No object with id ghost"], opIndex: 0 }); |
| 61 | + } finally { |
| 62 | + restore(); |
| 63 | + } |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe("buildEditDesignResult", () => { |
| 68 | + it("names the captures an edit costs", () => { |
| 69 | + const result = buildEditDesignResult( |
| 70 | + { id: 1, ok: true, designFile: designAfter(), capturesLost: [0], capturesAtRisk: [] }, |
| 71 | + [{ op: "update", id: "t1", props: { content: "edited" } }], |
| 72 | + ); |
| 73 | + expect(result.ok).toBe(true); |
| 74 | + if (!result.ok) return; |
| 75 | + expect(result.notes?.some((n) => n.startsWith("page 1:") && n.includes("structural edit"))).toBe(true); |
| 76 | + const atRisk = buildEditDesignResult({ id: 2, ok: true, designFile: designAfter(), capturesLost: [], capturesAtRisk: [0] }, []); |
| 77 | + expect(atRisk.ok && atRisk.notes?.some((n) => n.startsWith("page 1:") && n.includes("around an edit"))).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it("tells the agent not to retry when the applied edit could not be reported", () => { |
| 81 | + const result = buildEditDesignResult({ id: 1, ok: true, designFile: { not: "a design" } }, []); |
| 82 | + expect(result.ok).toBe(false); |
| 83 | + if (result.ok) return; |
| 84 | + expect(result.errors[0]).toMatch(/Do not retry/); |
| 85 | + }); |
| 86 | +}); |
0 commit comments