hermesMedia.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { expect, test, type APIRequestContext, type Locator } from '@playwright/test'
  2. const mediaPaths = [
  3. '/Users/samien/.hermes/profiles/yaoer/cache/images/openai_codex_gpt-image-2-high_20260902_194820_1d225f08.png',
  4. '/Users/samien/.hermes/profiles/yaoer/cache/images/国漫三视图.png',
  5. ]
  6. const mediaNames = mediaPaths.map(path => path.split('/').at(-1)!)
  7. async function signIn(request: APIRequestContext, origin: string) {
  8. for (const password of ['e2e-password', 'admin']) {
  9. const bootstrap = await (await request.get('/api/app/bootstrap')).json()
  10. const login = await request.post('/api/app/login', {
  11. headers: { Origin: origin, 'X-CSRF-Token': bootstrap.csrfToken },
  12. data: { username: 'admin', password },
  13. })
  14. if (!login.ok()) continue
  15. const account = await login.json()
  16. if (account.user.mustChangePassword) {
  17. const changed = await request.put('/api/app/account/credentials', {
  18. headers: { Origin: origin, 'X-CSRF-Token': account.csrfToken },
  19. data: { currentPassword: password, newPassword: 'e2e-password', username: 'admin' },
  20. })
  21. expect(changed.ok()).toBe(true)
  22. }
  23. return
  24. }
  25. throw new Error('Could not sign in to the fixture Web server')
  26. }
  27. async function expectDecodedImage(image: Locator) {
  28. await expect(image).toBeVisible()
  29. await expect.poll(() => image.evaluate(element => (element as HTMLImageElement).naturalWidth)).toBeGreaterThan(0)
  30. }
  31. test('loads Hermes profile cache images through the Web server and switches their full-size previews', async ({ page, baseURL }, testInfo) => {
  32. await signIn(page.request, new URL(baseURL!).origin)
  33. // No image interception: this exercises the real Web route, upstream account
  34. // authentication, attachment-to-inline response handling, and browser decode.
  35. const responses = mediaPaths.map(path => page.waitForResponse(response => (
  36. decodeURIComponent(new URL(response.url()).pathname) === path
  37. )))
  38. await page.goto('/chat/session-media?profile=yaoer')
  39. await expect(page).toHaveTitle('瑶儿生成图片验收 · 夭夭')
  40. for (const [index, name] of mediaNames.entries()) {
  41. const image = page.getByRole('button', { name: `预览图片 ${name}`, exact: true })
  42. await image.scrollIntoViewIfNeeded()
  43. await expectDecodedImage(image)
  44. const response = await responses[index]!
  45. expect(response.status()).toBe(200)
  46. expect(response.headers()['content-type']).toContain('image/png')
  47. expect(response.headers()['content-disposition'] ?? '').not.toMatch(/^attachment(?:;|$)/)
  48. expect(new URL(response.url()).origin).toBe(new URL(baseURL!).origin)
  49. }
  50. await page.getByRole('button', { name: `预览图片 ${mediaNames[0]}`, exact: true }).click()
  51. const preview = page.locator('.image-preview-overlay')
  52. await expect(preview).toHaveAccessibleName(`预览 ${mediaNames[0]}`)
  53. await expectDecodedImage(preview.getByRole('img', { name: mediaNames[0], exact: true }))
  54. await expect(preview.locator('.image-preview-counter')).toHaveText('1 / 2')
  55. await preview.getByRole('button', { name: '下一张媒体' }).click()
  56. await expect(preview).toHaveAccessibleName(`预览 ${mediaNames[1]}`)
  57. const secondPreview = preview.getByRole('img', { name: mediaNames[1], exact: true })
  58. await expectDecodedImage(secondPreview)
  59. expect(decodeURIComponent(new URL(await secondPreview.getAttribute('src') || '', baseURL).pathname)).toBe(mediaPaths[1])
  60. await expect(preview.locator('.image-preview-counter')).toHaveText('2 / 2')
  61. await page.keyboard.press('ArrowLeft')
  62. await expect(preview).toHaveAccessibleName(`预览 ${mediaNames[0]}`)
  63. await expectDecodedImage(preview.getByRole('img', { name: mediaNames[0], exact: true }))
  64. await expect(preview.locator('.image-preview-counter')).toHaveText('1 / 2')
  65. await page.screenshot({ path: testInfo.outputPath('hermes-profile-cache-preview.png') })
  66. await page.keyboard.press('Escape')
  67. await expect(preview).toBeHidden()
  68. })
  69. test('restores nine user Profile uploads as images and opens the whole batch in the preview', async ({ page, baseURL }, testInfo) => {
  70. await signIn(page.request, new URL(baseURL!).origin)
  71. await page.goto('/chat/session-user-media?profile=yaoer')
  72. const message = page.locator('.message--user')
  73. await expect(message).toContainText('读取图片中的提示词')
  74. await expect(message).not.toContainText('@image:')
  75. await expect(message).not.toContainText('[用户附加图片')
  76. await expect(message).not.toContainText('[screenshot]')
  77. const images = message.locator('img')
  78. await expect(images).toHaveCount(9)
  79. for (const image of await images.all()) {
  80. await image.scrollIntoViewIfNeeded()
  81. await expectDecodedImage(image)
  82. const url = new URL(await image.getAttribute('src') || '', baseURL)
  83. expect(url.origin).toBe(new URL(baseURL!).origin)
  84. expect(url.pathname).toContain('/.hermes/profiles/yaoer/images/')
  85. }
  86. await page.screenshot({ path: testInfo.outputPath('user-profile-images.png') })
  87. await images.first().click()
  88. const preview = page.locator('.image-preview-overlay')
  89. await expect(preview.locator('.image-preview-counter')).toHaveText('1 / 9')
  90. await expectDecodedImage(preview.getByRole('img', { name: '照片-1.png', exact: true }))
  91. await preview.getByRole('button', { name: '下一张媒体' }).click()
  92. await expect(preview.locator('.image-preview-counter')).toHaveText('2 / 9')
  93. await expectDecodedImage(preview.getByRole('img', { name: '照片-2.png', exact: true }))
  94. await page.screenshot({ path: testInfo.outputPath('user-profile-image-preview.png') })
  95. })