registry.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, expect, test } from "bun:test"
  2. import type { UIPlugin } from "@lambda-agent/ui-plugin"
  3. import { createPluginRegistry } from "./registry"
  4. const plugin = (id: string): UIPlugin => ({
  5. manifest: {
  6. id,
  7. name: id,
  8. version: "1.0.0",
  9. apiVersion: 1,
  10. author: "test",
  11. description: "fixture",
  12. capabilities: ["answer.render", "answer.export"],
  13. },
  14. answer: { matches: (text) => text.startsWith(id), render: () => null, copy: (text) => `export:${text}` },
  15. })
  16. describe("UI plugin registration and recovery", () => {
  17. test("independent plugins resolve only installed matching contributions", () => {
  18. const first = plugin("test.one"),
  19. second = plugin("test.two")
  20. const registry = createPluginRegistry([first, second])
  21. expect(registry.resolve("test.two answer", () => true)).toBe(second)
  22. expect(registry.resolve("test.two answer", (id) => id !== "test.two")).toBeUndefined()
  23. expect(registry.copy("test.one answer", () => true)).toBe("export:test.one answer")
  24. expect(registry.copy("ordinary answer", () => true)).toBe("ordinary answer")
  25. })
  26. test("incompatible and duplicate registrations never replace a working host", () => {
  27. const old = plugin("test.old")
  28. old.manifest.apiVersion = 2
  29. const unknown = plugin("test.unknown")
  30. unknown.manifest.capabilities.push("credentials.read")
  31. const registry = createPluginRegistry([
  32. old,
  33. unknown,
  34. plugin("test.duplicate"),
  35. plugin("test.duplicate"),
  36. plugin("test.valid"),
  37. ])
  38. expect([...registry.entries.keys()]).toEqual(["test.valid"])
  39. expect(registry.errors.size).toBe(3)
  40. })
  41. test("match and export failures preserve original text and can recover", () => {
  42. const broken = plugin("test.broken")
  43. broken.answer.matches = () => {
  44. throw new Error("bad matcher")
  45. }
  46. const registry = createPluginRegistry([broken, plugin("test.good")])
  47. expect(registry.copy("test.good text", () => true)).toBe("export:test.good text")
  48. expect(registry.errors.has("test.broken")).toBe(true)
  49. broken.answer.matches = () => true
  50. broken.answer.copy = () => {
  51. throw new Error("bad export")
  52. }
  53. registry.reset("test.broken")
  54. expect(registry.copy("raw data", () => true)).toBe("raw data")
  55. broken.answer.copy = (text) => text
  56. registry.reset("test.broken")
  57. expect(registry.copy("recovered data", () => true)).toBe("recovered data")
  58. })
  59. })