pluginStoreSourcesDialog.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { cleanup, render, screen } from "@testing-library/react";
  5. import { afterEach, describe, expect, it } from "vitest";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import { AddMarketplaceSourceDialog } from "@/settings/AddMarketplaceSourceDialog.js";
  8. import {
  9. PluginStoreSourceRefreshFailure,
  10. PluginStoreSourcesDialog,
  11. } from "@/settings/PluginStoreSourcesDialog.js";
  12. afterEach(() => {
  13. cleanup();
  14. });
  15. describe("PluginStoreSourcesDialog", () => {
  16. it("shows the persisted marketplace refresh failure beside its source", () => {
  17. const html = renderToStaticMarkup(
  18. createElement(
  19. ZCodeIntlProvider,
  20. { initialLocale: "en-US" },
  21. createElement(PluginStoreSourceRefreshFailure, {
  22. failure: {
  23. code: "plugin_archive_fetch_failed",
  24. failedAt: "2026-08-06T03:55:00.000Z",
  25. message: "Archive download timed out",
  26. },
  27. }),
  28. ),
  29. );
  30. expect(html).toContain("Archive download timed out");
  31. expect(html).toContain("Refresh failed");
  32. });
  33. // claude-plugins-official 的刷新失败不提示(任何原因);其它来源仍显示失败行。
  34. it("hides the refresh failure of claude-plugins-official but keeps it for other sources", () => {
  35. const failure = {
  36. code: "plugin_marketplace_invalid",
  37. failedAt: "2026-08-06T03:55:00.000Z",
  38. message: "git clone failed",
  39. };
  40. render(
  41. createElement(
  42. ZCodeIntlProvider,
  43. { initialLocale: "en-US" },
  44. createElement(PluginStoreSourcesDialog, {
  45. open: true,
  46. onOpenChange: () => {},
  47. marketplaces: [
  48. {
  49. id: "claude-plugins-official",
  50. name: "claude-plugins-official",
  51. source: {},
  52. pluginCount: 0,
  53. refreshFailure: failure,
  54. },
  55. {
  56. id: "zcode-labs",
  57. name: "zcode-labs",
  58. source: {},
  59. pluginCount: 0,
  60. refreshFailure: failure,
  61. },
  62. ],
  63. onUpdateMarketplace: () => {},
  64. onRemoveMarketplace: () => {},
  65. activeOperations: [],
  66. }),
  67. ),
  68. );
  69. const failures = screen.getAllByTestId("plugin-store-source-refresh-failure");
  70. expect(failures).toHaveLength(1);
  71. expect(
  72. failures[0]!
  73. .closest("[data-testid='plugin-store-source-row']")
  74. ?.getAttribute("data-marketplace-id"),
  75. ).toBe("zcode-labs");
  76. });
  77. it("renders an add failure inside the add marketplace dialog", () => {
  78. render(
  79. createElement(
  80. ZCodeIntlProvider,
  81. { initialLocale: "en-US" },
  82. createElement(AddMarketplaceSourceDialog, {
  83. open: true,
  84. onOpenChange: () => {},
  85. onAddMarketplace: async () => false,
  86. activeOperations: [],
  87. error: "Invalid marketplace source",
  88. }),
  89. ),
  90. );
  91. expect(screen.getByTestId("plugin-store-add-source-dialog")).toBeTruthy();
  92. expect(screen.getByTestId("plugin-store-add-source-error").textContent).toContain(
  93. "Invalid marketplace source",
  94. );
  95. });
  96. it("renders official sources first and custom sources by lastUpdated", () => {
  97. render(
  98. createElement(
  99. ZCodeIntlProvider,
  100. { initialLocale: "en-US" },
  101. createElement(PluginStoreSourcesDialog, {
  102. open: true,
  103. onOpenChange: () => {},
  104. marketplaces: [
  105. {
  106. id: "never-refreshed",
  107. name: "Never Refreshed",
  108. source: {},
  109. pluginCount: 0,
  110. },
  111. {
  112. id: "zcode-plugins-test",
  113. name: "zcode-plugins-test",
  114. source: {},
  115. pluginCount: 0,
  116. lastUpdated: "2026-08-20T12:00:00Z",
  117. },
  118. {
  119. id: "claude-plugins-official",
  120. name: "claude-plugins-official",
  121. source: {},
  122. pluginCount: 0,
  123. lastUpdated: "2026-01-01T00:00:00Z",
  124. },
  125. {
  126. id: "zcode-plugins-official",
  127. name: "zcode-plugins-official",
  128. source: {},
  129. pluginCount: 0,
  130. lastUpdated: "2026-08-20T23:00:00Z",
  131. },
  132. {
  133. id: "zcode-labs",
  134. name: "zcode-labs",
  135. source: {},
  136. pluginCount: 0,
  137. lastUpdated: "2026-08-20T12:00:00Z",
  138. },
  139. ],
  140. onUpdateMarketplace: () => {},
  141. onRemoveMarketplace: () => {},
  142. activeOperations: [],
  143. }),
  144. ),
  145. );
  146. expect(
  147. screen
  148. .getAllByTestId("plugin-store-source-row")
  149. .map((row) => row.getAttribute("data-marketplace-id")),
  150. ).toEqual([
  151. "zcode-plugins-official",
  152. "claude-plugins-official",
  153. "zcode-labs",
  154. "zcode-plugins-test",
  155. "never-refreshed",
  156. ]);
  157. });
  158. });