model-confirmation.spec.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { test, expect } from '@playwright/test'
  2. test('model confirmation survives reconnects and failed inputs retain their position after Allow, new replies and reload', async ({ page, browser }) => {
  3. const modelCommands: Record<string, unknown>[] = []
  4. let nativeApprovals = 0
  5. await page.route('**/api/realtime/channels/*/commands', async route => {
  6. const command = route.request().postDataJSON()
  7. if (command.method === 'approval.respond') nativeApprovals++
  8. if (command.method !== 'config.set' || command.params.key !== 'model') return route.continue()
  9. modelCommands.push(command.params)
  10. const result = command.params.confirm_expensive_model === true
  11. ? { key: 'model', value: 'gpt-5.5', scope: 'session' }
  12. : { confirm_required: true, confirm_message: '长上下文切换验收:请确认重新读取缓存。' }
  13. await route.fulfill({ json: { state: 'confirmed', response: { result } } })
  14. })
  15. await page.goto('/chat/session-demo?profile=yaoyao')
  16. if (await page.getByRole('heading', { name: '登录夭夭' }).isVisible()) {
  17. await page.getByLabel('账号').fill('admin')
  18. await page.getByLabel('密码').fill('e2e-password')
  19. await page.getByRole('button', { name: '登录', exact: true }).click()
  20. }
  21. await page.goto('/chat/session-demo?profile=yaoyao')
  22. await expect(page.locator('.composer-tool--model')).toContainText('gpt-5.6')
  23. await page.locator('.composer-tool--model').click()
  24. await page.locator('.model-dialog__item').filter({ hasText: 'gpt-5.5' }).click()
  25. // History can be opened without resuming Hermes; sending applies the pending pick.
  26. await page.locator('.composer-textarea').fill('等待模型确认')
  27. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  28. const confirmation = page.locator('.interaction-card--approval')
  29. await expect(confirmation).toContainText('长上下文切换验收')
  30. for (let i = 0; i < 2; i++) {
  31. const resumed = page.waitForResponse(response => response.url().includes('/api/app/chat/sessions/session-demo/events'))
  32. await page.evaluate(() => document.dispatchEvent(new Event('visibilitychange')))
  33. await resumed
  34. await expect(confirmation).toBeVisible()
  35. }
  36. await confirmation.getByRole('button', { name: '允许', exact: true }).click()
  37. await expect(confirmation).toBeHidden()
  38. expect(modelCommands).toHaveLength(2)
  39. expect(modelCommands[1]).toMatchObject({ confirm_expensive_model: true, value: 'gpt-5.5 --provider openai --session' })
  40. expect(nativeApprovals).toBe(0)
  41. await page.locator('.composer-textarea').fill('模型确认后发送')
  42. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  43. await expect(page.locator('.message__content').filter({ hasText: '这是来自假 Gateway 的流式回复。' })).toHaveCount(1)
  44. expect(modelCommands).toHaveLength(2)
  45. const relevantMessages = ['等待模型确认', '模型确认后发送', '这是来自假 Gateway 的流式回复。']
  46. const visibleOrder = async () => (await page.locator('.message__content').allTextContents())
  47. .map(text => text.trim()).filter(text => relevantMessages.includes(text))
  48. await expect.poll(visibleOrder).toEqual(relevantMessages)
  49. await page.reload()
  50. await expect.poll(visibleOrder).toEqual(relevantMessages)
  51. await expect(page.locator('.message--failed').filter({ hasText: '等待模型确认' })).toHaveCount(1)
  52. // A fresh device gets the accepted transcript, not this browser's unsent outbox.
  53. const other = await browser.newContext({ storageState: await page.context().storageState() })
  54. try {
  55. const otherPage = await other.newPage()
  56. await otherPage.goto(page.url())
  57. await expect(otherPage.locator('.message__content').filter({ hasText: '模型确认后发送' })).toHaveCount(1)
  58. await expect(otherPage.locator('.message__content').filter({ hasText: '等待模型确认' })).toHaveCount(0)
  59. } finally { await other.close() }
  60. })