models-settings.e2e.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Web e2e scenario: the Models settings page end to end through the real
  2. // wire — the add card offers the dormant pi-ai catalog, typing an API key
  3. // stores it write-only under the derived reference (`MINIMAX_CN_API_KEY`)
  4. // while the settings document records only that reference; the saved row
  5. // appears after the route topology invalidation without presenting liveness
  6. // as provider status. The customized-settings fold writes the curated
  7. // reasoning field as a merge patch. Zero model calls: configuration is pure
  8. // settings/credentials/llm-domain traffic, so there is no fixture and a
  9. // stray stream would fail loud on the open seam. The provider under test is
  10. // minimax-cn so a developer's real ANTHROPIC/OPENAI environment keys can
  11. // never shadow the derived reference. Removing that row is guarded by the
  12. // localized provider-confirmation dialog before the unset reaches the wire.
  13. import { readFile } from 'node:fs/promises'
  14. import { fileURLToPath } from 'node:url'
  15. import { join } from 'node:path'
  16. import type { Browser, Page } from 'playwright'
  17. import { chromium } from 'playwright'
  18. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  19. import {
  20. assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  21. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  22. } from './scaffold.ts'
  23. import { ZH_BROWSER_LOCALE, saveFailureShot } from './support.ts'
  24. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/models-settings', import.meta.url))
  25. const EMPTY_EXPECTED = join(SNAPSHOT_DIR, 'empty.expected.md')
  26. const CONFIGURED_EXPECTED = join(SNAPSHOT_DIR, 'configured.expected.md')
  27. const DELETE_EXPECTED = join(SNAPSHOT_DIR, 'delete.expected.md')
  28. const MODE = webSnapshotMode()
  29. describe('web e2e: Models settings page configures a dormant provider', () => {
  30. let scaffold: WebScaffold
  31. let browser: Browser
  32. let page: Page
  33. let tripwire: ReturnType<typeof watchConsole>
  34. beforeAll(async () => {
  35. scaffold = await launchWebScaffold({})
  36. browser = await chromium.launch()
  37. // The scenario asserts the shipped Chinese copy, so the browser asks for it.
  38. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  39. tripwire = watchConsole(page)
  40. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  41. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  42. }, 120_000)
  43. afterAll(async () => {
  44. await browser?.close()
  45. await scaffold?.close()
  46. })
  47. it('opens the add card over the dormant directory vocabulary', async () => {
  48. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-empty'))
  49. await page.getByRole('button', { name: '设置', exact: true }).click()
  50. const dialog = page.getByRole('dialog', { name: '设置' })
  51. await dialog.waitFor({ timeout: 10_000 })
  52. await dialog.getByRole('button', { name: '模型' }).click()
  53. await dialog.getByText('填入各提供方的 API 密钥即可使用其模型。').waitFor({ timeout: 10_000 })
  54. // The dormant pi-ai adapter contributes its whole installed catalog; no
  55. // provider is configured yet, so the page is one add button.
  56. const add = dialog.getByRole('button', { name: '添加提供方' })
  57. await add.waitFor({ timeout: 10_000 })
  58. // The button enables once the dormant catalog lands in the join.
  59. await expect.poll(async () => add.isEnabled(), { timeout: 10_000 }).toBe(true)
  60. await add.click()
  61. const pick = dialog.getByLabel('提供方')
  62. await pick.waitFor({ timeout: 10_000 })
  63. await expect.poll(async () => pick.locator('option').count(), { timeout: 10_000 }).toBeGreaterThan(30)
  64. const options = await pick.locator('option').allTextContents()
  65. expect(options).toContain('anthropic')
  66. expect(options).toContain('minimax-cn')
  67. await pick.selectOption('minimax-cn')
  68. await dialog.getByLabel('API 密钥').waitFor({ timeout: 10_000 })
  69. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  70. await compareOrRefreshGolden(EMPTY_EXPECTED, snapshot, MODE)
  71. }, 60_000)
  72. it('stores the key under the derived reference and the route registers live', async () => {
  73. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-add'))
  74. const dialog = page.getByRole('dialog', { name: '设置' })
  75. await dialog.getByLabel('API 密钥').fill('sk-e2e-minimax')
  76. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  77. // The profile lands in settings.yaml with only the derived reference, the
  78. // key value lands in the harness home's .env, the dormant route
  79. // registers, and the topology frame invalidates the page into the row.
  80. const row = dialog.getByText('minimax-cn', { exact: true }).first()
  81. await row.waitFor({ timeout: 10_000 })
  82. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  83. expect(document).toContain('minimax-cn:')
  84. expect(document).toContain('apiKeyEnv: MINIMAX_CN_API_KEY')
  85. expect(document).not.toContain('sk-e2e-minimax')
  86. const stored = await readFile(join(scaffold.harnessHome, '.env'), 'utf8')
  87. expect(stored).toContain('MINIMAX_CN_API_KEY=sk-e2e-minimax')
  88. expect(await page.content()).not.toContain('sk-e2e-minimax')
  89. }, 60_000)
  90. it('applies a customized-settings field as a merge patch', async () => {
  91. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-customized'))
  92. const dialog = page.getByRole('dialog', { name: '设置' })
  93. await dialog.getByRole('button', { name: '编辑' }).click()
  94. await dialog.getByText('自定义设置').click()
  95. const effort = dialog.getByLabel('推理强度')
  96. await effort.waitFor({ timeout: 10_000 })
  97. await effort.selectOption('high')
  98. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  99. // The editor closes back to the row; the fold's write merged into the
  100. // stored profile beside the reference.
  101. await expect.poll(async () => dialog.getByLabel('推理强度').count(), { timeout: 10_000 }).toBe(0)
  102. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  103. expect(document).toContain('reasoning: high')
  104. expect(document).toContain('apiKeyEnv: MINIMAX_CN_API_KEY')
  105. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  106. await compareOrRefreshGolden(CONFIGURED_EXPECTED, snapshot, MODE)
  107. expect(tripwire.pageErrors).toEqual([])
  108. }, 60_000)
  109. it('confirms provider deletion before removing its settings profile', async () => {
  110. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-delete'))
  111. const settingsDialog = page.getByRole('dialog', { name: '设置' })
  112. await settingsDialog.getByRole('button', { name: '删除', exact: true }).click()
  113. const deleteDialog = page.getByRole('dialog', { name: '删除模型提供方?' })
  114. await deleteDialog.waitFor({ timeout: 10_000 })
  115. const snapshot = await captureStableAria(
  116. page,
  117. '[role="dialog"][aria-label="删除模型提供方?"]',
  118. scaffold.workspaceCwd,
  119. )
  120. await compareOrRefreshGolden(DELETE_EXPECTED, snapshot, MODE)
  121. await deleteDialog.getByRole('button', { name: '取消', exact: true }).click()
  122. expect(await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')).toContain('minimax-cn:')
  123. await settingsDialog.getByRole('button', { name: '删除', exact: true }).click()
  124. await page.getByRole('dialog', { name: '删除模型提供方?' })
  125. .getByRole('button', { name: '删除提供方', exact: true }).click()
  126. await expect.poll(
  127. async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'),
  128. { timeout: 10_000 },
  129. ).not.toContain('minimax-cn:')
  130. expect(await readFile(join(scaffold.harnessHome, '.env'), 'utf8'))
  131. .toContain('MINIMAX_CN_API_KEY=sk-e2e-minimax')
  132. await expect.poll(
  133. async () => page.getByRole('dialog', { name: '删除模型提供方?' }).count(),
  134. { timeout: 10_000 },
  135. ).toBe(0)
  136. await page.keyboard.press('Escape')
  137. expect(tripwire.pageErrors).toEqual([])
  138. }, 60_000)
  139. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  140. await assertFixtureInventory(SNAPSHOT_DIR, ['configured.expected.md', 'delete.expected.md', 'empty.expected.md'])
  141. })
  142. })