agent-preset-authoring.e2e.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Web e2e scenario: the agent-preset settings section as copy-only authoring.
  2. // The browser never edits composition text — a shipped preset opens in a
  3. // read-only viewer, the copy dialog collects an id and an optional display
  4. // name, and the host copies the whole directory. The section's other job is
  5. // getting the user TO the files: this lane pins `nativeOpen: false` (see the
  6. // overlay), so the location affordance answers the preset directory as text —
  7. // the deterministic branch a golden can hold on every platform.
  8. //
  9. // Zero model calls: no replay fixture mounts, so a stray stream fails loud.
  10. import { existsSync } from 'node:fs'
  11. import { mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises'
  12. import { tmpdir } from 'node:os'
  13. import { fileURLToPath } from 'node:url'
  14. import { join } from 'node:path'
  15. import type { Browser, Page } from 'playwright'
  16. import { chromium } from 'playwright'
  17. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  18. import type { Locator } from 'playwright'
  19. import {
  20. captureStableAria, compareOrRefreshGolden, launchWebScaffold, watchConsole,
  21. webSnapshotMode, type WebScaffold,
  22. } from './scaffold.ts'
  23. import { ZH_BROWSER_LOCALE, connectFreshWorkspaceZh, saveFailureShot } from './support.ts'
  24. const SNAPSHOT_DIR = fileURLToPath(new URL('./expected/agent-preset-authoring', import.meta.url))
  25. const SECTION_EXPECTED = join(SNAPSHOT_DIR, 'section.expected.md')
  26. const COPY_DIALOG_EXPECTED = join(SNAPSHOT_DIR, 'copy-dialog.expected.md')
  27. const CREATED_EXPECTED = join(SNAPSHOT_DIR, 'created.expected.md')
  28. const DAMAGED_EXPECTED = join(SNAPSHOT_DIR, 'damaged.expected.md')
  29. /** The shipped roster, bundled inside the `dsh-agent-presets` package. */
  30. const SHIPPED_PRESETS = fileURLToPath(new URL('../../../packages/preset/agent-presets/presets', import.meta.url))
  31. const OVERLAY = fileURLToPath(new URL('./agent-preset-authoring.overlay.yml', import.meta.url))
  32. const MODE = webSnapshotMode()
  33. describe('web e2e: agent-preset authoring is a host-side copy', () => {
  34. let scaffold: WebScaffold
  35. let browser: Browser
  36. let page: Page
  37. let tripwire: ReturnType<typeof watchConsole>
  38. let userRoot: string
  39. /** The settings dialog, opened on the Agent-presets section. */
  40. function settingsDialog(): Locator {
  41. return page.getByRole('dialog', { name: '设置' })
  42. }
  43. beforeAll(async () => {
  44. userRoot = await realpath(await mkdtemp(join(tmpdir(), 'dsh-web-e2e-presets-')))
  45. scaffold = await launchWebScaffold({
  46. extraOverlayPath: OVERLAY,
  47. agentPresets: {
  48. // The shipped root is the plugin's own, prepended before this.
  49. roots: [{ path: userRoot, trust: 'user' }],
  50. default: 'standard',
  51. },
  52. })
  53. browser = await chromium.launch()
  54. // The scenario asserts the shipped Chinese copy, so the browser asks for it.
  55. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  56. tripwire = watchConsole(page)
  57. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  58. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  59. }, 120_000)
  60. afterAll(async () => {
  61. await browser?.close()
  62. await scaffold?.close()
  63. await rm(userRoot, { recursive: true, force: true })
  64. })
  65. it('offers the roster with copy as the only way to create', async () => {
  66. onTestFailed(() => saveFailureShot(page, 'web-e2e-preset-authoring-section'))
  67. await page.getByRole('button', { name: '设置', exact: true }).click()
  68. const dialog = settingsDialog()
  69. await dialog.waitFor({ timeout: 10_000 })
  70. await dialog.getByRole('button', { name: 'Agent 预设' }).click()
  71. await dialog.getByRole('heading', { name: 'Agent 预设' }).waitFor({ timeout: 10_000 })
  72. await dialog.getByText('标准模式').first().waitFor({ timeout: 10_000 })
  73. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  74. await compareOrRefreshGolden(SECTION_EXPECTED, snapshot, MODE)
  75. // The intro states the copy path directly, and the shipped rows offer
  76. // view/copy but never delete or a location — their
  77. // install is overwritten by upgrades and is not the user's to manage.
  78. expect(snapshot).toContain('或用「创造模式」让 Agent 帮你创建')
  79. expect(snapshot).not.toContain('新建预设')
  80. expect(snapshot).toContain('查看: 标准模式')
  81. expect(snapshot).not.toContain('删除: 标准模式')
  82. expect(snapshot).not.toContain('打开目录')
  83. }, 60_000)
  84. it('views a shipped composition read-only instead of editing it', async () => {
  85. onTestFailed(() => saveFailureShot(page, 'web-e2e-preset-authoring-view'))
  86. const dialog = settingsDialog()
  87. await dialog.getByRole('button', { name: '查看: 标准模式' }).click()
  88. const viewer = page.getByRole('dialog', { name: '查看 · 标准模式' })
  89. await viewer.waitFor({ timeout: 10_000 })
  90. // The real shipped composition, not a golden: the viewer shows whatever
  91. // the deployment ships, and this lane only asserts it is shown read-only.
  92. const shipped = await readFile(join(SHIPPED_PRESETS, 'standard', 'agent.cordis.yml'), 'utf8')
  93. expect(await viewer.locator('pre').textContent()).toBe(shipped)
  94. expect(await viewer.getByRole('textbox').count()).toBe(0)
  95. // The header X and the footer button share the 关闭 name; the footer one
  96. // is last in the dialog.
  97. await viewer.getByRole('button', { name: '关闭' }).last().click()
  98. await viewer.waitFor({ state: 'detached', timeout: 10_000 })
  99. }, 60_000)
  100. it('copies 极简模式 whole under a new id and lands in its files', async () => {
  101. onTestFailed(() => saveFailureShot(page, 'web-e2e-preset-authoring-copy'))
  102. const dialog = settingsDialog()
  103. await dialog.getByRole('button', { name: '复制: 极简模式' }).click()
  104. const copyDialog = page.getByRole('dialog', { name: '复制预设 · 复制自 极简模式' })
  105. await copyDialog.waitFor({ timeout: 10_000 })
  106. const dialogSnapshot = await captureStableAria(
  107. page, '[role="dialog"][aria-label^="复制预设"]', scaffold.workspaceCwd)
  108. await compareOrRefreshGolden(COPY_DIALOG_EXPECTED, dialogSnapshot, MODE)
  109. // Two fields and nothing else: the id is the directory name the host
  110. // needs up front; description and composition live in the files.
  111. expect(dialogSnapshot).toContain('标识符')
  112. expect(dialogSnapshot).not.toContain('描述')
  113. await copyDialog.getByPlaceholder('my-agent').fill('my-agent')
  114. await copyDialog.getByPlaceholder('选择器中显示的名字,缺省用标识符').fill('我的模式')
  115. await copyDialog.getByRole('button', { name: '创建' }).click()
  116. await copyDialog.waitFor({ state: 'detached', timeout: 10_000 })
  117. // The new row lands in the custom group, and — with no desktop opener —
  118. // its directory is revealed as text right away: landing in the files is
  119. // the completion of a copy, not a follow-up.
  120. await dialog.getByText('我的模式').first().waitFor({ timeout: 10_000 })
  121. await dialog.getByText('预设文件:').waitFor({ timeout: 10_000 })
  122. // The copy dialog is detached, so the settings dialog is the only one
  123. // left (it names itself via aria-labelledby, which a CSS attribute
  124. // selector cannot address).
  125. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd, {
  126. replacements: [[userRoot, '{{presetRoot}}']],
  127. })
  128. await compareOrRefreshGolden(CREATED_EXPECTED, snapshot, MODE)
  129. expect(snapshot).toContain('{{presetRoot}}/my-agent')
  130. // The host copied the whole directory and rewrote only the display
  131. // metadata: the composition is byte-identical to the shipped source, the
  132. // description rides along for the user to edit in place, and neither the
  133. // source's name nor its roster order survives into the copy.
  134. const composition = await readFile(join(userRoot, 'my-agent', 'agent.cordis.yml'), 'utf8')
  135. expect(composition).toBe(await readFile(join(SHIPPED_PRESETS, 'minimal', 'agent.cordis.yml'), 'utf8'))
  136. const metadata = await readFile(join(userRoot, 'my-agent', 'preset.yml'), 'utf8')
  137. expect(metadata).toContain('name: 我的模式')
  138. expect(metadata).toContain('description: 仅提供持久 shell 的单工具编码 Agent。')
  139. expect(metadata).not.toContain('order:')
  140. }, 60_000)
  141. it('deletes the copy after confirmation and reclaims the roster', async () => {
  142. onTestFailed(() => saveFailureShot(page, 'web-e2e-preset-authoring-delete'))
  143. const dialog = settingsDialog()
  144. await dialog.getByRole('button', { name: '删除: 我的模式' }).click()
  145. const confirm = page.getByRole('dialog', { name: '删除该预设?' })
  146. await confirm.waitFor({ timeout: 10_000 })
  147. await confirm.getByRole('button', { name: '删除', exact: true }).click()
  148. await confirm.waitFor({ state: 'detached', timeout: 10_000 })
  149. await expect.poll(async () => dialog.getByText('我的模式').count(), { timeout: 10_000 }).toBe(0)
  150. expect(existsSync(join(userRoot, 'my-agent'))).toBe(false)
  151. // The custom group outlives its only member: the heading stays with the
  152. // creator entry so the place to author a preset never disappears.
  153. expect(await dialog.getByRole('heading', { name: '自定义' }).count()).toBe(1)
  154. expect(await dialog.getByRole('button', { name: '用「创造模式」创作自定义预设' }).count()).toBe(1)
  155. expect(await dialog.getByText('标准模式').count()).toBeGreaterThan(0)
  156. }, 60_000)
  157. it('marks damaged presets broken and clears a ghost through delete', async () => {
  158. onTestFailed(() => saveFailureShot(page, 'web-e2e-preset-authoring-damaged'))
  159. // The two hand-edit damage shapes: a composition that no longer parses,
  160. // and a directory whose composition file was deleted outright.
  161. await mkdir(join(userRoot, 'broken-yaml'), { recursive: true })
  162. await writeFile(join(userRoot, 'broken-yaml', 'agent.cordis.yml'), '- id: x\n name: [unclosed\n')
  163. await mkdir(join(userRoot, 'ghost'), { recursive: true })
  164. await writeFile(join(userRoot, 'ghost', 'preset.yml'), 'name: 幽灵预设\ndescription: composition 已被手动删除。\n')
  165. // The section reads the roster when it mounts; hop away and back.
  166. const dialog = settingsDialog()
  167. await dialog.getByRole('button', { name: '通用设置' }).click()
  168. await dialog.getByRole('button', { name: 'Agent 预设' }).click()
  169. await dialog.getByText('加载失败').first().waitFor({ timeout: 10_000 })
  170. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd, {
  171. replacements: [[userRoot, '{{presetRoot}}']],
  172. })
  173. await compareOrRefreshGolden(DAMAGED_EXPECTED, snapshot, MODE)
  174. // Both damage shapes surface as marked, unselectable, uncopyable cards
  175. // that still carry their metadata and the discovery-reported reason.
  176. expect(snapshot).toContain('加载失败: broken-yaml')
  177. expect(snapshot).toContain('加载失败: 幽灵预设')
  178. expect(snapshot).toContain('not valid YAML')
  179. expect(snapshot).toContain('agent.cordis.yml is missing')
  180. expect(await dialog.getByRole('button', { name: '加载失败: broken-yaml' }).isDisabled()).toBe(true)
  181. expect(await dialog.getByRole('button', { name: '复制: 幽灵预设' }).isDisabled()).toBe(true)
  182. // A broken card offers no "set default" affordance at all — the aria name
  183. // IS the broken marking, so the picking name must not exist.
  184. expect(await dialog.getByRole('button', { name: '设为默认: broken-yaml' }).count()).toBe(0)
  185. // The ghost's way out is the card's own delete — and the id it blocked
  186. // is claimable again immediately afterwards.
  187. await dialog.getByRole('button', { name: '删除: 幽灵预设' }).click()
  188. const confirm = page.getByRole('dialog', { name: '删除该预设?' })
  189. await confirm.waitFor({ timeout: 10_000 })
  190. await confirm.getByRole('button', { name: '删除', exact: true }).click()
  191. await confirm.waitFor({ state: 'detached', timeout: 10_000 })
  192. await expect.poll(async () => dialog.getByText('幽灵预设').count(), { timeout: 10_000 }).toBe(0)
  193. expect(existsSync(join(userRoot, 'ghost'))).toBe(false)
  194. await dialog.getByRole('button', { name: '复制: 极简模式' }).click()
  195. const copyDialog = page.getByRole('dialog', { name: '复制预设 · 复制自 极简模式' })
  196. await copyDialog.waitFor({ timeout: 10_000 })
  197. await copyDialog.getByPlaceholder('my-agent').fill('ghost')
  198. await copyDialog.getByRole('button', { name: '创建' }).click()
  199. await copyDialog.waitFor({ state: 'detached', timeout: 10_000 })
  200. await dialog.getByRole('button', { name: '设为默认: ghost' }).waitFor({ timeout: 10_000 })
  201. // Leave the roster as the earlier tests shaped it.
  202. await dialog.getByRole('button', { name: '删除: ghost' }).click()
  203. const cleanup = page.getByRole('dialog', { name: '删除该预设?' })
  204. await cleanup.waitFor({ timeout: 10_000 })
  205. await cleanup.getByRole('button', { name: '删除', exact: true }).click()
  206. await cleanup.waitFor({ state: 'detached', timeout: 10_000 })
  207. await rm(join(userRoot, 'broken-yaml'), { recursive: true, force: true })
  208. }, 60_000)
  209. it('starts a creator-mode session from the section', async () => {
  210. onTestFailed(() => saveFailureShot(page, 'web-e2e-preset-authoring-creator'))
  211. // Without a workspace the flow only stages (there is no session to land
  212. // in until one is connected); connect first so the gesture carries all
  213. // the way to a composed host session.
  214. await settingsDialog().getByRole('button', { name: '关闭' }).last().click()
  215. await connectFreshWorkspaceZh(page, scaffold.workspaceCwd)
  216. await page.getByRole('button', { name: '设置', exact: true }).click()
  217. const dialog = settingsDialog()
  218. await dialog.waitFor({ timeout: 10_000 })
  219. await dialog.getByRole('button', { name: 'Agent 预设' }).click()
  220. await dialog.getByRole('button', { name: '用「创造模式」创作自定义预设' }).click()
  221. // Leaving settings is part of the gesture: the flow lands on the
  222. // new-session screen with the self-referential preset staged, and the
  223. // blank session the flow produces composes from it on the host.
  224. await dialog.waitFor({ state: 'detached', timeout: 10_000 })
  225. await page.getByRole('button', { name: '创造模式' }).waitFor({ timeout: 10_000 })
  226. await expect.poll(async () => {
  227. const response = await scaffold.hostFetch('/api/session/list', {
  228. method: 'POST',
  229. headers: { 'content-type': 'application/json' },
  230. body: JSON.stringify({
  231. type: 'client-request', rpcId: 'creator-draft-stage', method: 'session/list',
  232. payload: { args: { _request: {} } },
  233. }),
  234. })
  235. const body = await response.json() as {
  236. result: { value?: { items: unknown[] } }
  237. }
  238. return JSON.stringify(body.result.value?.items ?? body.result)
  239. }, { timeout: 15_000 }).toContain('"agentPreset":"cordis"')
  240. }, 60_000)
  241. it('drove every surface without a page error or a stream warning', () => {
  242. expect(tripwire.pageErrors).toEqual([])
  243. expect(tripwire.warnings).toEqual([])
  244. })
  245. })