| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413 |
- // @vitest-environment jsdom
- import { createElement, type ReactNode } from "react";
- import { SWRConfig } from "swr";
- import {
- act,
- cleanup,
- fireEvent,
- render as renderWithTestingLibrary,
- screen,
- waitFor,
- } from "@testing-library/react";
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
- import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
- import { ConversationDraftSuggestedPromptsContainer } from "@/v4/ConversationDraftSuggestedPromptsContainer.js";
- import { advanceComposerDraftRevision } from "@/v4/composer/composerDraftRevision.js";
- const getPluginReferenceCatalog = vi.hoisted(() => vi.fn());
- const resolveSuggestedPluginReference = vi.hoisted(() => vi.fn());
- const installPlugin = vi.hoisted(() => vi.fn());
- const setPluginEnabled = vi.hoisted(() => vi.fn());
- const cancelPluginOperation = vi.hoisted(() => vi.fn());
- const onDynamicPluginOperationProgress = vi.hoisted(() => vi.fn());
- const invalidateDraftSession = vi.hoisted(() => vi.fn());
- const listClientScenes = vi.hoisted(() => vi.fn());
- const loggerWarn = vi.hoisted(() => vi.fn());
- const toast = vi.hoisted(() => vi.fn());
- const reportTelemetryEvent = vi.hoisted(() => vi.fn());
- const dismissToast = vi.hoisted(() => vi.fn());
- const pluginOperationProgressListeners = new Map<
- string,
- Set<(event: { operationId: string; state: "refreshing" }) => void>
- >();
- function emitPluginOperationProgress(operationId: string) {
- for (const listener of pluginOperationProgressListeners.get(operationId) ?? []) {
- listener({ operationId, state: "refreshing" });
- }
- }
- function render(ui: ReactNode) {
- return renderWithTestingLibrary(
- createElement(
- SWRConfig,
- {
- value: {
- provider: () => new Map(),
- revalidateOnFocus: false,
- revalidateOnReconnect: false,
- },
- },
- ui,
- ),
- );
- }
- vi.mock("@/components/ui/toast.js", () => ({ dismissToast, toast }));
- // 推荐容器新增共享偏好读取;单测沿用独立 hook mock,避免依赖真实 ServiceProvider。
- vi.mock("@/hooks/useSettingService.js", () => {
- const value = {
- settings: { proactiveSuggestionsEnabled: false, onboardingOccupation: null },
- update: vi.fn(),
- };
- return { useSettings: () => value };
- });
- vi.mock("@/hooks/usePlatform.js", () => ({
- usePlatform: () => ({ reportTelemetryEvent }),
- }));
- vi.mock("@/lib/zcodeDraftSkillInvalidation.js", () => ({
- invalidateDeferredDraftSessionForSkillChange: invalidateDraftSession,
- }));
- vi.mock("@/logger.js", () => ({
- logger: {
- warn: loggerWarn,
- },
- }));
- vi.mock("@/hooks/useWorkspaceServices.js", () => {
- // 生产 hook 会 memoize resolution/services;测试也必须保持引用稳定,否则 effect setState 后会被假服务引用反复触发。
- const services = {
- clientScenesService: { list: listClientScenes },
- pluginManagementService: {
- cancelPluginOperation,
- getPluginReferenceCatalog,
- installPlugin,
- onDynamicPluginOperationProgress,
- resolveSuggestedPluginReference,
- setPluginEnabled,
- },
- zcodeSessionService: {},
- };
- return {
- useWorkspaceServicesResolution: (
- _workspacePath: string,
- preferredRemoteSessionId?: string,
- ) => ({
- services,
- remoteSessionId: preferredRemoteSessionId ?? null,
- isRemoteTarget: false,
- connectionKind: "local-ready",
- rpcReady: true,
- }),
- };
- });
- vi.mock("@/i18n/IntlProvider.js", () => ({
- useZCodeIntl: () => ({
- locale: "zh-CN",
- intl: {
- formatMessage: ({ id }: { id: string }, values?: { pluginLabel?: string; error?: string }) =>
- id === "chat.draft.suggestedPrompt.pluginFlow.installConfirmation"
- ? `安装${values?.pluginLabel ?? ""}插件`
- : id === "chat.draft.suggestedPrompt.pluginFlow.enableConfirmation"
- ? `开启${values?.pluginLabel ?? ""}插件`
- : id === "chat.draft.suggestedPrompt.pluginFlow.installFailureToast"
- ? `${values?.pluginLabel ?? ""} 安装失败:${values?.error ?? ""}`
- : ({
- "chat.draft.suggestedPrompt.pluginFlow.installing": "正在安装插件…",
- "chat.draft.suggestedPrompt.pluginFlow.enabling": "正在启用插件…",
- "chat.draft.suggestedPrompt.pluginFlow.checking": "正在检查插件状态…",
- "chat.draft.suggestedPrompt.pluginFlow.confirm": "确认",
- "chat.draft.suggestedPrompt.pluginFlow.installSucceeded": "安装成功",
- "chat.draft.suggestedPrompt.pluginFlow.installFailed": "插件安装失败",
- "chat.draft.suggestedPrompt.pluginFlow.installTimedOut": "安装超时",
- "chat.draft.suggestedPrompt.pluginFlow.installReturnedEmpty": "未返回已安装插件",
- "chat.draft.suggestedPrompt.pluginFlow.enableSucceeded": "开启成功",
- "chat.draft.suggestedPrompt.pluginFlow.enableFailed": "插件开启失败",
- }[id] ?? id),
- },
- }),
- }));
- const plainPrompt = "检查当前工作区近 7 天的 Git commit,概括主要改动并指出潜在风险。";
- const pluginPrompt = "根据当前工作区内容制作一份 PDF 文档。";
- const automationsPrompt = "打开自动化面板。";
- const offPeakPrompt = "打开闲时任务面板。";
- const plainLabel = "检查 commit";
- const pluginLabel = "制作 PDF";
- const automationsLabel = "自动化";
- const offPeakLabel = "闲时任务";
- async function applyPendingComposerTextInsert(workspacePath: string, workspaceIdentity?: string) {
- const request = await waitFor(() => {
- const current = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, workspaceIdentity).composerTextInsertRequest;
- if (!current) throw new Error("Composer insert request is not ready");
- return current;
- });
- act(() => {
- useZCodeSessionStore
- .getState()
- .clearComposerTextInsertRequest(workspacePath, request.requestId, workspaceIdentity);
- });
- return request;
- }
- const clientScenesResponse = {
- code: 0,
- msg: "",
- data: [
- {
- namespace: "zcode",
- scene: "draft-suggestion",
- options: {
- prompts: {
- id: "prompt",
- type: "prompt",
- contents: { cn: "提示词", en: "Prompt" },
- prompts: { cn: "", en: "" },
- items: [
- {
- id: "recent-commits",
- type: "prompt",
- contents: { cn: plainPrompt, en: "Check the workspace commits." },
- descs: { cn: "", en: "" },
- labels: { cn: plainLabel, en: "Check commits" },
- on_finish: null,
- img: null,
- share_urls: {},
- },
- {
- id: "create-pdf",
- type: "prompt",
- contents: {
- cn: pluginPrompt,
- en: "Create a PDF from the workspace.",
- },
- descs: { cn: "", en: "" },
- labels: { cn: pluginLabel, en: "Create PDF" },
- on_finish: null,
- img: null,
- share_urls: {},
- defaults: { plugin: ["document-skills"] },
- },
- {
- id: "open-automations",
- type: "prompt",
- contents: {
- cn: automationsPrompt,
- en: "Open Automations.",
- },
- descs: { cn: "", en: "" },
- labels: { cn: automationsLabel, en: "Automations" },
- on_finish: "UNKNOWN:ACTION, NAVIGATE:AUTOMATIONS",
- img: null,
- share_urls: {},
- },
- {
- id: "open-offpeak",
- type: "prompt",
- contents: {
- cn: offPeakPrompt,
- en: "Open Idle-time automations.",
- },
- descs: { cn: "", en: "" },
- labels: { cn: offPeakLabel, en: "Idle-time" },
- on_finish: "NAVIGATE:AUTOMATIONS, NAVIGATE:AUTOMATIONS:OFFPEAK",
- img: null,
- share_urls: {},
- },
- ],
- refer: "",
- cascades: {},
- },
- plugin: {
- id: "plugin",
- type: "plugin",
- contents: { cn: "插件", en: "Plugin" },
- prompts: { cn: "", en: "" },
- items: [
- {
- id: "document-skills",
- type: "plugin",
- contents: {
- cn: "document-skills@zcode-plugins-official",
- en: "document-skills@zcode-plugins-official",
- },
- descs: { cn: "", en: "" },
- labels: { cn: "文档技能", en: "Document skills" },
- on_finish: null,
- img: null,
- share_urls: {},
- },
- ],
- refer: "",
- cascades: {},
- },
- },
- created_at: 1,
- updated_at: 2,
- },
- ],
- };
- describe("ConversationDraftSuggestedPromptsContainer", () => {
- beforeEach(() => {
- getPluginReferenceCatalog.mockReset();
- getPluginReferenceCatalog.mockResolvedValue({ authority: "workspace", plugins: [] });
- resolveSuggestedPluginReference.mockReset();
- resolveSuggestedPluginReference.mockResolvedValue({
- stableId: "document-skills@zcode-plugins-official",
- status: "unavailable",
- diagnostics: [],
- });
- installPlugin.mockReset();
- installPlugin.mockResolvedValue({
- dependencyClosure: ["document-skills@zcode-plugins-official"],
- installedPlugins: [
- {
- id: "document-skills@zcode-plugins-official",
- name: "document-skills",
- marketplace: "zcode-plugins-official",
- enabled: true,
- scope: "user",
- },
- ],
- diagnostics: [],
- });
- setPluginEnabled.mockReset();
- setPluginEnabled.mockResolvedValue({ enabled: true, plugin: {} });
- cancelPluginOperation.mockReset();
- cancelPluginOperation.mockResolvedValue({ operationId: "operation", cancelled: true });
- pluginOperationProgressListeners.clear();
- onDynamicPluginOperationProgress.mockReset();
- onDynamicPluginOperationProgress.mockImplementation(
- (operationId: string) =>
- (listener: (event: { operationId: string; state: "refreshing" }) => void) => {
- const listeners =
- pluginOperationProgressListeners.get(operationId) ??
- new Set<(event: { operationId: string; state: "refreshing" }) => void>();
- listeners.add(listener);
- pluginOperationProgressListeners.set(operationId, listeners);
- return {
- dispose() {
- listeners.delete(listener);
- if (listeners.size === 0) pluginOperationProgressListeners.delete(operationId);
- },
- };
- },
- );
- invalidateDraftSession.mockReset();
- invalidateDraftSession.mockResolvedValue(undefined);
- listClientScenes.mockReset();
- listClientScenes.mockResolvedValue(clientScenesResponse);
- loggerWarn.mockReset();
- toast.mockReset();
- let nextToastId = 1;
- toast.mockImplementation(() => nextToastId++);
- dismissToast.mockReset();
- reportTelemetryEvent.mockReset();
- reportTelemetryEvent.mockResolvedValue(undefined);
- });
- afterEach(() => {
- cleanup();
- });
- it("reserves an in-flow slot for the async list below the composer", async () => {
- const view = render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- className: "mt-3",
- workspacePath: "/tmp/suggested-prompt-layout",
- }),
- );
- await screen.findByRole("button", { name: plainLabel });
- const slot = view.container.firstElementChild;
- expect(slot?.getAttribute("data-v4-draft-suggested-prompts-slot")).toBe("true");
- expect(slot?.classList.contains("h-8")).toBe(true);
- expect(slot?.classList.contains("mt-3")).toBe(true);
- expect(slot?.classList.contains("absolute")).toBe(false);
- expect(slot?.classList.contains("top-full")).toBe(false);
- });
- it("keeps the in-flow slot mounted while the async list is empty", () => {
- listClientScenes.mockImplementation(() => new Promise(() => {}));
- const view = render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath: "/tmp/suggested-prompt-empty-slot",
- }),
- );
- const slot = view.container.firstElementChild;
- expect(slot?.getAttribute("data-v4-draft-suggested-prompts-slot")).toBe("true");
- expect(slot?.classList.contains("h-8")).toBe(true);
- expect(slot?.childElementCount).toBe(0);
- });
- it("keeps the list empty when client scenes only has the legacy ai-writing scene", async () => {
- listClientScenes.mockResolvedValue({
- code: 0,
- msg: "",
- data: [{ ...clientScenesResponse.data[0], scene: "ai-writing" }],
- });
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath: "/tmp/suggested-prompt-scenes",
- }),
- );
- await waitFor(() => expect(listClientScenes).toHaveBeenCalledOnce());
- expect(screen.queryAllByRole("button")).toHaveLength(0);
- });
- it("keeps the list empty and logs a warning when client scenes fails", async () => {
- listClientScenes.mockRejectedValue(new Error("offline"));
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath: "/tmp/suggested-prompt-scenes-error",
- }),
- );
- await waitFor(() => expect(loggerWarn).toHaveBeenCalledOnce());
- expect(screen.queryAllByRole("button")).toHaveLength(0);
- });
- it("filters Automations navigation prompts when the navigation capability is absent", async () => {
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath: "/tmp/suggested-prompt-no-automations",
- }),
- );
- await screen.findByRole("button", { name: plainLabel });
- expect(screen.queryByRole("button", { name: automationsLabel })).toBeNull();
- expect(screen.queryByRole("button", { name: offPeakLabel })).toBeNull();
- });
- it("opens Automations without prefilling or sending", async () => {
- const workspacePath = "/tmp/suggested-prompt-automations";
- const onOpenAutomations = vi.fn();
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- onOpenAutomations,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: automationsLabel }));
- expect(onOpenAutomations).toHaveBeenCalledOnce();
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toBeNull();
- expect(getPluginReferenceCatalog).not.toHaveBeenCalled();
- });
- it("reports a Desktop template click before plugin resolution", async () => {
- const workspacePath = "/tmp/suggested-prompt-desktop-telemetry";
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- isDesktop: true,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- expect(reportTelemetryEvent).toHaveBeenCalledOnce();
- expect(reportTelemetryEvent).toHaveBeenCalledWith(
- expect.objectContaining({
- elementName: "prompt_template_ck",
- eventRegion: "app.session",
- eventType: "ck",
- eventText: pluginLabel,
- eventExtraDetail: {
- template_id: "create-pdf",
- template_prompt: pluginPrompt,
- },
- }),
- );
- });
- it("does not report template clicks on non-Desktop surfaces", async () => {
- const workspacePath = "/tmp/suggested-prompt-web-telemetry";
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- isDesktop: false,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: plainLabel }));
- expect(reportTelemetryEvent).not.toHaveBeenCalled();
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest
- ?.text,
- ).toBe(plainPrompt),
- );
- });
- it("opens the Idle-time Automations tab without prefilling or navigating twice", async () => {
- const workspacePath = "/tmp/suggested-prompt-offpeak";
- const onOpenAutomations = vi.fn();
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- onOpenAutomations,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: offPeakLabel }));
- expect(onOpenAutomations).toHaveBeenCalledOnce();
- expect(onOpenAutomations).toHaveBeenCalledWith("idle");
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toBeNull();
- expect(getPluginReferenceCatalog).not.toHaveBeenCalled();
- });
- it("replaces the workspace draft with the plain commit prompt without sending", async () => {
- const workspacePath = "/tmp/suggested-prompt-plain";
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: plainLabel }));
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toEqual({
- requestId: 1,
- text: plainPrompt,
- }),
- );
- expect(getPluginReferenceCatalog).not.toHaveBeenCalled();
- });
- it("waits for trusted resolution, then inserts Plugin and prompt together once", async () => {
- const workspacePath = "/tmp/suggested-prompt-plugin";
- const workspaceIdentity = "ssh://host/tmp/suggested-prompt-plugin";
- let settleResolution!: (value: Record<string, unknown>) => void;
- resolveSuggestedPluginReference.mockReturnValue(
- new Promise((resolve) => {
- settleResolution = resolve;
- }),
- );
- const insertSpy = vi.spyOn(useZCodeSessionStore.getState(), "requestComposerTextInsert");
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- workspaceIdentity,
- remoteSessionId: "remote-runtime",
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- await waitFor(() => expect(resolveSuggestedPluginReference).toHaveBeenCalledOnce());
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
- .composerTextInsertRequest,
- ).toBeNull();
- expect(insertSpy).not.toHaveBeenCalled();
- expect(toast).not.toHaveBeenCalled();
- expect(screen.queryByText("正在检查插件状态…")).toBeNull();
- expect(document.querySelector('[data-draft-suggested-plugin-popover="progress"]')).toBeNull();
- act(() => {
- settleResolution({
- stableId: "document-skills@zcode-plugins-official",
- status: "ready",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- icon: "https://cdn.example/document-skills.svg",
- diagnostics: [],
- });
- });
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
- .composerTextInsertRequest,
- ).toEqual({
- requestId: 1,
- text: `[@文档技能](plugin://document-skills@zcode-plugins-official) ${pluginPrompt}`,
- mention: expect.objectContaining({
- id: "plugin:document-skills@zcode-plugins-official",
- category: "plugins",
- value: "document-skills@zcode-plugins-official",
- data: {
- pluginId: "document-skills@zcode-plugins-official",
- icon: "https://cdn.example/document-skills.svg",
- },
- }),
- }),
- );
- expect(insertSpy).toHaveBeenCalledOnce();
- expect(resolveSuggestedPluginReference).toHaveBeenCalledWith({
- workspacePath,
- workspaceIdentity,
- remoteSessionId: "remote-runtime",
- stableId: "document-skills@zcode-plugins-official",
- operationId: expect.any(String),
- clientMode: "desktop-continuous",
- deliveryKind: "desktop-continuous",
- });
- expect(resolveSuggestedPluginReference).toHaveBeenCalledOnce();
- expect(getPluginReferenceCatalog).not.toHaveBeenCalled();
- expect(invalidateDraftSession).not.toHaveBeenCalled();
- expect(toast).not.toHaveBeenCalled();
- });
- it("uses the default Plugin icon when trusted resolution has no listing icon", async () => {
- const workspacePath = "/tmp/suggested-prompt-plugin-icon-fallback";
- resolveSuggestedPluginReference.mockResolvedValue({
- stableId: "document-skills@zcode-plugins-official",
- status: "ready",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- });
- render(createElement(ConversationDraftSuggestedPromptsContainer, { workspacePath }));
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest
- ?.mention?.data,
- ).toEqual({ pluginId: "document-skills@zcode-plugins-official" }),
- );
- expect(getPluginReferenceCatalog).not.toHaveBeenCalled();
- });
- it("keeps the plain PDF prompt when trusted resolution fails", async () => {
- const workspacePath = "/tmp/suggested-prompt-fallback";
- resolveSuggestedPluginReference.mockRejectedValue(new Error("disconnected"));
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toEqual({
- requestId: 1,
- text: pluginPrompt,
- }),
- );
- expect(toast).not.toHaveBeenCalled();
- });
- it("rejects an actionable result that does not attest the canonical official source", async () => {
- const workspacePath = "/tmp/suggested-prompt-untrusted-result";
- resolveSuggestedPluginReference.mockResolvedValue({
- stableId: "document-skills@zcode-plugins-official",
- status: "ready",
- marketplace: "personal",
- pluginName: "document-skills",
- diagnostics: [],
- });
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- await waitFor(() => expect(resolveSuggestedPluginReference).toHaveBeenCalledOnce());
- expect(getPluginReferenceCatalog).not.toHaveBeenCalled();
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toEqual({ requestId: 1, text: pluginPrompt });
- expect(toast).not.toHaveBeenCalled();
- });
- it("drops delayed Plugin resolution after the Composer revision changes", async () => {
- const workspacePath = "/tmp/suggested-prompt-plugin-user-edit";
- let settleResolution!: (value: Record<string, unknown>) => void;
- resolveSuggestedPluginReference.mockReturnValue(
- new Promise((resolve) => {
- settleResolution = resolve;
- }),
- );
- render(createElement(ConversationDraftSuggestedPromptsContainer, { workspacePath }));
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- await waitFor(() => expect(resolveSuggestedPluginReference).toHaveBeenCalledOnce());
- expect(toast).not.toHaveBeenCalled();
- await act(async () => {
- advanceComposerDraftRevision(workspacePath);
- settleResolution({
- stableId: "document-skills@zcode-plugins-official",
- status: "missing",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- });
- });
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toBeNull();
- expect(toast).not.toHaveBeenCalled();
- expect(dismissToast).not.toHaveBeenCalled();
- });
- it("installs a refreshed missing candidate in User scope, then prepends the chip", async () => {
- const workspacePath = "/tmp/suggested-prompt-missing";
- const workspaceIdentity = "ssh://host/tmp/suggested-prompt-missing";
- resolveSuggestedPluginReference
- .mockResolvedValueOnce({
- stableId: "document-skills@zcode-plugins-official",
- status: "missing",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- })
- .mockResolvedValueOnce({
- stableId: "document-skills@zcode-plugins-official",
- status: "ready",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- icon: "https://cdn.example/document-skills.svg",
- diagnostics: [],
- });
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- workspaceIdentity,
- remoteSessionId: "remote-runtime",
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
- .composerTextInsertRequest,
- ).toBeNull();
- expect(await applyPendingComposerTextInsert(workspacePath, workspaceIdentity)).toEqual({
- requestId: 1,
- text: pluginPrompt,
- });
- const installAction = await screen.findByRole("button", { name: "确认" });
- const confirmationPopover = document.querySelector<HTMLElement>(
- '[data-draft-suggested-plugin-popover="confirmation"]',
- );
- expect(confirmationPopover?.getAttribute("data-anchor-item-id")).toBe("create-pdf");
- expect(confirmationPopover?.textContent).toContain("安装文档技能插件");
- expect(installPlugin).not.toHaveBeenCalled();
- expect(toast).not.toHaveBeenCalled();
- fireEvent.click(installAction);
- await waitFor(() =>
- expect(installPlugin).toHaveBeenCalledWith({
- workspacePath,
- workspaceIdentity,
- remoteSessionId: "remote-runtime",
- pluginName: "document-skills",
- marketplace: "zcode-plugins-official",
- scope: "user",
- operationId: expect.any(String),
- }),
- );
- expect(await screen.findByText("正在安装插件…")).not.toBeNull();
- expect(
- document.querySelector('[data-draft-suggested-plugin-popover="progress"]'),
- ).not.toBeNull();
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
- .composerTextInsertRequest,
- ).toEqual(
- expect.objectContaining({
- requestId: 2,
- mode: "prepend-if-missing",
- }),
- ),
- );
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
- .composerTextInsertRequest?.mention?.data,
- ).toEqual({
- pluginId: "document-skills@zcode-plugins-official",
- icon: "https://cdn.example/document-skills.svg",
- });
- expect(screen.queryByText("安装成功")).toBeNull();
- vi.useFakeTimers();
- try {
- await act(async () => {
- useZCodeSessionStore
- .getState()
- .clearComposerTextInsertRequest(workspacePath, 2, workspaceIdentity);
- await Promise.resolve();
- await Promise.resolve();
- await Promise.resolve();
- });
- expect(screen.getByText("安装成功")).not.toBeNull();
- expect(
- document.querySelector('[data-draft-suggested-plugin-popover="success"]'),
- ).not.toBeNull();
- await act(async () => {
- vi.advanceTimersByTime(1_999);
- await Promise.resolve();
- });
- expect(screen.getByText("安装成功")).not.toBeNull();
- await act(async () => {
- vi.advanceTimersByTime(1);
- await Promise.resolve();
- });
- expect(screen.queryByText("安装成功")).toBeNull();
- expect(toast).not.toHaveBeenCalled();
- } finally {
- vi.useRealTimers();
- }
- expect(invalidateDraftSession).toHaveBeenCalledOnce();
- expect(resolveSuggestedPluginReference).toHaveBeenCalledTimes(2);
- expect(getPluginReferenceCatalog).not.toHaveBeenCalled();
- });
- it("keeps one Popover from missing-plugin refresh loading through confirmation", async () => {
- const workspacePath = "/tmp/suggested-prompt-stable-popover-anchor";
- let settleResolution!: (value: Record<string, unknown>) => void;
- resolveSuggestedPluginReference.mockReturnValue(
- new Promise((resolve) => {
- settleResolution = resolve;
- }),
- );
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- await waitFor(() => expect(resolveSuggestedPluginReference).toHaveBeenCalledOnce());
- const operationId = resolveSuggestedPluginReference.mock.calls[0]?.[0].operationId as string;
- act(() => emitPluginOperationProgress(operationId));
- expect(await screen.findByText("正在检查插件状态…")).not.toBeNull();
- const loadingPopover = document.querySelector<HTMLElement>(
- '[data-draft-suggested-plugin-popover="progress"]',
- );
- expect(loadingPopover?.getAttribute("data-anchor-item-id")).toBe("create-pdf");
- expect(document.querySelectorAll("[data-draft-suggested-plugin-popover]")).toHaveLength(1);
- act(() => {
- settleResolution({
- stableId: "document-skills@zcode-plugins-official",
- status: "missing",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- });
- });
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toEqual({ requestId: 1, text: pluginPrompt }),
- );
- expect(screen.queryByRole("button", { name: "确认" })).toBeNull();
- act(() => {
- useZCodeSessionStore.getState().clearComposerTextInsertRequest(workspacePath, 1);
- });
- expect(await screen.findByRole("button", { name: "确认" })).not.toBeNull();
- const confirmationPopover = document.querySelector<HTMLElement>(
- '[data-draft-suggested-plugin-popover="confirmation"]',
- );
- expect(confirmationPopover).toBe(loadingPopover);
- expect(document.querySelectorAll("[data-draft-suggested-plugin-popover]")).toHaveLength(1);
- });
- it("enables an installed disabled Plugin before inserting the chip", async () => {
- const workspacePath = "/tmp/suggested-prompt-disabled";
- resolveSuggestedPluginReference
- .mockResolvedValueOnce({
- stableId: "document-skills@zcode-plugins-official",
- status: "disabled",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- })
- .mockResolvedValueOnce({
- stableId: "document-skills@zcode-plugins-official",
- status: "ready",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- icon: "https://cdn.example/document-skills.svg",
- diagnostics: [],
- });
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toEqual({ requestId: 1, text: pluginPrompt }),
- );
- expect(await screen.findByText("正在检查插件状态…")).not.toBeNull();
- const checkingPopover = document.querySelector<HTMLElement>(
- '[data-draft-suggested-plugin-popover="progress"]',
- );
- expect(document.querySelectorAll("[data-draft-suggested-plugin-popover]")).toHaveLength(1);
- expect(screen.queryByRole("button", { name: "确认" })).toBeNull();
- act(() => {
- useZCodeSessionStore.getState().clearComposerTextInsertRequest(workspacePath, 1);
- });
- const enableAction = await screen.findByRole("button", { name: "确认" });
- const confirmationPopover = document.querySelector<HTMLElement>(
- '[data-draft-suggested-plugin-popover="confirmation"]',
- );
- expect(confirmationPopover).toBe(checkingPopover);
- expect(confirmationPopover?.getAttribute("data-anchor-item-id")).toBe("create-pdf");
- expect(confirmationPopover?.textContent).toContain("开启文档技能插件");
- expect(toast).not.toHaveBeenCalled();
- fireEvent.click(enableAction);
- await waitFor(() =>
- expect(setPluginEnabled).toHaveBeenCalledWith({
- workspacePath,
- pluginId: "document-skills@zcode-plugins-official",
- enabled: true,
- operationId: expect.any(String),
- }),
- );
- expect(await screen.findByText("正在启用插件…")).not.toBeNull();
- expect(
- document.querySelector('[data-draft-suggested-plugin-popover="progress"]'),
- ).not.toBeNull();
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest
- ?.requestId,
- ).toBe(2),
- );
- expect(screen.queryByText("安装成功")).toBeNull();
- act(() => {
- useZCodeSessionStore.getState().clearComposerTextInsertRequest(workspacePath, 2);
- });
- expect(screen.queryByText("安装成功")).toBeNull();
- expect(await screen.findByText("开启成功")).not.toBeNull();
- expect(
- document.querySelector('[data-draft-suggested-plugin-popover="success"]'),
- ).not.toBeNull();
- expect(toast).not.toHaveBeenCalled();
- expect(getPluginReferenceCatalog).not.toHaveBeenCalled();
- });
- it("replaces the persistent enablement Popover with failure and never calls toast", async () => {
- const workspacePath = "/tmp/suggested-prompt-enable-failed";
- resolveSuggestedPluginReference.mockResolvedValue({
- stableId: "document-skills@zcode-plugins-official",
- status: "disabled",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- });
- setPluginEnabled.mockRejectedValue(new Error("enable failed"));
- render(createElement(ConversationDraftSuggestedPromptsContainer, { workspacePath }));
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- await applyPendingComposerTextInsert(workspacePath);
- fireEvent.click(await screen.findByRole("button", { name: "确认" }));
- expect(await screen.findByText("正在启用插件…")).not.toBeNull();
- expect(await screen.findByText("插件开启失败")).not.toBeNull();
- expect(document.querySelector('[data-draft-suggested-plugin-popover="error"]')).not.toBeNull();
- expect(toast).not.toHaveBeenCalled();
- });
- it.each(["missing", "disabled"] as const)(
- "keeps a %s Plugin confirmation open without a timeout and cancels it on outside pointer down",
- async (status) => {
- const workspacePath = `/tmp/suggested-prompt-${status}-confirmation-outside`;
- resolveSuggestedPluginReference.mockResolvedValue({
- stableId: "document-skills@zcode-plugins-official",
- status,
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- });
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- }),
- );
- await screen.findByRole("button", { name: pluginLabel });
- vi.useFakeTimers();
- try {
- fireEvent.click(screen.getByRole("button", { name: pluginLabel }));
- await act(async () => {
- await Promise.resolve();
- await Promise.resolve();
- });
- const plainRequest = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath).composerTextInsertRequest;
- expect(plainRequest).toEqual({ requestId: 1, text: pluginPrompt });
- act(() => {
- useZCodeSessionStore
- .getState()
- .clearComposerTextInsertRequest(workspacePath, plainRequest!.requestId);
- });
- await act(async () => {
- await Promise.resolve();
- });
- expect(screen.getByRole("button", { name: "确认" })).not.toBeNull();
- await act(async () => {
- vi.advanceTimersByTime(60_000);
- await Promise.resolve();
- await Promise.resolve();
- });
- expect(installPlugin).not.toHaveBeenCalled();
- expect(setPluginEnabled).not.toHaveBeenCalled();
- expect(cancelPluginOperation).not.toHaveBeenCalled();
- expect(screen.getByRole("button", { name: "确认" })).not.toBeNull();
- fireEvent.pointerDown(document.body);
- await act(async () => {
- await Promise.resolve();
- await Promise.resolve();
- });
- expect(cancelPluginOperation).toHaveBeenCalledOnce();
- expect(screen.queryByRole("button", { name: "确认" })).toBeNull();
- expect(toast).not.toHaveBeenCalled();
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath)
- .composerTextInsertRequest,
- ).toBeNull();
- } finally {
- vi.useRealTimers();
- }
- },
- );
- it("closes a confirmation and still applies the recommendation clicked outside it", async () => {
- const workspacePath = "/tmp/suggested-prompt-confirmation-outside-recommendation";
- resolveSuggestedPluginReference.mockResolvedValue({
- stableId: "document-skills@zcode-plugins-official",
- status: "missing",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- });
- render(createElement(ConversationDraftSuggestedPromptsContainer, { workspacePath }));
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- await applyPendingComposerTextInsert(workspacePath);
- expect(await screen.findByRole("button", { name: "确认" })).not.toBeNull();
- const plainRecommendation = screen.getByRole("button", { name: plainLabel });
- fireEvent.pointerDown(plainRecommendation);
- fireEvent.click(plainRecommendation);
- await waitFor(() => expect(cancelPluginOperation).toHaveBeenCalledOnce());
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toEqual({ requestId: 2, text: plainPrompt }),
- );
- expect(screen.queryByRole("button", { name: "确认" })).toBeNull();
- expect(installPlugin).not.toHaveBeenCalled();
- });
- it("restores the same confirmation Popover after installation failure and retries with a new operation", async () => {
- const workspacePath = "/tmp/suggested-prompt-install-failed";
- let settleInstall!: (value: Record<string, unknown>) => void;
- resolveSuggestedPluginReference.mockResolvedValue({
- stableId: "document-skills@zcode-plugins-official",
- status: "missing",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- });
- installPlugin
- .mockReturnValueOnce(
- new Promise((resolve) => {
- settleInstall = resolve;
- }),
- )
- .mockReturnValueOnce(
- new Promise(() => {
- // 第二次安装保持 pending,用于验证恢复后的确认按钮确实可重试。
- }),
- );
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- expect(await applyPendingComposerTextInsert(workspacePath)).toEqual({
- requestId: 1,
- text: pluginPrompt,
- });
- fireEvent.click(await screen.findByRole("button", { name: "确认" }));
- expect(await screen.findByText("正在安装插件…")).not.toBeNull();
- const progressPopover = document.querySelector(
- '[data-draft-suggested-plugin-popover="progress"]',
- );
- act(() => {
- settleInstall({
- dependencyClosure: [],
- installedPlugins: [],
- diagnostics: [{ code: "install_failed", message: "offline", severity: "error" }],
- });
- });
- const retryAction = await screen.findByRole("button", { name: "确认" });
- const retryPopover = document.querySelector(
- '[data-draft-suggested-plugin-popover="confirmation"]',
- );
- expect(retryPopover).toBe(progressPopover);
- expect(screen.queryByText("插件安装失败")).toBeNull();
- expect(document.querySelector('[data-draft-suggested-plugin-popover="error"]')).toBeNull();
- expect(toast).toHaveBeenCalledWith("文档技能 安装失败:offline", {
- variant: "warning",
- });
- expect(loggerWarn).toHaveBeenCalledWith(
- "[v4-suggested-prompts] 推荐插件安装失败,恢复确认态以便重试",
- expect.objectContaining({
- error: "offline",
- operationId: expect.any(String),
- pluginId: "document-skills@zcode-plugins-official",
- workspaceKey: workspacePath,
- }),
- );
- const firstOperationId = installPlugin.mock.calls[0]?.[0]?.operationId;
- fireEvent.click(retryAction);
- await waitFor(() => expect(installPlugin).toHaveBeenCalledTimes(2));
- expect(installPlugin.mock.calls[1]?.[0]?.operationId).not.toBe(firstOperationId);
- expect(await screen.findByText("正在安装插件…")).not.toBeNull();
- expect(getPluginReferenceCatalog).not.toHaveBeenCalled();
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toBeNull();
- });
- it("fails a plugin installation at ten seconds, cancels it, and keeps the plain prompt", async () => {
- const workspacePath = "/tmp/suggested-prompt-install-timeout";
- let settleLateInstall!: (value: Record<string, unknown>) => void;
- resolveSuggestedPluginReference.mockResolvedValue({
- stableId: "document-skills@zcode-plugins-official",
- status: "missing",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- });
- installPlugin.mockReturnValue(
- new Promise((resolve) => {
- settleLateInstall = resolve;
- }),
- );
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- }),
- );
- await screen.findByRole("button", { name: pluginLabel });
- vi.useFakeTimers();
- try {
- fireEvent.click(screen.getByRole("button", { name: pluginLabel }));
- await act(async () => {
- await Promise.resolve();
- await Promise.resolve();
- });
- const plainRequest = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath).composerTextInsertRequest;
- expect(plainRequest).toEqual({ requestId: 1, text: pluginPrompt });
- act(() => {
- useZCodeSessionStore
- .getState()
- .clearComposerTextInsertRequest(workspacePath, plainRequest!.requestId);
- });
- await act(async () => {
- await Promise.resolve();
- });
- await vi.waitFor(() => expect(screen.getByRole("button", { name: "确认" })).not.toBeNull());
- fireEvent.click(screen.getByRole("button", { name: "确认" }));
- expect(installPlugin).toHaveBeenCalledOnce();
- expect(screen.getByText("正在安装插件…")).not.toBeNull();
- await act(async () => {
- vi.advanceTimersByTime(9_999);
- await Promise.resolve();
- });
- expect(cancelPluginOperation).not.toHaveBeenCalled();
- expect(screen.queryByRole("button", { name: "确认" })).toBeNull();
- await act(async () => {
- vi.advanceTimersByTime(1);
- await Promise.resolve();
- await Promise.resolve();
- await Promise.resolve();
- });
- expect(cancelPluginOperation).toHaveBeenCalledWith({ operationId: expect.any(String) });
- expect(screen.getByRole("button", { name: "确认" })).not.toBeNull();
- expect(
- document.querySelector('[data-draft-suggested-plugin-popover="confirmation"]'),
- ).not.toBeNull();
- expect(document.querySelector('[data-draft-suggested-plugin-popover="error"]')).toBeNull();
- expect(toast).toHaveBeenCalledWith("文档技能 安装失败:安装超时", {
- variant: "warning",
- });
- expect(loggerWarn).toHaveBeenCalledWith(
- "[v4-suggested-prompts] 推荐插件安装失败,恢复确认态以便重试",
- expect.objectContaining({
- error: "安装超时",
- operationId: expect.any(String),
- pluginId: "document-skills@zcode-plugins-official",
- workspaceKey: workspacePath,
- }),
- );
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toBeNull();
- await act(async () => {
- settleLateInstall({
- dependencyClosure: ["document-skills@zcode-plugins-official"],
- installedPlugins: [
- {
- id: "document-skills@zcode-plugins-official",
- name: "document-skills",
- marketplace: "zcode-plugins-official",
- enabled: true,
- scope: "user",
- },
- ],
- diagnostics: [],
- });
- await Promise.resolve();
- await Promise.resolve();
- });
- expect(resolveSuggestedPluginReference).toHaveBeenCalledOnce();
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toBeNull();
- expect(screen.queryByText("安装成功")).toBeNull();
- expect(screen.getByRole("button", { name: "确认" })).not.toBeNull();
- } finally {
- vi.useRealTimers();
- }
- });
- it("keeps the pure prompt and does not offer installation when refresh fails closed", async () => {
- const workspacePath = "/tmp/suggested-prompt-refresh-failed";
- resolveSuggestedPluginReference.mockResolvedValue({
- stableId: "document-skills@zcode-plugins-official",
- status: "unavailable",
- diagnostics: [
- {
- code: "marketplace_refresh_failed",
- message: "offline",
- severity: "error",
- },
- ],
- });
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- isWebRemoteControl: true,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- await waitFor(() => expect(resolveSuggestedPluginReference).toHaveBeenCalledOnce());
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toEqual({ requestId: 1, text: pluginPrompt });
- expect(toast).not.toHaveBeenCalled();
- expect(resolveSuggestedPluginReference).toHaveBeenCalledWith(
- expect.objectContaining({
- clientMode: "web-remote-replayable",
- deliveryKind: "web-remote-replayable",
- }),
- );
- });
- it("cancels the older operation, disables recommendations until cancellation settles, and drops late results", async () => {
- const workspacePath = "/tmp/suggested-prompt-cancel";
- let resolveOld!: (value: Record<string, unknown>) => void;
- let resolveCancel!: (value: Record<string, unknown>) => void;
- resolveSuggestedPluginReference.mockResolvedValue({
- stableId: "document-skills@zcode-plugins-official",
- status: "missing",
- marketplace: "zcode-plugins-official",
- pluginName: "document-skills",
- sourceTrust: "official",
- diagnostics: [],
- });
- installPlugin.mockReturnValue(
- new Promise((resolve) => {
- resolveOld = resolve;
- }),
- );
- cancelPluginOperation.mockReturnValue(
- new Promise((resolve) => {
- resolveCancel = resolve;
- }),
- );
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- expect(await applyPendingComposerTextInsert(workspacePath)).toEqual({
- requestId: 1,
- text: pluginPrompt,
- });
- fireEvent.click(await screen.findByRole("button", { name: "确认" }));
- await waitFor(() => expect(installPlugin).toHaveBeenCalledOnce());
- expect(await screen.findByText("正在安装插件…")).not.toBeNull();
- fireEvent.click(
- screen.getByRole("button", {
- name: plainLabel,
- }),
- );
- await waitFor(() =>
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest
- ?.text,
- ).toBe(plainPrompt),
- );
- await waitFor(() => expect(cancelPluginOperation).toHaveBeenCalledOnce());
- expect(screen.queryByText("正在安装插件…")).toBeNull();
- expect((screen.getByRole("button", { name: pluginLabel }) as HTMLButtonElement).disabled).toBe(
- true,
- );
- expect((screen.getByRole("button", { name: plainLabel }) as HTMLButtonElement).disabled).toBe(
- true,
- );
- await act(async () => {
- resolveCancel({ operationId: "old", cancelled: true });
- await Promise.resolve();
- });
- expect((screen.getByRole("button", { name: pluginLabel }) as HTMLButtonElement).disabled).toBe(
- true,
- );
- expect((screen.getByRole("button", { name: plainLabel }) as HTMLButtonElement).disabled).toBe(
- true,
- );
- await act(async () => {
- resolveOld({
- dependencyClosure: ["document-skills@zcode-plugins-official"],
- installedPlugins: [
- {
- id: "document-skills@zcode-plugins-official",
- name: "document-skills",
- marketplace: "zcode-plugins-official",
- enabled: true,
- scope: "user",
- },
- ],
- diagnostics: [],
- });
- await Promise.resolve();
- });
- await waitFor(() =>
- expect(
- (screen.getByRole("button", { name: pluginLabel }) as HTMLButtonElement).disabled,
- ).toBe(false),
- );
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toEqual({
- requestId: 2,
- text: plainPrompt,
- });
- expect(getPluginReferenceCatalog).not.toHaveBeenCalled();
- expect(
- toast.mock.calls.some(
- ([message]) => String(message).includes("取消") || String(message).includes("中断"),
- ),
- ).toBe(false);
- expect(document.querySelector("[data-draft-suggested-plugin-popover]")).toBeNull();
- });
- it("invalidates an older Plugin prefill when a later click navigates to Automations", async () => {
- const workspacePath = "/tmp/suggested-prompt-navigation-latest-wins";
- const onOpenAutomations = vi.fn();
- let resolveCatalog!: (value: { authority: "workspace"; plugins: [] }) => void;
- getPluginReferenceCatalog.mockReturnValue(
- new Promise((resolve) => {
- resolveCatalog = resolve;
- }),
- );
- render(
- createElement(ConversationDraftSuggestedPromptsContainer, {
- workspacePath,
- onOpenAutomations,
- }),
- );
- fireEvent.click(await screen.findByRole("button", { name: pluginLabel }));
- fireEvent.click(screen.getByRole("button", { name: automationsLabel }));
- expect(onOpenAutomations).toHaveBeenCalledOnce();
- await act(async () => {
- resolveCatalog({ authority: "workspace", plugins: [] });
- await Promise.resolve();
- });
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).composerTextInsertRequest,
- ).toBeNull();
- expect(toast).not.toHaveBeenCalled();
- });
- });
|