pluginStoreSourcesDialog.test.ts 6.3 KB

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