plugin-manager.e2e.ts 13 KB

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