import { describe, expect, it } from "vitest"; import { createVisualTools } from "../visual-tools"; import { WidgetStore } from "../widget-state"; describe("ui visual tools", () => { it("creates the expected visual tool family", () => { const tools = createVisualTools({ store: new WidgetStore({ now: () => 1 }), }); expect(tools.map((tool) => tool.name)).toEqual([ "ui_card", "ui_table", "ui_diagram", "ui_form", "ui_mockup", "ui_html", "ui_update", "ui_wait_action", ]); }); it("mounts raw HTML widgets with fallback text", async () => { const events: unknown[] = []; const [, , , , , htmlTool] = createVisualTools({ store: new WidgetStore({ now: () => 2 }), publish: (event) => events.push(event), }); const result = await htmlTool.execute( "call-1", { title: "Demo", fallback: "fallback", html: "" }, undefined, undefined, {} as never, ); expect(result.content).toEqual([{ type: "text", text: "fallback" }]); expect(result.details).toMatchObject({ widgetId: "widget-call-1", type: "widget_mount", title: "Demo", }); expect(events).toHaveLength(1); }); it("updates existing widgets", async () => { const store = new WidgetStore({ now: () => 3 }); store.mount({ toolCallId: "call-2", title: "Demo", fallback: "fallback", html: "
old
", }); const tools = createVisualTools({ store }); const updateTool = tools.find((tool) => tool.name === "ui_update"); expect(updateTool).toBeDefined(); const result = await updateTool?.execute( "call-update", { widgetId: "widget-call-2", html: "new
" }, undefined, undefined, {} as never, ); expect(result?.content).toEqual([ { type: "text", text: "Updated widget-call-2" }, ]); expect(store.get("widget-call-2")?.html).toBe("new
"); }); });