plugin-config.e2e.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Web e2e scenario: the configurable tab in Plugins settings — the cards a
  2. // deployment's exposed host-plane namespaces produce, one field edited through the real
  3. // wire down to `$DSH_HOME/settings.yaml`, and the override badge and reset
  4. // that layering produces. Zero model calls: everything is client state plus
  5. // the settings document on a blank frame, so there is no fixture and a stray
  6. // stream would fail loud on the open llm seam.
  7. import { readFile } from 'node:fs/promises'
  8. import { fileURLToPath } from 'node:url'
  9. import type { Browser, Page } from 'playwright'
  10. import { chromium } from 'playwright'
  11. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  12. import { join } from 'node:path'
  13. import {
  14. assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  15. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  16. } from './scaffold.ts'
  17. import { ZH_BROWSER_LOCALE, saveFailureShot } from './support.ts'
  18. const SNAPSHOT_DIR = fileURLToPath(new URL('./expected/plugin-config', import.meta.url))
  19. const SECTION_EXPECTED = join(SNAPSHOT_DIR, 'section.expected.md')
  20. const MODE = webSnapshotMode()
  21. describe('web e2e: plugin configuration section', () => {
  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. browser = await chromium.launch()
  29. // Chinese browser: the section asserts the localized copy the client
  30. // derives from it, as the rest of the settings surface does.
  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. /**
  41. * Open the settings dialog on the Plugins section. The scenarios share one
  42. * page so the settings document accumulates across them, so this leaves any
  43. * dialog a previous scenario opened closed first — its mask would otherwise
  44. * swallow the trigger click.
  45. */
  46. async function openPlugins() {
  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. await page.getByRole('button', { name: '设置', exact: true }).click()
  52. const dialog = page.getByRole('dialog', { name: '设置' })
  53. await dialog.waitFor({ timeout: 10_000 })
  54. await dialog.getByRole('button', { name: '插件', exact: true }).click()
  55. await expect
  56. .poll(() => dialog.getByRole('button', { name: '插件', exact: true }).getAttribute('aria-current'), { timeout: 5_000 })
  57. .toBe('true')
  58. return dialog
  59. }
  60. /** The settings document as the Host has written it so far. */
  61. async function settingsDocument(): Promise<string> {
  62. return readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8').catch(() => '')
  63. }
  64. it('groups both Subagent namespaces into one card', async () => {
  65. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-cards'))
  66. const dialog = await openPlugins()
  67. // Every card the shipped web composition exposes: the shell executor, the
  68. // agent loop, subagent selection, and the DeepSeek search provider.
  69. await dialog.getByText('Subagent', { exact: true }).waitFor({ timeout: 10_000 })
  70. expect(await dialog.getByRole('button', { name: '展开设置: Subagent' }).count()).toBe(1)
  71. await dialog.getByText('终端', { exact: true }).waitFor({ timeout: 10_000 })
  72. expect(await dialog.getByText('Agent 循环', { exact: true }).count()).toBe(1)
  73. expect(await dialog.getByText('网页搜索', { exact: true }).count()).toBe(1)
  74. // Collapsed: a card's fields appear only once it is expanded.
  75. expect(await dialog.getByLabel('命令超时(毫秒)').count()).toBe(0)
  76. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  77. await compareOrRefreshGolden(SECTION_EXPECTED, snapshot, MODE)
  78. expect(tripwire.pageErrors).toEqual([])
  79. }, 60_000)
  80. it('saves subagent limits and resets them to the deployment defaults', async () => {
  81. const dialog = await openPlugins()
  82. await dialog.getByRole('button', { name: '展开设置: Subagent', exact: true }).click()
  83. const depth = dialog.getByLabel('最大递归深度', { exact: true })
  84. const capacity = dialog.getByLabel('Subagent 并行数量上限', { exact: true })
  85. expect(await depth.inputValue()).toBe('3')
  86. expect(await capacity.inputValue()).toBe('8')
  87. await depth.fill('2')
  88. await capacity.fill('12')
  89. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  90. await dialog.getByRole('button', { name: '展开设置: Subagent', exact: true }).waitFor()
  91. await expect.poll(settingsDocument).toContain('maxActiveSubagents: 12')
  92. await expect.poll(settingsDocument).toContain('maxDepth: 2')
  93. await dialog.getByRole('button', { name: '展开设置: Subagent', exact: true }).click()
  94. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  95. await compareOrRefreshGolden(join(SNAPSHOT_DIR, 'subagent.expected.md'), snapshot, MODE)
  96. const controlHeight = await depth.evaluate(element => element.getBoundingClientRect().height)
  97. await depth.fill('1.5')
  98. expect(await depth.evaluate(element => element.getBoundingClientRect().height)).toBe(controlHeight)
  99. expect(await dialog.getByRole('button', { name: '保存', exact: true }).isDisabled()).toBe(true)
  100. await depth.fill('2')
  101. await dialog.getByRole('button', { name: '恢复默认', exact: true }).first().click()
  102. await dialog.getByRole('button', { name: '恢复默认', exact: true }).first().click()
  103. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  104. await dialog.getByRole('button', { name: '展开设置: Subagent', exact: true }).waitFor()
  105. await dialog.getByRole('button', { name: '展开设置: Subagent', exact: true }).click()
  106. expect(await depth.inputValue()).toBe('3')
  107. expect(await capacity.inputValue()).toBe('8')
  108. await dialog.getByRole('button', { name: '收起设置: Subagent', exact: true }).click()
  109. })
  110. it('opens field explanations with the keyboard and retains unsaved edits', async () => {
  111. const dialog = await openPlugins()
  112. await dialog.getByRole('button', { name: '展开设置: Subagent', exact: true }).click()
  113. const depth = dialog.getByLabel('最大递归深度', { exact: true })
  114. await depth.fill('2')
  115. const depthHelp = dialog.getByRole('button', { name: '最大递归深度说明', exact: true })
  116. expect(await dialog.getByRole('region', { name: '最大递归深度说明', exact: true }).count()).toBe(0)
  117. await depthHelp.press('Enter')
  118. const depthRules = dialog.getByRole('region', { name: '最大递归深度说明', exact: true })
  119. await depthRules.waitFor()
  120. expect(await depthRules.getByText('限制 Agent 创建 Subagent 的递归层级。', { exact: true }).count()).toBe(1)
  121. const depthTable = depthRules.getByRole('table', { name: '最大递归深度说明', exact: true })
  122. expect(await depthTable.getByRole('row', { name: '0 禁用 Subagent', exact: true }).count()).toBe(1)
  123. expect(await depthTable.getByRole('row', { name: '1 仅允许主 Agent 创建 Subagent', exact: true }).count()).toBe(1)
  124. expect(await depthRules.getByText('如果某个工具单独设置了最大递归深度,以该工具的设置为准。', { exact: true }).count()).toBe(1)
  125. await depthHelp.press('Enter')
  126. expect(await depthRules.count()).toBe(0)
  127. expect(await depth.inputValue()).toBe('2')
  128. await dialog.getByRole('button', { name: 'Subagent 并行数量上限说明', exact: true }).click()
  129. const capacityRules = dialog.getByRole('region', { name: 'Subagent 并行数量上限说明', exact: true })
  130. expect(await capacityRules.getByText('同一主 Agent 下,所有递归层级同时存活的 Subagent 总数,等待中的 Subagent 和主 Agent 不计入。', { exact: true }).count()).toBe(1)
  131. await dialog.getByRole('button', { name: '放弃修改', exact: true }).click()
  132. expect(await depth.inputValue()).toBe('3')
  133. })
  134. it('saves limits and the model allowlist together from the shared card', async () => {
  135. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-subagent-model-selection'))
  136. const dialog = await openPlugins()
  137. await dialog.getByText('Subagent', { exact: true }).click()
  138. const toggle = dialog.getByRole('switch', { name: '允许 Agent 为 Subagent 选择模型' })
  139. await dialog.getByLabel('最大递归深度', { exact: true }).fill('2')
  140. await toggle.click()
  141. const models = dialog.getByRole('group', { name: 'Agent 可选择的模型' })
  142. await models.waitFor({ timeout: 10_000 })
  143. const firstModel = models.getByRole('checkbox').first()
  144. await firstModel.check()
  145. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  146. const expandSubagent = dialog.getByRole('button', { name: '展开设置: Subagent' })
  147. await expandSubagent.waitFor({ timeout: 5_000 })
  148. await expect.poll(async () => (await settingsDocument()).includes('subagent-model-selection:'), { timeout: 10_000 })
  149. .toBe(true)
  150. expect(await settingsDocument()).toContain('maxDepth: 2')
  151. expect(await settingsDocument()).toContain('enabled: true')
  152. expect(await settingsDocument()).toContain('allowedModels:')
  153. expect(await settingsDocument()).toContain('provider:')
  154. expect(await settingsDocument()).toContain('model:')
  155. await expandSubagent.click()
  156. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 5_000 }).toBe('true')
  157. await expect.poll(() => dialog.getByRole('button', { name: '保存', exact: true }).isDisabled()).toBe(true)
  158. expect(await dialog.getByText('未保存', { exact: true }).count()).toBe(0)
  159. await toggle.click()
  160. await dialog.getByRole('button', { name: '恢复默认', exact: true }).click()
  161. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  162. await expandSubagent.waitFor({ timeout: 5_000 })
  163. await expect.poll(async () => (await settingsDocument()).includes('enabled: false'), { timeout: 10_000 })
  164. .toBe(true)
  165. expect(await settingsDocument()).toContain('allowedModels:')
  166. expect(await settingsDocument()).toContain('provider:')
  167. expect(await settingsDocument()).toContain('model:')
  168. await expandSubagent.click()
  169. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 5_000 }).toBe('false')
  170. expect(tripwire.pageErrors).toEqual([])
  171. }, 60_000)
  172. it('stages an edit and writes it only when saved', async () => {
  173. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-write'))
  174. const dialog = await openPlugins()
  175. await dialog.getByText('终端', { exact: true }).click()
  176. const timeout = dialog.getByLabel('命令超时(毫秒)')
  177. await timeout.waitFor({ timeout: 10_000 })
  178. // The composed default this deployment ships, before any user layer.
  179. expect(await timeout.inputValue()).toBe('60000')
  180. await timeout.fill('12000')
  181. await timeout.blur()
  182. // Nothing crosses the wire until the user saves: leaving the control is
  183. // not a decision to store the value.
  184. expect(await settingsDocument()).not.toContain('timeoutMs')
  185. const save = dialog.getByRole('button', { name: '保存', exact: true })
  186. await expect.poll(() => save.isEnabled(), { timeout: 5_000 }).toBe(true)
  187. await save.click()
  188. await expect.poll(async () => (await settingsDocument()).includes('timeoutMs: 12000'), { timeout: 10_000 })
  189. .toBe(true)
  190. const expandTerminal = dialog.getByRole('button', { name: '展开设置: 终端' })
  191. await expandTerminal.waitFor({ timeout: 5_000 })
  192. await expandTerminal.click()
  193. // Presence in the user layer is what the badge reports, and the reset is
  194. // offered only for a field that has one.
  195. await expect.poll(() => dialog.getByText('已覆盖').count(), { timeout: 5_000 }).toBe(1)
  196. expect(await dialog.getByRole('button', { name: '恢复默认' }).count()).toBe(1)
  197. // A settled form offers no save to repeat.
  198. await expect.poll(() => save.isDisabled(), { timeout: 5_000 }).toBe(true)
  199. expect(tripwire.pageErrors).toEqual([])
  200. }, 60_000)
  201. it('drops a staged edit on discard without touching the document', async () => {
  202. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-discard'))
  203. const dialog = await openPlugins()
  204. await dialog.getByText('终端', { exact: true }).click()
  205. const timeout = dialog.getByLabel('命令超时(毫秒)')
  206. await timeout.waitFor({ timeout: 10_000 })
  207. await timeout.fill('7000')
  208. await dialog.getByRole('button', { name: '放弃修改' }).click()
  209. await expect.poll(() => timeout.inputValue(), { timeout: 5_000 }).toBe('12000')
  210. expect(await settingsDocument()).toContain('timeoutMs: 12000')
  211. expect(tripwire.pageErrors).toEqual([])
  212. }, 60_000)
  213. it('refuses to save a draft that is not a number', async () => {
  214. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-invalid'))
  215. const dialog = await openPlugins()
  216. await dialog.getByText('终端', { exact: true }).click()
  217. const timeout = dialog.getByLabel('命令超时(毫秒)')
  218. await timeout.waitFor({ timeout: 10_000 })
  219. await timeout.fill('soon')
  220. const save = dialog.getByRole('button', { name: '保存', exact: true })
  221. await expect.poll(() => save.isDisabled(), { timeout: 5_000 }).toBe(true)
  222. expect(await dialog.getByText('请填数字;留空表示使用默认值。').count()).toBe(1)
  223. await dialog.getByRole('button', { name: '放弃修改' }).click()
  224. expect(tripwire.pageErrors).toEqual([])
  225. }, 60_000)
  226. it('clears the field back to the composed default on reset', async () => {
  227. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-reset'))
  228. const dialog = await openPlugins()
  229. await dialog.getByText('终端', { exact: true }).click()
  230. const timeout = dialog.getByLabel('命令超时(毫秒)')
  231. await timeout.waitFor({ timeout: 10_000 })
  232. expect(await timeout.inputValue()).toBe('12000')
  233. // The reset stages the composed default; the document still carries the
  234. // override until the save lands.
  235. await dialog.getByRole('button', { name: '恢复默认' }).click()
  236. await expect.poll(() => timeout.inputValue(), { timeout: 5_000 }).toBe('60000')
  237. expect(await settingsDocument()).toContain('timeoutMs: 12000')
  238. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  239. await expect.poll(async () => (await settingsDocument()).includes('timeoutMs'), { timeout: 10_000 })
  240. .toBe(false)
  241. const expandTerminal = dialog.getByRole('button', { name: '展开设置: 终端' })
  242. await expandTerminal.waitFor({ timeout: 5_000 })
  243. await expandTerminal.click()
  244. expect(await timeout.inputValue()).toBe('60000')
  245. expect(await dialog.getByText('已覆盖').count()).toBe(0)
  246. expect(tripwire.pageErrors).toEqual([])
  247. }, 60_000)
  248. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  249. expect(tripwire.warnings).toEqual([])
  250. await assertFixtureInventory(SNAPSHOT_DIR, ['section.expected.md', 'subagent.expected.md'])
  251. })
  252. })