1
0

plugin-manager.e2e.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Web e2e scenario: the plugin manager page behind the sidebar's Plugins entry over a
  2. // managed scaffold profile: installed bundles, their rows, and bundle enablement. Zero
  3. // model calls: everything is client state plus the profile files and the settings
  4. // document, so there is no fixture and a stray stream would fail loud on the open llm seam.
  5. import { readFile } from 'node:fs/promises'
  6. import { fileURLToPath } from 'node:url'
  7. import type { Browser, Page } from 'playwright'
  8. import { chromium } from 'playwright'
  9. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  10. import { join } from 'node:path'
  11. import {
  12. assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  13. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  14. } from './scaffold.ts'
  15. import { ZH_BROWSER_LOCALE, saveFailureShot } from './support.ts'
  16. const SNAPSHOT_DIR = fileURLToPath(new URL('./expected/plugin-manager', import.meta.url))
  17. const MANAGER_EXPECTED = join(SNAPSHOT_DIR, 'manager.expected.md')
  18. const LIVE_EXPECTED = join(SNAPSHOT_DIR, 'live-enabled.expected.md')
  19. const FIXTURE_PLUGINS = fileURLToPath(new URL('./fixtures/plugins', import.meta.url))
  20. const MODE = webSnapshotMode()
  21. describe('web e2e: plugin manager', () => {
  22. let scaffold: WebScaffold
  23. let browser: Browser
  24. let page: Page
  25. let tripwire: ReturnType<typeof watchConsole>
  26. beforeAll(async () => {
  27. scaffold = await launchWebScaffold({
  28. profile: { packages: [{ dir: join(FIXTURE_PLUGINS, 'fixture-bundle') }] },
  29. })
  30. browser = await chromium.launch()
  31. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  32. tripwire = watchConsole(page)
  33. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  34. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  35. }, 120_000)
  36. afterAll(async () => {
  37. await browser?.close()
  38. await scaffold?.close()
  39. })
  40. /** Close any open settings dialog, so the sidebar and the main column are clickable. */
  41. async function closeSettings() {
  42. if (await page.getByRole('dialog', { name: '设置' }).count() > 0) {
  43. await page.keyboard.press('Escape')
  44. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  45. }
  46. }
  47. /** Select the sidebar's Plugins entry and wait for the management page in the main column. */
  48. async function openPluginsPanel() {
  49. await closeSettings()
  50. await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
  51. const panel = page.locator('[data-plugin-panel]')
  52. await panel.getByRole('heading', { name: '插件', exact: true }).waitFor({ timeout: 10_000 })
  53. return panel
  54. }
  55. /** One file under the harness home, or the empty string while it does not exist. */
  56. async function homeFile(...segments: string[]): Promise<string> {
  57. return readFile(join(scaffold.harnessHome, ...segments), 'utf8').catch(() => '')
  58. }
  59. it('lists the installed bundles with their switches and leaves the installation\'s own to Settings', async () => {
  60. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-list'))
  61. const panel = await openPluginsPanel()
  62. await panel.getByText('bundle', { exact: true }).waitFor({ timeout: 20_000 })
  63. const toggle = panel.getByRole('switch', { name: '启用 bundle' })
  64. expect(await toggle.getAttribute('aria-checked')).toBe('false')
  65. // The profile's own group holds its one bundle; the installation's optional bundles open the Official
  66. // group, followed by the official plugins that registered their configuration, and its other bundles
  67. // stay off the page.
  68. expect(await panel.locator('[data-plugin-group="bundles"] [data-plugin-package]').count()).toBe(1)
  69. expect(await panel.locator('[data-plugin-group="official"] [data-plugin-package]').count()).toBe(2)
  70. expect(await panel.locator('[data-plugin-group="official"] [data-plugin-item]').count()).toBe(4)
  71. expect(await panel.getByText('Beta', { exact: true }).count()).toBe(2)
  72. // A bundle that is off still shows the rows its patch declares, without switches.
  73. await panel.getByRole('button', { name: '查看 bundle' }).click()
  74. await panel.locator('[data-plugin-row]', { hasText: 'fixture-row' }).waitFor({ timeout: 10_000 })
  75. expect(await panel.getByRole('switch', { name: '启用组件 fixture-row' }).count()).toBe(0)
  76. await panel.getByRole('button', { name: '卸载 bundle' }).waitFor({ timeout: 5_000 })
  77. await panel.getByRole('button', { name: '返回插件列表' }).click()
  78. await expect.poll(() => panel.getByRole('button', { name: '卸载 bundle' }).count(), { timeout: 5_000 }).toBe(0)
  79. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  80. await compareOrRefreshGolden(MANAGER_EXPECTED, snapshot, MODE)
  81. expect(tripwire.pageErrors).toEqual([])
  82. }, 60_000)
  83. it('updates built-in names and descriptions when the UI language changes', async () => {
  84. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-locale'))
  85. const panel = await openPluginsPanel()
  86. await panel.getByRole('button', { name: '查看 智能体团队', exact: true }).click()
  87. const packageName = panel.locator('[data-plugin-name]')
  88. expect(await packageName.textContent()).toBe('@deepseek-ai/dsh-experimental-agent-team-profile')
  89. expect(await panel.getByText('启用智能体团队协作与团队工具。').count()).toBe(1)
  90. try {
  91. await page.getByRole('button', { name: '设置', exact: true }).click()
  92. await page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '中文' }).click()
  93. await page.getByRole('menuitem', { name: 'English' }).click()
  94. await page.getByRole('dialog', { name: 'Settings' }).waitFor()
  95. await page.keyboard.press('Escape')
  96. await panel.getByRole('heading', { name: 'Agent Teams', exact: true }).waitFor()
  97. expect(await packageName.textContent()).toBe('@deepseek-ai/dsh-experimental-agent-team-profile')
  98. expect(await panel.getByText('Enable agent team collaboration and team tools.').count()).toBe(1)
  99. await panel.getByRole('button', { name: 'Back to plugins' }).click()
  100. for (const title of ['Agent Teams', 'Agent Teams Web UI']) {
  101. await panel.getByRole('button', { name: `View ${title}`, exact: true }).waitFor()
  102. expect(await panel.getByRole('switch', { name: `Enable ${title}`, exact: true }).count()).toBe(1)
  103. }
  104. expect(await panel.getByText('View team members, the task board, and teammate sessions in the browser.').count()).toBe(1)
  105. // The official configuration pages follow the language too, from their own dictionary.
  106. for (const title of ['Shell', 'Agent loop', 'Subagent', 'Web search']) {
  107. await panel.getByRole('button', { name: `View ${title}`, exact: true }).waitFor()
  108. }
  109. } finally {
  110. if (await page.locator('html').getAttribute('lang') === 'en') {
  111. if (await page.getByRole('dialog', { name: 'Settings' }).count() === 0) {
  112. await page.getByRole('button', { name: 'Settings', exact: true }).click()
  113. }
  114. await page.getByRole('dialog', { name: 'Settings' }).getByRole('button', { name: 'English' }).click()
  115. await page.getByRole('menuitem', { name: '中文' }).click()
  116. await page.getByRole('dialog', { name: '设置' }).waitFor()
  117. }
  118. await closeSettings()
  119. }
  120. await panel.getByRole('button', { name: '查看 智能体团队', exact: true }).waitFor()
  121. expect(tripwire.pageErrors).toEqual([])
  122. }, 60_000)
  123. it('checks a spec before installing it and words what the check refused', async () => {
  124. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-install'))
  125. const panel = await openPluginsPanel()
  126. await panel.getByRole('button', { name: '添加插件', exact: true }).click()
  127. const dialog = page.getByRole('dialog', { name: '添加插件' })
  128. await dialog.waitFor({ timeout: 10_000 })
  129. const field = dialog.getByRole('textbox', { name: '包名或地址' })
  130. const install = dialog.getByRole('button', { name: '安装', exact: true })
  131. expect(await install.isDisabled()).toBe(true)
  132. // A name the list already shows is refused without asking the Host.
  133. await field.fill('@fixture/bundle')
  134. await install.click()
  135. await dialog.getByRole('alert').waitFor({ timeout: 5_000 })
  136. expect(await dialog.getByRole('alert').textContent()).toBe('该插件已安装')
  137. // A path the Host cannot read as a package is refused with its reason, and the spec stays editable.
  138. await field.fill(join(scaffold.harnessHome, 'no-such-plugin'))
  139. await install.click()
  140. await expect.poll(() => dialog.getByRole('alert').textContent(), { timeout: 10_000 }).toBe('该路径不存在或不是有效的插件包')
  141. expect(await field.isDisabled()).toBe(false)
  142. // A name the registry would refuse never reaches it.
  143. await field.fill('Not A Package')
  144. await install.click()
  145. await expect.poll(() => dialog.getByRole('alert').textContent(), { timeout: 10_000 }).toContain('无法识别这个包名或地址')
  146. await dialog.getByRole('button', { name: '关闭' }).click()
  147. await expect.poll(() => page.getByRole('dialog', { name: '添加插件' }).count(), { timeout: 5_000 }).toBe(0)
  148. expect(tripwire.pageErrors).toEqual([])
  149. }, 60_000)
  150. it('enables a bundle into the profile manifest, mounts its rows live, and switches one of them', async () => {
  151. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-enable'))
  152. const panel = await openPluginsPanel()
  153. const toggle = panel.getByRole('switch', { name: '启用 bundle' })
  154. await toggle.waitFor({ timeout: 20_000 })
  155. const mounted = () => [...scaffold.ctx.loader.entries()].find(entry => entry.options.id === 'fixture-row')
  156. expect(mounted()?.fiber?.state).toBeUndefined()
  157. await toggle.click()
  158. const bundles = async () => (JSON.parse(await homeFile('profiles', 'scaffold', 'package.json')) as {
  159. dsh: { profile: { bundles: string[] } }
  160. }).dsh.profile.bundles
  161. await expect.poll(bundles, { timeout: 10_000 }).toEqual(['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app', '@fixture/bundle'])
  162. // A live profile: the row mounts once the whole tree recomposed, the switch is on, and nothing waits for a restart.
  163. await expect.poll(() => mounted()?.fiber?.state, { timeout: 20_000 }).toBe(2)
  164. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 10_000 }).toBe('true')
  165. expect(await panel.getByText(/下次启动生效/).count()).toBe(0)
  166. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  167. await compareOrRefreshGolden(LIVE_EXPECTED, snapshot, MODE)
  168. // The pack's page lists its rows as the Host runs them, each with a switch that writes the profile patch.
  169. await panel.getByRole('button', { name: '查看 bundle' }).click()
  170. const rowSwitch = panel.getByRole('switch', { name: '启用组件 fixture-row' })
  171. await rowSwitch.waitFor({ timeout: 10_000 })
  172. expect(await rowSwitch.getAttribute('aria-checked')).toBe('true')
  173. await rowSwitch.click()
  174. await expect.poll(async () => (await homeFile('profiles', 'scaffold', 'cordis.patch.yml')).includes('fixture-row'), { timeout: 10_000 }).toBe(true)
  175. await expect.poll(() => rowSwitch.getAttribute('aria-checked'), { timeout: 10_000 }).toBe('false')
  176. // A disabled entry keeps its disposed fiber; only an active one counts as mounted.
  177. await expect.poll(() => mounted()?.fiber?.state, { timeout: 20_000 }).not.toBe(2)
  178. await rowSwitch.click()
  179. await expect.poll(() => mounted()?.fiber?.state, { timeout: 20_000 }).toBe(2)
  180. await panel.getByRole('button', { name: '返回插件列表' }).click()
  181. await toggle.click()
  182. await expect.poll(() => mounted()?.fiber?.state, { timeout: 20_000 }).not.toBe(2)
  183. await expect.poll(() => toggle.getAttribute('aria-checked')).toBe('false')
  184. expect(tripwire.pageErrors).toEqual([])
  185. }, 90_000)
  186. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  187. expect(tripwire.warnings).toEqual([])
  188. await assertFixtureInventory(SNAPSHOT_DIR, ['manager.expected.md', 'live-enabled.expected.md'])
  189. })
  190. })
  191. describe('web e2e: startup-applied plugin management', () => {
  192. it('saves a bundle selection that waits for the next start and keeps its rows read-only', async () => {
  193. const scaffold = await launchWebScaffold({
  194. profile: { hmr: false, packages: [{ dir: join(FIXTURE_PLUGINS, 'fixture-bundle') }] },
  195. })
  196. let browser: Browser | undefined
  197. try {
  198. browser = await chromium.launch()
  199. const page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  200. const tripwire = watchConsole(page)
  201. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-live'))
  202. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  203. await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
  204. const panel = page.locator('[data-plugin-panel]')
  205. const toggle = panel.getByRole('switch', { name: '启用 bundle' })
  206. await toggle.waitFor({ timeout: 20_000 })
  207. const mounted = () => [...scaffold.ctx.loader.entries()].find(entry => entry.options.id === 'fixture-row')
  208. const bundles = async () => {
  209. const text = await readFile(join(scaffold.harnessHome, 'profiles', 'scaffold', 'package.json'), 'utf8')
  210. return (JSON.parse(text) as { dsh: { profile: { bundles: string[] } } }).dsh.profile.bundles
  211. }
  212. expect(mounted()?.fiber?.state).toBeUndefined()
  213. await toggle.click()
  214. // The selection is saved and the switch turns on, but nothing mounts before the next start; a toast says so.
  215. await expect.poll(bundles).toEqual(['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app', '@fixture/bundle'])
  216. await expect.poll(() => toggle.getAttribute('aria-checked')).toBe('true')
  217. await page.getByText('更改将在下次启动生效', { exact: true }).waitFor({ timeout: 10_000 })
  218. expect(mounted()?.fiber?.state).toBeUndefined()
  219. // The pack's page lists its rows from their declarations, with no live entry to switch.
  220. await panel.getByRole('button', { name: '查看 bundle' }).click()
  221. await panel.locator('[data-plugin-row]', { hasText: 'fixture-row' }).waitFor({ timeout: 10_000 })
  222. expect(await panel.getByRole('switch', { name: '启用组件 fixture-row' }).isDisabled()).toBe(true)
  223. await panel.getByRole('button', { name: '返回插件列表' }).click()
  224. await toggle.click()
  225. await expect.poll(bundles).toEqual(['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'])
  226. await expect.poll(() => toggle.getAttribute('aria-checked')).toBe('false')
  227. expect(tripwire.pageErrors).toEqual([])
  228. } finally {
  229. await browser?.close()
  230. await scaffold.close()
  231. }
  232. }, 60_000)
  233. })