plugin-update-all.fixture.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { useState, type ComponentType } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import type { IBroadcastService, IServiceAccessor } from "@zcode/services";
  4. import { ServiceProvider } from "@/hooks/useServices.js";
  5. import { TabStoreProvider } from "@/store/TabStoreProvider.js";
  6. import { StoreContext } from "@/store/StoreProvider.js";
  7. import { createZCodeStore } from "@/store/index.js";
  8. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  9. import { TooltipProvider } from "@/components/ui/tooltip.js";
  10. import * as section from "@/settings/PluginsSection.js";
  11. import { logger } from "@/logger.js";
  12. import "@/styles.css";
  13. // 浏览器 harness 仅开放现有内部组件,不在产品代码中新增测试入口。
  14. const PluginList = (section as unknown as { PluginList: ComponentType<Record<string, unknown>> })
  15. .PluginList;
  16. const params = new URLSearchParams(location.search);
  17. const store = createZCodeStore({
  18. send: async () => {},
  19. onMessage: () => ({ dispose() {} }),
  20. } as unknown as IBroadcastService);
  21. store.getState().setTheme(params.get("theme") === "light" ? "light" : "dark");
  22. const target = { workspacePath: "/fixture", title: "Fixture" };
  23. const calls: string[] = [];
  24. let release: (() => void) | undefined;
  25. const services = ["original", "replacement"].map((host) => {
  26. const updated = new Set<string>();
  27. const plugins = () =>
  28. ["first", "second"].map((name) => ({
  29. id: `${name}@fixture`,
  30. name,
  31. marketplace: "fixture",
  32. enabled: true,
  33. source: "installed",
  34. rootPath: `/fixture/${name}`,
  35. version: updated.has(name) ? "2.0.0" : "1.0.0",
  36. skillCount: 0,
  37. skillRootCount: 0,
  38. commandRootCount: 0,
  39. components: [],
  40. declaredMcpServerNames: [],
  41. mcpServerNames: [],
  42. enabledSource: "user",
  43. }));
  44. return {
  45. pluginManagementService: {
  46. listPlugins: async () => ({ plugins: plugins(), diagnostics: [] }),
  47. getPluginsOverview: async () => ({
  48. marketplaces: [{ id: "fixture", name: "Fixture", source: {}, pluginCount: 2 }],
  49. availablePlugins: [],
  50. restorableBuiltins: [],
  51. diagnostics: [],
  52. installedPlugins: plugins().map((p) => ({
  53. ...p,
  54. updateStatus: updated.has(p.name) ? "none" : "update-available",
  55. })),
  56. }),
  57. updatePlugin: async ({ pluginId }: { pluginId: string }) => {
  58. calls.push(`${host}:${pluginId}`);
  59. logger.info("[plugin-update-all-fixture] update", { host, pluginId });
  60. document.querySelector('[data-testid="calls"]')!.textContent = JSON.stringify(calls);
  61. if (calls.length === 1)
  62. await new Promise<void>((resolve) => {
  63. release = resolve;
  64. });
  65. updated.add(pluginId.split("@")[0]!);
  66. return { diagnostics: [] };
  67. },
  68. },
  69. } as unknown as IServiceAccessor;
  70. });
  71. function Fixture() {
  72. const [host, setHost] = useState(0);
  73. const [scope, setScope] = useState("user");
  74. const [mounted, setMounted] = useState(true);
  75. return (
  76. <ServiceProvider services={services[host]!}>
  77. <main className="min-h-screen bg-background p-4 text-foreground">
  78. <button data-testid="replace" onClick={() => setHost(1)}>
  79. Replace service
  80. </button>
  81. <button
  82. data-testid="scope"
  83. onClick={() => setScope(scope === "user" ? "workspace" : "user")}
  84. >
  85. Switch scope
  86. </button>
  87. <button data-testid="unmount" onClick={() => setMounted(false)}>
  88. Close
  89. </button>
  90. <button data-testid="release" onClick={() => release?.()}>
  91. Release request
  92. </button>
  93. <output data-testid="calls">{JSON.stringify(calls)}</output>
  94. {mounted && (
  95. <PluginList
  96. target={target}
  97. configScope={scope}
  98. searchQuery=""
  99. isDesktop={false}
  100. isMacDesktop={false}
  101. isWindowsDesktop={false}
  102. isWebRemoteControl={params.get("mobile") === "1"}
  103. unscopedServices={services[host]}
  104. onOpenPluginStore={() => {}}
  105. />
  106. )}
  107. </main>
  108. </ServiceProvider>
  109. );
  110. }
  111. createRoot(document.getElementById("root")!).render(
  112. <StoreContext.Provider value={store}>
  113. <TabStoreProvider>
  114. <ZCodeIntlProvider initialLocale={params.get("locale") === "zh-CN" ? "zh-CN" : "en-US"}>
  115. <TooltipProvider>
  116. <Fixture />
  117. </TooltipProvider>
  118. </ZCodeIntlProvider>
  119. </TabStoreProvider>
  120. </StoreContext.Provider>,
  121. );