pluginManagementStoreErrorTarget.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // @vitest-environment jsdom
  2. /**
  3. * pluginManagementStore 错误归属(lastFailedPluginId)测试。
  4. *
  5. * 背景(CR-01,2026-08-19):store.error 是插件面所有操作共享的字段
  6. * (marketplace/validate/load/任意插件 setEnabled 失败都会写)。CUA 输入框
  7. * 按钮曾用 `error && cuaPlugin !== undefined` 误映射——无关插件操作失败也会
  8. * 把按钮翻成错误态。修复契约:error 写入时同步记录失败操作的目标插件
  9. * (带 pluginId 的操作写该 id,无插件目标的操作写 null),消费方只认目标为
  10. * 自己的 error。
  11. */
  12. import { beforeEach, describe, expect, it, vi } from "vitest";
  13. vi.mock("@/logger.js", () => ({
  14. logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
  15. }));
  16. const { usePluginManagementStore } = await import("@/store/pluginManagementStore.js");
  17. const WORKSPACE = "/tmp/workspace";
  18. const CUA_PLUGIN_ID = "computer-use@zcode-plugins-official";
  19. const OTHER_PLUGIN_ID = "some-other-plugin@mp";
  20. function resetStore(overrides: Record<string, unknown> = {}): void {
  21. usePluginManagementStore.setState({
  22. workspacePath: WORKSPACE,
  23. workspaceIdentity: null,
  24. plugins: [],
  25. error: null,
  26. lastFailedPluginId: null,
  27. togglingPluginId: null,
  28. operationId: null,
  29. ...overrides,
  30. });
  31. }
  32. function pluginService(overrides: Record<string, unknown> = {}): never {
  33. return {
  34. listPlugins: vi.fn(async () => ({ plugins: [], diagnostics: [] })),
  35. getPluginsOverview: vi.fn(async () => ({
  36. plugins: [],
  37. marketplaces: [],
  38. availablePlugins: [],
  39. installedPlugins: [],
  40. restorableBuiltins: [],
  41. diagnostics: [],
  42. })),
  43. setPluginEnabled: vi.fn(async () => {}),
  44. validatePlugin: vi.fn(async () => ({ diagnostics: [] })),
  45. ...overrides,
  46. } as never;
  47. }
  48. beforeEach(() => {
  49. resetStore();
  50. });
  51. describe("pluginManagementStore 错误归属", () => {
  52. it("setEnabled 失败记录目标插件 id", async () => {
  53. const service = pluginService({
  54. setPluginEnabled: vi.fn(async () => {
  55. throw new Error("boom");
  56. }),
  57. });
  58. const ok = await usePluginManagementStore.getState().setEnabled(OTHER_PLUGIN_ID, true, service);
  59. expect(ok).toBe(false);
  60. const state = usePluginManagementStore.getState();
  61. expect(state.error).toContain("boom");
  62. expect(state.lastFailedPluginId).toBe(OTHER_PLUGIN_ID);
  63. });
  64. it("validateSource 失败(无插件目标)记录 null", async () => {
  65. const service = pluginService({
  66. validatePlugin: vi.fn(async () => {
  67. throw new Error("bad source");
  68. }),
  69. });
  70. await usePluginManagementStore.getState().validateSource("https://example.com/mp", service);
  71. const state = usePluginManagementStore.getState();
  72. expect(state.error).toContain("bad source");
  73. expect(state.lastFailedPluginId).toBeNull();
  74. });
  75. it("新的成功操作清空 error 与归属", async () => {
  76. const failing = pluginService({
  77. setPluginEnabled: vi.fn(async () => {
  78. throw new Error("boom");
  79. }),
  80. });
  81. await usePluginManagementStore.getState().setEnabled(CUA_PLUGIN_ID, true, failing);
  82. expect(usePluginManagementStore.getState().lastFailedPluginId).toBe(CUA_PLUGIN_ID);
  83. // 下一次操作进入时(任何操作开头)先清空,成功后保持为空。
  84. const succeeding = pluginService();
  85. const ok = await usePluginManagementStore
  86. .getState()
  87. .setEnabled(CUA_PLUGIN_ID, true, succeeding);
  88. expect(ok).toBe(true);
  89. const state = usePluginManagementStore.getState();
  90. expect(state.error).toBeNull();
  91. expect(state.lastFailedPluginId).toBeNull();
  92. });
  93. });