models-settings.e2e.ts 16 KB

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