plugin-config.e2e.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. await dialog.getByRole('tab', { name: '插件配置', exact: true }).click()
  59. await expect
  60. .poll(() => dialog.getByRole('tab', { name: '插件配置', exact: true }).getAttribute('aria-selected'), { timeout: 5_000 })
  61. .toBe('true')
  62. return dialog
  63. }
  64. /** The settings document as the Host has written it so far. */
  65. async function settingsDocument(): Promise<string> {
  66. return readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8').catch(() => '')
  67. }
  68. it('shows one card per exposed host-plane namespace', async () => {
  69. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-cards'))
  70. const dialog = await openPlugins()
  71. // Every card the shipped web composition exposes: the shell executor, the
  72. // agent loop, subagent selection, and the DeepSeek search provider.
  73. await dialog.getByText('Subagent', { exact: true }).waitFor({ timeout: 10_000 })
  74. expect(await dialog.getByRole('button', { name: '展开设置: Subagent' }).count()).toBe(1)
  75. await dialog.getByText('终端', { exact: true }).waitFor({ timeout: 10_000 })
  76. expect(await dialog.getByText('Agent 循环', { exact: true }).count()).toBe(1)
  77. expect(await dialog.getByText('网页搜索', { exact: true }).count()).toBe(1)
  78. // Collapsed: a card's fields appear only once it is expanded.
  79. expect(await dialog.getByLabel('命令超时(毫秒)').count()).toBe(0)
  80. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  81. await compareOrRefreshGolden(SECTION_EXPECTED, snapshot, MODE)
  82. expect(tripwire.pageErrors).toEqual([])
  83. }, 60_000)
  84. it('persists selected adapter routes as the subagent model allowlist', async () => {
  85. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-subagent-model-selection'))
  86. const dialog = await openPlugins()
  87. await dialog.getByText('Subagent', { exact: true }).click()
  88. const toggle = dialog.getByRole('switch', { name: '允许 Agent 为 Subagent 选择模型' })
  89. await toggle.click()
  90. const models = dialog.getByRole('group', { name: 'Agent 可选择的模型' })
  91. await models.waitFor({ timeout: 10_000 })
  92. const firstModel = models.getByRole('checkbox').first()
  93. await firstModel.check()
  94. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  95. const expandSubagent = dialog.getByRole('button', { name: '展开设置: Subagent' })
  96. await expandSubagent.waitFor({ timeout: 5_000 })
  97. await expect.poll(async () => (await settingsDocument()).includes('subagent-model-selection:'), { timeout: 10_000 })
  98. .toBe(true)
  99. expect(await settingsDocument()).toContain('enabled: true')
  100. expect(await settingsDocument()).toContain('allowedModels:')
  101. expect(await settingsDocument()).toContain('provider:')
  102. expect(await settingsDocument()).toContain('model:')
  103. await expandSubagent.click()
  104. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 5_000 }).toBe('true')
  105. await expect.poll(() => dialog.getByRole('button', { name: '保存', exact: true }).isDisabled()).toBe(true)
  106. expect(await dialog.getByText('未保存', { exact: true }).count()).toBe(0)
  107. await toggle.click()
  108. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  109. await expandSubagent.waitFor({ timeout: 5_000 })
  110. await expect.poll(async () => (await settingsDocument()).includes('enabled: false'), { timeout: 10_000 })
  111. .toBe(true)
  112. expect(await settingsDocument()).toContain('allowedModels:')
  113. expect(await settingsDocument()).toContain('provider:')
  114. expect(await settingsDocument()).toContain('model:')
  115. await expandSubagent.click()
  116. await expect.poll(() => toggle.getAttribute('aria-checked'), { timeout: 5_000 }).toBe('false')
  117. expect(tripwire.pageErrors).toEqual([])
  118. }, 60_000)
  119. it('stages an edit and writes it only when saved', async () => {
  120. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-write'))
  121. const dialog = await openPlugins()
  122. await dialog.getByText('终端', { exact: true }).click()
  123. const timeout = dialog.getByLabel('命令超时(毫秒)')
  124. await timeout.waitFor({ timeout: 10_000 })
  125. // The composed default this deployment ships, before any user layer.
  126. expect(await timeout.inputValue()).toBe('60000')
  127. await timeout.fill('12000')
  128. await timeout.blur()
  129. // Nothing crosses the wire until the user saves: leaving the control is
  130. // not a decision to store the value.
  131. expect(await settingsDocument()).not.toContain('timeoutMs')
  132. const save = dialog.getByRole('button', { name: '保存', exact: true })
  133. await expect.poll(() => save.isEnabled(), { timeout: 5_000 }).toBe(true)
  134. await save.click()
  135. await expect.poll(async () => (await settingsDocument()).includes('timeoutMs: 12000'), { timeout: 10_000 })
  136. .toBe(true)
  137. const expandTerminal = dialog.getByRole('button', { name: '展开设置: 终端' })
  138. await expandTerminal.waitFor({ timeout: 5_000 })
  139. await expandTerminal.click()
  140. // Presence in the user layer is what the badge reports, and the reset is
  141. // offered only for a field that has one.
  142. await expect.poll(() => dialog.getByText('已覆盖').count(), { timeout: 5_000 }).toBe(1)
  143. expect(await dialog.getByRole('button', { name: '恢复默认' }).count()).toBe(1)
  144. // A settled form offers no save to repeat.
  145. await expect.poll(() => save.isDisabled(), { timeout: 5_000 }).toBe(true)
  146. expect(tripwire.pageErrors).toEqual([])
  147. }, 60_000)
  148. it('drops a staged edit on discard without touching the document', async () => {
  149. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-discard'))
  150. const dialog = await openPlugins()
  151. await dialog.getByText('终端', { exact: true }).click()
  152. const timeout = dialog.getByLabel('命令超时(毫秒)')
  153. await timeout.waitFor({ timeout: 10_000 })
  154. await timeout.fill('7000')
  155. await dialog.getByRole('button', { name: '放弃修改' }).click()
  156. await expect.poll(() => timeout.inputValue(), { timeout: 5_000 }).toBe('12000')
  157. expect(await settingsDocument()).toContain('timeoutMs: 12000')
  158. expect(tripwire.pageErrors).toEqual([])
  159. }, 60_000)
  160. it('refuses to save a draft that is not a number', async () => {
  161. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-invalid'))
  162. const dialog = await openPlugins()
  163. await dialog.getByText('终端', { exact: true }).click()
  164. const timeout = dialog.getByLabel('命令超时(毫秒)')
  165. await timeout.waitFor({ timeout: 10_000 })
  166. await timeout.fill('soon')
  167. const save = dialog.getByRole('button', { name: '保存', exact: true })
  168. await expect.poll(() => save.isDisabled(), { timeout: 5_000 }).toBe(true)
  169. expect(await dialog.getByText('请填数字;留空表示使用默认值。').count()).toBe(1)
  170. await dialog.getByRole('button', { name: '放弃修改' }).click()
  171. expect(tripwire.pageErrors).toEqual([])
  172. }, 60_000)
  173. it('clears the field back to the composed default on reset', async () => {
  174. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-reset'))
  175. const dialog = await openPlugins()
  176. await dialog.getByText('终端', { exact: true }).click()
  177. const timeout = dialog.getByLabel('命令超时(毫秒)')
  178. await timeout.waitFor({ timeout: 10_000 })
  179. expect(await timeout.inputValue()).toBe('12000')
  180. // The reset stages the composed default; the document still carries the
  181. // override until the save lands.
  182. await dialog.getByRole('button', { name: '恢复默认' }).click()
  183. await expect.poll(() => timeout.inputValue(), { timeout: 5_000 }).toBe('60000')
  184. expect(await settingsDocument()).toContain('timeoutMs: 12000')
  185. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  186. await expect.poll(async () => (await settingsDocument()).includes('timeoutMs'), { timeout: 10_000 })
  187. .toBe(false)
  188. const expandTerminal = dialog.getByRole('button', { name: '展开设置: 终端' })
  189. await expandTerminal.waitFor({ timeout: 5_000 })
  190. await expandTerminal.click()
  191. expect(await timeout.inputValue()).toBe('60000')
  192. expect(await dialog.getByText('已覆盖').count()).toBe(0)
  193. expect(tripwire.pageErrors).toEqual([])
  194. }, 60_000)
  195. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  196. expect(tripwire.warnings).toEqual([])
  197. await assertFixtureInventory(SNAPSHOT_DIR, ['section.expected.md'])
  198. })
  199. })