plugin-discovery-telemetry-facts.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { describe, expect, it } from "vitest";
  2. import { SessionEventType, type SessionEvent } from "@zcode/contracts";
  3. import { PluginDiscoveryFactNormalizer } from "../src/zcode-protocol-v4/conversation-telemetry-facts.js";
  4. const PLUGIN_ID = "notion@zcode-plugins-official";
  5. let seq = 0;
  6. function event(type: SessionEvent["type"], payload: unknown, at = 1_000): SessionEvent {
  7. seq += 1;
  8. return {
  9. id: `event-${seq}`,
  10. sessionId: "s",
  11. turnId: "turn",
  12. traceId: "trace",
  13. sequenceNumber: seq,
  14. timestamp: new Date(at),
  15. type,
  16. payload,
  17. } as SessionEvent;
  18. }
  19. const scheduled = (toolName: string, toolCallId: string) =>
  20. event(SessionEventType.ToolCallScheduled, { toolCallId, toolName, input: { query: "secret" } });
  21. const result = (toolCallId: string, content: unknown, at?: number) =>
  22. event(
  23. SessionEventType.ToolCallResult,
  24. { toolCallId, duration: 1, result: { success: true, content: JSON.stringify(content) } },
  25. at,
  26. );
  27. describe("plugin discovery telemetry facts", () => {
  28. it("maps search, card, decision and install outcome without query or reason text", () => {
  29. const n = new PluginDiscoveryFactNormalizer();
  30. expect(n.normalize("s", scheduled("SearchPlugins", "search"))).toBeNull();
  31. const searched = n.normalize(
  32. "s",
  33. result("search", { plugins: [], stale: false }),
  34. );
  35. expect(searched).toMatchObject({ phase: "searched", result: "success", resultCount: 0 });
  36. n.normalize("s", scheduled("SuggestPluginInstall", "suggest"));
  37. const shown = n.normalize(
  38. "s",
  39. event(SessionEventType.PluginInstallRequested, {
  40. interactionId: "i",
  41. request: { pluginId: PLUGIN_ID, action: "install", toolCallId: "suggest", reason: "secret" },
  42. }),
  43. );
  44. expect(shown).toMatchObject({ phase: "card_shown", pluginId: PLUGIN_ID, action: "install" });
  45. const resolved = n.normalize(
  46. "s",
  47. event(
  48. SessionEventType.PluginInstallResolved,
  49. { interactionId: "i", pluginId: PLUGIN_ID, toolCallId: "suggest", decision: "install" },
  50. 2_000,
  51. ),
  52. );
  53. expect(resolved).toMatchObject({ phase: "card_resolved", decision: "install" });
  54. const settled = n.normalize(
  55. "s",
  56. result("suggest", { pluginId: PLUGIN_ID, status: "installed", message: "ok" }, 5_000),
  57. );
  58. expect(settled).toMatchObject({
  59. phase: "install_settled",
  60. pluginId: PLUGIN_ID,
  61. action: "install",
  62. result: "success",
  63. durationMs: 3_000,
  64. });
  65. expect(JSON.stringify([searched, shown, resolved, settled])).not.toContain("secret");
  66. });
  67. it("does not report an install outcome when the user skipped or no card was shown", () => {
  68. const n = new PluginDiscoveryFactNormalizer();
  69. n.normalize("s", scheduled("SuggestPluginInstall", "skip"));
  70. n.normalize(
  71. "s",
  72. event(SessionEventType.PluginInstallResolved, {
  73. interactionId: "i",
  74. pluginId: PLUGIN_ID,
  75. toolCallId: "skip",
  76. decision: "skip",
  77. }),
  78. );
  79. expect(
  80. n.normalize("s", result("skip", { pluginId: PLUGIN_ID, status: "skipped", message: "" })),
  81. ).toBeNull();
  82. n.normalize("s", scheduled("SuggestPluginInstall", "nocard"));
  83. expect(
  84. n.normalize("s", result("nocard", { pluginId: PLUGIN_ID, status: "unavailable", message: "" })),
  85. ).toBeNull();
  86. n.normalize("s", scheduled("Read", "other"));
  87. expect(n.normalize("s", result("other", "file"))).toBeNull();
  88. });
  89. });