import { createElement, type ReactNode } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { ZCodeAvailablePluginSummary, ZCodePluginInfo } from "@zcode/shared"; import type { StorePluginItem } from "../src/settings/pluginStoreListing.js"; import type { PluginStoreActions } from "../src/settings/PluginStoreCard.js"; // 复用 installedPluginManagementUpdate.test.ts 的「mock Button 捕获 onClick」套路: // SSR 渲染时桩组件把 props 入栈,渲染后直接调用捕获到的按钮 onClick, // 确定性验证回调——无需真实 DOM/卸载/计时器。 const { buttonProps } = vi.hoisted(() => ({ buttonProps: [] as Array>, })); vi.mock("@/components/ui/button.js", () => ({ Button: ({ children, ...props }: { children?: ReactNode }) => { buttonProps.push(props); return createElement("button", props as Record, children); }, })); function restorableItem(overrides: Partial = {}): StorePluginItem { const summary: ZCodeAvailablePluginSummary = { id: "skill-creator@zcode-plugins-official", name: "skill-creator", marketplace: "zcode-plugins-official", version: "1.0.0", installed: false, }; return { id: summary.id, name: summary.name, marketplace: summary.marketplace, installed: false, restorable: true, orphaned: false, summary, ...overrides, }; } function makeActions(overrides: Partial = {}): PluginStoreActions { return { onOpenDetail: () => {}, onInstall: () => {}, onUninstall: () => {}, onSetEnabled: () => {}, onUpdate: () => {}, activeOperations: [], togglingPluginId: null, ...overrides, }; } function installedItemWithRuntimeInfo(): StorePluginItem { const item = restorableItem({ installed: true, restorable: false }); const info: ZCodePluginInfo = { id: item.id, name: item.name, marketplace: item.marketplace, enabled: true, source: "official", skillCount: 1, skillRootCount: 1, commandRootCount: 0, declaredMcpServerNames: [], mcpServerNames: [], rootPath: "/cache/skill-creator", }; return { ...item, info }; } async function renderCard(item: StorePluginItem, actions: PluginStoreActions): Promise { const { PluginStoreCard } = await import("../src/settings/PluginStoreCard.js"); const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js"); return renderToStaticMarkup( createElement( ZCodeIntlProvider, { initialLocale: "en-US" }, createElement(PluginStoreCard, { item, actions, locale: "en-US" }), ), ); } async function renderDetail( item: StorePluginItem, actions: PluginStoreActions, onUsePrompt: (item: StorePluginItem, prompt: string) => void = () => {}, ): Promise { const { PluginStoreDetailView } = await import("../src/settings/PluginStoreDetailView.js"); const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js"); return renderToStaticMarkup( createElement( ZCodeIntlProvider, { initialLocale: "en-US" }, createElement(PluginStoreDetailView, { item, actions, onBack: () => {}, onRetryDescribe: () => {}, onUsePrompt, }), ), ); } describe("PluginStoreCard restorable built-ins", () => { afterEach(() => { buttonProps.length = 0; }); it("renders an install button for an uninstalled (restorable) built-in", async () => { const html = await renderCard(restorableItem(), makeActions()); expect(html).toContain("skill-creator"); expect(html).toContain("Install"); }); it.each([true, false])( "side pane market adds an open button for installed UI plugins with enabled=%s", async (enabled) => { const item = installedItemWithRuntimeInfo(); item.info!.enabled = enabled; item.info!.uiSurfaces = [ { pluginId: item.id, pluginName: item.name, id: "panel", title: "Panel", server: "panel", resourceUri: "ui://panel/app", availability: "session", }, ]; const onOpenUi = vi.fn(); const html = await renderCard(item, makeActions({ onOpenUi })); expect(html).toContain(enabled ? "Open" : "Enable"); // 卡片整行进入详情,不再提供独立「详情」按钮。 expect(html).not.toContain('data-testid="plugin-store-ui-details"'); const button = buttonProps.find((props) => props["data-testid"] === "plugin-store-open-ui"); expect(button).toBeDefined(); (button!.onClick as (event: { stopPropagation: () => void }) => void)({ stopPropagation: () => {}, }); expect(onOpenUi).toHaveBeenCalledWith(item); }, ); it("main navigation market has no UI open or details buttons", async () => { const item = installedItemWithRuntimeInfo(); item.info!.uiSurfaces = [ { pluginId: item.id, pluginName: item.name, id: "panel", title: "Panel", server: "panel", resourceUri: "ui://panel/app", availability: "session", }, ]; const card = await renderCard(item, makeActions()); expect(card).not.toContain('data-testid="plugin-store-open-ui"'); expect(card).not.toContain('data-testid="plugin-store-ui-details"'); // 主导航详情不打开 UI,恢复普通插件的「立即试用」。 const detail = await renderDetail(item, makeActions(), vi.fn()); expect(detail).not.toContain('data-testid="plugin-store-open-ui"'); expect(detail).toContain('data-testid="plugin-store-try-now"'); }); it("shows a UI opening action and capability in details without creating a chat", async () => { const item = installedItemWithRuntimeInfo(); item.info!.uiSurfaces = [ { pluginId: item.id, pluginName: item.name, id: "panel", title: "Panel", server: "panel", resourceUri: "ui://panel/app", availability: "session", }, ]; const onOpenUi = vi.fn(); const onUsePrompt = vi.fn(); const html = await renderDetail(item, makeActions({ onOpenUi }), onUsePrompt); expect(html).toContain('data-testid="plugin-store-ui-capability"'); expect(html).not.toContain('data-testid="plugin-store-try-now"'); const button = buttonProps.find((props) => props["data-testid"] === "plugin-store-open-ui"); (button?.onClick as (event: { stopPropagation: () => void }) => void)({ stopPropagation: () => {}, }); expect(onOpenUi).toHaveBeenCalledWith(item); expect(onUsePrompt).not.toHaveBeenCalled(); }); it("keeps project reset accessible outside the more menu", async () => { const item = installedItemWithRuntimeInfo(); const onResetConfig = vi.fn(); await renderDetail(item, makeActions({ onResetConfig })); const button = buttonProps.find( (props) => props["data-testid"] === "plugin-store-reset-config", ); (button?.onClick as () => void)(); expect(onResetConfig).toHaveBeenCalledWith(item.id); }); it("keeps project reset enabled while another plugin installs and disables it for its own reset", async () => { const item = installedItemWithRuntimeInfo(); const resetButton = () => buttonProps.findLast((props) => props["data-testid"] === "plugin-store-reset-config"); await renderDetail( item, makeActions({ onResetConfig: () => {}, activeOperations: [{ id: "plugin:install:other@personal" }], }), ); expect(resetButton()?.disabled).toBe(false); await renderDetail( item, makeActions({ onResetConfig: () => {}, activeOperations: [{ id: `plugin:reset-config:${item.id}` }], }), ); expect(resetButton()?.disabled).toBe(true); }); it("disables project reset while the same plugin saves its config", async () => { const item = installedItemWithRuntimeInfo(); const resetButton = () => buttonProps.findLast((props) => props["data-testid"] === "plugin-store-reset-config"); await renderDetail( item, makeActions({ onResetConfig: () => {}, activeOperations: [{ id: `plugin:configure:${item.id}` }], }), ); expect(resetButton()?.disabled).toBe(true); await renderDetail( item, makeActions({ onResetConfig: () => {}, activeOperations: [{ id: "plugin:configure:other@personal" }], }), ); expect(resetButton()?.disabled).toBe(false); }); it("invokes onInstall with the restorable item when the install button is clicked", async () => { const onInstall = vi.fn(); await renderCard(restorableItem(), makeActions({ onInstall })); const clickHandlers = buttonProps .map((props) => props.onClick) .filter((onClick): onClick is (event?: unknown) => void => typeof onClick === "function"); expect(clickHandlers.length).toBeGreaterThan(0); for (const onClick of clickHandlers) { // 安装按钮的 onClick 依赖事件对象做 stopPropagation,给一个最小事件桩。 onClick({ stopPropagation: () => {} }); } expect(onInstall).toHaveBeenCalledTimes(1); expect(onInstall.mock.calls[0]?.[0]).toMatchObject({ id: "skill-creator@zcode-plugins-official", restorable: true, }); }); it("keeps the management menu when an installed plugin has no runtime info", async () => { const item = restorableItem({ installed: true, restorable: false }); const html = await renderCard(item, makeActions()); expect(html).not.toContain("Install"); expect(html).toContain('data-testid="plugin-store-item-menu"'); }); it("keeps the detail management menu when an installed plugin has no runtime info", async () => { const item = restorableItem({ installed: true, restorable: false }); const html = await renderDetail(item, makeActions()); expect(html).not.toContain("Install"); expect(html).toContain('data-testid="plugin-store-item-menu"'); }); it("shows Try now for an installed plugin without runtime info and delegates an empty prompt", async () => { const item = restorableItem({ installed: true, restorable: false }); const onUsePrompt = vi.fn(); const html = await renderDetail(item, makeActions(), onUsePrompt); expect(html).toContain('data-testid="plugin-store-try-now"'); expect(html).toContain("Try now"); const tryNowButton = buttonProps.find( (props) => props["data-testid"] === "plugin-store-try-now", ); expect(tryNowButton?.onClick).toBeTypeOf("function"); (tryNowButton?.onClick as (() => void) | undefined)?.(); expect(onUsePrompt).toHaveBeenCalledWith(item, ""); }); it("keeps Try now hidden while an uninstalled plugin shows Install", async () => { const html = await renderDetail(restorableItem(), makeActions()); expect(html).not.toContain('data-testid="plugin-store-try-now"'); expect(html).toContain("Install"); }); it("keeps Workspace reset outside the menu for a missing package", async () => { const html = await renderDetail( restorableItem({ info: { id: "skill-creator@zcode-plugins-official", name: "skill-creator", marketplace: "zcode-plugins-official", enabled: true, source: "missing", packageStatus: "missing", skillRootCount: 0, commandRootCount: 0, mcpServerNames: [], rootPath: "", }, }), makeActions({ onResetConfig: () => {} }), ); expect(html).toContain('data-testid="plugin-store-reset-config"'); expect(html).not.toContain('data-testid="plugin-store-menu-uninstall"'); expect(html).toContain("Install"); }); it("keeps installed card and detail enablement inside the actions menu", async () => { const item = installedItemWithRuntimeInfo(); const cardHtml = await renderCard(item, makeActions()); const detailHtml = await renderDetail(item, makeActions()); expect(cardHtml).toContain('data-testid="plugin-store-item-menu"'); expect(cardHtml).not.toContain('data-testid="plugin-store-enabled-switch"'); expect(detailHtml).not.toContain('data-testid="plugin-store-enabled-switch"'); expect(detailHtml).toContain('data-testid="plugin-store-item-menu"'); }); });