message-feedback.e2e.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Keyless browser regression for durable per-message feedback. Cold-seeds a
  2. // settled two-turn transcript (zero model calls), records a Like through the
  3. // feedback dialog, replaces it through the same dialog with a Dislike, proves
  4. // the judgment survives a full page reload from the Host's canonical log, then
  5. // retracts it.
  6. import { readFile } from 'node:fs/promises'
  7. import { fileURLToPath } from 'node:url'
  8. import type { Browser, Page } from 'playwright'
  9. import { chromium } from 'playwright'
  10. import { SessionId } from '@deepseek-ai/dsh-session'
  11. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  12. import {
  13. acknowledgeReloadConnectionLoss, launchWebScaffold,
  14. seedSession, watchConsole, webSnapshotMode, type WebScaffold,
  15. } from './scaffold.ts'
  16. import { newEnglishPage, saveFailureShot } from './support.ts'
  17. // Borrowed read-only: this scenario needs any settled assistant message to
  18. // address, not a new recording (message-actions / sidebar-scrollbar pattern).
  19. const SEED = fileURLToPath(new URL('../../../snapshots/web/seeded-history/session.v3.jsonl', import.meta.url))
  20. const MODE = webSnapshotMode()
  21. const SEED_ID = 'message-feedback-web-e2e'
  22. const POSITIVE_NOTE = 'Clear and complete.'
  23. const NOTE = 'Read both files before answering.'
  24. describe('web e2e: durable per-message feedback', () => {
  25. let scaffold: WebScaffold
  26. let browser: Browser
  27. let page: Page
  28. let tripwire: ReturnType<typeof watchConsole>
  29. beforeAll(async () => {
  30. scaffold = await launchWebScaffold({})
  31. await seedSession(scaffold, await readFile(SEED, 'utf8'), SEED_ID)
  32. browser = await chromium.launch()
  33. page = await newEnglishPage(browser)
  34. tripwire = watchConsole(page)
  35. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  36. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  37. }, 120_000)
  38. afterAll(async () => {
  39. await browser?.close()
  40. await scaffold?.close()
  41. })
  42. /**
  43. * Open the seeded transcript. The first treeitem is the collapsible group
  44. * row; the session itself is the row beneath it. The group is already
  45. * expanded on a fresh load, so clicking it unconditionally would collapse it
  46. * and hide the session row.
  47. */
  48. async function openSeededSession(): Promise<void> {
  49. const groupRow = page.locator('[role="treeitem"]').first()
  50. await groupRow.waitFor({ timeout: 15_000 })
  51. if (await groupRow.getAttribute('aria-expanded') !== 'true') await groupRow.click()
  52. const sessionRow = page.locator('[role="treeitem"]').nth(1)
  53. await sessionRow.waitFor({ timeout: 15_000 })
  54. await sessionRow.click()
  55. }
  56. it.skipIf(MODE === 'record')('submits both ratings through the dialog, persists the Dislike, then retracts it', async () => {
  57. onTestFailed(() => saveFailureShot(page, 'web-e2e-message-feedback'))
  58. await openSeededSession()
  59. // The controls live in the assistant message's IconActions row, which the
  60. // transcript reveals on hover/focus like copy and branch. Wait for the
  61. // settled closing text first: the strip mounts with that turn's tail.
  62. await page.getByText('DONE', { exact: true }).waitFor({ timeout: 30_000 })
  63. const like = page.getByRole('button', { name: 'Good response' }).first()
  64. await like.waitFor({ timeout: 30_000 })
  65. await like.scrollIntoViewIfNeeded()
  66. await like.hover()
  67. await like.click()
  68. const dialog = page.getByRole('dialog', { name: 'Submit feedback' })
  69. await dialog.waitFor({ timeout: 10_000 })
  70. await dialog.getByRole('button', { name: 'Stability and speed', exact: true }).click()
  71. await dialog.getByRole('textbox', { name: 'Feedback details' }).fill(POSITIVE_NOTE)
  72. await dialog.getByRole('button', { name: 'Submit', exact: true }).click()
  73. await expect.poll(() => dialog.count(), { timeout: 10_000 }).toBe(0)
  74. await page.getByRole('alert').filter({ hasText: 'Thanks for your feedback' }).waitFor({ timeout: 10_000 })
  75. const rated = page.getByRole('button', { name: 'Remove rating' }).first()
  76. await expect.poll(() => rated.getAttribute('aria-pressed'), { timeout: 10_000 }).toBe('true')
  77. // The same dialog records a negative judgment with its own category and
  78. // note, replacing the positive judgment only after submission.
  79. await page.getByRole('button', { name: 'Bad response' }).first().click()
  80. await dialog.waitFor({ timeout: 10_000 })
  81. await expect.poll(() => dialog.getByRole('textbox', { name: 'Feedback details' }).getAttribute('placeholder'))
  82. .toBe('Add details to help us improve. Your submission will include the current conversation log.')
  83. await dialog.getByRole('button', { name: 'Task result', exact: true }).click()
  84. const details = dialog.getByRole('textbox', { name: 'Feedback details' })
  85. await details.fill('x'.repeat(8193))
  86. await dialog.getByRole('button', { name: 'Submit', exact: true }).click()
  87. await page.getByRole('alert').filter({ hasText: 'The description is too long' }).waitFor({ timeout: 10_000 })
  88. expect(await dialog.count()).toBe(1)
  89. await details.fill(NOTE)
  90. await dialog.getByRole('button', { name: 'Submit', exact: true }).click()
  91. await expect.poll(() => dialog.count(), { timeout: 10_000 }).toBe(0)
  92. await expect.poll(() => rated.getAttribute('aria-label'), { timeout: 10_000 }).toBe('Remove rating')
  93. await expect.poll(() => like.getAttribute('aria-pressed'), { timeout: 10_000 }).toBe('false')
  94. // The durable assertion: a cold browser re-reads the sidecar over the wire.
  95. const warningStart = tripwire.warnings.length
  96. await page.reload({ waitUntil: 'load' })
  97. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  98. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  99. await openSeededSession()
  100. await page.getByText('DONE', { exact: true }).waitFor({ timeout: 30_000 })
  101. // The controller defers its list read to the first hover or focus, so a
  102. // cold reload shows the unrated label until the strip is touched. Hovering
  103. // the unrated control is what triggers the authoritative re-read.
  104. const cold = page.getByRole('button', { name: 'Good response' }).first()
  105. await cold.waitFor({ timeout: 30_000 })
  106. await cold.scrollIntoViewIfNeeded()
  107. await cold.hover()
  108. const restored = page.getByRole('button', { name: 'Remove rating' }).first()
  109. await restored.waitFor({ timeout: 30_000 })
  110. await restored.scrollIntoViewIfNeeded()
  111. await restored.hover()
  112. await expect.poll(() => restored.getAttribute('aria-pressed'), { timeout: 15_000 }).toBe('true')
  113. // The retract label sits on the Dislike side: the Like stays unpressed.
  114. await expect.poll(() => cold.getAttribute('aria-pressed'), { timeout: 10_000 }).toBe('false')
  115. const agent = scaffold.ctx.agents.get(SessionId(SEED_ID))
  116. if (agent === undefined) throw new Error('seeded session did not attach an agent')
  117. const puts = agent.session.snapshotEvents().filter(event => event.type === 'feedback/message-put')
  118. expect(puts.map(event => event.type === 'feedback/message-put' ? event.data.item : undefined)).toMatchObject([
  119. { rating: 'positive', note: POSITIVE_NOTE, category: 'service-stability' },
  120. { rating: 'negative', note: NOTE, category: 'task-result' },
  121. ])
  122. // Re-clicking the active rating retracts it, and the note goes with it.
  123. await restored.click()
  124. await expect.poll(
  125. () => page.getByRole('button', { name: 'Bad response' }).first().getAttribute('aria-pressed'),
  126. { timeout: 10_000 },
  127. ).toBe('false')
  128. const last = agent.session.snapshotEvents().at(-1)
  129. expect(last?.type).toBe('feedback/message-delete')
  130. }, 90_000)
  131. it.skipIf(MODE === 'record')('kept the console clean', () => {
  132. expect(tripwire.pageErrors).toEqual([])
  133. expect(tripwire.warnings).toEqual([])
  134. })
  135. })