plugin-manager.e2e.ts 12 KB

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