pluginManagementService.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { describe, expect, it, vi } from "vitest";
  2. import { withPluginTelemetry } from "../src/plugins/pluginManagement.js";
  3. import { createPluginManagementService } from "../src/plugins/pluginManagementService.js";
  4. import type { IZCodeAgentService } from "../src/zcode-agent/zcodeAgent.js";
  5. describe("plugin management terminal telemetry", () => {
  6. const plugin = {
  7. id: "demo@market",
  8. name: "demo",
  9. marketplace: "market",
  10. version: "1.0",
  11. enabled: true,
  12. scope: "user" as const,
  13. };
  14. function setup(result: unknown, reject = false) {
  15. const reportTelemetryEvent = vi.fn();
  16. const installPlugin = reject
  17. ? vi.fn().mockRejectedValue(result)
  18. : vi.fn().mockResolvedValue(result);
  19. const service = createPluginManagementService({
  20. zcodeAgentService: { installPlugin } as unknown as IZCodeAgentService,
  21. reportTelemetryEvent,
  22. });
  23. return { service, installPlugin, reportTelemetryEvent };
  24. }
  25. const params = {
  26. workspacePath: "/private/work",
  27. pluginName: "demo",
  28. marketplace: "market",
  29. telemetry: { entry: "store_detail" as const, client_kind: "desktop" as const },
  30. };
  31. it("reports a single final result and strips telemetry before forwarding", async () => {
  32. const result = { installedPlugins: [plugin], dependencyClosure: [plugin.id], diagnostics: [] };
  33. const { service, installPlugin, reportTelemetryEvent } = setup(result);
  34. expect(await service.installPlugin(params)).toBe(result);
  35. expect(installPlugin).toHaveBeenCalledWith({
  36. workspacePath: params.workspacePath,
  37. pluginName: "demo",
  38. marketplace: "market",
  39. });
  40. expect(reportTelemetryEvent).toHaveBeenCalledTimes(1);
  41. expect(reportTelemetryEvent.mock.calls[0][0]).toMatchObject({
  42. elementName: "plugin_lifecycle",
  43. eventExtraDetail: {
  44. action: "install",
  45. result: "success",
  46. plugin_id: plugin.id,
  47. version_after: "1.0",
  48. entry: "store_detail",
  49. },
  50. });
  51. expect(JSON.stringify(reportTelemetryEvent.mock.calls)).not.toContain(params.workspacePath);
  52. expect(JSON.stringify(reportTelemetryEvent.mock.calls)).not.toContain("operation_id");
  53. });
  54. it("uses diagnostics, not Promise resolution, as success", async () => {
  55. const { service, reportTelemetryEvent } = setup({
  56. installedPlugins: [],
  57. dependencyClosure: [],
  58. diagnostics: [{ severity: "error", message: "secret" }],
  59. });
  60. await service.installPlugin(params);
  61. expect(reportTelemetryEvent.mock.calls[0][0].eventExtraDetail).toMatchObject({
  62. result: "failure",
  63. error_category: "diagnostic",
  64. });
  65. expect(JSON.stringify(reportTelemetryEvent.mock.calls)).not.toContain("secret");
  66. });
  67. it("does not report dry runs", async () => {
  68. const { service, reportTelemetryEvent } = setup({
  69. installedPlugins: [plugin],
  70. dependencyClosure: [],
  71. diagnostics: [],
  72. });
  73. await service.installPlugin({ ...params, dryRun: true });
  74. expect(reportTelemetryEvent).not.toHaveBeenCalled();
  75. });
  76. it("does not let reporter failure change the business result", async () => {
  77. const result = { installedPlugins: [plugin], dependencyClosure: [], diagnostics: [] };
  78. const { service, reportTelemetryEvent } = setup(result);
  79. reportTelemetryEvent.mockImplementation(() => {
  80. throw new Error("report unavailable");
  81. });
  82. await expect(service.installPlugin(params)).resolves.toBe(result);
  83. });
  84. it("preserves the original rejection without disclosing it", async () => {
  85. const error = new Error("token=secret");
  86. const { service, reportTelemetryEvent } = setup(error, true);
  87. await expect(service.installPlugin(params)).rejects.toBe(error);
  88. expect(reportTelemetryEvent.mock.calls[0][0].eventExtraDetail.result).toBe("failure");
  89. expect(JSON.stringify(reportTelemetryEvent.mock.calls)).not.toContain("secret");
  90. });
  91. });
  92. describe("plugin telemetry operation boundaries", () => {
  93. it("reports no-op uninstall and preserves the cancellation protocol", async () => {
  94. const reportTelemetryEvent = vi.fn();
  95. const cancelPluginOperation = vi.fn(async () => ({ cancelled: true, operationId: "cancel-1" }));
  96. const service = createPluginManagementService({
  97. zcodeAgentService: {
  98. uninstallPlugin: vi.fn(async () => ({ diagnostics: [] })),
  99. cancelPluginOperation,
  100. } as unknown as IZCodeAgentService,
  101. reportTelemetryEvent,
  102. });
  103. await service.uninstallPlugin({ workspacePath: "/work", pluginId: "demo@market" });
  104. expect(reportTelemetryEvent.mock.calls[0][0].eventExtraDetail).toMatchObject({
  105. action: "uninstall",
  106. result: "noop",
  107. });
  108. await service.cancelPluginOperation({ operationId: "cancel-1" });
  109. expect(cancelPluginOperation).toHaveBeenCalledWith({ operationId: "cancel-1" });
  110. expect(reportTelemetryEvent).toHaveBeenCalledTimes(1);
  111. });
  112. it("records explicit cancellation and batch partial separately", async () => {
  113. const reportTelemetryEvent = vi.fn();
  114. const service = createPluginManagementService({
  115. zcodeAgentService: {
  116. installPlugin: vi.fn(async () => {
  117. throw Object.assign(new Error("private"), { name: "AbortError" });
  118. }),
  119. updatePlugin: vi.fn(async () => ({
  120. installedPlugins: [{ id: "a@market", marketplace: "market", scope: "user" }],
  121. diagnostics: [{ severity: "error", code: "failed", message: "secret" }],
  122. dependencyClosure: [],
  123. })),
  124. } as unknown as IZCodeAgentService,
  125. reportTelemetryEvent,
  126. });
  127. await expect(
  128. service.installPlugin({ workspacePath: "/work", pluginName: "a", marketplace: "market" }),
  129. ).rejects.toThrow();
  130. await service.updatePlugin({ workspacePath: "/work" });
  131. expect(reportTelemetryEvent.mock.calls.map(([x]) => x.eventExtraDetail.result)).toEqual([
  132. "cancelled",
  133. "partial",
  134. ]);
  135. });
  136. it("isolates concurrent durations and does not wait for the reporter", async () => {
  137. const reportTelemetryEvent = vi.fn(() => new Promise<void>(() => {}));
  138. let clock = 0;
  139. const finishes: Array<(value: unknown) => void> = [];
  140. const service = createPluginManagementService({
  141. zcodeAgentService: {
  142. installPlugin: vi.fn(() => new Promise((resolve) => finishes.push(resolve))),
  143. } as unknown as IZCodeAgentService,
  144. reportTelemetryEvent,
  145. now: () => clock,
  146. });
  147. const a = service.installPlugin({
  148. workspacePath: "/a",
  149. pluginName: "a",
  150. marketplace: "market",
  151. });
  152. clock = 10;
  153. const b = service.installPlugin({
  154. workspacePath: "/b",
  155. pluginName: "b",
  156. marketplace: "market",
  157. });
  158. clock = 25;
  159. finishes[1]({
  160. installedPlugins: [{ id: "b@market", marketplace: "market", scope: "user" }],
  161. diagnostics: [],
  162. dependencyClosure: [],
  163. });
  164. await b;
  165. clock = 50;
  166. finishes[0]({
  167. installedPlugins: [{ id: "a@market", marketplace: "market", scope: "user" }],
  168. diagnostics: [],
  169. dependencyClosure: [],
  170. });
  171. await a;
  172. expect(
  173. reportTelemetryEvent.mock.calls.map(([x]) => [
  174. x.eventExtraDetail.plugin_id,
  175. x.eventExtraDetail.duration_ms,
  176. ]),
  177. ).toEqual([
  178. ["b@market", "15"],
  179. ["a@market", "50"],
  180. ]);
  181. });
  182. });
  183. describe("serialized cancellation telemetry", () => {
  184. it("recognizes original AbortError type preserved by JSON-RPC", async () => {
  185. const error = Object.assign(new Error("opaque transport error"), {
  186. data: { name: "AbortError", stack: "private" },
  187. });
  188. const reportTelemetryEvent = vi.fn();
  189. const service = createPluginManagementService({
  190. zcodeAgentService: {
  191. installPlugin: vi.fn().mockRejectedValue(error),
  192. } as unknown as IZCodeAgentService,
  193. reportTelemetryEvent,
  194. });
  195. await expect(
  196. service.installPlugin({
  197. workspacePath: "/private",
  198. pluginName: "demo",
  199. marketplace: "market",
  200. }),
  201. ).rejects.toBe(error);
  202. expect(reportTelemetryEvent.mock.calls[0][0].eventExtraDetail.result).toBe("cancelled");
  203. expect(JSON.stringify(reportTelemetryEvent.mock.calls)).not.toContain("private");
  204. });
  205. });
  206. it("preserves non-enumerable RPC methods when tagging plugin operations", async () => {
  207. const listPlugins = vi.fn(async () => ({ plugins: [], diagnostics: [] }));
  208. const installPlugin = vi.fn(async () => ({ installedPlugins: [], diagnostics: [] }));
  209. const remote = new Proxy(
  210. {},
  211. { get: (_target, key) => ({ listPlugins, installPlugin })[key as "listPlugins"] },
  212. );
  213. const tagged = withPluginTelemetry(remote as never, {
  214. entry: "store_detail",
  215. client_kind: "desktop",
  216. });
  217. const params = { workspacePath: "/workspace", pluginName: "demo", marketplace: "market" };
  218. await tagged.listPlugins(params);
  219. await tagged.installPlugin(params);
  220. expect(listPlugins).toHaveBeenCalledWith(params);
  221. expect(installPlugin).toHaveBeenCalledWith({
  222. ...params,
  223. telemetry: { entry: "store_detail", client_kind: "desktop" },
  224. });
  225. });