plugin-manager.e2e.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Web e2e scenario: the plugin manager page behind the sidebar's Plugins entry over a scaffold
  2. // profile runtime: installed packages and bundle enablement. Zero model calls: everything is
  3. // client state plus the profile files and the settings document, so there is
  4. // 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. profileRuntime: {
  29. packages: [
  30. { dir: join(FIXTURE_PLUGINS, 'fixture-bundle') },
  31. { dir: join(FIXTURE_PLUGINS, 'fixture-plain-plugin') },
  32. ],
  33. },
  34. })
  35. browser = await chromium.launch()
  36. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  37. tripwire = watchConsole(page)
  38. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  39. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  40. }, 120_000)
  41. afterAll(async () => {
  42. await browser?.close()
  43. await scaffold?.close()
  44. })
  45. /** Close any open settings dialog, so the sidebar and the main column are clickable. */
  46. async function closeSettings() {
  47. if (await page.getByRole('dialog', { name: '设置' }).count() > 0) {
  48. await page.keyboard.press('Escape')
  49. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  50. }
  51. }
  52. /** Select the sidebar's Plugins entry and wait for the management page in the main column. */
  53. async function openPluginsPanel() {
  54. await closeSettings()
  55. await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
  56. const panel = page.locator('[data-plugin-panel]')
  57. await panel.getByRole('heading', { name: '插件管理' }).waitFor({ timeout: 10_000 })
  58. return panel
  59. }
  60. /** One file under the harness home, or the empty string while it does not exist. */
  61. async function homeFile(...segments: string[]): Promise<string> {
  62. return readFile(join(scaffold.harnessHome, ...segments), 'utf8').catch(() => '')
  63. }
  64. it('lists the profile packages with their switches', async () => {
  65. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-list'))
  66. const panel = await openPluginsPanel()
  67. await panel.getByText('示例组合包', { exact: true }).waitFor({ timeout: 20_000 })
  68. expect(await panel.getByText('示例插件', { exact: true }).count()).toBe(1)
  69. // Non-bundle packages remain installed and expose their uninstall on the detail page.
  70. const toggle = panel.getByRole('switch', { name: '启用 示例组合包' })
  71. expect(await toggle.getAttribute('aria-checked')).toBe('false')
  72. expect(await panel.getByRole('switch', { name: '启用 示例插件' }).count()).toBe(0)
  73. expect(await panel.getByRole('button', { name: '加入全局' }).count()).toBe(0)
  74. expect(await panel.getByRole('button', { name: '卸载 示例插件' }).count()).toBe(0)
  75. await panel.getByRole('button', { name: '查看 示例插件' }).click()
  76. await panel.getByRole('button', { name: '卸载 示例插件' }).waitFor({ timeout: 5_000 })
  77. await panel.getByText('此包未提供组合包 patch,可通过 Cordis 配置手动加载模块。', { exact: true }).waitFor({ timeout: 5_000 })
  78. await panel.getByRole('button', { name: '返回插件列表' }).click()
  79. await expect.poll(() => panel.getByRole('button', { name: '卸载 示例插件' }).count(), { timeout: 5_000 }).toBe(0)
  80. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  81. await compareOrRefreshGolden(MANAGER_EXPECTED, snapshot, MODE)
  82. expect(tripwire.pageErrors).toEqual([])
  83. }, 60_000)
  84. it('enables a bundle into the profile manifest and reports the restart it waits for', async () => {
  85. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-enable'))
  86. const panel = await openPluginsPanel()
  87. const toggle = panel.getByRole('switch', { name: '启用 示例组合包' })
  88. await toggle.waitFor({ timeout: 20_000 })
  89. await toggle.click()
  90. await expect.poll(async () => (await homeFile('profiles', 'scaffold', 'package.json')).includes('"@fixture/bundle"'), {
  91. timeout: 10_000,
  92. }).toBe(true)
  93. const manifest = JSON.parse(await homeFile('profiles', 'scaffold', 'package.json')) as {
  94. dsh: { profile: { bundles: string[] } }
  95. }
  96. expect(manifest.dsh.profile.bundles).toEqual(['@fixture/bundle'])
  97. // A startup-applied profile: the switch is on, and the banner names the package.
  98. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 10_000 }).toBe('true')
  99. await panel.getByText('以下更改会在下次启动生效:示例组合包').waitFor({ timeout: 10_000 })
  100. expect(await panel.getByText('需重启', { exact: true }).count()).toBe(1)
  101. // The pack's page lists its rows from its static declarations. A profile that applies
  102. // patches at its next start keeps them read-only: no row switch.
  103. await panel.getByRole('button', { name: '查看 示例组合包' }).click()
  104. await panel.locator('[data-plugin-row]', { hasText: 'fixture-row' }).waitFor({ timeout: 10_000 })
  105. expect(await panel.getByRole('switch', { name: '启用组件 fixture-row' }).count()).toBe(0)
  106. await panel.getByRole('button', { name: '返回插件列表' }).click()
  107. expect(tripwire.pageErrors).toEqual([])
  108. }, 60_000)
  109. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  110. expect(tripwire.warnings).toEqual([])
  111. await assertFixtureInventory(SNAPSHOT_DIR, ['manager.expected.md', 'live-enabled.expected.md'])
  112. })
  113. })
  114. describe('web e2e: live plugin management', () => {
  115. it('applies bundle enable and disable through the Remote without a restart', async () => {
  116. const scaffold = await launchWebScaffold({
  117. profileRuntime: { patchReload: 'live', packages: [{ dir: join(FIXTURE_PLUGINS, 'fixture-bundle') }] },
  118. })
  119. let browser: Browser | undefined
  120. try {
  121. browser = await chromium.launch()
  122. const page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  123. const tripwire = watchConsole(page)
  124. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-manager-live'))
  125. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  126. await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
  127. const panel = page.locator('[data-plugin-panel]')
  128. const toggle = panel.getByRole('switch', { name: '启用 示例组合包' })
  129. await toggle.waitFor({ timeout: 20_000 })
  130. const mounted = () => [...scaffold.ctx.loader.entries()].find(entry => entry.options.id === 'fixture-row')
  131. const bundles = async () => {
  132. const text = await readFile(join(scaffold.harnessHome, 'profiles', 'scaffold', 'package.json'), 'utf8')
  133. return (JSON.parse(text) as { dsh: { profile: { bundles: string[] } } }).dsh.profile.bundles
  134. }
  135. expect(mounted()).toBeUndefined()
  136. await toggle.click()
  137. await expect.poll(() => mounted()?.fiber?.state, { timeout: 10_000 }).toBe(2)
  138. await expect.poll(bundles).toEqual(['@fixture/bundle'])
  139. await expect.poll(() => toggle.getAttribute('aria-checked')).toBe('true')
  140. expect(await panel.getByText('需重启', { exact: true }).count()).toBe(0)
  141. expect(await panel.getByText(/以下更改会在下次启动生效/).count()).toBe(0)
  142. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  143. await compareOrRefreshGolden(LIVE_EXPECTED, snapshot, MODE)
  144. await toggle.click()
  145. await expect.poll(mounted, { timeout: 10_000 }).toBeUndefined()
  146. await expect.poll(bundles).toEqual([])
  147. await expect.poll(() => toggle.getAttribute('aria-checked')).toBe('false')
  148. expect(await panel.getByText('需重启', { exact: true }).count()).toBe(0)
  149. expect(await panel.getByText(/以下更改会在下次启动生效/).count()).toBe(0)
  150. expect(tripwire.pageErrors).toEqual([])
  151. } finally {
  152. await browser?.close()
  153. await scaffold.close()
  154. }
  155. }, 60_000)
  156. })