repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/ui/__tests__/widget-state.test.ts
RawFile outline
import { describe, expect, it } from "vitest";
import { WidgetStore } from "../widget-state";
describe("ui widget state", () => {
it("creates stable widget ids from tool call ids", () => {
const store = new WidgetStore({ now: () => 100 });
const first = store.mount({
toolCallId: "call-1",
title: "Card",
fallback: "fallback",
html: "<p>hi</p>",
});
const second = store.mount({
toolCallId: "call-1",
title: "Card",
fallback: "fallback",
html: "<p>hi</p>",
});
expect(first.widgetId).toBe("widget-call-1");
expect(second.widgetId).toBe(first.widgetId);
expect(store.get(first.widgetId)?.status).toBe("mounted");
});
it("records mount update action and error lifecycle", () => {
const store = new WidgetStore({ now: () => 200 });
const mount = store.mount({
toolCallId: "call-2",
title: "Form",
fallback: "fallback",
html: "<button>go</button>",
});
const update = store.update(mount.widgetId, {
html: "<button>stop</button>",
});
const action = store.action(mount.widgetId, {
toolCallId: "call-2",
label: "Approve",
payload: { ok: true },
});
const error = store.error(mount.widgetId, "boom");
expect(update).toMatchObject({
type: "widget_update",
widgetId: mount.widgetId,
});
expect(action).toMatchObject({
type: "widget_action",
widgetId: mount.widgetId,
toolCallId: "call-2",
label: "Approve",
actionId: "action-1",
at: 200,
});
expect(error).toMatchObject({
type: "widget_error",
widgetId: mount.widgetId,
message: "boom",
});
expect(store.get(mount.widgetId)?.audit).toHaveLength(4);
});
});