plugin-config.e2e.ts 11 KB

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