models-settings.e2e.ts 18 KB

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