plugin-manager.e2e.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Web e2e scenario: the plugin manager tab in Plugins settings 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 FIXTURE_PLUGINS = fileURLToPath(new URL('./fixtures/plugins', import.meta.url))
  22. const MODE = webSnapshotMode()
  23. describe('web e2e: plugin manager', () => {
  24. let scaffold: WebScaffold
  25. let browser: Browser
  26. let page: Page
  27. let tripwire: ReturnType<typeof watchConsole>
  28. /** The writable preset root the scenario's preset overlays land in. */
  29. let presetRoot: string
  30. beforeAll(async () => {
  31. presetRoot = await mkdtemp(join(tmpdir(), 'dsh-web-e2e-presets-'))
  32. scaffold = await launchWebScaffold({
  33. // The scaffold pins a roster without a user root, so a preset's user
  34. // patch layer needs one: an empty root beside the shipped presets.
  35. agentPresets: { roots: [{ path: presetRoot, trust: 'user' }], default: 'standard' },
  36. profileRuntime: {
  37. packages: [
  38. { dir: join(FIXTURE_PLUGINS, 'fixture-bundle') },
  39. { dir: join(FIXTURE_PLUGINS, 'fixture-plain-plugin') },
  40. ],
  41. },
  42. })
  43. browser = await chromium.launch()
  44. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  45. tripwire = watchConsole(page)
  46. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  47. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  48. }, 120_000)
  49. afterAll(async () => {
  50. await browser?.close()
  51. await scaffold?.close()
  52. await rm(presetRoot, { recursive: true, force: true })
  53. })
  54. /** Open the settings dialog on the Plugins section and select one of its tabs. */
  55. async function openPluginsTab(tab: string) {
  56. if (await page.getByRole('dialog', { name: '设置' }).count() > 0) {
  57. await page.keyboard.press('Escape')
  58. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  59. }
  60. await page.getByRole('button', { name: '设置', exact: true }).click()
  61. const dialog = page.getByRole('dialog', { name: '设置' })
  62. await dialog.waitFor({ timeout: 10_000 })
  63. await dialog.getByRole('button', { name: '插件', exact: true }).click()
  64. await dialog.getByRole('tab', { name: tab, exact: true }).click()
  65. await expect
  66. .poll(() => dialog.getByRole('tab', { name: tab, exact: true }).getAttribute('aria-selected'), { timeout: 5_000 })
  67. .toBe('true')
  68. return dialog
  69. }
  70. /** One file under the harness home, or the empty string while it does not exist. */
  71. async function homeFile(...segments: string[]): Promise<string> {
  72. return readFile(join(scaffold.harnessHome, ...segments), 'utf8').catch(() => '')
  73. }
  74. it('lists the profile packages with their switches and the preset composition', async () => {
  75. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-list'))
  76. const dialog = await openPluginsTab('插件管理')
  77. await dialog.getByText('示例组合包', { exact: true }).waitFor({ timeout: 20_000 })
  78. expect(await dialog.getByText('示例插件', { exact: true }).count()).toBe(1)
  79. // The bundle is installed but not enabled; the plain plugin carries no switch.
  80. const toggle = dialog.getByRole('switch', { name: '启用 示例组合包' })
  81. expect(await toggle.getAttribute('aria-checked')).toBe('false')
  82. expect(await dialog.getByRole('switch', { name: '启用 示例插件' }).count()).toBe(0)
  83. // The default preset's composition renders beside the packages.
  84. expect(await dialog.getByRole('button', { name: '选择要管理的 Agent 预设' }).textContent()).toContain('默认')
  85. expect(await dialog.getByRole('switch', { name: 'Enable row bash' }).count()).toBe(0)
  86. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  87. await compareOrRefreshGolden(MANAGER_EXPECTED, snapshot, MODE)
  88. expect(tripwire.pageErrors).toEqual([])
  89. }, 60_000)
  90. it('enables a bundle into the profile manifest and reports the restart it waits for', async () => {
  91. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-enable'))
  92. const dialog = await openPluginsTab('插件管理')
  93. const toggle = dialog.getByRole('switch', { name: '启用 示例组合包' })
  94. await toggle.waitFor({ timeout: 20_000 })
  95. await toggle.click()
  96. await expect.poll(async () => (await homeFile('profiles', 'scaffold', 'package.json')).includes('"@fixture/bundle"'), {
  97. timeout: 10_000,
  98. }).toBe(true)
  99. const manifest = JSON.parse(await homeFile('profiles', 'scaffold', 'package.json')) as {
  100. dsh: { profile: { bundles: string[] } }
  101. }
  102. expect(manifest.dsh.profile.bundles).toEqual(['@fixture/bundle'])
  103. // A startup-applied profile: the switch is on, and the banner names the package.
  104. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 10_000 }).toBe('true')
  105. await dialog.getByText('以下更改会在下次启动生效:示例组合包').waitFor({ timeout: 10_000 })
  106. expect(await dialog.getByText('待重启', { exact: true }).count()).toBe(1)
  107. expect(tripwire.pageErrors).toEqual([])
  108. }, 60_000)
  109. it('adds a plain plugin to a preset, switches its row off there, and removes it again', async () => {
  110. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-preset-row'))
  111. const dialog = await openPluginsTab('插件管理')
  112. await dialog.getByRole('button', { name: '展开 示例插件' }).click()
  113. await dialog.getByRole('button', { name: '添加到…' }).click()
  114. await page.getByRole('menuitem', { name: '预设:标准模式' }).click()
  115. const overlay = (): Promise<string> => readFile(join(presetRoot, 'standard', 'cordis.patch.yml'), 'utf8').catch(() => '')
  116. await expect.poll(async () => (await overlay()).includes('@fixture/plain-plugin'), { timeout: 10_000 }).toBe(true)
  117. expect(await overlay()).toContain('id: fixture/plain-plugin')
  118. // The preset composition lists the new row as the user's, switched on.
  119. const rowToggle = dialog.getByRole('switch', { name: '启用行 fixture/plain-plugin' })
  120. await rowToggle.waitFor({ timeout: 10_000 })
  121. expect(await rowToggle.getAttribute('aria-checked')).toBe('true')
  122. expect(await dialog.locator('[data-preset-row="fixture/plain-plugin"]').getAttribute('data-plugin-source')).toBe('user')
  123. await rowToggle.click()
  124. await expect.poll(async () => (await overlay()).includes('disabled: true'), { timeout: 10_000 }).toBe(true)
  125. await expect.poll(() => rowToggle.getAttribute('aria-checked'), { timeout: 10_000 }).toBe('false')
  126. expect(await dialog.getByText('用户停用', { exact: true }).count()).toBe(1)
  127. await dialog.getByRole('button', { name: '移除行 fixture/plain-plugin' }).click()
  128. await expect.poll(async () => (await overlay()).includes('@fixture/plain-plugin'), { timeout: 10_000 }).toBe(false)
  129. await expect.poll(() => rowToggle.count(), { timeout: 10_000 }).toBe(0)
  130. expect(tripwire.pageErrors).toEqual([])
  131. }, 60_000)
  132. it('writes a configuration field under one preset scope and leaves the shared value alone', async () => {
  133. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-scope'))
  134. const dialog = await openPluginsTab('插件配置')
  135. const scope = dialog.getByRole('button', { name: '选择这些设置生效的 Agent 预设' })
  136. await scope.waitFor({ timeout: 10_000 })
  137. expect(await scope.textContent()).toBe('所有预设')
  138. await scope.click()
  139. await page.getByRole('menuitem', { name: '标准模式(默认)' }).click()
  140. await expect.poll(() => scope.getAttribute('data-settings-scope'), { timeout: 5_000 }).toBe('preset/standard')
  141. await dialog.getByText('这里的值只对本预设生效;未覆盖的字段继承"所有预设"的值。').waitFor({ timeout: 5_000 })
  142. await dialog.getByText('终端', { exact: true }).click()
  143. const timeout = dialog.getByLabel('命令超时(毫秒)')
  144. await timeout.waitFor({ timeout: 10_000 })
  145. expect(await timeout.inputValue()).toBe('60000')
  146. await timeout.fill('12000')
  147. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  148. const settings = (): Promise<string> => homeFile('settings.yaml')
  149. await expect.poll(async () => (await settings()).includes('timeoutMs: 12000'), { timeout: 10_000 }).toBe(true)
  150. const document = await settings()
  151. expect(document).toContain('scopes:')
  152. expect(document).toContain('preset/standard:')
  153. expect(document.indexOf('scopes:')).toBeLessThan(document.indexOf('timeoutMs: 12000'))
  154. // Back under the shared instance the field still shows the composed default.
  155. await scope.click()
  156. await page.getByRole('menuitem', { name: '所有预设' }).click()
  157. await expect.poll(() => scope.getAttribute('data-settings-scope'), { timeout: 5_000 }).toBe('global')
  158. const expandTerminal = dialog.getByRole('button', { name: '展开设置: 终端' })
  159. if (await expandTerminal.count() > 0) await expandTerminal.click()
  160. await expect.poll(() => dialog.getByLabel('命令超时(毫秒)').inputValue(), { timeout: 5_000 }).toBe('60000')
  161. expect(await dialog.getByText('已覆盖').count()).toBe(0)
  162. expect(tripwire.pageErrors).toEqual([])
  163. }, 60_000)
  164. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  165. expect(tripwire.warnings).toEqual([])
  166. await assertFixtureInventory(SNAPSHOT_DIR, ['manager.expected.md'])
  167. })
  168. })