Files
TradarrV2/frontend/e2e/ticket54-summary-qa.spec.mjs

215 lines
13 KiB
JavaScript

import { expect, test } from '@playwright/test'
import { readStackManifest } from './stack-manifest.mjs'
for (const width of [1280, 375]) test(`ticket #65: fixed chat launcher, synthesis chat and web citations at ${width}px`, async ({ page }) => {
const stack = readStackManifest()
await page.setViewportSize({ width, height: 850 })
await page.context().route('**/*', route => {
const host = new URL(route.request().url()).hostname
return host === '127.0.0.1' || host === '[::1]' ? route.continue() : route.abort('blockedbyclient')
})
const excerpt = 'Passage de la synthèse pour poser une question. '.repeat(12).trim()
const secondExcerpt = 'Autre passage à joindre explicitement.'
const summary = { id: '00000000-0000-0000-0000-000000005401', user_id: 'reader', content: excerpt + '\n\n' + secondExcerpt + '\n\n' + Array.from({ length: 30 }, (_, i) => `Contexte ${i}. ` + 'Texte de contexte. '.repeat(20)).join('\n'), ai_provider_id: null, generated_at: '2026-11-25T00:00:00Z' }
const other = { ...summary, id: '00000000-0000-0000-0000-000000005402', content: 'Autre synthèse de test.' }
const conversations = new Map([summary, other].map(item => [item.id, { id: `c-${item.id}`, web_search_supported: true, summary_id: item.id, mode: 'stateless', uses_source_articles: false, provider_session_supported: false, source_expires_at: '2026-01-01T00:00:00Z', sources_expired: true, source_count: 0, messages: [] }]))
const questions = []
await page.route('**/api/**', async route => {
const path = new URL(route.request().url()).pathname
const respond = data => route.fulfill({ json: { data } })
if (path === '/api/summaries') return respond([summary, other])
if (path === '/api/summaries/generations' || path === '/api/me/assets') return respond([])
if (path === '/api/summaries/generation-jobs/recent') return respond(null)
for (const [id, conversation] of conversations) {
if (path === `/api/summaries/${id}/conversation`) return respond(conversation)
if (path === `/api/summaries/${id}/conversation/messages`) {
const question = route.request().postDataJSON()
questions.push(question)
const turn = { ...question, id: `t-${questions.length}`, web_search_performed: question.use_web_search, citations: question.use_web_search ? [{ title: 'Source de test', url: 'https://example.test/source' }] : [], answer: question.use_web_search ? 'Réponse avec recherche simulée.' : 'Réponse globale sur cette synthèse.', status: 'answered', replayed: false, created_at: '2026-11-25T00:00:00Z' }
conversation.messages.push(turn)
return respond(turn)
}
}
return route.continue()
})
await page.goto(new URL('/login', stack.frontendUrl).href)
await page.getByLabel('Adresse email').fill(stack.bootstrapAdmin.email)
await page.getByLabel('Mot de passe').fill(stack.bootstrapAdmin.password)
await page.getByRole('button', { name: 'Se connecter' }).click()
const passage = page.getByText(excerpt, { exact: true })
await expect(passage).toBeVisible()
const action = page.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' })
await expect(action).toBeVisible()
await expect(action).toHaveAttribute('title', 'Ouvrir le chat de cette synthèse')
const wrapper = action.locator('..')
expect(await wrapper.evaluate(element => element.parentElement === document.body)).toBe(true)
await expect(wrapper).toHaveCSS('position', 'fixed')
await expect(action).toHaveCSS('width', '48px')
await expect(action).toHaveCSS('height', '48px')
const nav = page.getByRole('navigation', { name: 'Navigation mobile' })
async function checkPosition() {
const box = await action.boundingBox()
const viewport = page.viewportSize()
if (viewport.width >= 768) {
expect(viewport.width - box.x - box.width).toBeCloseTo(24)
expect(viewport.height - box.y - box.height).toBeCloseTo(24)
await expect(nav).not.toBeVisible()
} else {
const safeRight = await page.evaluate(() => {
const probe = document.createElement('div')
probe.style.paddingRight = 'env(safe-area-inset-right, 0px)'
document.body.append(probe)
const inset = parseFloat(getComputedStyle(probe).paddingRight)
probe.remove()
return inset
})
expect(viewport.width - box.x - box.width).toBeCloseTo(16 + safeRight)
await expect.poll(async () => {
const navBox = await nav.boundingBox(), launcherBox = await action.boundingBox()
return Math.round((navBox.y - launcherBox.y - launcherBox.height) * 10) / 10
}).toBe(16)
expect(Number(await wrapper.evaluate(element => getComputedStyle(element).zIndex))).toBeGreaterThan(Number(await nav.evaluate(element => getComputedStyle(element).zIndex)))
}
expect(await action.evaluate(element => {
const rect = element.getBoundingClientRect()
return element.contains(document.elementFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2))
})).toBe(true)
}
await checkPosition()
const initialBox = await action.boundingBox()
const main = page.locator('main')
await main.evaluate(element => { element.scrollTop = element.scrollHeight })
expect(await main.evaluate(element => element.scrollTop)).toBeGreaterThan(0)
const scrolledBox = await action.boundingBox()
expect(scrolledBox.x).toBeCloseTo(initialBox.x)
expect(scrolledBox.y).toBeCloseTo(initialBox.y)
await checkPosition()
if (width < 768) {
// Exercise actual nav height changes (including extra safe-area-like padding).
await nav.evaluate(element => { element.style.paddingBottom = '23px' })
await checkPosition()
await nav.evaluate(element => { element.style.removeProperty('padding-bottom') })
await checkPosition()
await page.setViewportSize({ width, height: 650 })
await page.evaluate(() => window.dispatchEvent(new Event('orientationchange')))
await checkPosition()
await page.setViewportSize({ width, height: 850 })
await checkPosition()
// Extra main padding keeps the final history card above the launcher and nav.
await main.evaluate(element => { element.scrollTop = element.scrollHeight })
const lastCard = await page.getByRole('button', { name: 'Autre synthèse de test.…' }).boundingBox()
const launcherBox = await action.boundingBox()
expect(lastCard.y + lastCard.height).toBeLessThanOrEqual(launcherBox.y)
}
const mainScrollBeforeChat = await main.evaluate(element => element.scrollTop)
const launcherZ = Number(await wrapper.evaluate(element => getComputedStyle(element).zIndex))
// Global question is possible without selecting or attaching any text.
await action.click()
const dialog = page.getByRole('dialog', { name: 'Questions sur cette synthèse' })
const question = dialog.getByRole('textbox', { name: 'Votre question' })
const webSearch = dialog.getByRole('checkbox', { name: 'Utiliser la recherche Web si nécessaire pour répondre ; la synthèse complète, les échanges précédents, votre question et tout passage joint seront transmis au fournisseur du modèle sélectionné.' })
const add = dialog.getByRole('button', { name: 'Ajouter le passage sélectionné à la question' })
await expect(dialog.getByRole('heading')).toBeFocused()
expect(Number(await dialog.evaluate(element => getComputedStyle(element).zIndex))).toBeGreaterThan(launcherZ)
await expect(question).toHaveValue('')
await expect(add).toHaveCount(0)
expect(questions).toHaveLength(0)
await expect(webSearch).toBeChecked()
await webSearch.uncheck()
await question.fill('Que signifie cette synthèse ?')
await dialog.getByRole('button', { name: 'Envoyer la question' }).click()
await expect(dialog.getByText('Réponse globale sur cette synthèse.', { exact: true })).toBeVisible()
expect(questions[0]).toMatchObject({ passage: '', question: 'Que signifie cette synthèse ?', use_web_search: false })
await expect(dialog.getByRole('link')).toHaveCount(0)
await expect(webSearch).toBeChecked()
await expect(question).toHaveValue('')
await page.keyboard.press('Escape')
await expect(action).toBeFocused()
expect(await main.evaluate(element => element.scrollTop)).toBe(mainScrollBeforeChat)
async function selectText(element) {
await element.evaluate(element => {
for (const name of ['contextmenu', 'selectstart', 'copy', 'touchstart']) {
if (!element.dispatchEvent(new Event(name, { bubbles: true, cancelable: true }))) throw new Error(`${name} blocked`)
}
if (getComputedStyle(element).userSelect === 'none') throw new Error('selection disabled')
const range = document.createRange(); range.selectNodeContents(element)
const selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range)
document.dispatchEvent(new Event('selectionchange'))
})
}
await selectText(passage)
await action.click()
await expect(dialog.getByLabel('Passage joint', { exact: true })).toHaveCount(0)
await add.click()
await expect(dialog.getByLabel('Passage joint', { exact: true })).toHaveText(excerpt)
await expect(dialog.getByLabel('Passage joint', { exact: true })).toHaveCSS('-webkit-line-clamp', '3')
await question.fill('Que signifie ce passage ?')
await page.keyboard.press('Escape')
await selectText(page.getByText(secondExcerpt, { exact: true }))
await action.click()
await expect(question).toHaveValue('Que signifie ce passage ?')
await expect(dialog.getByLabel('Passage joint', { exact: true })).toHaveText(excerpt)
await add.click()
await expect(dialog.getByLabel('Passage joint', { exact: true })).toHaveText(secondExcerpt)
await dialog.getByRole('button', { name: 'Retirer le passage joint' }).click()
await expect(dialog.getByLabel('Passage joint', { exact: true })).toHaveCount(0)
await add.click()
await page.setViewportSize({ width, height: 450 })
const box = await dialog.boundingBox()
expect(box.y).toBeGreaterThanOrEqual(0)
expect(box.y + box.height).toBeLessThanOrEqual(450)
for (let i = 0; i < 10; i++) {
await page.keyboard.press('Tab')
expect(await dialog.evaluate(element => element.contains(document.activeElement))).toBe(true)
}
await dialog.getByRole('button', { name: 'Envoyer la question' }).click()
await expect(dialog.getByText('Réponse avec recherche simulée.', { exact: true })).toBeVisible()
expect(questions).toHaveLength(2)
expect(questions[1]).toMatchObject({ passage: secondExcerpt, question: 'Que signifie ce passage ?', use_web_search: true })
expect(questions[1].request_id).not.toBe(questions[0].request_id)
await expect(dialog.getByRole('article', { name: 'Vous', exact: true }).first()).toHaveCSS('align-self', 'flex-end')
const assistant = dialog.getByRole('article', { name: 'Assistant', exact: true }).last()
await expect(assistant).toHaveCSS('align-self', 'flex-start')
const source = assistant.getByRole('link', { name: 'Source de test (nouvel onglet)' })
await expect(source).toHaveAttribute('href', 'https://example.test/source')
await expect(source).toHaveAttribute('target', '_blank')
await expect(source).toHaveAttribute('rel', 'noopener noreferrer')
await expect(question).toHaveValue('')
await expect(dialog.getByLabel('Passage joint', { exact: true })).toHaveCount(0)
expect(await dialog.evaluate(element => element.scrollWidth <= element.clientWidth)).toBe(true)
expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true)
await add.click()
await question.fill('Brouillon non envoyé')
await webSearch.uncheck()
await page.keyboard.press('Escape')
await expect(action).toBeFocused()
await action.click()
await expect(question).toHaveValue('Brouillon non envoyé')
await expect(webSearch).not.toBeChecked()
await page.keyboard.press('Escape')
await page.getByRole('button', { name: 'Autre synthèse de test.…' }).click()
await action.click()
await expect(question).toHaveValue('')
await expect(webSearch).toBeChecked()
await expect(dialog.getByText('Aucune question pour cette synthèse.')).toBeVisible()
await expect(dialog.getByText('Réponse globale sur cette synthèse.', { exact: true })).toHaveCount(0)
await question.fill('Brouillon autre synthèse')
await page.keyboard.press('Escape')
await page.getByRole('button', { name: summary.content.slice(0, 120) + '…', exact: true }).click()
await action.click()
await expect(question).toHaveValue('Brouillon non envoyé')
await expect(dialog.getByLabel('Passage joint', { exact: true })).toHaveText(secondExcerpt)
await expect(webSearch).not.toBeChecked()
await expect(dialog.getByText('Réponse globale sur cette synthèse.', { exact: true })).toBeVisible()
await page.keyboard.press('Escape')
await page.getByRole('button', { name: 'Autre synthèse de test.…' }).click()
await action.click()
await expect(question).toHaveValue('Brouillon autre synthèse')
await expect(webSearch).toBeChecked()
await expect(dialog.getByLabel('Passage joint', { exact: true })).toHaveCount(0)
await expect(dialog.getByText('Aucune question pour cette synthèse.')).toBeVisible()
})