| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349 |
- /* eslint-disable max-lines -- 插件 lifecycle E2E 的确定性文件夹 fixture、UI 驱动和第二层证据集中维护,避免各 pending spec 重复脆弱 DOM/存储逻辑。 */
- import { existsSync } from "node:fs";
- import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
- import { dirname, join } from "node:path";
- import {
- type ZCodePluginsInstallResult,
- type ZCodePluginsListResult,
- type ZCodePluginsMarketplaceMutationResult,
- type ZCodePluginsOverviewResult,
- type ZCodePluginsReferenceCatalogResult,
- } from "@zcode/shared";
- import {
- TID_PLUGIN_STORE_BROWSE,
- TID_SETTINGS_PAGE,
- TID_SETTINGS_SECTION_NAV,
- TID_TASK_SETTINGS_BUTTON,
- testId,
- } from "@zcode/shared/test-ids";
- import {
- DEFAULT_WORKSPACE,
- clickTestIdByDom,
- getE2EAppDataPaths,
- setCurrentElectronRendererContentSize,
- setInputValueByTestIdDom,
- waitForDefaultWorkspaceReady,
- waitForTestIdByDom,
- waitForWorkspaceApp,
- } from "./desktop-app.js";
- import { dismissOccupationOnboarding } from "#e2e/helpers/occupation-onboarding";
- export const PLUGIN_LIFECYCLE_MARKETPLACE_ID = "e2e-plugin-lifecycle-market";
- export const PLUGIN_LIFECYCLE_MARKETPLACE_NAME = "E2E Plugin Lifecycle Market";
- export const PLUGIN_LIFECYCLE_NAME = "e2e-plugin-lifecycle";
- export const PLUGIN_LIFECYCLE_ID = `${PLUGIN_LIFECYCLE_NAME}@${PLUGIN_LIFECYCLE_MARKETPLACE_ID}`;
- export const PLUGIN_LIFECYCLE_EXAMPLE_PROMPT =
- "E2E_PLUGIN_LIFECYCLE_EXAMPLE_PROMPT: summarize the fixture lifecycle";
- export const PLUGIN_LIFECYCLE_CONFIG_KEY = "display_label";
- export const PLUGIN_LIFECYCLE_CONFIG_VALUE = "configured-by-plugin-lifecycle-e2e";
- export const PLUGIN_LIFECYCLE_INHERITED_CONFIG_KEY = "timeout_seconds";
- export const PLUGIN_LIFECYCLE_MCP_TOOL_NAME =
- "mcp__plugin_e2e-plugin-lifecycle_lifecycle__version_marker";
- export const PLUGIN_LIFECYCLE_RUNTIME_MARKER = "E2E_PLUGIN_LIFECYCLE_RUNTIME";
- export const PLUGIN_LIFECYCLE_EXTRA_NAME = "e2e-lifecycle-extra-1";
- export const PLUGIN_LIFECYCLE_EXTRA_ID = `${PLUGIN_LIFECYCLE_EXTRA_NAME}@${PLUGIN_LIFECYCLE_MARKETPLACE_ID}`;
- export const PLUGIN_INLINE_NAME = "e2e-inline-lifecycle";
- export const PLUGIN_INLINE_ID = `${PLUGIN_INLINE_NAME}@inline`;
- export const PLUGIN_INLINE_SUBAGENT_NAME = `${PLUGIN_INLINE_NAME}:inline-reviewer`;
- export interface PluginLifecycleFixture {
- marketplaceRoot: string;
- pluginRoot: string;
- version: string;
- }
- type PluginTestActionName =
- | "addPluginMarketplace"
- | "getPluginsOverview"
- | "installPlugin"
- | "listPlugins"
- | "getPluginReferenceCatalog"
- | "updatePluginMarketplace";
- interface SerializedActionResult<T> {
- ok: boolean;
- error?: string;
- value?: T;
- }
- interface PluginControlAttributes {
- componentKind?: string;
- configKey?: string;
- groupKey?: string;
- marketplaceId?: string;
- pluginId?: string;
- }
- export function requirePluginManualReview(): void {
- const runningPendingSpec = process.argv.some((arg) =>
- arg.replaceAll("\\", "/").includes("/plugins/manual-review/"),
- );
- if (runningPendingSpec && process.env.ZCODE_E2E_MANUAL_REVIEW !== "1") {
- throw new Error("Plugin lifecycle pending specs require ZCODE_E2E_MANUAL_REVIEW=1");
- }
- }
- export async function createPluginLifecycleFixture(
- version = "1.0.0",
- withUi = false,
- ): Promise<PluginLifecycleFixture> {
- const { homeDir } = getE2EAppDataPaths();
- const marketplaceRoot = join(homeDir, "fixtures", "plugin-management-lifecycle-market");
- const pluginRoot = join(marketplaceRoot, "plugins", PLUGIN_LIFECYCLE_NAME);
- await rm(marketplaceRoot, { force: true, recursive: true });
- await writeLifecyclePlugin(pluginRoot, version, withUi);
- for (let index = 1; index <= 7; index += 1) {
- await writeSimplePlugin(
- join(marketplaceRoot, "plugins", `e2e-lifecycle-extra-${index}`),
- index,
- );
- }
- await writeMarketplaceManifest(marketplaceRoot, version);
- if (withUi) {
- const path = join(marketplaceRoot, ".claude-plugin", "marketplace.json");
- const catalog = await readJsonRecord(path);
- const entries = catalog.plugins as Array<Record<string, unknown>>;
- entries.find((entry) => entry.name === PLUGIN_LIFECYCLE_NAME)!.componentTypes = ["mcp", "ui"];
- await writeJson(path, catalog);
- }
- const managedPluginRoot = lifecycleManagedPluginRoot(homeDir);
- if (existsSync(dirname(dirname(managedPluginRoot)))) {
- // 已登记的 directory marketplace 会复制进 managed storage;失败恢复必须同步修复该副本。
- await rm(managedPluginRoot, { force: true, recursive: true });
- await mkdir(dirname(managedPluginRoot), { recursive: true });
- await cp(pluginRoot, managedPluginRoot, { force: true, recursive: true });
- }
- return { marketplaceRoot, pluginRoot, version };
- }
- export async function updatePluginLifecycleFixture(
- fixture: PluginLifecycleFixture,
- version: string,
- ): Promise<PluginLifecycleFixture> {
- await writeLifecyclePlugin(fixture.pluginRoot, version);
- await writeMarketplaceManifest(fixture.marketplaceRoot, version);
- return { ...fixture, version };
- }
- /**
- * 「全部更新」需要第二个可更新插件:只改 extra-1 的 payload 与目录版本。
- * 须在 updatePluginLifecycleFixture 之后调用,后者会按默认版本重写整份 marketplace.json。
- */
- export async function updateLifecycleExtraPluginFixture(
- fixture: PluginLifecycleFixture,
- version: string,
- ): Promise<void> {
- await writeSimplePlugin(
- join(fixture.marketplaceRoot, "plugins", PLUGIN_LIFECYCLE_EXTRA_NAME),
- 1,
- version,
- );
- const path = join(fixture.marketplaceRoot, ".claude-plugin", "marketplace.json");
- const catalog = await readJsonRecord(path);
- const entries = catalog.plugins as Array<Record<string, unknown>>;
- entries.find((entry) => entry.name === PLUGIN_LIFECYCLE_EXTRA_NAME)!.version = version;
- await writeJson(path, catalog);
- }
- export async function removeLifecyclePluginSource(fixture: PluginLifecycleFixture): Promise<void> {
- const { homeDir } = getE2EAppDataPaths();
- await rm(fixture.marketplaceRoot, { force: true, recursive: true });
- // directory marketplace 在添加时会复制到 managed storage;仅删除原目录不会触发
- // describe/install/update 失败,因此故障注入必须同时移除权威 managed plugin 副本。
- await rm(lifecycleManagedPluginRoot(homeDir), {
- force: true,
- recursive: true,
- });
- }
- export async function removeLifecyclePluginPayload(fixture: PluginLifecycleFixture): Promise<void> {
- const { homeDir } = getE2EAppDataPaths();
- // 保留 marketplace manifest 与登记关系,只移除其声明的插件 payload。这样 Manage 仍展示
- // update 入口,而安装器会在真正复制版本时确定性失败,正好覆盖“关联存在但升级源损坏”。
- await rm(fixture.pluginRoot, { force: true, recursive: true });
- await rm(lifecycleManagedPluginRoot(homeDir), {
- force: true,
- recursive: true,
- });
- }
- export async function corruptLifecycleMarketplaceManifest(
- fixture: PluginLifecycleFixture,
- ): Promise<void> {
- await writeText(
- join(fixture.marketplaceRoot, ".claude-plugin", "marketplace.json"),
- "{ invalid plugin lifecycle marketplace fixture",
- );
- }
- function lifecycleManagedPluginRoot(homeDir: string): string {
- return join(
- homeDir,
- ".zcode",
- "cli",
- "plugins",
- "marketplaces",
- PLUGIN_LIFECYCLE_MARKETPLACE_ID,
- "plugins",
- PLUGIN_LIFECYCLE_NAME,
- );
- }
- export async function createInvalidOfficialCollisionFixture(): Promise<string> {
- const { homeDir } = getE2EAppDataPaths();
- const root = join(homeDir, "fixtures", "plugin-official-name-collision");
- await writeJson(join(root, ".claude-plugin", "marketplace.json"), {
- name: "zcode-plugins-official",
- plugins: [],
- });
- return root;
- }
- export async function seedInlineLifecyclePlugin(): Promise<string> {
- const { homeDir } = getE2EAppDataPaths();
- const root = join(homeDir, "fixtures", PLUGIN_INLINE_NAME);
- await writeJson(join(root, ".zcode-plugin", "plugin.json"), {
- name: PLUGIN_INLINE_NAME,
- version: "1.0.0",
- description: "E2E inline plugin lifecycle smoke fixture",
- agents: "agents",
- skills: "skills",
- });
- await writeText(
- join(root, "agents", "inline-reviewer.md"),
- [
- "---",
- "name: inline-reviewer",
- "description: E2E inline Plugin reviewer profile",
- "tools:",
- " - Read",
- "---",
- "",
- "Review the requested files for the inline Plugin fixture.",
- ].join("\n"),
- );
- await writeText(
- join(root, "skills", "inline-lifecycle", "SKILL.md"),
- [
- "---",
- "name: inline-lifecycle",
- "description: E2E inline lifecycle runtime marker",
- "---",
- "",
- "# Inline lifecycle",
- "",
- "E2E_PLUGIN_INLINE_LIFECYCLE_RUNTIME",
- ].join("\n"),
- );
- const configPath = join(homeDir, ".zcode", "cli", "config.json");
- const config = await readJsonRecord(configPath);
- const plugins = isRecord(config.plugins) ? config.plugins : {};
- const enabledPlugins = isRecord(plugins.enabledPlugins) ? plugins.enabledPlugins : {};
- await writeJson(configPath, {
- ...config,
- plugins: {
- ...plugins,
- dirs: [root],
- enabled: true,
- enabledPlugins: { ...enabledPlugins, [PLUGIN_INLINE_ID]: true },
- },
- });
- return root;
- }
- async function waitForPluginSettingsEntry(): Promise<void> {
- // 隔离 profile 会先显示职业引导;仅等设置按钮会一直卡住,固定项目等待又不适用创建后的未归类草稿。
- await browser.waitUntil(
- async () => {
- if (await $('[data-testid="onboarding-page"]').isExisting()) {
- await dismissOccupationOnboarding();
- return false;
- }
- return $(`[data-testid="${TID_TASK_SETTINGS_BUTTON}"]`).isExisting();
- },
- { timeout: 30000, timeoutMsg: "退出职业引导后没有出现全局设置入口" },
- );
- }
- export async function openPluginSettings(scope: "user" | "workspace" = "user"): Promise<void> {
- if (scope === "workspace") {
- await openPluginManagementSettings();
- await selectPluginSettingsScope("workspace");
- return;
- }
- await waitForPluginSettingsEntry();
- await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
- timeout: 15000,
- timeoutMsg: "Plugin lifecycle E2E could not open Settings",
- });
- await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 15000 });
- // Settings 路由已统一使用单数 section id;"plugins" 是旧导航契约。
- await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "plugin"), {
- timeout: 15000,
- timeoutMsg: "Settings did not expose the Plugins section",
- });
- // Plugin 设置页现在只管理已安装插件;商店作为 WorkspaceShell 主视图,由
- // “浏览插件/创建”入口进入。旧用例直接等待商店根节点会绕过真实导航路径。
- // 该入口有稳定 testid,不按可见文案匹配——否则 i18n 改文案就会断。
- await clickTestIdByDom(TID_PLUGIN_STORE_BROWSE, {
- timeout: 15000,
- timeoutMsg: "Plugins 设置页没有进入商店的真实入口",
- });
- await waitForPluginControl("plugin-store-root", {}, 30000);
- await waitForPluginControl("plugin-store-list", {}, 30000);
- }
- export async function openPluginManagementSettings(): Promise<void> {
- // 插件创建会进入未归类草稿;设置入口属于全局外壳,不能强制等回固定项目。
- await waitForPluginSettingsEntry();
- await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
- timeout: 15000,
- timeoutMsg: "Workspace Plugin E2E could not open Settings",
- });
- await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 15000 });
- await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "plugin"), {
- timeout: 15000,
- timeoutMsg: "Settings did not expose the Plugin management section",
- });
- await waitForPluginControl("plugin-settings-scope-trigger", {}, 30000);
- }
- export async function selectPluginSettingsScope(scope: "user" | "workspace"): Promise<void> {
- await browser.waitUntil(
- async () =>
- browser.execute(() => {
- const trigger = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
- (element) => element.dataset.testid === "plugin-settings-scope-trigger",
- );
- if (!trigger) return false;
- if (trigger.getAttribute("aria-expanded") === "true") return true;
- // Radix DropdownMenu 在 pointerdown 上切换 open;仅调用 click 不会打开菜单,
- // 这会让 Workspace scope E2E 误判为没有 Workspace 选项。
- trigger.dispatchEvent(
- new PointerEvent("pointerdown", {
- bubbles: true,
- button: 0,
- ctrlKey: false,
- pointerType: "mouse",
- }),
- );
- return true;
- }),
- { timeout: 10000, timeoutMsg: "Plugin scope trigger did not render" },
- );
- if (scope === "user") {
- await clickTestIdByDom("plugin-settings-scope-user-option");
- } else {
- await browser.waitUntil(
- async () =>
- browser.execute(() => {
- if (document.querySelector('[data-testid^="plugin-settings-scope-option"]')) return true;
- // 项目在各设备的「项目」子菜单里(19 号票):打开第一个设备的子菜单,等同旧的第一项。
- const projects = document.querySelector<HTMLElement>(
- '[data-scope-projects-trigger="true"]',
- );
- if (projects && projects.dataset.state !== "open") projects.click();
- return false;
- }),
- {
- timeout: 15000,
- timeoutMsg: `Workspace Plugin scope option did not render: ${JSON.stringify(
- await browser.execute(() => {
- const tabStore = (
- window as typeof window & {
- __zcodeTabStoreE2E?: {
- getState?: () => {
- activeWorkspacePath?: string | null;
- activeWorkspaceIdentity?: string | null;
- tabs?: Array<Record<string, unknown>>;
- };
- };
- }
- ).__zcodeTabStoreE2E;
- const state = tabStore?.getState?.();
- return {
- activeWorkspacePath: state?.activeWorkspacePath ?? null,
- activeWorkspaceIdentity: state?.activeWorkspaceIdentity ?? null,
- tabs: state?.tabs ?? [],
- };
- }),
- )}`,
- },
- );
- await browser.execute(() => {
- document.querySelector<HTMLElement>('[data-testid^="plugin-settings-scope-option"]')?.click();
- });
- }
- await waitForPluginScopeSelection("plugin-settings-scope-trigger", scope);
- }
- export async function waitForPluginSettingsScope(scope: "user" | "workspace"): Promise<void> {
- await waitForPluginScopeSelection("plugin-settings-scope-trigger", scope);
- }
- export async function clickPluginSettingsListBreadcrumb(): Promise<void> {
- await browser.waitUntil(
- async () =>
- browser.execute(() => {
- // Marketplace 来源详情的 breadcrumb 是
- // Plugin Marketplace > Plugins > 当前插件;这里必须回到中间的 Plugins
- // 配置视图,不能点击最左侧 Marketplace 再绕一圈。
- const listButton = document.querySelector<HTMLButtonElement>(
- '[data-testid="settings-breadcrumb-item"]',
- );
- if (!listButton) return false;
- listButton.click();
- return true;
- }),
- {
- timeout: 15000,
- timeoutMsg: "Plugin settings breadcrumb did not expose the installed-list back action",
- },
- );
- await waitForPluginControl("plugin-settings-scope-trigger", {}, 30000);
- }
- export async function openInstalledPluginSettingsDetail(pluginId: string): Promise<void> {
- await waitForPluginControl("plugin-settings-plugin-row", { pluginId });
- const rowSelector = pluginControlSelector("plugin-settings-plugin-row", {
- pluginId,
- });
- const opened = await browser.execute((selector) => {
- const row = document.querySelector<HTMLElement>(selector);
- const card = row?.querySelector<HTMLElement>('[data-testid="plugin-store-card"]');
- card?.click();
- return Boolean(card);
- }, rowSelector);
- if (!opened) {
- throw new Error(`Could not open installed Plugin detail: ${pluginId}`);
- }
- await waitForPluginControl("plugin-store-detail", { pluginId });
- }
- export async function openInstalledPluginSettingsAdvanced(pluginId: string): Promise<void> {
- await openInstalledPluginSettingsDetail(pluginId);
- const detailSelector = pluginControlSelector("plugin-store-detail", {
- pluginId,
- });
- const expanded = await browser.execute((selector) => {
- const details = document.querySelector<HTMLDetailsElement>(`${selector} details`);
- if (!details) return false;
- details.open = true;
- return true;
- }, detailSelector);
- if (!expanded) {
- throw new Error(`Installed Plugin detail has no advanced section: ${pluginId}`);
- }
- }
- export async function waitForDisplayedPluginControl(
- testIdValue: string,
- attributes: PluginControlAttributes = {},
- timeout = 15000,
- ): Promise<void> {
- const selector = pluginControlSelector(testIdValue, attributes);
- await browser.waitUntil(
- async () => {
- const element = await browser.$(selector);
- return (await element.isExisting()) && (await element.isDisplayed());
- },
- {
- timeout,
- timeoutMsg: `Plugin UI did not display ${testIdValue} ${JSON.stringify(attributes)}`,
- },
- );
- }
- export async function addMarketplaceSourceThroughUi(source: string): Promise<void> {
- await waitForPluginControl("plugin-store-create");
- await browser.execute(() => {
- document
- .querySelector<HTMLElement>('[data-testid="plugin-store-create"]')
- ?.dispatchEvent(
- new PointerEvent("pointerdown", { bubbles: true, button: 0, pointerType: "mouse" }),
- );
- });
- await waitForPluginControl("plugin-store-add-source-menu-item");
- await clickPluginControl("plugin-store-add-source-menu-item");
- await waitForPluginControl("plugin-store-add-source-dialog");
- await setInputValueByTestIdDom("plugin-store-add-source-input", source, {
- timeout: 15000,
- });
- await clickPluginControl("plugin-store-add-source-risk-acknowledgement");
- await clickPluginControl("plugin-store-add-source-submit");
- }
- export async function addLifecycleMarketplaceThroughUi(
- fixture: PluginLifecycleFixture,
- ): Promise<void> {
- await addMarketplaceSourceThroughUi(fixture.marketplaceRoot);
- await waitUntilPluginOverview(
- (overview) =>
- overview.marketplaces.some(
- (marketplace) => marketplace.id === PLUGIN_LIFECYCLE_MARKETPLACE_ID,
- ),
- "Lifecycle marketplace was not persisted after the UI submission",
- 60000,
- );
- await browser.waitUntil(async () => !(await hasPluginControl("plugin-store-add-source-dialog")), {
- timeout: 60000,
- timeoutMsg: "Lifecycle marketplace completed but the source dialog remained open",
- });
- }
- export async function waitForPluginCard(pluginId: string, timeout = 30000): Promise<void> {
- if (!(await hasPluginControl("plugin-store-card", { pluginId }))) {
- const marketplaceSeparator = pluginId.lastIndexOf("@");
- const marketplaceId =
- marketplaceSeparator >= 0 ? pluginId.slice(marketplaceSeparator + 1) : undefined;
- const groupKey = marketplaceId ? `marketplace:${marketplaceId}` : undefined;
- if (groupKey) {
- await browser.waitUntil(
- async () =>
- (await hasPluginControl("plugin-store-card", { pluginId })) ||
- (await hasPluginControl("plugin-store-group-toggle", { groupKey })),
- {
- timeout,
- timeoutMsg: `Plugin marketplace group did not render: ${groupKey}`,
- },
- );
- if (
- !(await hasPluginControl("plugin-store-card", { pluginId })) &&
- (await hasPluginControl("plugin-store-group-toggle", { groupKey }))
- ) {
- // 折叠分组只渲染前六张卡片;测试仍通过真实展开入口寻找目标插件。
- await clickPluginControl("plugin-store-group-toggle", { groupKey });
- }
- }
- }
- await waitForPluginControl("plugin-store-card", { pluginId }, timeout);
- }
- export async function openPluginCard(pluginId: string): Promise<void> {
- await waitForPluginCard(pluginId);
- await clickPluginControl("plugin-store-card", { pluginId });
- await waitForPluginControl("plugin-store-detail", { pluginId }, 30000);
- }
- export async function installPluginThroughUi(pluginId: string): Promise<void> {
- await clickPluginControl("plugin-store-install", { pluginId });
- await waitUntilPluginOverview(
- (overview) => overview.installedPlugins.some((plugin) => plugin.id === pluginId),
- `Plugin did not install through UI: ${pluginId}`,
- 120000,
- );
- }
- export async function setPluginEnabledThroughUi(
- pluginId: string,
- enabled: boolean,
- entry: "detail" | "manage" | "market" = "detail",
- workspacePath = DEFAULT_WORKSPACE,
- ): Promise<void> {
- if (entry === "manage" || entry === "market") {
- const testId =
- entry === "manage" ? "plugin-settings-enabled-switch" : "plugin-store-enabled-switch";
- const current = await readPluginControlChecked(testId, { pluginId });
- if (current !== enabled) await clickPluginControl(testId, { pluginId });
- } else {
- const list = await getPluginList(workspacePath);
- if (list.plugins.find((plugin) => plugin.id === pluginId)?.enabled !== enabled) {
- await clickPluginControl("plugin-store-item-menu", { pluginId });
- await clickPluginControl("plugin-store-menu-enabled", { pluginId });
- }
- }
- await waitUntilPluginList(
- (list) => list.plugins.some((plugin) => plugin.id === pluginId && plugin.enabled === enabled),
- `Plugin enabled state did not become ${enabled}: ${pluginId}`,
- 30000,
- workspacePath,
- );
- }
- export async function configureLifecyclePluginThroughUi(value: string): Promise<void> {
- // 当前市场详情只展示信息;配置写入归管理页详情,必须走真实管理入口。
- const fromStore = !(await hasPluginControl("plugin-settings-detail-back"));
- if (fromStore) {
- await clickPluginControl("plugin-store-detail-back");
- await openManageView();
- // 整行卡片负责打开详情,行内第一个 button 是菜单,会误打开卸载操作。
- await clickPluginControl("plugin-store-card", { pluginId: PLUGIN_LIFECYCLE_ID });
- }
- await clickPluginControl("plugin-store-advanced");
- await setPluginControlValue(
- "plugin-store-config-input",
- { configKey: PLUGIN_LIFECYCLE_CONFIG_KEY },
- value,
- );
- await clickPluginControl("plugin-store-config-save", {
- pluginId: PLUGIN_LIFECYCLE_ID,
- });
- await browser.waitUntil(
- async () => (await readPluginConfigOptions())[PLUGIN_LIFECYCLE_CONFIG_KEY] === value,
- { timeout: 30000, timeoutMsg: "Plugin configuration did not persist" },
- );
- if (fromStore) {
- await clickPluginControl("plugin-settings-detail-back");
- // 管理页「浏览全部」按钮已改为返回图标。
- await clickPluginControl("plugin-store-manage-back");
- await clickPluginControl("plugin-store-segment-personal");
- await openPluginCard(PLUGIN_LIFECYCLE_ID);
- }
- }
- export async function uninstallPluginThroughUi(pluginId: string, confirm: boolean): Promise<void> {
- await clickPluginControl("plugin-store-item-menu", { pluginId });
- await clickPluginControl("plugin-store-menu-uninstall", { pluginId });
- await waitForPluginControl("plugin-store-uninstall-dialog");
- await clickPluginControl(
- confirm ? "plugin-store-uninstall-confirm" : "plugin-store-uninstall-cancel",
- );
- if (confirm) {
- await waitUntilPluginOverview(
- (overview) => !overview.installedPlugins.some((plugin) => plugin.id === pluginId),
- `Plugin remained installed after confirmation: ${pluginId}`,
- 120000,
- );
- } else {
- await waitUntilPluginOverview(
- (overview) => overview.installedPlugins.some((plugin) => plugin.id === pluginId),
- `Cancelling uninstall changed installation: ${pluginId}`,
- );
- }
- }
- export async function openManageView(): Promise<void> {
- await clickPluginControl("plugin-store-manage-open");
- await waitForPluginControl("plugin-store-manage");
- }
- export async function openSourcesDialog(): Promise<void> {
- await clickPluginControl("plugin-store-sources-open");
- await waitForPluginControl("plugin-store-sources-dialog");
- }
- export async function removeMarketplaceSourceThroughUi(marketplaceId: string): Promise<void> {
- await openSourcesDialog();
- await clickPluginControl("plugin-store-source-remove", { marketplaceId });
- await browser.waitUntil(
- async () => !(await hasPluginControl("plugin-store-source-row", { marketplaceId })),
- {
- timeout: 30000,
- timeoutMsg: `Marketplace source was not removed: ${marketplaceId}`,
- },
- );
- // 来源删除完成后通过弹窗标准 Escape 入口返回商店;否则 overlay 会拦截后续管理入口。
- await browser.keys(["Escape"]);
- await browser.waitUntil(async () => !(await hasPluginControl("plugin-store-sources-dialog")), {
- timeout: 10000,
- timeoutMsg: "Marketplace sources dialog did not close",
- });
- }
- export async function clickPluginControl(
- testIdValue: string,
- attributes: PluginControlAttributes = {},
- ): Promise<void> {
- let latest = "not-started";
- const selector = pluginControlSelector(testIdValue, attributes);
- try {
- await browser.waitUntil(
- async () => {
- const elements = await browser.$$(selector);
- if ((await elements.length) === 0) {
- latest = "missing";
- return false;
- }
- let sawHidden = false;
- let sawDisabled = false;
- for (const element of elements) {
- if (!(await element.isDisplayed())) {
- sawHidden = true;
- continue;
- }
- if (!(await element.isEnabled())) {
- sawDisabled = true;
- continue;
- }
- try {
- await element.scrollIntoView();
- await element.click();
- latest = "clicked";
- return true;
- } catch (error) {
- latest = error instanceof Error ? error.message : String(error);
- }
- }
- latest = sawDisabled ? "disabled" : sawHidden ? "hidden" : latest;
- return false;
- },
- { timeout: 30000 },
- );
- } catch (error) {
- const dialogs = await browser.execute(() =>
- Array.from(document.querySelectorAll<HTMLElement>('[role="alertdialog"], [role="dialog"]'))
- .filter((element) => element.getBoundingClientRect().height > 0)
- .map((element) => element.innerText.slice(0, 1000)),
- );
- await capturePluginManualReview("PLM-UI-FAIL", testIdValue).catch(() => {});
- throw new Error(
- `Could not click ${testIdValue}: ${latest}; dialogs=${JSON.stringify(dialogs)}`,
- { cause: error },
- );
- }
- }
- function pluginControlSelector(testIdValue: string, attributes: PluginControlAttributes): string {
- const selectors = [`[data-testid=${JSON.stringify(testIdValue)}]`];
- const attributeEntries: Array<[string, string | undefined]> = [
- ["data-plugin-id", attributes.pluginId],
- ["data-marketplace-id", attributes.marketplaceId],
- ["data-group-key", attributes.groupKey],
- ["data-component-kind", attributes.componentKind],
- ["data-config-key", attributes.configKey],
- ];
- for (const [name, value] of attributeEntries) {
- if (value) selectors.push(`[${name}=${JSON.stringify(value)}]`);
- }
- return selectors.join("");
- }
- export async function waitForPluginControl(
- testIdValue: string,
- attributes: PluginControlAttributes = {},
- timeout = 15000,
- ): Promise<void> {
- await browser.waitUntil(() => hasPluginControl(testIdValue, attributes), {
- timeout,
- timeoutMsg: `Plugin UI did not render ${testIdValue} ${JSON.stringify(attributes)}`,
- });
- }
- export async function hasPluginControl(
- testIdValue: string,
- attributes: PluginControlAttributes = {},
- ): Promise<boolean> {
- return browser.execute(
- (id, expected) =>
- Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).some(
- (candidate) =>
- candidate.dataset.testid === id &&
- (!expected.pluginId || candidate.dataset.pluginId === expected.pluginId) &&
- (!expected.marketplaceId || candidate.dataset.marketplaceId === expected.marketplaceId) &&
- (!expected.groupKey || candidate.dataset.groupKey === expected.groupKey) &&
- (!expected.componentKind || candidate.dataset.componentKind === expected.componentKind) &&
- (!expected.configKey || candidate.dataset.configKey === expected.configKey),
- ),
- testIdValue,
- attributes,
- );
- }
- async function waitForPluginScopeSelection(
- triggerTestId: string,
- scope: "user" | "workspace",
- ): Promise<void> {
- await browser.waitUntil(
- () =>
- browser.execute(
- (id, expectedScope) => {
- const trigger = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
- (candidate) => candidate.dataset.testid === id,
- );
- const selectedKey = trigger?.dataset.pluginScopeKey;
- return expectedScope === "user"
- ? selectedKey === "user"
- : Boolean(selectedKey && selectedKey !== "user");
- },
- triggerTestId,
- scope,
- ),
- {
- timeout: 15000,
- timeoutMsg: `Plugin scope trigger did not select ${scope}: ${triggerTestId}`,
- },
- );
- }
- export async function getPluginControlText(
- testIdValue: string,
- attributes: PluginControlAttributes = {},
- ): Promise<string> {
- return browser.execute(
- (id, expected) =>
- Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
- (candidate) =>
- candidate.dataset.testid === id &&
- (!expected.pluginId || candidate.dataset.pluginId === expected.pluginId) &&
- (!expected.marketplaceId || candidate.dataset.marketplaceId === expected.marketplaceId),
- )?.textContent ?? "",
- testIdValue,
- attributes,
- );
- }
- export async function setPluginPresentation(input: {
- locale: "zh-CN" | "en-US";
- theme: "zai-light" | "zai-dark";
- width: number;
- height: number;
- }): Promise<void> {
- await setCurrentElectronRendererContentSize(input.width, input.height);
- await browser.executeAsync((next, done: (result?: string) => void) => {
- const actions = (
- window as typeof window & {
- __testActions?: Record<string, unknown>;
- }
- ).__testActions;
- const setLocale = actions?.setLocale;
- const setTheme = actions?.setTheme;
- if (typeof setLocale !== "function" || typeof setTheme !== "function") {
- done("missing locale/theme test action");
- return;
- }
- (setLocale as (value: string) => void)(next.locale);
- (setTheme as (value: string) => void)(next.theme);
- requestAnimationFrame(() => done());
- }, input);
- }
- export async function assertPluginStoreFitsViewport(): Promise<void> {
- const layout = await browser.execute(() => {
- const root = document.querySelector<HTMLElement>('[data-testid="plugin-store-root"]');
- return {
- clientWidth: root?.clientWidth ?? 0,
- scrollWidth: root?.scrollWidth ?? 0,
- viewportWidth: window.innerWidth,
- overflow: root
- ? Array.from(root.querySelectorAll<HTMLElement>("*"))
- .filter(
- (element) =>
- element.getBoundingClientRect().right > root.getBoundingClientRect().right + 1,
- )
- .map((element) => ({
- tag: element.tagName,
- testId: element.dataset.testid,
- className: element.className,
- text: element.textContent?.slice(0, 80),
- }))
- .slice(0, 10)
- : [],
- };
- });
- expect(layout.clientWidth).toBeGreaterThan(0);
- if (layout.scrollWidth > layout.clientWidth + 1) {
- throw new Error(`Plugin store overflows: ${JSON.stringify(layout)}`);
- }
- expect(layout.clientWidth).toBeLessThanOrEqual(layout.viewportWidth);
- }
- export async function capturePluginManualReview(caseId: string, label: string): Promise<string> {
- const artifactDir = join(
- process.cwd(),
- ".e2e-artifacts",
- "plugin-management",
- caseId.toLowerCase(),
- );
- await mkdir(artifactDir, { recursive: true });
- const path = join(artifactDir, `${label.replaceAll(/[^a-z0-9-]/giu, "-")}.png`);
- // 人工评审截图要反映稳定态:等有限时长的过渡/动画(分段切换、面板展开)结束,
- // 否则会截到旧选中态与新内容并存的中间帧。无限循环的加载动画不参与判断。
- await browser
- .waitUntil(
- async () =>
- browser.execute(
- () =>
- document
- .getAnimations()
- .filter(
- (animation) =>
- animation.playState === "running" &&
- animation.effect?.getTiming().iterations !== Infinity,
- ).length === 0,
- ),
- { timeout: 3000, interval: 100 },
- )
- .catch(() => undefined);
- await browser.saveScreenshot(path);
- return path;
- }
- export async function restartPluginLifecycleApp(): Promise<void> {
- await browser.reloadSession();
- await browser.waitUntil(
- async () => {
- try {
- return (await browser.getUrl()).startsWith("file://");
- } catch {
- return false;
- }
- },
- {
- timeout: 60000,
- timeoutMsg: "Electron renderer did not recover after reloadSession",
- },
- );
- await waitForWorkspaceApp(DEFAULT_WORKSPACE, 60000);
- }
- export async function getPluginOverview(
- workspacePath = DEFAULT_WORKSPACE,
- configScope?: "user" | "workspace",
- ): Promise<ZCodePluginsOverviewResult> {
- return callPluginTestAction("getPluginsOverview", {
- workspacePath,
- ...(configScope ? { configScope } : {}),
- });
- }
- export async function addMarketplaceSourceThroughAgent(
- source: string,
- workspacePath = DEFAULT_WORKSPACE,
- ): Promise<ZCodePluginsMarketplaceMutationResult> {
- return callPluginTestAction("addPluginMarketplace", {
- source,
- workspacePath,
- });
- }
- export async function updateMarketplaceThroughAgent(
- marketplace: string,
- workspacePath = DEFAULT_WORKSPACE,
- ): Promise<ZCodePluginsMarketplaceMutationResult> {
- return callPluginTestAction("updatePluginMarketplace", {
- marketplace,
- workspacePath,
- });
- }
- export async function installMarketplacePluginThroughAgent(
- pluginName: string,
- marketplace: string,
- workspacePath = DEFAULT_WORKSPACE,
- ): Promise<ZCodePluginsInstallResult> {
- return callPluginTestAction("installPlugin", {
- marketplace,
- pluginName,
- scope: "user",
- workspacePath,
- });
- }
- export async function getPluginList(
- workspacePath = DEFAULT_WORKSPACE,
- workspaceIdentity?: string,
- configScope?: "user" | "workspace",
- ): Promise<ZCodePluginsListResult> {
- return callPluginTestAction("listPlugins", {
- workspacePath,
- ...(workspaceIdentity ? { workspaceIdentity } : {}),
- ...(configScope ? { configScope } : {}),
- });
- }
- export async function getPluginReferenceCatalog(
- options: {
- sessionId?: string;
- workspaceIdentity?: string;
- workspacePath?: string;
- } = {},
- ): Promise<ZCodePluginsReferenceCatalogResult> {
- return callPluginTestAction("getPluginReferenceCatalog", {
- workspacePath: options.workspacePath ?? DEFAULT_WORKSPACE,
- ...(options.workspaceIdentity ? { workspaceIdentity: options.workspaceIdentity } : {}),
- ...(options.sessionId ? { sessionId: options.sessionId } : {}),
- });
- }
- export async function waitUntilPluginOverview(
- predicate: (overview: ZCodePluginsOverviewResult) => boolean,
- timeoutMsg: string,
- timeout = 30000,
- ): Promise<void> {
- await browser.waitUntil(async () => predicate(await getPluginOverview()), {
- timeout,
- timeoutMsg,
- });
- }
- export async function waitUntilPluginList(
- predicate: (list: ZCodePluginsListResult) => boolean,
- timeoutMsg: string,
- timeout = 30000,
- workspacePath = DEFAULT_WORKSPACE,
- ): Promise<void> {
- // 启停属于操作时的 workspace;未归类任务不能用默认项目的状态确认 UI 已生效。
- let latest: ZCodePluginsListResult | undefined;
- try {
- await browser.waitUntil(
- async () => {
- latest = await getPluginList(workspacePath);
- return predicate(latest);
- },
- { timeout, timeoutMsg },
- );
- } catch (error) {
- // 只保留启停与来源事实,便于区分配置范围错误和保存失败,不输出插件 secret/options。
- const plugins = latest?.plugins.map(({ id, enabled, enabledSource }) => ({
- id,
- enabled,
- enabledSource,
- }));
- throw new Error(`${timeoutMsg}; latest=${JSON.stringify(plugins)}`, { cause: error });
- }
- }
- export async function waitForPluginDraftRuntimeInvalidation(
- workspacePath = DEFAULT_WORKSPACE,
- timeout = 30000,
- ): Promise<void> {
- await browser.waitUntil(
- () =>
- browser.execute((path) => {
- const store = (
- window as typeof window & {
- __zcodeSessionStoreE2E?: {
- getState: () => {
- getWorkspaceState: (workspacePath: string) => {
- draftSessionId: string | null;
- };
- };
- };
- }
- ).__zcodeSessionStoreE2E;
- return store?.getState().getWorkspaceState(path).draftSessionId === null;
- }, workspacePath),
- {
- timeout,
- timeoutMsg: "Plugin runtime refresh did not invalidate the deferred draft session",
- },
- );
- }
- export async function readPluginConfigOptions(): Promise<Record<string, unknown>> {
- const config = await readJsonRecord(
- join(getE2EAppDataPaths().homeDir, ".zcode", "cli", "config.json"),
- );
- const plugins = isRecord(config.plugins) ? config.plugins : {};
- const options = isRecord(plugins.options) ? plugins.options : {};
- return isRecord(options[PLUGIN_LIFECYCLE_ID]) ? options[PLUGIN_LIFECYCLE_ID] : {};
- }
- export function pluginStoragePath(...segments: string[]): string {
- return join(getE2EAppDataPaths().homeDir, ".zcode", "cli", "plugins", ...segments);
- }
- export function pluginDataPath(pluginId: string): string {
- return pluginStoragePath("data", pluginId);
- }
- export function pluginPathExists(path: string): boolean {
- return existsSync(path);
- }
- async function readPluginControlChecked(
- testIdValue: string,
- attributes: PluginControlAttributes,
- ): Promise<boolean> {
- await waitForPluginControl(testIdValue, attributes);
- return browser.execute(
- (id, expected) => {
- const element = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
- (candidate) =>
- candidate.dataset.testid === id &&
- (!expected.pluginId || candidate.dataset.pluginId === expected.pluginId),
- );
- return element?.getAttribute("data-state") === "checked";
- },
- testIdValue,
- attributes,
- );
- }
- export async function setPluginControlValue(
- testIdValue: string,
- attributes: PluginControlAttributes,
- value: string,
- ): Promise<void> {
- await waitForPluginControl(testIdValue, attributes);
- const result = await browser.execute(
- (id, expected, nextValue) => {
- const element = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
- (candidate) =>
- candidate.dataset.testid === id &&
- (!expected.configKey || candidate.dataset.configKey === expected.configKey),
- );
- if (!(element instanceof HTMLInputElement)) return false;
- const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set;
- setter?.call(element, nextValue);
- element.dispatchEvent(new InputEvent("input", { bubbles: true, data: nextValue }));
- element.dispatchEvent(new Event("change", { bubbles: true }));
- return true;
- },
- testIdValue,
- attributes,
- value,
- );
- if (!result) throw new Error(`Could not set ${testIdValue} to ${value}`);
- }
- async function callPluginTestAction<T>(
- actionName: PluginTestActionName,
- params: unknown,
- ): Promise<T> {
- // 这些 action 随应用页面挂载,首次职业引导尚未退出时不存在。
- await waitForPluginSettingsEntry();
- await browser.waitUntil(
- () =>
- browser.execute(
- (name) =>
- typeof (
- window as typeof window & {
- __testActions?: Record<string, unknown>;
- }
- ).__testActions?.[name] === "function",
- actionName,
- ),
- {
- timeout: 30_000,
- timeoutMsg: `plugin test action did not become ready: ${actionName}`,
- },
- );
- const result = (await browser.executeAsync(
- (
- name: PluginTestActionName,
- input: unknown,
- done: (result: SerializedActionResult<unknown>) => void,
- ) => {
- const actions = (
- window as typeof window & {
- __testActions?: Record<string, unknown>;
- }
- ).__testActions;
- const action = actions?.[name];
- if (typeof action !== "function") {
- done({ ok: false, error: `missing test action: ${name}` });
- return;
- }
- Promise.resolve((action as (value: unknown) => Promise<unknown>)(input))
- .then((value) => done({ ok: true, value }))
- .catch((error: unknown) =>
- done({
- ok: false,
- error: error instanceof Error ? error.message : String(error),
- }),
- );
- },
- actionName,
- params,
- )) as SerializedActionResult<unknown>;
- if (!result.ok) throw new Error(result.error ?? `test action failed: ${actionName}`);
- return result.value as T;
- }
- async function writeLifecyclePlugin(
- pluginRoot: string,
- version: string,
- withUi = false,
- ): Promise<void> {
- await writeJson(join(pluginRoot, ".zcode-plugin", "plugin.json"), {
- name: PLUGIN_LIFECYCLE_NAME,
- version,
- description: `E2E plugin management lifecycle fixture ${version}`,
- author: { name: "ZCode E2E" },
- homepage: "https://example.com/e2e-plugin-lifecycle",
- commands: "commands",
- agents: "agents",
- skills: "skills",
- hooks: "hooks/hooks.json",
- mcpServers: {
- lifecycle: {
- type: "stdio",
- command: "node",
- args: ["${ZCODE_PLUGIN_ROOT}/server.mjs", version],
- },
- },
- ...(withUi
- ? {
- ui: {
- surfaces: [
- {
- id: "panel",
- title: "Lifecycle Panel",
- server: "lifecycle",
- resourceUri: "ui://lifecycle/panel.html",
- availability: "session",
- },
- ],
- },
- }
- : {}),
- userConfig: {
- [PLUGIN_LIFECYCLE_CONFIG_KEY]: {
- type: "string",
- title: "Lifecycle label",
- default: `fixture-${version}`,
- description: "Non-sensitive E2E lifecycle option",
- },
- [PLUGIN_LIFECYCLE_INHERITED_CONFIG_KEY]: {
- type: "number",
- title: "Timeout seconds",
- default: 30,
- description: "Inherited non-sensitive E2E lifecycle option",
- },
- },
- });
- await writeText(
- join(pluginRoot, "skills", "lifecycle-version", "SKILL.md"),
- [
- "---",
- "name: lifecycle-version",
- `description: ${PLUGIN_LIFECYCLE_RUNTIME_MARKER}_${version}`,
- "---",
- "",
- "# Lifecycle version",
- "",
- `${PLUGIN_LIFECYCLE_RUNTIME_MARKER}_${version}`,
- ].join("\n"),
- );
- await writeText(
- join(pluginRoot, "commands", "lifecycle-command.md"),
- `---\ndescription: Lifecycle command ${version}\n---\n\nReport ${PLUGIN_LIFECYCLE_RUNTIME_MARKER}_${version}.\n`,
- );
- await writeText(
- join(pluginRoot, "agents", "lifecycle-reviewer.md"),
- `---\nname: lifecycle-reviewer\ndescription: Lifecycle subagent ${version}\ntools:\n - Read\n---\nReport ${PLUGIN_LIFECYCLE_RUNTIME_MARKER}_${version}.\n`,
- );
- await writeJson(join(pluginRoot, "hooks", "hooks.json"), {
- hooks: {
- SessionStart: [
- {
- matcher: "startup",
- hooks: [{ type: "command", command: "echo E2E_PLUGIN_LIFECYCLE_HOOK" }],
- },
- ],
- },
- });
- await writeText(join(pluginRoot, "server.mjs"), buildDependencyFreeMcpServer(version, withUi));
- }
- async function writeSimplePlugin(
- pluginRoot: string,
- index: number,
- version = "1.0.0",
- ): Promise<void> {
- await writeJson(join(pluginRoot, ".zcode-plugin", "plugin.json"), {
- name: `e2e-lifecycle-extra-${index}`,
- version,
- description: `E2E lifecycle category expansion plugin ${index}`,
- skills: "skills",
- });
- await writeText(
- join(pluginRoot, "skills", `extra-${index}`, "SKILL.md"),
- `---\ndescription: E2E extra lifecycle skill ${index}\n---\n\n# Extra ${index}\n`,
- );
- }
- async function writeMarketplaceManifest(marketplaceRoot: string, version: string): Promise<void> {
- const extras = Array.from({ length: 7 }, (_, offset) => {
- const index = offset + 1;
- return {
- name: `e2e-lifecycle-extra-${index}`,
- version: "1.0.0",
- description: `E2E lifecycle category expansion plugin ${index}`,
- category: "productivity",
- source: `./plugins/e2e-lifecycle-extra-${index}`,
- };
- });
- await writeJson(join(marketplaceRoot, ".claude-plugin", "marketplace.json"), {
- name: PLUGIN_LIFECYCLE_MARKETPLACE_ID,
- description: PLUGIN_LIFECYCLE_MARKETPLACE_NAME,
- plugins: [
- {
- name: PLUGIN_LIFECYCLE_NAME,
- version,
- displayName: "Lifecycle Fixture",
- displayName_i18n: { "zh-CN": "生命周期测试插件" },
- description: `Deterministic plugin lifecycle fixture ${version}`,
- description_i18n: { "zh-CN": `确定性插件生命周期测试 ${version}` },
- category: "productivity",
- author: { name: "ZCode E2E", url: "https://example.com" },
- homepage: "https://example.com/e2e-plugin-lifecycle",
- privacyPolicy: "https://example.com/privacy",
- termsOfService: "https://example.com/terms",
- examplePrompts: [PLUGIN_LIFECYCLE_EXAMPLE_PROMPT],
- source: `./plugins/${PLUGIN_LIFECYCLE_NAME}`,
- },
- ...extras,
- ],
- });
- }
- function buildDependencyFreeMcpServer(version: string, withUi = false): string {
- const html =
- '<!doctype html><html><body><h1 id="market-ui-ready">Connecting</h1><script>window.zcode.ready().then(() => { document.getElementById("market-ui-ready").textContent = "PLUGIN_MARKET_UI_READY"; });</script></body></html>';
- return `#!/usr/bin/env node
- import readline from "node:readline";
- const version = process.argv[2] || ${JSON.stringify(version)};
- const input = readline.createInterface({ input: process.stdin });
- function send(message) { process.stdout.write(JSON.stringify(message) + "\\n"); }
- input.on("line", (line) => {
- let request;
- try { request = JSON.parse(line); } catch { return; }
- if (request.method === "initialize") {
- send({ jsonrpc: "2.0", id: request.id, result: { protocolVersion: "2024-11-05", capabilities: { tools: {}, ${withUi ? "resources: {}," : ""} }, serverInfo: { name: "e2e-plugin-lifecycle", version } } });
- return;
- }
- if (request.method === "tools/list") {
- send({ jsonrpc: "2.0", id: request.id, result: { tools: [{ name: "version_marker", description: "Return the deterministic lifecycle version marker", inputSchema: { type: "object", properties: {} } }] } });
- return;
- }
- if (request.method === "tools/call") {
- send({ jsonrpc: "2.0", id: request.id, result: { content: [{ type: "text", text: ${JSON.stringify(PLUGIN_LIFECYCLE_RUNTIME_MARKER)} + "_" + version }] } });
- return;
- }
- ${
- withUi
- ? `if (request.method === "resources/list") {
- send({ jsonrpc: "2.0", id: request.id, result: { resources: [{ uri: "ui://lifecycle/panel.html", name: "Lifecycle Panel", mimeType: "text/html;profile=mcp-app" }] } }); return;
- }
- if (request.method === "resources/read") {
- send({ jsonrpc: "2.0", id: request.id, result: { contents: [{ uri: request.params.uri, mimeType: "text/html;profile=mcp-app", text: ${JSON.stringify(html)} }] } }); return;
- }`
- : ""
- }
- if (request.id !== undefined) send({ jsonrpc: "2.0", id: request.id, result: {} });
- });
- `;
- }
- async function readJsonRecord(path: string): Promise<Record<string, unknown>> {
- try {
- const parsed = JSON.parse(await readFile(path, "utf-8")) as unknown;
- return isRecord(parsed) ? parsed : {};
- } catch (error) {
- if (
- typeof error === "object" &&
- error !== null &&
- "code" in error &&
- (error as { code?: unknown }).code === "ENOENT"
- ) {
- return {};
- }
- throw error;
- }
- }
- async function writeJson(path: string, value: unknown): Promise<void> {
- await writeText(path, `${JSON.stringify(value, null, 2)}\n`);
- }
- async function writeText(path: string, value: string): Promise<void> {
- await mkdir(dirname(path), { recursive: true });
- await writeFile(path, value, "utf-8");
- }
- function isRecord(value: unknown): value is Record<string, unknown> {
- return typeof value === "object" && value !== null && !Array.isArray(value);
- }
|