plugin-config.e2e.ts 11 KB

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