plugin-config.e2e.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Web e2e scenario: the configuration pages on the Plugins page — the official
  2. // pages a deployment's exposed host-plane namespaces produce, one field edited
  3. // through the real wire down to `$DSH_HOME/settings.yaml`, the override badge
  4. // and reset that layering produces, and a community bundle's row configuration
  5. // registered by its own browser half. Zero model calls: everything is client
  6. // state plus the settings document and the profile on a blank frame, so there
  7. // is no fixture and a stray stream would fail loud on the open llm seam.
  8. import { readFile } from 'node:fs/promises'
  9. import { fileURLToPath } from 'node:url'
  10. import type { Browser, Locator, 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-config', import.meta.url))
  20. const OFFICIAL_EXPECTED = join(SNAPSHOT_DIR, 'official.expected.md')
  21. const ROW_EXPECTED = join(SNAPSHOT_DIR, 'row.expected.md')
  22. const FIXTURE_PLUGINS = fileURLToPath(new URL('./fixtures/plugins', import.meta.url))
  23. const MODE = webSnapshotMode()
  24. describe('web e2e: plugin configuration pages', () => {
  25. let scaffold: WebScaffold
  26. let browser: Browser
  27. let page: Page
  28. let tripwire: ReturnType<typeof watchConsole>
  29. beforeAll(async () => {
  30. // The live-client fixture is a bundle with a browser half; switched on
  31. // below, that half registers its row's configuration into the page.
  32. scaffold = await launchWebScaffold({
  33. profile: { packages: [{ dir: join(FIXTURE_PLUGINS, 'fixture-live-client') }] },
  34. })
  35. browser = await chromium.launch()
  36. // Chinese browser: the pages assert the localized copy the client derives
  37. // from it, as the rest of the settings surface does.
  38. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  39. tripwire = watchConsole(page)
  40. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  41. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  42. }, 120_000)
  43. afterAll(async () => {
  44. await browser?.close()
  45. await scaffold?.close()
  46. })
  47. /**
  48. * Show the Plugins page's cards. The scenarios share one page so the
  49. * settings document accumulates across them, so this closes any settings
  50. * dialog a previous scenario left open and leaves whatever page it opened.
  51. */
  52. async function openPlugins(): Promise<Locator> {
  53. if (await page.getByRole('dialog', { name: '设置' }).count() > 0) {
  54. await page.keyboard.press('Escape')
  55. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  56. }
  57. await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
  58. const panel = page.locator('[data-plugin-panel]')
  59. await panel.waitFor({ timeout: 10_000 })
  60. while (await panel.getByRole('button', { name: /^返回/ }).count() > 0) {
  61. await panel.getByRole('button', { name: /^返回/ }).first().click()
  62. }
  63. await panel.getByRole('heading', { name: '官方', exact: true }).waitFor({ timeout: 20_000 })
  64. return panel
  65. }
  66. /** Open one official plugin's page from its card and wait for its form. */
  67. async function openPage(panel: Locator, title: string): Promise<void> {
  68. await panel.getByRole('button', { name: `查看 ${title}`, exact: true }).click()
  69. await panel.locator('[data-plugin-config]').waitFor({ timeout: 10_000 })
  70. }
  71. /** The settings document as the Host has written it so far. */
  72. async function settingsDocument(): Promise<string> {
  73. return readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8').catch(() => '')
  74. }
  75. it('lists one official page per exposed host-plane namespace after the official bundles', async () => {
  76. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-cards'))
  77. const panel = await openPlugins()
  78. // Every page the shipped web composition exposes: the shell executor, the
  79. // agent loop, subagent selection, and the DeepSeek search provider, after
  80. // the two official bundles the installation ships switched off.
  81. await panel.getByRole('button', { name: '查看 网页搜索', exact: true }).waitFor({ timeout: 20_000 })
  82. const official = panel.locator('[data-plugin-group="official"]')
  83. expect(await official.locator('[data-plugin-package]').count()).toBe(2)
  84. expect(await official.locator('[data-plugin-item]').count()).toBe(4)
  85. for (const title of ['终端', 'Agent 循环', 'Subagent', '网页搜索']) {
  86. expect(await official.getByRole('button', { name: `查看 ${title}`, exact: true }).count()).toBe(1)
  87. }
  88. // A card carries the one-liner; the fields wait for the page.
  89. expect(await official.getByText('限制 agent 运行的每一条命令。', { exact: true }).count()).toBe(1)
  90. expect(await panel.getByLabel('命令超时(毫秒)').count()).toBe(0)
  91. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  92. await compareOrRefreshGolden(OFFICIAL_EXPECTED, snapshot, MODE)
  93. expect(tripwire.pageErrors).toEqual([])
  94. }, 60_000)
  95. it('saves subagent limits and resets them to the deployment defaults', async () => {
  96. const panel = await openPlugins()
  97. await openPage(panel, 'Subagent')
  98. const depth = panel.getByLabel('最大递归深度', { exact: true })
  99. const capacity = panel.getByLabel('Subagent 并行数量上限', { exact: true })
  100. expect(await depth.inputValue()).toBe('1')
  101. expect(await capacity.inputValue()).toBe('8')
  102. await depth.fill('2')
  103. await capacity.fill('12')
  104. await panel.getByRole('button', { name: '保存', exact: true }).click()
  105. await expect.poll(() => panel.getByRole('button', { name: '保存', exact: true }).isDisabled()).toBe(true)
  106. await expect.poll(settingsDocument).toContain('maxActiveSubagents: 12')
  107. await expect.poll(settingsDocument).toContain('maxDepth: 2')
  108. await openPlugins()
  109. await openPage(panel, 'Subagent')
  110. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  111. await compareOrRefreshGolden(join(SNAPSHOT_DIR, 'subagent.expected.md'), snapshot, MODE)
  112. const controlHeight = await depth.evaluate(element => element.getBoundingClientRect().height)
  113. await depth.fill('1.5')
  114. expect(await depth.evaluate(element => element.getBoundingClientRect().height)).toBe(controlHeight)
  115. expect(await panel.getByRole('button', { name: '保存', exact: true }).isDisabled()).toBe(true)
  116. await depth.fill('2')
  117. await panel.getByRole('button', { name: '恢复默认', exact: true }).first().click()
  118. await panel.getByRole('button', { name: '恢复默认', exact: true }).first().click()
  119. await panel.getByRole('button', { name: '保存', exact: true }).click()
  120. await expect.poll(() => panel.getByRole('button', { name: '保存', exact: true }).isDisabled()).toBe(true)
  121. await openPlugins()
  122. await openPage(panel, 'Subagent')
  123. expect(await depth.inputValue()).toBe('1')
  124. expect(await capacity.inputValue()).toBe('8')
  125. await panel.getByRole('button', { name: '返回插件列表', exact: true }).click()
  126. })
  127. it('opens field explanations with the keyboard and retains unsaved edits', async () => {
  128. const panel = await openPlugins()
  129. await openPage(panel, 'Subagent')
  130. const depth = panel.getByLabel('最大递归深度', { exact: true })
  131. await depth.fill('2')
  132. const depthHelp = panel.getByRole('button', { name: '最大递归深度说明', exact: true })
  133. expect(await panel.getByRole('region', { name: '最大递归深度说明', exact: true }).count()).toBe(0)
  134. await depthHelp.press('Enter')
  135. const depthRules = panel.getByRole('region', { name: '最大递归深度说明', exact: true })
  136. await depthRules.waitFor()
  137. expect(await depthRules.getByText('限制 Agent 创建 Subagent 的递归层级。', { exact: true }).count()).toBe(1)
  138. const depthTable = depthRules.getByRole('table', { name: '最大递归深度说明', exact: true })
  139. expect(await depthTable.getByRole('row', { name: '0 禁用 Subagent', exact: true }).count()).toBe(1)
  140. expect(await depthTable.getByRole('row', { name: '1 仅允许主 Agent 创建 Subagent', exact: true }).count()).toBe(1)
  141. expect(await depthRules.getByText('如果某个工具单独设置了最大递归深度,以该工具的设置为准。', { exact: true }).count()).toBe(1)
  142. await depthHelp.press('Enter')
  143. expect(await depthRules.count()).toBe(0)
  144. expect(await depth.inputValue()).toBe('2')
  145. await panel.getByRole('button', { name: 'Subagent 并行数量上限说明', exact: true }).click()
  146. const capacityRules = panel.getByRole('region', { name: 'Subagent 并行数量上限说明', exact: true })
  147. expect(await capacityRules.getByText('同一主 Agent 下,所有递归层级同时存活的 Subagent 总数,主 Agent 不计入。达到上限时,新的启动请求会被拒绝。', { exact: true }).count()).toBe(1)
  148. await panel.getByRole('button', { name: '返回插件列表', exact: true }).click()
  149. await openPage(panel, 'Subagent')
  150. expect(await depth.inputValue()).toBe('1')
  151. })
  152. it('saves limits and the model allowlist together from the shared card', async () => {
  153. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-subagent-model-selection'))
  154. const panel = await openPlugins()
  155. await openPage(panel, 'Subagent')
  156. const toggle = panel.getByRole('switch', { name: '允许 Agent 为 Subagent 选择模型' })
  157. await panel.getByLabel('最大递归深度', { exact: true }).fill('2')
  158. await toggle.click()
  159. const models = panel.getByRole('group', { name: 'Agent 可选择的模型' })
  160. await models.waitFor({ timeout: 10_000 })
  161. const firstModel = models.getByRole('checkbox').first()
  162. await firstModel.check()
  163. const save = panel.getByRole('button', { name: '保存', exact: true })
  164. await save.click()
  165. await expect.poll(async () => (await settingsDocument()).includes('subagent-model-selection:'), { timeout: 10_000 })
  166. .toBe(true)
  167. expect(await settingsDocument()).toContain('maxDepth: 2')
  168. expect(await settingsDocument()).toContain('enabled: true')
  169. expect(await settingsDocument()).toContain('allowedModels:')
  170. expect(await settingsDocument()).toContain('provider:')
  171. expect(await settingsDocument()).toContain('model:')
  172. // The page stays open once the save landed; a settled form offers no save to repeat.
  173. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 5_000 }).toBe('true')
  174. await expect.poll(() => save.isDisabled(), { timeout: 5_000 }).toBe(true)
  175. await toggle.click()
  176. await save.click()
  177. await expect.poll(async () => (await settingsDocument()).includes('enabled: false'), { timeout: 10_000 })
  178. .toBe(true)
  179. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 5_000 }).toBe('false')
  180. expect(tripwire.pageErrors).toEqual([])
  181. }, 60_000)
  182. it('stages an edit and writes it only when saved', async () => {
  183. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-write'))
  184. const panel = await openPlugins()
  185. await openPage(panel, '终端')
  186. const timeout = panel.getByLabel('命令超时(毫秒)')
  187. await timeout.waitFor({ timeout: 10_000 })
  188. // The composed default this deployment ships, before any user layer.
  189. expect(await timeout.inputValue()).toBe('60000')
  190. await timeout.fill('12000')
  191. await timeout.blur()
  192. // Nothing crosses the wire until the user saves: leaving the control is
  193. // not a decision to store the value.
  194. expect(await settingsDocument()).not.toContain('timeoutMs')
  195. const save = panel.getByRole('button', { name: '保存', exact: true })
  196. await expect.poll(() => save.isEnabled(), { timeout: 5_000 }).toBe(true)
  197. await save.click()
  198. await expect.poll(async () => (await settingsDocument()).includes('timeoutMs: 12000'), { timeout: 10_000 })
  199. .toBe(true)
  200. // Presence in the user layer is what the badge reports, and the reset is
  201. // offered only for a field that has one.
  202. await expect.poll(() => panel.getByText('已覆盖').count(), { timeout: 5_000 }).toBe(1)
  203. expect(await panel.getByRole('button', { name: '恢复默认' }).count()).toBe(1)
  204. // A settled form offers no save to repeat.
  205. await expect.poll(() => save.isDisabled(), { timeout: 5_000 }).toBe(true)
  206. expect(tripwire.pageErrors).toEqual([])
  207. }, 60_000)
  208. it('drops a staged edit when the page is left, without touching the document', async () => {
  209. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-leave'))
  210. const panel = await openPlugins()
  211. await openPage(panel, '终端')
  212. const timeout = panel.getByLabel('命令超时(毫秒)')
  213. await timeout.waitFor({ timeout: 10_000 })
  214. await timeout.fill('7000')
  215. await panel.getByRole('button', { name: '返回插件列表' }).click()
  216. await panel.getByRole('heading', { name: '官方', exact: true }).waitFor({ timeout: 10_000 })
  217. await openPage(panel, '终端')
  218. await expect.poll(() => panel.getByLabel('命令超时(毫秒)').inputValue(), { timeout: 5_000 }).toBe('12000')
  219. expect(await settingsDocument()).toContain('timeoutMs: 12000')
  220. expect(tripwire.pageErrors).toEqual([])
  221. }, 60_000)
  222. it('refuses to save a draft that is not a number', async () => {
  223. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-invalid'))
  224. const panel = await openPlugins()
  225. await openPage(panel, '终端')
  226. const timeout = panel.getByLabel('命令超时(毫秒)')
  227. await timeout.waitFor({ timeout: 10_000 })
  228. await timeout.fill('soon')
  229. const save = panel.getByRole('button', { name: '保存', exact: true })
  230. await expect.poll(() => save.isDisabled(), { timeout: 5_000 }).toBe(true)
  231. expect(await panel.getByText('请填数字;留空表示使用默认值。').count()).toBe(1)
  232. expect(tripwire.pageErrors).toEqual([])
  233. }, 60_000)
  234. it('clears the field back to the composed default on reset', async () => {
  235. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-reset'))
  236. const panel = await openPlugins()
  237. await openPage(panel, '终端')
  238. const timeout = panel.getByLabel('命令超时(毫秒)')
  239. await timeout.waitFor({ timeout: 10_000 })
  240. expect(await timeout.inputValue()).toBe('12000')
  241. // The reset stages the composed default; the document still carries the
  242. // override until the save lands.
  243. await panel.getByRole('button', { name: '恢复默认' }).click()
  244. await expect.poll(() => timeout.inputValue(), { timeout: 5_000 }).toBe('60000')
  245. expect(await settingsDocument()).toContain('timeoutMs: 12000')
  246. await panel.getByRole('button', { name: '保存', exact: true }).click()
  247. await expect.poll(async () => (await settingsDocument()).includes('timeoutMs'), { timeout: 10_000 })
  248. .toBe(false)
  249. expect(await timeout.inputValue()).toBe('60000')
  250. expect(await panel.getByText('已覆盖').count()).toBe(0)
  251. expect(tripwire.pageErrors).toEqual([])
  252. }, 60_000)
  253. it('renders a community bundle\'s row configuration, registered by its browser half, on the row\'s page', async () => {
  254. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-row'))
  255. const panel = await openPlugins()
  256. // Off, the bundle's browser half is not loaded and the row has no configuration to open.
  257. await panel.getByRole('button', { name: '查看 live-client', exact: true }).click()
  258. const row = panel.locator('[data-plugin-row]', { hasText: 'fixture-live-client' })
  259. await row.waitFor({ timeout: 10_000 })
  260. expect(await panel.getByRole('button', { name: '配置 fixture-live-client' }).count()).toBe(0)
  261. // Switched on, the Host recomposes and the browser half mounts without a
  262. // reload; its registration puts the configure control on the row.
  263. await panel.getByRole('switch', { name: '启用 live-client' }).click()
  264. const configure = panel.getByRole('button', { name: '配置 fixture-live-client' })
  265. await configure.waitFor({ timeout: 30_000 })
  266. await configure.click()
  267. const rowPage = panel.locator('[data-plugin-row-detail="@fixture/live-client#fixture-live-client"]')
  268. await rowPage.waitFor({ timeout: 10_000 })
  269. expect(await rowPage.getByRole('heading', { level: 3 }).textContent()).toBe('fixture-live-client')
  270. expect(await rowPage.getByText('示例配置项', { exact: true }).count()).toBe(1)
  271. const form = rowPage.getByRole('form', { name: '动态插件配置' })
  272. await form.getByLabel('问候语').fill('你好')
  273. await form.getByRole('button', { name: '保存' }).click()
  274. await expect.poll(() => page.evaluate(() => document.documentElement.dataset.liveSaves), { timeout: 5_000 }).toBe('1')
  275. const snapshot = await captureStableAria(page, '[data-plugin-panel]', scaffold.workspaceCwd)
  276. await compareOrRefreshGolden(ROW_EXPECTED, snapshot, MODE)
  277. await rowPage.getByRole('button', { name: '返回 live-client' }).click()
  278. await panel.locator('[data-plugin-detail="@fixture/live-client"]').waitFor({ timeout: 10_000 })
  279. expect(tripwire.pageErrors).toEqual([])
  280. }, 90_000)
  281. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  282. expect(tripwire.warnings).toEqual([])
  283. await assertFixtureInventory(SNAPSHOT_DIR, ['official.expected.md', 'row.expected.md', 'subagent.expected.md'])
  284. })
  285. })