plugin-manager.e2e.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 form the built-in
  66. // group, and its other bundles stay off the page.
  67. expect(await panel.locator('[data-plugin-group="bundles"] [data-plugin-package]').count()).toBe(1)
  68. expect(await panel.locator('[data-plugin-group="builtin"] [data-plugin-package]').count()).toBe(3)
  69. // A bundle that is off still shows the rows its patch declares, without switches.
  70. await panel.getByRole('button', { name: '查看 bundle' }).click()
  71. await panel.locator('[data-plugin-row]', { hasText: 'fixture-row' }).waitFor({ timeout: 10_000 })
  72. expect(await panel.getByRole('switch', { name: '启用组件 fixture-row' }).count()).toBe(0)
  73. await panel.getByRole('button', { name: '卸载 bundle' }).waitFor({ timeout: 5_000 })
  74. await panel.getByRole('button', { name: '返回插件列表' }).click()
  75. await expect.poll(() => panel.getByRole('button', { name: '卸载 bundle' }).count(), { timeout: 5_000 }).toBe(0)
  76. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  77. await compareOrRefreshGolden(MANAGER_EXPECTED, snapshot, MODE)
  78. expect(tripwire.pageErrors).toEqual([])
  79. }, 60_000)
  80. it('checks a spec before installing it and words what the check refused', async () => {
  81. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-install'))
  82. const panel = await openPluginsPanel()
  83. await panel.getByRole('button', { name: '添加插件', exact: true }).click()
  84. const dialog = page.getByRole('dialog', { name: '添加插件' })
  85. await dialog.waitFor({ timeout: 10_000 })
  86. const field = dialog.getByRole('textbox', { name: '包名或地址' })
  87. const install = dialog.getByRole('button', { name: '安装', exact: true })
  88. expect(await install.isDisabled()).toBe(true)
  89. // A name the list already shows is refused without asking the Host.
  90. await field.fill('@fixture/bundle')
  91. await install.click()
  92. await dialog.getByRole('alert').waitFor({ timeout: 5_000 })
  93. expect(await dialog.getByRole('alert').textContent()).toBe('该插件已安装')
  94. // A path the Host cannot read as a package is refused with its reason, and the spec stays editable.
  95. await field.fill(join(scaffold.harnessHome, 'no-such-plugin'))
  96. await install.click()
  97. await expect.poll(() => dialog.getByRole('alert').textContent(), { timeout: 10_000 }).toBe('该路径不存在或不是有效的插件包')
  98. expect(await field.isDisabled()).toBe(false)
  99. // A name the registry would refuse never reaches it.
  100. await field.fill('Not A Package')
  101. await install.click()
  102. await expect.poll(() => dialog.getByRole('alert').textContent(), { timeout: 10_000 }).toContain('无法识别这个包名或地址')
  103. await dialog.getByRole('button', { name: '关闭' }).click()
  104. await expect.poll(() => page.getByRole('dialog', { name: '添加插件' }).count(), { timeout: 5_000 }).toBe(0)
  105. expect(tripwire.pageErrors).toEqual([])
  106. }, 60_000)
  107. it('enables a bundle into the profile manifest, mounts its rows live, and switches one of them', async () => {
  108. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-enable'))
  109. const panel = await openPluginsPanel()
  110. const toggle = panel.getByRole('switch', { name: '启用 bundle' })
  111. await toggle.waitFor({ timeout: 20_000 })
  112. const mounted = () => [...scaffold.ctx.loader.entries()].find(entry => entry.options.id === 'fixture-row')
  113. expect(mounted()?.fiber?.state).toBeUndefined()
  114. await toggle.click()
  115. const bundles = async () => (JSON.parse(await homeFile('profiles', 'scaffold', 'package.json')) as {
  116. dsh: { profile: { bundles: string[] } }
  117. }).dsh.profile.bundles
  118. await expect.poll(bundles, { timeout: 10_000 }).toEqual(['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app', '@fixture/bundle'])
  119. // A live profile: the row mounts once the whole tree recomposed, the switch is on, and nothing waits for a restart.
  120. await expect.poll(() => mounted()?.fiber?.state, { timeout: 20_000 }).toBe(2)
  121. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 10_000 }).toBe('true')
  122. expect(await panel.getByText(/下次启动生效/).count()).toBe(0)
  123. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  124. await compareOrRefreshGolden(LIVE_EXPECTED, snapshot, MODE)
  125. // The pack's page lists its rows as the Host runs them, each with a switch that writes the profile patch.
  126. await panel.getByRole('button', { name: '查看 bundle' }).click()
  127. const rowSwitch = panel.getByRole('switch', { name: '启用组件 fixture-row' })
  128. await rowSwitch.waitFor({ timeout: 10_000 })
  129. expect(await rowSwitch.getAttribute('aria-checked')).toBe('true')
  130. await rowSwitch.click()
  131. await expect.poll(async () => (await homeFile('profiles', 'scaffold', 'cordis.patch.yml')).includes('fixture-row'), { timeout: 10_000 }).toBe(true)
  132. await expect.poll(() => rowSwitch.getAttribute('aria-checked'), { timeout: 10_000 }).toBe('false')
  133. // A disabled entry keeps its disposed fiber; only an active one counts as mounted.
  134. await expect.poll(() => mounted()?.fiber?.state, { timeout: 20_000 }).not.toBe(2)
  135. await rowSwitch.click()
  136. await expect.poll(() => mounted()?.fiber?.state, { timeout: 20_000 }).toBe(2)
  137. await panel.getByRole('button', { name: '返回插件列表' }).click()
  138. await toggle.click()
  139. await expect.poll(() => mounted()?.fiber?.state, { timeout: 20_000 }).not.toBe(2)
  140. await expect.poll(() => toggle.getAttribute('aria-checked')).toBe('false')
  141. expect(tripwire.pageErrors).toEqual([])
  142. }, 90_000)
  143. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  144. expect(tripwire.warnings).toEqual([])
  145. await assertFixtureInventory(SNAPSHOT_DIR, ['manager.expected.md', 'live-enabled.expected.md'])
  146. })
  147. })
  148. describe('web e2e: startup-applied plugin management', () => {
  149. it('saves a bundle selection that waits for the next start and keeps its rows read-only', async () => {
  150. const scaffold = await launchWebScaffold({
  151. profile: { hmr: false, packages: [{ dir: join(FIXTURE_PLUGINS, 'fixture-bundle') }] },
  152. })
  153. let browser: Browser | undefined
  154. try {
  155. browser = await chromium.launch()
  156. const page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  157. const tripwire = watchConsole(page)
  158. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-live'))
  159. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  160. await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
  161. const panel = page.locator('[data-plugin-panel]')
  162. const toggle = panel.getByRole('switch', { name: '启用 bundle' })
  163. await toggle.waitFor({ timeout: 20_000 })
  164. const mounted = () => [...scaffold.ctx.loader.entries()].find(entry => entry.options.id === 'fixture-row')
  165. const bundles = async () => {
  166. const text = await readFile(join(scaffold.harnessHome, 'profiles', 'scaffold', 'package.json'), 'utf8')
  167. return (JSON.parse(text) as { dsh: { profile: { bundles: string[] } } }).dsh.profile.bundles
  168. }
  169. expect(mounted()?.fiber?.state).toBeUndefined()
  170. await toggle.click()
  171. // The selection is saved and the switch turns on, but nothing mounts before the next start; a toast says so.
  172. await expect.poll(bundles).toEqual(['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app', '@fixture/bundle'])
  173. await expect.poll(() => toggle.getAttribute('aria-checked')).toBe('true')
  174. await page.getByText('更改将在下次启动生效', { exact: true }).waitFor({ timeout: 10_000 })
  175. expect(mounted()?.fiber?.state).toBeUndefined()
  176. // The pack's page lists its rows from their declarations, with no live entry to switch.
  177. await panel.getByRole('button', { name: '查看 bundle' }).click()
  178. await panel.locator('[data-plugin-row]', { hasText: 'fixture-row' }).waitFor({ timeout: 10_000 })
  179. expect(await panel.getByRole('switch', { name: '启用组件 fixture-row' }).isDisabled()).toBe(true)
  180. await panel.getByRole('button', { name: '返回插件列表' }).click()
  181. await toggle.click()
  182. await expect.poll(bundles).toEqual(['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'])
  183. await expect.poll(() => toggle.getAttribute('aria-checked')).toBe('false')
  184. expect(tripwire.pageErrors).toEqual([])
  185. } finally {
  186. await browser?.close()
  187. await scaffold.close()
  188. }
  189. }, 60_000)
  190. })