plugin-config.e2e.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.baseUrl, { 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, and the DeepSeek search provider.
  72. await dialog.getByText('终端', { exact: true }).waitFor({ timeout: 10_000 })
  73. expect(await dialog.getByText('Agent 循环', { exact: true }).count()).toBe(1)
  74. expect(await dialog.getByText('网页搜索', { exact: true }).count()).toBe(1)
  75. // Collapsed: a card's fields appear only once it is expanded.
  76. expect(await dialog.getByLabel('命令超时(毫秒)').count()).toBe(0)
  77. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  78. await compareOrRefreshGolden(SECTION_EXPECTED, snapshot, MODE)
  79. expect(tripwire.pageErrors).toEqual([])
  80. }, 60_000)
  81. it('stages an edit and writes it only when saved', async () => {
  82. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-write'))
  83. const dialog = await openPlugins()
  84. await dialog.getByText('终端', { exact: true }).click()
  85. const timeout = dialog.getByLabel('命令超时(毫秒)')
  86. await timeout.waitFor({ timeout: 10_000 })
  87. // The composed default this deployment ships, before any user layer.
  88. expect(await timeout.inputValue()).toBe('60000')
  89. await timeout.fill('12000')
  90. await timeout.blur()
  91. // Nothing crosses the wire until the user saves: leaving the control is
  92. // not a decision to store the value.
  93. expect(await settingsDocument()).not.toContain('timeoutMs')
  94. const save = dialog.getByRole('button', { name: '保存', exact: true })
  95. await expect.poll(() => save.isEnabled(), { timeout: 5_000 }).toBe(true)
  96. await save.click()
  97. await expect.poll(async () => (await settingsDocument()).includes('timeoutMs: 12000'), { timeout: 10_000 })
  98. .toBe(true)
  99. // Presence in the user layer is what the badge reports, and the reset is
  100. // offered only for a field that has one.
  101. await expect.poll(() => dialog.getByText('已覆盖').count(), { timeout: 5_000 }).toBe(1)
  102. expect(await dialog.getByRole('button', { name: '恢复默认' }).count()).toBe(1)
  103. // A settled form offers no save to repeat.
  104. await expect.poll(() => save.isDisabled(), { timeout: 5_000 }).toBe(true)
  105. expect(tripwire.pageErrors).toEqual([])
  106. }, 60_000)
  107. it('drops a staged edit on discard without touching the document', async () => {
  108. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-discard'))
  109. const dialog = await openPlugins()
  110. await dialog.getByText('终端', { exact: true }).click()
  111. const timeout = dialog.getByLabel('命令超时(毫秒)')
  112. await timeout.waitFor({ timeout: 10_000 })
  113. await timeout.fill('7000')
  114. await dialog.getByRole('button', { name: '放弃修改' }).click()
  115. await expect.poll(() => timeout.inputValue(), { timeout: 5_000 }).toBe('12000')
  116. expect(await settingsDocument()).toContain('timeoutMs: 12000')
  117. expect(tripwire.pageErrors).toEqual([])
  118. }, 60_000)
  119. it('refuses to save a draft that is not a number', async () => {
  120. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-invalid'))
  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. await timeout.fill('soon')
  126. const save = dialog.getByRole('button', { name: '保存', exact: true })
  127. await expect.poll(() => save.isDisabled(), { timeout: 5_000 }).toBe(true)
  128. expect(await dialog.getByText('请填数字;留空表示使用默认值。').count()).toBe(1)
  129. await dialog.getByRole('button', { name: '放弃修改' }).click()
  130. expect(tripwire.pageErrors).toEqual([])
  131. }, 60_000)
  132. it('clears the field back to the composed default on reset', async () => {
  133. onTestFailed(() => saveFailureShot(page, 'web-e2e-plugin-config-reset'))
  134. const dialog = await openPlugins()
  135. await dialog.getByText('终端', { exact: true }).click()
  136. const timeout = dialog.getByLabel('命令超时(毫秒)')
  137. await timeout.waitFor({ timeout: 10_000 })
  138. expect(await timeout.inputValue()).toBe('12000')
  139. // The reset stages the composed default; the document still carries the
  140. // override until the save lands.
  141. await dialog.getByRole('button', { name: '恢复默认' }).click()
  142. await expect.poll(() => timeout.inputValue(), { timeout: 5_000 }).toBe('60000')
  143. expect(await settingsDocument()).toContain('timeoutMs: 12000')
  144. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  145. await expect.poll(async () => (await settingsDocument()).includes('timeoutMs'), { timeout: 10_000 })
  146. .toBe(false)
  147. expect(await timeout.inputValue()).toBe('60000')
  148. expect(await dialog.getByText('已覆盖').count()).toBe(0)
  149. expect(tripwire.pageErrors).toEqual([])
  150. }, 60_000)
  151. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  152. expect(tripwire.warnings).toEqual([])
  153. await assertFixtureInventory(SNAPSHOT_DIR, ['section.expected.md'])
  154. })
  155. })