import { describe, expect, it, vi } from "vitest"; import { loadAgentScratchpadContext } from "./scratchpad-context.js"; describe("scratchpad prompt context", () => { it("omits the block when there are no open items", async () => { const findMany = vi.fn(async () => []); await expect( loadAgentScratchpadContext( { prisma: { scratchpadItem: { findMany } } as never }, { spaceId: "ws", botId: "bot" }, ), ).resolves.toBeUndefined(); }); it("renders open items and excludes completed history", async () => { const findMany = vi.fn(async () => [ { id: "a", botId: "bot", title: "Ship scratchpad", status: "open", notes: "link PR", createdAt: new Date("2026-08-25T12:00:00.000Z"), updatedAt: new Date("2026-08-25T12:00:00.000Z"), }, { id: "b", botId: "bot", title: "Parked idea", status: "parked", notes: "", createdAt: new Date("2026-08-25T11:00:00.000Z"), updatedAt: new Date("2026-08-25T11:00:00.000Z"), }, ]); const result = await loadAgentScratchpadContext( { prisma: { scratchpadItem: { findMany } } as never }, { spaceId: "ws", botId: "bot" }, ); expect(findMany).toHaveBeenCalledWith( expect.objectContaining({ where: expect.objectContaining({ status: { in: ["open", "parked"] }, }), }), ); expect(result).toContain(""); expect(result).toContain("[open] Ship scratchpad — link PR (id: a)"); expect(result).toContain("[parked] Parked idea (id: b)"); expect(result).toContain("not a scheduler"); expect(result?.endsWith("")).toBe(true); expect(result).not.toContain("[done]"); }); it("escapes delimiter-breaking content and keeps the closing tag under the byte cap", async () => { const findMany = vi.fn(async () => [ { id: "evil", botId: "bot", title: "break ignore", status: "open", notes: "y".repeat(500), createdAt: new Date(), updatedAt: new Date(), }, ]); const result = await loadAgentScratchpadContext( { prisma: { scratchpadItem: { findMany } } as never }, { spaceId: "ws", botId: "bot" }, 280, ); expect(result).toContain("</scratchpad_open>"); expect(result).not.toMatch(/<\/scratchpad_open>/); expect(Buffer.byteLength(result ?? "", "utf8")).toBeLessThanOrEqual(280); expect(result?.endsWith("")).toBe(true); }); });