plugin-manager.e2e.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Web e2e scenario: the plugin manager page behind the sidebar's Plugins entry over a scaffold
  2. // profile runtime — one installed bundle switched on, one plain plugin added to
  3. // an agent preset and switched off there, and the configuration tab writing one
  4. // field under that preset's settings scope. Zero model calls: everything is
  5. // client state plus the profile files and the settings document, so there is
  6. // no fixture and a stray stream would fail loud on the open llm seam.
  7. import { mkdtemp, readFile, rm } from 'node:fs/promises'
  8. import { tmpdir } from 'node:os'
  9. import { fileURLToPath } from 'node:url'
  10. import type { Browser, Page } from 'playwright'
  11. import { chromium } from 'playwright'
  12. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  13. import { join } from 'node:path'
  14. import {
  15. assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  16. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  17. } from './scaffold.ts'
  18. import { ZH_BROWSER_LOCALE, saveFailureShot } from './support.ts'
  19. const SNAPSHOT_DIR = fileURLToPath(new URL('./expected/plugin-manager', import.meta.url))
  20. const MANAGER_EXPECTED = join(SNAPSHOT_DIR, 'manager.expected.md')
  21. const DETAIL_EXPECTED = join(SNAPSHOT_DIR, 'preset-detail.expected.md')
  22. const LIVE_EXPECTED = join(SNAPSHOT_DIR, 'live-enabled.expected.md')
  23. const FIXTURE_PLUGINS = fileURLToPath(new URL('./fixtures/plugins', import.meta.url))
  24. const MODE = webSnapshotMode()
  25. describe('web e2e: plugin manager', () => {
  26. let scaffold: WebScaffold
  27. let browser: Browser
  28. let page: Page
  29. let tripwire: ReturnType<typeof watchConsole>
  30. /** The writable preset root the scenario's preset overlays land in. */
  31. let presetRoot: string
  32. beforeAll(async () => {
  33. presetRoot = await mkdtemp(join(tmpdir(), 'dsh-web-e2e-presets-'))
  34. scaffold = await launchWebScaffold({
  35. // The scaffold pins a roster without a user root, so a preset's user
  36. // patch layer needs one: an empty root beside the shipped presets.
  37. agentPresets: { roots: [{ path: presetRoot, trust: 'user' }], default: 'standard' },
  38. profileRuntime: {
  39. packages: [
  40. { dir: join(FIXTURE_PLUGINS, 'fixture-bundle') },
  41. { dir: join(FIXTURE_PLUGINS, 'fixture-plain-plugin') },
  42. ],
  43. },
  44. })
  45. browser = await chromium.launch()
  46. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  47. tripwire = watchConsole(page)
  48. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  49. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  50. }, 120_000)
  51. afterAll(async () => {
  52. await browser?.close()
  53. await scaffold?.close()
  54. await rm(presetRoot, { recursive: true, force: true })
  55. })
  56. /** Close any open settings dialog, so the sidebar and the main column are clickable. */
  57. async function closeSettings() {
  58. if (await page.getByRole('dialog', { name: '设置' }).count() > 0) {
  59. await page.keyboard.press('Escape')
  60. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  61. }
  62. }
  63. /** Select the sidebar's Plugins entry and wait for the management page in the main column. */
  64. async function openPluginsPanel() {
  65. await closeSettings()
  66. await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
  67. const panel = page.locator('[data-plugin-panel]')
  68. await panel.getByRole('heading', { name: '插件管理' }).waitFor({ timeout: 10_000 })
  69. return panel
  70. }
  71. /** Open the settings dialog on the Plugins section: the configuration page. */
  72. async function openPluginsSettings() {
  73. await closeSettings()
  74. await page.getByRole('button', { name: '设置', exact: true }).click()
  75. const dialog = page.getByRole('dialog', { name: '设置' })
  76. await dialog.waitFor({ timeout: 10_000 })
  77. await dialog.getByRole('button', { name: '插件', exact: true }).click()
  78. await dialog.getByRole('heading', { name: '插件', exact: true }).waitFor({ timeout: 10_000 })
  79. return dialog
  80. }
  81. /** Open the settings dialog on one preset's detail page. */
  82. async function openPresetDetail(name: string) {
  83. await closeSettings()
  84. await page.getByRole('button', { name: '设置', exact: true }).click()
  85. const dialog = page.getByRole('dialog', { name: '设置' })
  86. await dialog.waitFor({ timeout: 10_000 })
  87. await dialog.getByRole('button', { name: 'Agent 预设', exact: true }).click()
  88. await dialog.getByRole('button', { name: `配置: ${name}` }).click()
  89. await dialog.getByRole('heading', { name }).waitFor({ timeout: 10_000 })
  90. return dialog
  91. }
  92. /** One file under the harness home, or the empty string while it does not exist. */
  93. async function homeFile(...segments: string[]): Promise<string> {
  94. return readFile(join(scaffold.harnessHome, ...segments), 'utf8').catch(() => '')
  95. }
  96. it('lists the profile packages with their switches', async () => {
  97. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-list'))
  98. const panel = await openPluginsPanel()
  99. await panel.getByText('示例组合包', { exact: true }).waitFor({ timeout: 20_000 })
  100. expect(await panel.getByText('示例插件', { exact: true }).count()).toBe(1)
  101. // The bundle is installed but not enabled; the plain plugin carries no
  102. // switch, only its **Add to…** menu; uninstall waits on the plugin's page,
  103. // which names the module and where it is composed, and the crumb leads back.
  104. const toggle = panel.getByRole('switch', { name: '启用 示例组合包' })
  105. expect(await toggle.getAttribute('aria-checked')).toBe('false')
  106. expect(await panel.getByRole('switch', { name: '启用 示例插件' }).count()).toBe(0)
  107. expect(await panel.getByRole('button', { name: '加入到…' }).count()).toBe(1)
  108. expect(await panel.getByRole('button', { name: '卸载 示例插件' }).count()).toBe(0)
  109. await panel.getByRole('button', { name: '查看 示例插件' }).click()
  110. await panel.getByRole('button', { name: '卸载 示例插件' }).waitFor({ timeout: 5_000 })
  111. await panel.getByText('尚未加入', { exact: true }).waitFor({ timeout: 5_000 })
  112. await panel.getByRole('button', { name: '返回插件列表' }).click()
  113. await expect.poll(() => panel.getByRole('button', { name: '卸载 示例插件' }).count(), { timeout: 5_000 }).toBe(0)
  114. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  115. await compareOrRefreshGolden(MANAGER_EXPECTED, snapshot, MODE)
  116. expect(tripwire.pageErrors).toEqual([])
  117. }, 60_000)
  118. it('enables a bundle into the profile manifest and reports the restart it waits for', async () => {
  119. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-enable'))
  120. const panel = await openPluginsPanel()
  121. const toggle = panel.getByRole('switch', { name: '启用 示例组合包' })
  122. await toggle.waitFor({ timeout: 20_000 })
  123. await toggle.click()
  124. await expect.poll(async () => (await homeFile('profiles', 'scaffold', 'package.json')).includes('"@fixture/bundle"'), {
  125. timeout: 10_000,
  126. }).toBe(true)
  127. const manifest = JSON.parse(await homeFile('profiles', 'scaffold', 'package.json')) as {
  128. dsh: { profile: { bundles: string[] } }
  129. }
  130. expect(manifest.dsh.profile.bundles).toEqual(['@fixture/bundle'])
  131. // A startup-applied profile: the switch is on, and the banner names the package.
  132. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 10_000 }).toBe('true')
  133. await panel.getByText('以下更改会在下次启动生效:示例组合包').waitFor({ timeout: 10_000 })
  134. expect(await panel.getByText('需重启', { exact: true }).count()).toBe(1)
  135. // The pack's page lists its rows from its static declarations. A profile that applies
  136. // patches at its next start keeps them read-only: no row switch.
  137. await panel.getByRole('button', { name: '查看 示例组合包' }).click()
  138. await panel.locator('[data-plugin-row]', { hasText: 'fixture-row' }).waitFor({ timeout: 10_000 })
  139. expect(await panel.getByRole('switch', { name: '启用组件 fixture-row' }).count()).toBe(0)
  140. await panel.getByRole('button', { name: '返回插件列表' }).click()
  141. expect(tripwire.pageErrors).toEqual([])
  142. }, 60_000)
  143. it('adds a plain plugin to a preset, then switches it off and deletes it on the preset\'s detail page', async () => {
  144. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-preset-row'))
  145. const panel = await openPluginsPanel()
  146. await panel.getByRole('button', { name: '加入到…' }).click()
  147. await page.getByRole('menuitem', { name: '预设:标准模式' }).click()
  148. const overlay = (): Promise<string> => readFile(join(presetRoot, 'standard', 'cordis.patch.yml'), 'utf8').catch(() => '')
  149. await expect.poll(async () => (await overlay()).includes('@fixture/plain-plugin'), { timeout: 10_000 }).toBe(true)
  150. expect(await overlay()).toContain('id: fixture/plain-plugin')
  151. // The **Add to…** menu now marks that preset as joined; the menu closes
  152. // through its anchor.
  153. await panel.getByRole('button', { name: '加入到…' }).click()
  154. await page.getByRole('menuitem', { name: '预设:标准模式(已加入)' }).waitFor({ timeout: 5_000 })
  155. await panel.getByRole('button', { name: '加入到…' }).click()
  156. await expect.poll(() => page.getByRole('menuitem').count(), { timeout: 5_000 }).toBe(0)
  157. // The preset's detail page, behind the gear on its card, lists the new
  158. // row under the package's title as the user's, switched on, with the
  159. // local mark.
  160. const dialog = await openPresetDetail('标准模式')
  161. const presetRow = dialog.locator('[data-preset-row="fixture/plain-plugin"]')
  162. const rowToggle = presetRow.getByRole('switch', { name: '启用 示例插件' })
  163. await rowToggle.waitFor({ timeout: 10_000 })
  164. expect(await rowToggle.getAttribute('aria-checked')).toBe('true')
  165. expect(await presetRow.getAttribute('data-plugin-source')).toBe('user')
  166. expect(await presetRow.getByText('本地', { exact: true }).count()).toBe(1)
  167. const settingsSection = dialog.locator('[data-settings-scope="preset/standard"]')
  168. await expect.poll(() => settingsSection.getByRole('button', { name: /^展开设置:/ }).count(), { timeout: 10_000 }).toBe(4)
  169. const detailSnapshot = await captureStableAria(page, '[data-preset-detail="standard"]', scaffold.workspaceCwd)
  170. await compareOrRefreshGolden(DETAIL_EXPECTED, detailSnapshot, MODE)
  171. await rowToggle.click()
  172. await expect.poll(async () => (await overlay()).includes('disabled: true'), { timeout: 10_000 }).toBe(true)
  173. await expect.poll(() => rowToggle.getAttribute('aria-checked'), { timeout: 10_000 }).toBe('false')
  174. await presetRow.getByRole('button', { name: '从这个预设删除 示例插件' }).click()
  175. await expect.poll(async () => (await overlay()).includes('@fixture/plain-plugin'), { timeout: 10_000 }).toBe(false)
  176. await expect.poll(() => rowToggle.count(), { timeout: 10_000 }).toBe(0)
  177. // The breadcrumb leads back to the roster.
  178. await dialog.getByRole('button', { name: '返回 Agent 预设' }).click()
  179. await dialog.getByRole('button', { name: '配置: 标准模式' }).waitFor({ timeout: 10_000 })
  180. expect(tripwire.pageErrors).toEqual([])
  181. }, 60_000)
  182. it('writes a configuration field under one preset scope and leaves the shared value alone', async () => {
  183. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-scope'))
  184. const dialog = await openPresetDetail('标准模式')
  185. // The detail page's settings section edits the preset's own scope.
  186. const settingsSection = dialog.locator('[data-settings-scope="preset/standard"]')
  187. await settingsSection.getByRole('button', { name: '展开设置: 终端' }).click()
  188. const timeout = settingsSection.getByLabel('命令超时(毫秒)')
  189. await timeout.waitFor({ timeout: 10_000 })
  190. expect(await timeout.inputValue()).toBe('60000')
  191. await timeout.fill('12000')
  192. await settingsSection.getByRole('button', { name: '保存', exact: true }).click()
  193. const settings = (): Promise<string> => homeFile('settings.yaml')
  194. await expect.poll(async () => (await settings()).includes('timeoutMs: 12000'), { timeout: 10_000 }).toBe(true)
  195. const document = await settings()
  196. expect(document).toContain('scopes:')
  197. expect(document).toContain('preset/standard:')
  198. expect(document.indexOf('scopes:')).toBeLessThan(document.indexOf('timeoutMs: 12000'))
  199. // On the configuration tab the shared instance still shows the composed default.
  200. const plugins = await openPluginsSettings()
  201. await plugins.getByRole('button', { name: '展开设置: 终端' }).click()
  202. await expect.poll(() => plugins.getByLabel('命令超时(毫秒)').inputValue(), { timeout: 5_000 }).toBe('60000')
  203. expect(await plugins.getByText('已覆盖').count()).toBe(0)
  204. expect(tripwire.pageErrors).toEqual([])
  205. }, 60_000)
  206. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  207. expect(tripwire.warnings).toEqual([])
  208. await assertFixtureInventory(SNAPSHOT_DIR, ['manager.expected.md', 'preset-detail.expected.md', 'live-enabled.expected.md'])
  209. })
  210. })
  211. describe('web e2e: live plugin management', () => {
  212. it('applies bundle enable and disable through the Remote without a restart', async () => {
  213. const scaffold = await launchWebScaffold({
  214. profileRuntime: { patchReload: 'live', packages: [{ dir: join(FIXTURE_PLUGINS, 'fixture-bundle') }] },
  215. })
  216. let browser: Browser | undefined
  217. try {
  218. browser = await chromium.launch()
  219. const page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  220. const tripwire = watchConsole(page)
  221. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-live'))
  222. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  223. await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
  224. const panel = page.locator('[data-plugin-panel]')
  225. const toggle = panel.getByRole('switch', { name: '启用 示例组合包' })
  226. await toggle.waitFor({ timeout: 20_000 })
  227. const mounted = () => [...scaffold.ctx.loader.entries()].find(entry => entry.options.id === 'fixture-row')
  228. const bundles = async () => {
  229. const text = await readFile(join(scaffold.harnessHome, 'profiles', 'scaffold', 'package.json'), 'utf8')
  230. return (JSON.parse(text) as { dsh: { profile: { bundles: string[] } } }).dsh.profile.bundles
  231. }
  232. expect(mounted()).toBeUndefined()
  233. await toggle.click()
  234. await expect.poll(() => mounted()?.fiber?.state, { timeout: 10_000 }).toBe(2)
  235. await expect.poll(bundles).toEqual(['@fixture/bundle'])
  236. await expect.poll(() => toggle.getAttribute('aria-checked')).toBe('true')
  237. expect(await panel.getByText('需重启', { exact: true }).count()).toBe(0)
  238. expect(await panel.getByText(/以下更改会在下次启动生效/).count()).toBe(0)
  239. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  240. await compareOrRefreshGolden(LIVE_EXPECTED, snapshot, MODE)
  241. await toggle.click()
  242. await expect.poll(mounted, { timeout: 10_000 }).toBeUndefined()
  243. await expect.poll(bundles).toEqual([])
  244. await expect.poll(() => toggle.getAttribute('aria-checked')).toBe('false')
  245. expect(await panel.getByText('需重启', { exact: true }).count()).toBe(0)
  246. expect(await panel.getByText(/以下更改会在下次启动生效/).count()).toBe(0)
  247. expect(tripwire.pageErrors).toEqual([])
  248. } finally {
  249. await browser?.close()
  250. await scaffold.close()
  251. }
  252. }, 60_000)
  253. })