models-settings.e2e.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.authenticatedUrl, { 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('filters the discovered model catalog and clears hidden selections', 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. const search = picker.getByRole('searchbox', { name: '搜索模型' })
  184. await search.fill('highspeed')
  185. await expect.poll(async () => boxes.count()).toBe(1)
  186. await picker.getByRole('button', { name: '取消全选' }).click()
  187. expect(await boxes.evaluateAll(nodes => nodes.map(node => (node as HTMLInputElement).checked))).toEqual(
  188. [false],
  189. )
  190. await search.fill('')
  191. await expect.poll(async () => boxes.count()).toBe(count)
  192. const restored = await boxes.evaluateAll(nodes => nodes.map(node => (node as HTMLInputElement).checked))
  193. expect(restored).toEqual(Array.from({ length: count }, () => false))
  194. await picker.getByRole('button', { name: '全选' }).waitFor()
  195. await picker.getByRole('button', { name: '全选' }).click()
  196. const snapshot = await captureStableAria(
  197. page,
  198. '[role="dialog"][aria-label="选择要添加的模型"]',
  199. scaffold.workspaceCwd,
  200. )
  201. await compareOrRefreshGolden(MODEL_PICKER_EXPECTED, snapshot, MODE)
  202. expect(await boxes.evaluateAll(nodes => nodes.map(node => (node as HTMLInputElement).checked))).toEqual(
  203. Array.from({ length: count }, () => true),
  204. )
  205. await picker.getByRole('button', { name: '取消', exact: true }).click()
  206. await settingsDialog.getByRole('button', { name: '取消', exact: true }).click()
  207. }, 60_000)
  208. it('declares a route the adapter does not ship', async () => {
  209. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-declare'))
  210. const dialog = page.getByRole('dialog', { name: '设置' })
  211. const declare = dialog.getByRole('button', { name: '添加自定义提供方' })
  212. await expect.poll(async () => declare.isEnabled(), { timeout: 10_000 }).toBe(true)
  213. await declare.click()
  214. await dialog.getByLabel('Provider ID').fill('acme-gateway')
  215. await dialog.getByLabel('显示名称').fill('Acme Gateway')
  216. await dialog.getByLabel('API 地址').fill('https://gateway.acme.example/v1')
  217. // No reasoning effort on a provider card at all: effort is a per-model
  218. // capability, the models under one provider disagree about it, and a
  219. // switch in the composer already records provider+model+effort together.
  220. expect(await dialog.getByLabel('推理强度').count()).toBe(0)
  221. await dialog.getByRole('button', { name: '添加模型' }).click()
  222. await dialog.getByLabel('模型 ID 1').fill('acme-large')
  223. await dialog.getByRole('button', { name: '创建提供方', exact: true }).click()
  224. const row = dialog.getByText('Acme Gateway', { exact: true }).first()
  225. await row.waitFor({ timeout: 10_000 })
  226. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  227. expect(document).toContain('acme-gateway:')
  228. // The tag follows the adapter's installed catalog: this route is in no
  229. // catalog, while minimax-cn is — even though both now have profiles.
  230. const rowCard = (name: string) => dialog.locator('li').filter({ hasText: name }).first()
  231. await expect.poll(async () => rowCard('Acme Gateway').getByText('自定义').count(), { timeout: 10_000 }).toBe(1)
  232. expect(await rowCard('minimax-cn').getByText('自定义').count()).toBe(0)
  233. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  234. await compareOrRefreshGolden(DECLARED_EXPECTED, snapshot, MODE)
  235. expect(tripwire.pageErrors).toEqual([])
  236. }, 60_000)
  237. it('reopens the name and protocol a declared route was created with', async () => {
  238. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-declared-identity'))
  239. const dialog = page.getByRole('dialog', { name: '设置' })
  240. await dialog.getByRole('button', { name: '编辑 Acme Gateway (acme-gateway)' }).click()
  241. await dialog.getByText('自定义设置').click()
  242. // The create card asked this route for a name and a protocol because
  243. // nothing can default them; the editor reaches the same two fields rather
  244. // than sending the user to settings.yaml for what only this route names.
  245. const protocol = dialog.getByLabel('API 协议')
  246. await protocol.waitFor({ timeout: 10_000 })
  247. expect(await protocol.inputValue()).toBe('openai-completions')
  248. const name = dialog.getByLabel('显示名称', { exact: true })
  249. expect(await name.inputValue()).toBe('Acme Gateway')
  250. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  251. await compareOrRefreshGolden(DECLARED_EDIT_EXPECTED, snapshot, MODE)
  252. await protocol.selectOption('anthropic-messages')
  253. await name.fill('Acme 网关')
  254. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  255. await expect.poll(async () => dialog.getByLabel('API 协议').count(), { timeout: 10_000 }).toBe(0)
  256. // The adapter re-resolved the route under the new protocol and re-registered
  257. // it under the new name: an unserviceable profile would have been refused
  258. // at the write instead, and a rename that did not re-register would leave
  259. // the old label on the row.
  260. await dialog.getByText('Acme 网关', { exact: true }).first().waitFor({ timeout: 10_000 })
  261. // The status line names the route as the refreshed directory reports it;
  262. // the target captured when the card opened still carries the old name.
  263. await dialog.getByText('已保存 Acme 网关 (acme-gateway)。', { exact: true }).waitFor({ timeout: 10_000 })
  264. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  265. expect(document).toContain('api: anthropic-messages')
  266. expect(document).toContain('displayName: Acme 网关')
  267. expect(tripwire.pageErrors).toEqual([])
  268. }, 60_000)
  269. it('confirms an identified provider deletion before removing its profile and key', async () => {
  270. onTestFailed(() => saveFailureShot(page, 'web-e2e-models-delete'))
  271. const settingsDialog = page.getByRole('dialog', { name: '设置' })
  272. await settingsDialog.getByRole('button', { name: '删除 minimax-cn', exact: true }).click()
  273. const deleteDialog = page.getByRole('dialog', { name: '删除 minimax-cn?' })
  274. await deleteDialog.waitFor({ timeout: 10_000 })
  275. const snapshot = await captureStableAria(
  276. page,
  277. '[role="dialog"][aria-label="删除 minimax-cn?"]',
  278. scaffold.workspaceCwd,
  279. )
  280. await compareOrRefreshGolden(DELETE_EXPECTED, snapshot, MODE)
  281. await deleteDialog.getByRole('button', { name: '取消', exact: true }).click()
  282. expect(await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')).toContain('minimax-cn:')
  283. await settingsDialog.getByRole('button', { name: '删除 minimax-cn', exact: true }).click()
  284. await page.getByRole('dialog', { name: '删除 minimax-cn?' })
  285. .getByRole('button', { name: '删除 minimax-cn', exact: true }).click()
  286. await expect.poll(
  287. async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'),
  288. { timeout: 10_000 },
  289. ).not.toContain('minimax-cn:')
  290. expect(await readFile(join(scaffold.harnessHome, '.credentials.yaml'), 'utf8'))
  291. .not.toContain('MINIMAX_CN_API_KEY')
  292. await expect.poll(
  293. async () => page.getByRole('dialog', { name: '删除 minimax-cn?' }).count(),
  294. { timeout: 10_000 },
  295. ).toBe(0)
  296. await page.keyboard.press('Escape')
  297. expect(tripwire.pageErrors).toEqual([])
  298. }, 60_000)
  299. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  300. await assertFixtureInventory(SNAPSHOT_DIR, [
  301. 'configured.expected.md', 'declared-edit.expected.md', 'declared.expected.md',
  302. 'delete.expected.md', 'empty.expected.md', 'model-picker.expected.md',
  303. 'native-delete.expected.md',
  304. ])
  305. })
  306. })