teqo-backup/tests/e2e/campaignSpeechAcervo.e2e.spec.ts

323 lines
14 KiB
TypeScript

import { request as playwrightRequest } from '@playwright/test'
import type { SpeechScope, SpeechTopic } from '@/lib/speechFacets'
import type { CampaignE2EOwnership } from './fixtures/campaignE2EFixtures.js'
import { assertCampaignRedirect, expect, rendered, test } from './fixtures/campaignHttpTest.js'
/**
* C154/C162 — the communication vertical over real HTTP (browserless,
* OPS35/OPS87): role gates, the acervo search rendering (highlight + excerpt),
* the filter URL contract and the detail's player/transcript/actions. The
* player picks the surface from the row coordinates — YouTube default when the
* session link parses, stored-VOD capa resolved on click otherwise, honest
* unavailable state when neither exists — and the stored VOD link itself is
* never rendered (C162). Playback is browser territory and is not asserted
* here; the HTML carries the contract (`<video src>` only after a click,
* `data-start-seconds`, the iframe `start`).
*/
const YOUTUBE_VIDEO_ID = 'lLhRDkSPw0A'
const EXCERPT_TMS = 1786473834650
const EXCERPT_SESSION = '2026-08-11T15:00'
const EXCERPT_OFFSET_SECONDS = 2634
const createSpeech = async (
campaign: { fixtures: CampaignE2EOwnership },
input: {
marker: string
speechAt?: string
topics?: SpeechTopic[]
scopes?: SpeechScope[]
keywords?: string[]
durationSeconds?: number
withVideo?: boolean
youtubeUrl?: string | null
eventStartAt?: string
},
) => {
const marker = input.marker
const speech = await campaign.fixtures.payload.create({
collection: 'speech',
data: {
sourceKey: campaign.fixtures.value('speech'),
speechAt: input.speechAt ?? '2026-03-10T18:00',
type: 'BREVES COMUNICAÇÕES',
phase: 'Breves Comunicações',
durationSeconds: input.durationSeconds ?? 252,
summary: `Sumário oficial com o marcador ${marker}.`,
officialTranscript: 'O SR. JORGE SOLLA (Bloco/PT - BA) - Sr. Presidente...',
keywords: input.keywords ?? ['Farmácia Popular'],
topics: input.topics ?? ['saude'],
scopes: input.scopes ?? ['bahia'],
classifiedBy: 'gazetteer',
// Mirrors what `upsertSpeechBundle` derives from the segments (C154).
searchText: `a retomada da saude ${marker} na bahia`,
presidingOfficer: 'Pompeo de Mattos',
officialTextUrl: 'https://camara.leg.br/discurso',
eventId: 67091,
audioId: 558641,
excerptTMs: EXCERPT_TMS,
eventStartAt: input.eventStartAt ?? EXCERPT_SESSION,
youtubeUrl: input.youtubeUrl ?? null,
...(input.withVideo === false
? {}
: {
vodPlaybackUrl: 'https://vod.camara.leg.br/excerpt.mp4',
vodDownloadUrl: 'https://vod.camara.leg.br/excerpt.mp4',
}),
},
depth: 0,
})
await campaign.fixtures.payload.create({
collection: 'speechSegment',
data: {
speech: speech.id,
order: 1,
startSeconds: 0,
endSeconds: 4,
text: `A retomada da saúde ${marker} na Bahia`,
searchText: `a retomada da saude ${marker} na bahia`,
},
depth: 0,
})
return speech
}
test.describe('communication vertical (C154/C162)', () => {
test('communicator lands on the acervo and sees only the vertical nav, with her scoped Sollinha', async ({
campaign,
campaignRequest,
}) => {
const user = await campaign.fixtures.createCampaignUser('communicator')
const request = await campaignRequest(user, user.password)
await assertCampaignRedirect(request, '/campanha', '/campanha/comunicacao')
await assertCampaignRedirect(request, '/campanha/comunicacao', '/campanha/comunicacao/acervo')
const response = await request.get('/campanha/comunicacao/acervo')
expect(response.status()).toBe(200)
const html = rendered(await response.text())
expect(html).toContain('Acervo de falas')
expect(html).toContain('href="/campanha/comunicacao"')
expect(html).not.toContain('href="/campanha/apoiadores"')
expect(html).not.toContain('href="/campanha/municipios"')
// C159 — the communication assessor gets the Sollinha scoped to the
// acervo: the surface exists, the greeting speaks acervo and the opening
// chips are hers (never the staff set).
expect(html).toContain('campaign-ai-shell')
expect(html).toContain('Olá! Eu sou o Sollinha')
expect(html).toContain('O que o Solla já falou sobre Farmácia Popular?')
expect(html).toContain('Qual um trecho bom sobre o hospital do subúrbio?')
expect(html).not.toContain('Quem foi o deputado mais votado em Feira de Santana?')
})
test('coordinator sees the vertical from the staff nav; advisor does not', async ({
campaign,
campaignRequest,
}) => {
const coordinator = await campaign.fixtures.createCampaignUser('coordinator')
const coordinatorRequest = await campaignRequest(coordinator, coordinator.password)
const coordinatorHome = await coordinatorRequest.get('/campanha')
expect(coordinatorHome.status()).toBe(200)
expect(rendered(await coordinatorHome.text())).toContain('href="/campanha/comunicacao"')
const advisor = await campaign.fixtures.createCampaignUser('advisor')
const advisorRequest = await campaignRequest(advisor, advisor.password)
const advisorHome = await advisorRequest.get('/campanha')
expect(advisorHome.status()).toBe(200)
expect(rendered(await advisorHome.text())).not.toContain('href="/campanha/comunicacao"')
})
test('search keeps the download off the card; the both-sources detail defaults to YouTube', async ({
campaign,
campaignRequest,
}) => {
const marker = campaign.fixtures.value('falateste')
const speech = await createSpeech(campaign, {
marker,
youtubeUrl: `https://www.youtube.com/watch?v=${YOUTUBE_VIDEO_ID}`,
})
const user = await campaign.fixtures.createCampaignUser('communicator')
const request = await campaignRequest(user, user.password)
const results = await request.get(`/campanha/comunicacao/acervo?q=${marker}`)
expect(results.status()).toBe(200)
const resultsHtml = rendered(await results.text())
expect(resultsHtml).toContain(marker)
expect(resultsHtml).toContain('<mark')
expect(resultsHtml).toContain('Assistir no trecho')
expect(resultsHtml).toContain('1 fala encontrada')
// C162 — the direct download left the card, and the stored ephemeral link
// never reaches any list HTML.
expect(resultsHtml).not.toContain('Baixar')
expect(resultsHtml).not.toContain('vod.camara.leg.br')
const detail = await request.get(`/campanha/comunicacao/acervo/${speech.id}?t=43&q=${marker}`)
expect(detail.status()).toBe(200)
const detailHtml = rendered(await detail.text())
expect(detailHtml).toContain('data-slot="speech-player"')
// Default surface is the YouTube embed, positioned by the session offset
// (2634) plus the deep-linked transcript second (43).
expect(detailHtml).toContain(`youtube-nocookie.com/embed/${YOUTUBE_VIDEO_ID}`)
expect(detailHtml).toContain(`start=${EXCERPT_OFFSET_SECONDS + 43}`)
expect(detailHtml).toContain('data-start-seconds="0"')
expect(detailHtml).toContain('Baixar vídeo (MP4)')
expect(detailHtml).toContain('Abrir fonte')
expect(detailHtml).toContain('Fonte: Câmara dos Deputados')
// C166 — the excerpt picker's door is server-rendered; its link limit is
// not in this quadrant (the share control itself only appears in the mode,
// which is client interaction).
expect(detailHtml).toContain('Selecionar trecho')
expect(detailHtml).not.toContain('Compartilhar por link exige o vídeo no YouTube')
expect(detailHtml).not.toContain('<video')
expect(detailHtml).not.toContain('vod.camara.leg.br')
})
test('a VOD-only speech offers the on-click resolution instead of a player', async ({
campaign,
campaignRequest,
}) => {
const marker = campaign.fixtures.value('sovod')
const speech = await createSpeech(campaign, { marker })
const user = await campaign.fixtures.createCampaignUser('communicator')
const request = await campaignRequest(user, user.password)
const detail = await request.get(`/campanha/comunicacao/acervo/${speech.id}`)
expect(detail.status()).toBe(200)
const html = rendered(await detail.text())
expect(html).toContain('Assistir o trecho')
expect(html).toContain('Baixar vídeo (MP4)')
// C166 — no YouTube means no excerpt link: the honest notice replaces it
// while the selection door stays offered.
expect(html).toContain('Selecionar trecho')
expect(html).toContain('Compartilhar por link exige o vídeo no YouTube')
expect(html).not.toContain('<video')
expect(html).not.toContain('<iframe')
// The stored link is an eligibility signal, never a rendered URL.
expect(html).not.toContain('vod.camara.leg.br')
})
test('facet filters narrow the results and the empty state suggests clearing', async ({
campaign,
campaignRequest,
}) => {
const marker = campaign.fixtures.value('filtroteste')
await createSpeech(campaign, {
marker,
speechAt: '2019-05-10T15:00',
topics: ['igualdade-racial'],
scopes: ['brasil'],
durationSeconds: 90,
})
const user = await campaign.fixtures.createCampaignUser('communicator')
const request = await campaignRequest(user, user.password)
const matching = await request.get(
`/campanha/comunicacao/acervo?q=${marker}&topic=igualdade-racial&year=2019&duration=curta`,
)
expect(rendered(await matching.text())).toContain(marker)
const filteredOut = await request.get(`/campanha/comunicacao/acervo?q=${marker}&topic=saude`)
const filteredHtml = rendered(await filteredOut.text())
// The term still rides the search chip; what must be gone is the result.
expect(filteredHtml).not.toContain('Assistir no trecho')
expect(filteredHtml).toContain('Nenhuma fala encontrada')
expect(filteredHtml).toContain('Limpar busca e filtros')
})
test('advisor and leader are redirected away from the acervo', async ({
campaign,
campaignRequest,
}) => {
const advisor = await campaign.fixtures.createCampaignUser('advisor')
const advisorRequest = await campaignRequest(advisor, advisor.password)
await assertCampaignRedirect(advisorRequest, '/campanha/comunicacao/acervo', '/campanha')
const leader = await campaign.fixtures.createCampaignUser('leader')
const leaderRequest = await campaignRequest(leader, leader.password)
await assertCampaignRedirect(
leaderRequest,
'/campanha/comunicacao/acervo',
'/campanha/meus-contatos',
)
})
test('a speech without video renders the unavailable block instead of a player', async ({
campaign,
campaignRequest,
}) => {
const marker = campaign.fixtures.value('semvod')
const speech = await createSpeech(campaign, { marker, withVideo: false })
const user = await campaign.fixtures.createCampaignUser('communicator')
const request = await campaignRequest(user, user.password)
const detail = await request.get(`/campanha/comunicacao/acervo/${speech.id}`)
const html = rendered(await detail.text())
expect(html).toContain('Vídeo indisponível neste momento')
expect(html).toContain('Abrir fonte')
// No stored VOD means nothing to resolve: no MP4 button and no retry.
expect(html).not.toContain('Baixar vídeo (MP4)')
expect(html).not.toContain('Tentar novamente')
})
test.describe('POST /campanha/comunicacao/acervo/resolver-vod (C162)', () => {
const endpoint = '/campanha/comunicacao/acervo/resolver-vod'
test('rejects a request without a campaign session', async ({ campaign }) => {
const anonymous = await playwrightRequest.newContext({ baseURL: campaign.baseURL })
try {
const response = await anonymous.post(endpoint, { data: { speechId: 1 } })
expect(response.status()).toBe(401)
} finally {
await anonymous.dispose()
}
})
test('rejects a cross-origin request before any read', async ({
campaign,
campaignRequest,
}) => {
const marker = campaign.fixtures.value('origem')
const speech = await createSpeech(campaign, { marker })
const user = await campaign.fixtures.createCampaignUser('communicator')
const request = await campaignRequest(user, user.password)
const response = await request.post(endpoint, {
data: { speechId: speech.id },
headers: { Origin: 'https://evil.example' },
})
expect(response.status()).toBe(403)
expect(await response.text()).toContain('Requisição inválida.')
})
test('refuses an actor outside the catalog gate', async ({ campaign, campaignRequest }) => {
const marker = campaign.fixtures.value('assessor')
const speech = await createSpeech(campaign, { marker })
const advisor = await campaign.fixtures.createCampaignUser('advisor')
const request = await campaignRequest(advisor, advisor.password)
const response = await request.post(endpoint, { data: { speechId: speech.id } })
expect(response.status()).toBe(400)
expect(await response.text()).toContain('Você não tem acesso ao acervo de falas.')
})
test('refuses a speech without a stored VOD without calling the Câmara', async ({
campaign,
campaignRequest,
}) => {
const marker = campaign.fixtures.value('inelegivel')
const speech = await createSpeech(campaign, { marker, withVideo: false })
const user = await campaign.fixtures.createCampaignUser('communicator')
const request = await campaignRequest(user, user.password)
const response = await request.post(endpoint, { data: { speechId: speech.id } })
expect(response.status()).toBe(400)
expect(await response.text()).toContain('não tem trecho de vídeo')
})
})
})