import { readFile } from "node:fs/promises"; import { describe, expect, it } from "vitest"; // 各资源页头部动作已统一由 SettingsResourceHeaderActions 渲染: // markup 断言落在共享组件上,分区文件只断言委托与传入的 action id。 const delegatingSections = [ "CommandsSection.tsx", "SubagentsSection.tsx", "HooksSection.tsx", "McpSettingsSection.tsx", "SkillsSection.tsx", ] as const; describe("settings list create actions", () => { it("renders the shared create action as a default icon-and-label button", async () => { const source = await readFile( new URL("../src/settings/SettingsResourceHeaderActions.tsx", import.meta.url), "utf8", ); const markerStart = source.indexOf("data-settings-create-action={newActionId}"); expect(markerStart).toBeGreaterThan(-1); const actionStart = source.lastIndexOf("", actionStart) + "".length; const action = source.slice(actionStart, actionEnd); expect(action).toContain('variant="default"'); expect(action).toContain('size="default"'); expect(action).toContain('className="rounded-lg"'); expect(action).toContain(' { const source = await readFile( new URL(`../src/settings/${fileName}`, import.meta.url), "utf8", ); expect(source).toContain(" { const source = await readFile( new URL("../src/settings/SettingsResourceHeaderActions.tsx", import.meta.url), "utf8", ); const markerStart = source.indexOf("data-settings-import-action={importActionId}"); expect(markerStart).toBeGreaterThan(-1); const itemStart = source.lastIndexOf("", itemStart) + "".length; const item = source.slice(itemStart, itemEnd); expect(item).toContain("onSelect={onImport}"); expect(item).toContain("{importLabel}"); expect(source).toContain('id: "settings.resourceActions.import"'); }); it.each([ ["McpSettingsSection.tsx", "settings.mcp.import.open", "settings.mcp.create.open"], ["SkillsSection.tsx", "settings.skills.import.open", "settings.skills.create.open"], ["HooksSection.tsx", undefined, "settings.hooks.add"], ] as const)( "%s passes its action ids to the shared header actions", async (fileName, importActionId, newActionId) => { const source = await readFile( new URL(`../src/settings/${fileName}`, import.meta.url), "utf8", ); if (importActionId) { expect(source).toContain(`importActionId="${importActionId}"`); } expect(source).toContain(`newActionId="${newActionId}"`); }, ); it.each([ ["McpSettingsSection.tsx", "settings.mcp.import.action"], ["SkillsSection.tsx", "settings.skills.import.action"], ] as const)( "%s keeps a visible label on the inline import dialog button", async (fileName, messageId) => { const source = await readFile( new URL(`../src/settings/${fileName}`, import.meta.url), "utf8", ); expect(source).toMatch( /variant="outline"\s+size="lg"[^]*? { const source = await readFile( new URL("../src/settings/AutomationsSection.tsx", import.meta.url), "utf8", ); expect(source).not.toContain("SettingsResourceHeaderActions"); }); it("shares the two-item Add menu across plugin pages", async () => { const menu = await readFile( new URL("../src/settings/PluginAddMenu.tsx", import.meta.url), "utf8", ); expect(menu.match(/]/g)).toHaveLength(2); expect(menu).toContain('id: "pluginCreator.create"'); expect(menu).toContain('id: "pluginCreator.addMarketplace"'); for (const file of ["PluginStorePage.tsx", "PluginsSection.tsx"]) { const source = await readFile(new URL(`../src/settings/${file}`, import.meta.url), "utf8"); expect(source).toContain(" { const source = await readFile( new URL("../src/settings/PluginStorePage.tsx", import.meta.url), "utf8", ); const actionStart = source.indexOf("", actionStart)); expect(action).toContain("onAddMarketplace={() => {"); expect(action).toContain("setAddMarketplaceError(null)"); expect(action).toContain("setAddSourceOpen(true)"); }); });