| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import { describe, expect, test } from "bun:test"
- import type { UIPlugin } from "@lambda-agent/ui-plugin"
- import { createPluginRegistry } from "./registry"
- const plugin = (id: string): UIPlugin => ({
- manifest: {
- id,
- name: id,
- version: "1.0.0",
- apiVersion: 1,
- author: "test",
- description: "fixture",
- capabilities: ["answer.render", "answer.export"],
- },
- answer: { matches: (text) => text.startsWith(id), render: () => null, copy: (text) => `export:${text}` },
- })
- describe("UI plugin registration and recovery", () => {
- test("independent plugins resolve only installed matching contributions", () => {
- const first = plugin("test.one"),
- second = plugin("test.two")
- const registry = createPluginRegistry([first, second])
- expect(registry.resolve("test.two answer", () => true)).toBe(second)
- expect(registry.resolve("test.two answer", (id) => id !== "test.two")).toBeUndefined()
- expect(registry.copy("test.one answer", () => true)).toBe("export:test.one answer")
- expect(registry.copy("ordinary answer", () => true)).toBe("ordinary answer")
- })
- test("incompatible and duplicate registrations never replace a working host", () => {
- const old = plugin("test.old")
- old.manifest.apiVersion = 2
- const unknown = plugin("test.unknown")
- unknown.manifest.capabilities.push("credentials.read")
- const registry = createPluginRegistry([
- old,
- unknown,
- plugin("test.duplicate"),
- plugin("test.duplicate"),
- plugin("test.valid"),
- ])
- expect([...registry.entries.keys()]).toEqual(["test.valid"])
- expect(registry.errors.size).toBe(3)
- })
- test("match and export failures preserve original text and can recover", () => {
- const broken = plugin("test.broken")
- broken.answer.matches = () => {
- throw new Error("bad matcher")
- }
- const registry = createPluginRegistry([broken, plugin("test.good")])
- expect(registry.copy("test.good text", () => true)).toBe("export:test.good text")
- expect(registry.errors.has("test.broken")).toBe(true)
- broken.answer.matches = () => true
- broken.answer.copy = () => {
- throw new Error("bad export")
- }
- registry.reset("test.broken")
- expect(registry.copy("raw data", () => true)).toBe("raw data")
- broken.answer.copy = (text) => text
- registry.reset("test.broken")
- expect(registry.copy("recovered data", () => true)).toBe("recovered data")
- })
- })
|