models-settings.e2e.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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, a blank key saves a
  3. // reference-free profile for provider-native auth, and typing an API key later
  4. // stores it write-only under the derived reference (`MINIMAX_CN_API_KEY`)
  5. // while the settings document records only that reference. Each saved row
  6. // appears after route topology invalidation without presenting liveness as
  7. // provider status. The customized-settings fold writes the curated
  8. // reasoning field as a merge patch. Zero model calls: configuration is pure
  9. // settings/credentials/llm-domain traffic, so there is no fixture and a
  10. // stray stream would fail loud on the open seam. The provider under test is
  11. // minimax-cn so a developer's real ANTHROPIC/OPENAI environment keys can
  12. // never shadow the derived reference. The deletion dialog distinguishes a
  13. // reference-free profile from a page-managed key before the credential and
  14. // settings unsets reach the wire.
  15. import { readFile } from 'node:fs/promises'
  16. import { fileURLToPath } from 'node:url'
  17. import { join } from 'node:path'
  18. import type { Browser, Page } from 'playwright'
  19. import { chromium } from 'playwright'
  20. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  21. import {
  22. assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  23. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  24. } from './scaffold.ts'
  25. import { ZH_BROWSER_LOCALE, saveFailureShot } from './support.ts'
  26. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/models-settings', import.meta.url))
  27. const EMPTY_EXPECTED = join(SNAPSHOT_DIR, 'empty.expected.md')
  28. const CONFIGURED_EXPECTED = join(SNAPSHOT_DIR, 'configured.expected.md')
  29. const DECLARED_EXPECTED = join(SNAPSHOT_DIR, 'declared.expected.md')
  30. const NATIVE_DELETE_EXPECTED = join(SNAPSHOT_DIR, 'native-delete.expected.md')
  31. const DELETE_EXPECTED = join(SNAPSHOT_DIR, 'delete.expected.md')
  32. const MODE = webSnapshotMode()
  33. describe('web e2e: Models settings page configures a dormant provider', () => {
  34. let scaffold: WebScaffold
  35. let browser: Browser
  36. let page: Page
  37. let tripwire: ReturnType<typeof watchConsole>
  38. beforeAll(async () => {
  39. scaffold = await launchWebScaffold({})
  40. browser = await chromium.launch()
  41. // The scenario asserts the shipped Chinese copy, so the browser asks for it.
  42. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  43. tripwire = watchConsole(page)
  44. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  45. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  46. }, 120_000)
  47. afterAll(async () => {
  48. await browser?.close()
  49. await scaffold?.close()
  50. })
  51. it('opens the add card over the dormant directory vocabulary', async () => {
  52. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-empty'))
  53. await page.getByRole('button', { name: '设置', exact: true }).click()
  54. const dialog = page.getByRole('dialog', { name: '设置' })
  55. await dialog.waitFor({ timeout: 10_000 })
  56. await dialog.getByRole('button', { name: '模型' }).click()
  57. await dialog.getByText('填入各提供方的 API 密钥即可使用其模型。').waitFor({ timeout: 10_000 })
  58. // The dormant pi-ai adapter contributes its whole installed catalog; no
  59. // provider is configured yet, so the page is one add button.
  60. const add = dialog.getByRole('button', { name: '添加提供方' })
  61. await add.waitFor({ timeout: 10_000 })
  62. // The button enables once the dormant catalog lands in the join.
  63. await expect.poll(async () => add.isEnabled(), { timeout: 10_000 }).toBe(true)
  64. await add.click()
  65. const pick = dialog.getByLabel('提供方')
  66. await pick.waitFor({ timeout: 10_000 })
  67. await expect.poll(async () => pick.locator('option').count(), { timeout: 10_000 }).toBeGreaterThan(30)
  68. const options = await pick.locator('option').allTextContents()
  69. expect(options).toContain('anthropic')
  70. expect(options).toContain('minimax-cn')
  71. await pick.selectOption('minimax-cn')
  72. await dialog.getByRole('textbox', { name: 'API 密钥', exact: true }).waitFor({ timeout: 10_000 })
  73. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  74. await compareOrRefreshGolden(EMPTY_EXPECTED, snapshot, MODE)
  75. }, 60_000)
  76. it('refuses a key no HTTP header can carry before anything is written', async () => {
  77. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-illegal-key'))
  78. const dialog = page.getByRole('dialog', { name: '设置' })
  79. const key = dialog.getByLabel('API 密钥')
  80. const save = dialog.getByRole('button', { name: '保存', exact: true })
  81. // The paste that used to save cleanly and then fail the first turn with a
  82. // ByteString TypeError now names the field that holds it.
  83. await key.fill('sk-\u{1F600}minimax')
  84. await dialog.getByText('该 API 密钥格式错误,请检查。').waitFor({ timeout: 10_000 })
  85. await expect.poll(async () => save.isEnabled(), { timeout: 10_000 }).toBe(false)
  86. // Clearing it restores submit: an empty field means "keep what is stored",
  87. // never a refusal, or editing any other setting would demand the key.
  88. await key.fill('')
  89. await expect.poll(async () => save.isEnabled(), { timeout: 10_000 }).toBe(true)
  90. expect(await dialog.getByText('该 API 密钥格式错误,请检查。').count()).toBe(0)
  91. }, 60_000)
  92. it('saves a blank key as a reference-free provider-native profile', async () => {
  93. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-native-auth'))
  94. const dialog = page.getByRole('dialog', { name: '设置' })
  95. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  96. const row = dialog.getByText('minimax-cn', { exact: true }).first()
  97. await row.waitFor({ timeout: 10_000 })
  98. await dialog.getByText('已保存 minimax-cn。', { exact: true }).waitFor({ timeout: 10_000 })
  99. expect(await dialog.getByRole('img', { name: 'API 密钥已配置' }).count()).toBe(0)
  100. expect(await dialog.getByRole('img', { name: 'API 密钥缺失' }).count()).toBe(0)
  101. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  102. expect(document).toContain('minimax-cn: {}')
  103. expect(document).not.toContain('MINIMAX_CN_API_KEY')
  104. }, 60_000)
  105. it('describes reference-free deletion without claiming a credential exists', async () => {
  106. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-native-delete'))
  107. const settingsDialog = page.getByRole('dialog', { name: '设置' })
  108. await settingsDialog.getByRole('button', { name: '删除 minimax-cn', exact: true }).click()
  109. const deleteDialog = page.getByRole('dialog', { name: '删除 minimax-cn?' })
  110. await deleteDialog.waitFor({ timeout: 10_000 })
  111. const snapshot = await captureStableAria(
  112. page,
  113. '[role="dialog"][aria-label="删除 minimax-cn?"]',
  114. scaffold.workspaceCwd,
  115. )
  116. await compareOrRefreshGolden(NATIVE_DELETE_EXPECTED, snapshot, MODE)
  117. await deleteDialog.getByRole('button', { name: '取消', exact: true }).click()
  118. }, 60_000)
  119. it('stores the key under the derived reference and keeps the route live', async () => {
  120. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-add'))
  121. const dialog = page.getByRole('dialog', { name: '设置' })
  122. await dialog.getByRole('button', { name: '编辑 minimax-cn' }).click()
  123. await dialog.getByRole('textbox', { name: 'API 密钥', exact: true }).fill('sk-e2e-minimax')
  124. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  125. // The profile lands in settings.yaml with only the derived reference, the
  126. // key value lands in the harness home's .env, the dormant route
  127. // registers, and the topology frame invalidates the page into the row.
  128. await expect.poll(
  129. async () => dialog.getByRole('textbox', { name: 'API 密钥', exact: true }).count(),
  130. { timeout: 10_000 },
  131. ).toBe(0)
  132. await dialog.getByRole('img', { name: 'API 密钥已配置' }).waitFor({ timeout: 10_000 })
  133. await dialog.getByText('已保存 minimax-cn。', { exact: true }).waitFor({ timeout: 10_000 })
  134. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  135. expect(document).toContain('minimax-cn:')
  136. expect(document).toContain('apiKeyEnv: MINIMAX_CN_API_KEY')
  137. expect(document).not.toContain('sk-e2e-minimax')
  138. const credentialFile = join(scaffold.harnessHome, '.env')
  139. await expect.poll(
  140. async () => readFile(credentialFile, 'utf8').catch(() => ''),
  141. { timeout: 10_000 },
  142. ).toContain('MINIMAX_CN_API_KEY=sk-e2e-minimax')
  143. expect(await page.content()).not.toContain('sk-e2e-minimax')
  144. }, 60_000)
  145. it('applies a customized-settings field as a merge patch', async () => {
  146. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-customized'))
  147. const dialog = page.getByRole('dialog', { name: '设置' })
  148. await dialog.getByRole('button', { name: '编辑 minimax-cn' }).click()
  149. await dialog.getByText('自定义设置').click()
  150. const url = dialog.getByLabel('API 地址')
  151. await url.waitFor({ timeout: 10_000 })
  152. await url.fill('https://gateway.minimax.example/v1')
  153. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  154. // The editor closes back to the row; the fold's write merged into the
  155. // stored profile beside the reference.
  156. await expect.poll(async () => dialog.getByLabel('API 地址').count(), { timeout: 10_000 }).toBe(0)
  157. await dialog.getByText('已保存 minimax-cn。', { exact: true }).waitFor({ timeout: 10_000 })
  158. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  159. expect(document).toContain('baseURL: https://gateway.minimax.example/v1')
  160. expect(document).toContain('apiKeyEnv: MINIMAX_CN_API_KEY')
  161. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  162. await compareOrRefreshGolden(CONFIGURED_EXPECTED, snapshot, MODE)
  163. expect(tripwire.pageErrors).toEqual([])
  164. }, 60_000)
  165. it('declares a route the adapter does not ship', async () => {
  166. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-declare'))
  167. const dialog = page.getByRole('dialog', { name: '设置' })
  168. const declare = dialog.getByRole('button', { name: '添加自定义提供方' })
  169. await expect.poll(async () => declare.isEnabled(), { timeout: 10_000 }).toBe(true)
  170. await declare.click()
  171. await dialog.getByLabel('Provider ID').fill('acme-gateway')
  172. await dialog.getByLabel('显示名称').fill('Acme Gateway')
  173. await dialog.getByLabel('API 地址').fill('https://gateway.acme.example/v1')
  174. // No reasoning effort on a provider card at all: effort is a per-model
  175. // capability, the models under one provider disagree about it, and a
  176. // switch in the composer already records provider+model+effort together.
  177. expect(await dialog.getByLabel('推理强度').count()).toBe(0)
  178. await dialog.getByRole('button', { name: '添加模型' }).click()
  179. await dialog.getByLabel('模型 ID 1').fill('acme-large')
  180. await dialog.getByRole('button', { name: '创建提供方', exact: true }).click()
  181. const row = dialog.getByText('Acme Gateway', { exact: true }).first()
  182. await row.waitFor({ timeout: 10_000 })
  183. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  184. expect(document).toContain('acme-gateway:')
  185. // The tag follows the adapter's installed catalog: this route is in no
  186. // catalog, while minimax-cn is — even though both now have profiles.
  187. const rowCard = (name: string) => dialog.locator('li').filter({ hasText: name }).first()
  188. await expect.poll(async () => rowCard('Acme Gateway').getByText('自定义').count(), { timeout: 10_000 }).toBe(1)
  189. expect(await rowCard('minimax-cn').getByText('自定义').count()).toBe(0)
  190. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  191. await compareOrRefreshGolden(DECLARED_EXPECTED, snapshot, MODE)
  192. expect(tripwire.pageErrors).toEqual([])
  193. }, 60_000)
  194. it('confirms an identified provider deletion before removing its profile and key', async () => {
  195. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-delete'))
  196. const settingsDialog = page.getByRole('dialog', { name: '设置' })
  197. await settingsDialog.getByRole('button', { name: '删除 minimax-cn', exact: true }).click()
  198. const deleteDialog = page.getByRole('dialog', { name: '删除 minimax-cn?' })
  199. await deleteDialog.waitFor({ timeout: 10_000 })
  200. const snapshot = await captureStableAria(
  201. page,
  202. '[role="dialog"][aria-label="删除 minimax-cn?"]',
  203. scaffold.workspaceCwd,
  204. )
  205. await compareOrRefreshGolden(DELETE_EXPECTED, snapshot, MODE)
  206. await deleteDialog.getByRole('button', { name: '取消', exact: true }).click()
  207. expect(await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')).toContain('minimax-cn:')
  208. await settingsDialog.getByRole('button', { name: '删除 minimax-cn', exact: true }).click()
  209. await page.getByRole('dialog', { name: '删除 minimax-cn?' })
  210. .getByRole('button', { name: '删除 minimax-cn', exact: true }).click()
  211. await expect.poll(
  212. async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'),
  213. { timeout: 10_000 },
  214. ).not.toContain('minimax-cn:')
  215. expect(await readFile(join(scaffold.harnessHome, '.env'), 'utf8'))
  216. .not.toContain('MINIMAX_CN_API_KEY')
  217. await expect.poll(
  218. async () => page.getByRole('dialog', { name: '删除 minimax-cn?' }).count(),
  219. { timeout: 10_000 },
  220. ).toBe(0)
  221. await page.keyboard.press('Escape')
  222. expect(tripwire.pageErrors).toEqual([])
  223. }, 60_000)
  224. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  225. await assertFixtureInventory(SNAPSHOT_DIR, [
  226. 'configured.expected.md', 'declared.expected.md', 'delete.expected.md',
  227. 'empty.expected.md', 'native-delete.expected.md',
  228. ])
  229. })
  230. })