mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-17 04:23:07 +00:00
Papel communicator com nav própria e gate speechCatalog; acervo em /campanha/comunicacao/acervo com busca normalizada (destaque no trecho), filtros de ano/tema/alcance/fase/município/duração em URL canônica e paginação por discurso via speech.searchText (GIN trgm + backfill). Detalhe com player nativo no trecho, transcrição clicável, baixar/abrir fonte e fallback sem VOD. Resolve o débito do C153 (Sollinha fora da vertical) e corrige o parser de keywords oficiais no dono. Closes #956
478 lines
15 KiB
TypeScript
478 lines
15 KiB
TypeScript
// @vitest-environment node
|
|
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
import {
|
|
buildVodUrl,
|
|
clockToSeconds,
|
|
legislatureForDate,
|
|
matchExcerpt,
|
|
normalizeSpeakerName,
|
|
normalizeTranscription,
|
|
parseDurationToSeconds,
|
|
parseEventExcerpts,
|
|
parseOfficialKeywords,
|
|
parsePresidingOfficerTransitions,
|
|
parseVodStatus,
|
|
resolvePresidingOfficer,
|
|
selectSpeechEvents,
|
|
speechContentHash,
|
|
speechDate,
|
|
speechSourceKey,
|
|
speechTimeOfDaySeconds,
|
|
} from '../../scripts/lib/camaraSpeeches.mjs'
|
|
|
|
const excerptHtml = (
|
|
eventId: number,
|
|
{
|
|
audioId,
|
|
tMs,
|
|
speaker,
|
|
party,
|
|
startTime,
|
|
duration,
|
|
}: {
|
|
audioId: number
|
|
tMs: number
|
|
speaker: string
|
|
party: string
|
|
startTime: string
|
|
duration: string
|
|
},
|
|
) => `
|
|
<article class="g-chamada">
|
|
<a id="link-trecho-video" href="https://www.camara.leg.br/evento-legislativo/${eventId}?a=${audioId}&t=${tMs}&trechosOrador=&crawl=no" class="chamada__link-trecho" titulo="${speaker}">
|
|
<div class="l-chamada-conteudo">
|
|
<h4 class="g-chamada__titulo"><b>${speaker}</b></h4>
|
|
<span class="chamada__cargo">${party}</span>
|
|
<div class="chamada__info-video">
|
|
<span class="chamada__info-video-hora">Horário - ${startTime}</span>
|
|
<br><span class="chamada__info-video-duracao">Duração - ${duration}</span>
|
|
</div>
|
|
</div>
|
|
</a>
|
|
</article>`
|
|
|
|
describe('normalizeSpeakerName', () => {
|
|
it('strips accents, case and punctuation', () => {
|
|
expect(normalizeSpeakerName(' Jorge Solla ')).toBe('JORGE SOLLA')
|
|
expect(normalizeSpeakerName('José Ação')).toBe('JOSE ACAO')
|
|
expect(normalizeSpeakerName('')).toBe('')
|
|
})
|
|
})
|
|
|
|
describe('clock helpers', () => {
|
|
it('parses HH:MM and HH:MM:SS', () => {
|
|
expect(clockToSeconds('17:30')).toBe(63000)
|
|
expect(clockToSeconds('09:28:15')).toBe(34095)
|
|
expect(clockToSeconds('24:00')).toBeNull()
|
|
expect(clockToSeconds('abc')).toBeNull()
|
|
})
|
|
|
|
it('extracts time-of-day and date from a naive datetime', () => {
|
|
expect(speechTimeOfDaySeconds('2023-02-07T17:28')).toBe(62880)
|
|
expect(speechTimeOfDaySeconds('2023-02-07')).toBeNull()
|
|
expect(speechDate('2023-02-07T17:28')).toBe('2023-02-07')
|
|
})
|
|
})
|
|
|
|
describe('parseEventExcerpts', () => {
|
|
it('reads the speaker anchors with card metadata', () => {
|
|
const html = [
|
|
excerptHtml(67091, {
|
|
audioId: 558641,
|
|
tMs: 1675801808560,
|
|
speaker: 'JORGE SOLLA',
|
|
party: 'DEPUTADO (PT-BA)',
|
|
startTime: '17:30',
|
|
duration: '0h04\'07"',
|
|
}),
|
|
excerptHtml(67091, {
|
|
audioId: 558641,
|
|
tMs: 1675792829010,
|
|
speaker: 'POMPEO DE MATTOS',
|
|
party: 'DEPUTADO (PDT-RS)',
|
|
startTime: '21:22',
|
|
duration: '0h04\'03"',
|
|
}),
|
|
].join('\n')
|
|
|
|
expect(parseEventExcerpts(html, 67091)).toEqual([
|
|
{
|
|
audioId: 558641,
|
|
tMs: 1675801808560,
|
|
speaker: 'JORGE SOLLA',
|
|
party: 'DEPUTADO (PT-BA)',
|
|
startTime: '17:30',
|
|
duration: '0h04\'07"',
|
|
},
|
|
{
|
|
audioId: 558641,
|
|
tMs: 1675792829010,
|
|
speaker: 'POMPEO DE MATTOS',
|
|
party: 'DEPUTADO (PDT-RS)',
|
|
startTime: '21:22',
|
|
duration: '0h04\'03"',
|
|
},
|
|
])
|
|
})
|
|
|
|
it('returns [] when the page has no excerpt anchors or the event id differs', () => {
|
|
const html = excerptHtml(111, {
|
|
audioId: 5,
|
|
tMs: 1000,
|
|
speaker: 'X',
|
|
party: 'Y',
|
|
startTime: '10:00',
|
|
duration: '0h01\'00"',
|
|
})
|
|
expect(parseEventExcerpts(html, 67091)).toEqual([])
|
|
expect(parseEventExcerpts('', 67091)).toEqual([])
|
|
})
|
|
|
|
it('degrades card metadata to null when the spans are missing', () => {
|
|
const html =
|
|
'<a id="link-trecho-video" href="https://www.camara.leg.br/evento-legislativo/67091?a=5&t=1000&trechosOrador=&crawl=no" class="chamada__link-trecho" titulo="JORGE SOLLA">x</a>'
|
|
expect(parseEventExcerpts(html, 67091)).toEqual([
|
|
{
|
|
audioId: 5,
|
|
tMs: 1000,
|
|
speaker: 'JORGE SOLLA',
|
|
party: null,
|
|
startTime: null,
|
|
duration: null,
|
|
},
|
|
])
|
|
})
|
|
})
|
|
|
|
describe('buildVodUrl', () => {
|
|
it('targets the async VOD endpoint with audio id and excerpt epoch', () => {
|
|
expect(buildVodUrl(67091, 558641, 1675801808560)).toBe(
|
|
'https://www.camara.leg.br/evento-legislativo/67091/video-sob-demanda?idAudio=558641&trecho=1675801808560',
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('parseVodStatus', () => {
|
|
it('normalizes the PRONTO payload', () => {
|
|
expect(
|
|
parseVodStatus({
|
|
estado: 'PRONTO',
|
|
video: {
|
|
titulo: 'Jorge Solla',
|
|
subtitulo: 'DEPUTADO (PT-BA)',
|
|
duracao: '0:04:07',
|
|
horario: "17h30'08''",
|
|
linkParaDownload: 'https://vod.camara.leg.br/vod/download?p=abc.mp4',
|
|
linkParaReproducao: 'https://vod.camara.leg.br/videos/abc.mp4',
|
|
},
|
|
}),
|
|
).toEqual({
|
|
state: 'PRONTO',
|
|
video: {
|
|
title: 'Jorge Solla',
|
|
subtitle: 'DEPUTADO (PT-BA)',
|
|
duration: '0:04:07',
|
|
clock: "17h30'08''",
|
|
downloadUrl: 'https://vod.camara.leg.br/vod/download?p=abc.mp4',
|
|
playbackUrl: 'https://vod.camara.leg.br/videos/abc.mp4',
|
|
},
|
|
})
|
|
})
|
|
|
|
it('handles GERANDO/INDISPONIVEL and malformed input', () => {
|
|
expect(parseVodStatus({ estado: 'GERANDO', video: null })).toEqual({
|
|
state: 'GERANDO',
|
|
video: null,
|
|
})
|
|
expect(parseVodStatus({ estado: 'INDISPONIVEL', video: null })).toEqual({
|
|
state: 'INDISPONIVEL',
|
|
video: null,
|
|
})
|
|
expect(parseVodStatus(null)).toEqual({ state: 'DESCONHECIDO', video: null })
|
|
})
|
|
})
|
|
|
|
describe('selectSpeechEvents', () => {
|
|
const events: Array<{ id: number; dataHoraInicio: string }> = [
|
|
{ id: 1, dataHoraInicio: '2023-02-07T15:00' },
|
|
{ id: 2, dataHoraInicio: '2023-02-07T20:00' },
|
|
{ id: 3, dataHoraInicio: '2023-02-08T15:00' },
|
|
]
|
|
|
|
it('keeps only the speech date and sorts nearest-first', () => {
|
|
const selected = selectSpeechEvents(events, '2023-02-07T17:28')
|
|
expect(selected.map((e) => e.id)).toEqual([1, 2])
|
|
})
|
|
|
|
it('is empty when nothing matches the date', () => {
|
|
expect(selectSpeechEvents(events, '2020-01-01T10:00')).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('matchExcerpt', () => {
|
|
const excerpts = [
|
|
{
|
|
audioId: 1,
|
|
tMs: 100,
|
|
speaker: 'JORGE SOLLA',
|
|
party: 'DEPUTADO (PT-BA)',
|
|
startTime: '09:28',
|
|
duration: null,
|
|
},
|
|
{
|
|
audioId: 2,
|
|
tMs: 200,
|
|
speaker: 'JORGE SOLLA',
|
|
party: 'DEPUTADO (PT-BA)',
|
|
startTime: '17:30',
|
|
duration: null,
|
|
},
|
|
{
|
|
audioId: 3,
|
|
tMs: 300,
|
|
speaker: 'OUTRO',
|
|
party: null,
|
|
startTime: '09:00',
|
|
duration: null,
|
|
},
|
|
]
|
|
|
|
it('picks the speaker excerpt nearest the speech time (accent-insensitive)', () => {
|
|
expect(matchExcerpt(excerpts, '2016-03-10T09:28', 'Jorge Solla')?.audioId).toBe(1)
|
|
expect(matchExcerpt(excerpts, '2023-02-07T17:28', 'Jorge Solla')?.audioId).toBe(2)
|
|
})
|
|
|
|
it('returns null when the speaker is absent or empty', () => {
|
|
expect(matchExcerpt(excerpts, '2016-03-10T09:28', 'Fulano de Tal')).toBeNull()
|
|
expect(matchExcerpt(excerpts, '2016-03-10T09:28', '')).toBeNull()
|
|
})
|
|
|
|
it('prefers an excerpt with a clock over one without', () => {
|
|
const withMissing = [
|
|
{ audioId: 9, tMs: 1, speaker: 'JORGE SOLLA', party: null, startTime: null, duration: null },
|
|
...excerpts,
|
|
]
|
|
expect(matchExcerpt(withMissing, '2016-03-10T09:28', 'Jorge Solla')?.audioId).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('speechSourceKey', () => {
|
|
it('joins datetime, type and phase into the stable identity', () => {
|
|
expect(
|
|
speechSourceKey({
|
|
dataHoraInicio: '2023-02-07T17:28',
|
|
tipoDiscurso: 'BREVES COMUNICAÇÕES',
|
|
faseEvento: { titulo: 'Breves Comunicações' },
|
|
}),
|
|
).toBe('2023-02-07T17:28|BREVES COMUNICAÇÕES|Breves Comunicações')
|
|
})
|
|
|
|
it('degrades missing parts to empty strings', () => {
|
|
expect(speechSourceKey({})).toBe('||')
|
|
expect(speechSourceKey(null)).toBe('||')
|
|
})
|
|
})
|
|
|
|
describe('speechContentHash', () => {
|
|
it('is stable for the same content and differs otherwise', () => {
|
|
expect(speechContentHash('Sumário', 'Transcrição')).toBe(
|
|
speechContentHash('Sumário', 'Transcrição'),
|
|
)
|
|
expect(speechContentHash('Sumário', 'Transcrição')).not.toBe(
|
|
speechContentHash('Outro sumário', 'Transcrição'),
|
|
)
|
|
expect(speechContentHash(null, null)).toHaveLength(12)
|
|
})
|
|
})
|
|
|
|
describe('parseOfficialKeywords', () => {
|
|
it('splits newline-separated groups and comma-separated keywords', () => {
|
|
expect(parseOfficialKeywords('Saúde, SUS\r\nEducação\r\n\r\nEscola')).toEqual([
|
|
'Saúde',
|
|
'SUS',
|
|
'Educação',
|
|
'Escola',
|
|
])
|
|
// Live Câmara shape: one group per line, keywords joined by commas.
|
|
expect(parseOfficialKeywords('Governo federal,reconstrução,Política pública')).toEqual([
|
|
'Governo federal',
|
|
'reconstrução',
|
|
'Política pública',
|
|
])
|
|
expect(parseOfficialKeywords(null)).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('legislatureForDate', () => {
|
|
it('maps dates to the legislature ranges at the boundaries', () => {
|
|
expect(legislatureForDate('2023-02-01')).toBe('57')
|
|
expect(legislatureForDate('2023-01-31')).toBe('56')
|
|
expect(legislatureForDate('2015-02-01')).toBe('55')
|
|
expect(legislatureForDate('2015-01-31')).toBe('54')
|
|
expect(legislatureForDate('2011-01-01')).toBe('54')
|
|
})
|
|
|
|
it('returns null outside the supported range or for invalid input', () => {
|
|
expect(legislatureForDate('2010-12-31')).toBeNull()
|
|
expect(legislatureForDate('')).toBeNull()
|
|
expect(legislatureForDate('2023-2-1')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('parseDurationToSeconds', () => {
|
|
it('parses the excerpt card and VOD spellings', () => {
|
|
expect(parseDurationToSeconds('0h04\'03"')).toBe(243)
|
|
expect(parseDurationToSeconds('1h00\'00"')).toBe(3600)
|
|
expect(parseDurationToSeconds('0:04:07')).toBe(247)
|
|
expect(parseDurationToSeconds('4:07')).toBe(247)
|
|
})
|
|
|
|
it('rejects empty, malformed and out-of-range values', () => {
|
|
expect(parseDurationToSeconds('')).toBeNull()
|
|
expect(parseDurationToSeconds(null)).toBeNull()
|
|
expect(parseDurationToSeconds('abc')).toBeNull()
|
|
expect(parseDurationToSeconds('1::30')).toBeNull()
|
|
expect(parseDurationToSeconds('1:99:99')).toBeNull()
|
|
expect(parseDurationToSeconds('0h99\'00"')).toBeNull()
|
|
expect(parseDurationToSeconds('1:99')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('parsePresidingOfficerTransitions', () => {
|
|
const transitionHtml = (heading: string, times: number[]) => `
|
|
<h4 class="g-l-assista__categoria-outros-videos">${heading}</h4>
|
|
<ul class="g-l-assista__lista-outros-videos">
|
|
${times
|
|
.map(
|
|
(tMs) =>
|
|
`<li><a id="link-trecho-video" href="https://www.camara.leg.br/evento-legislativo/67091?a=558641&t=${tMs}&trechosOrador=&crawl=no" class="chamada__link-trecho" titulo="X">y</a></li>`,
|
|
)
|
|
.join('\n')}
|
|
</ul>`
|
|
|
|
it('reads the transition groups, using the newest clip of each as the change moment', () => {
|
|
const html = [
|
|
transitionHtml(
|
|
'Troca da mesa Presidente Bohn Gass por Participante Pompeo de Mattos',
|
|
[1675815220717, 1675814951403],
|
|
),
|
|
transitionHtml(
|
|
'Troca da mesa Presidente Pompeo de Mattos por Participante Bohn Gass',
|
|
[1675814710153],
|
|
),
|
|
].join('\n')
|
|
|
|
expect(parsePresidingOfficerTransitions(html)).toEqual([
|
|
{ from: 'Pompeo de Mattos', to: 'Bohn Gass', tMs: 1675814710153 },
|
|
{ from: 'Bohn Gass', to: 'Pompeo de Mattos', tMs: 1675815220717 },
|
|
])
|
|
})
|
|
|
|
it('returns [] when the page has no transitions or the markup changes', () => {
|
|
const html = excerptHtml(67091, {
|
|
audioId: 5,
|
|
tMs: 1000,
|
|
speaker: 'JORGE SOLLA',
|
|
party: 'DEPUTADO (PT-BA)',
|
|
startTime: '17:30',
|
|
duration: '0h01\'00"',
|
|
})
|
|
expect(parsePresidingOfficerTransitions(html)).toEqual([])
|
|
expect(parsePresidingOfficerTransitions('')).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('resolvePresidingOfficer', () => {
|
|
// Real transition groups of event 67091 (2023-02-07), newest-clip times.
|
|
const transitions = [
|
|
{ from: 'Bohn Gass', to: 'Pompeo de Mattos', tMs: 1675815220717 },
|
|
{ from: 'Gilberto Nascimento', to: 'Pompeo de Mattos', tMs: 1675800828283 },
|
|
{ from: 'Pompeo de Mattos', to: 'Adriana Ventura', tMs: 1675804114707 },
|
|
{ from: 'Adriana Ventura', to: 'Pompeo de Mattos', tMs: 1675804616513 },
|
|
{ from: 'Pompeo de Mattos', to: 'Bohn Gass', tMs: 1675814710153 },
|
|
]
|
|
|
|
it('returns the incoming officer of the latest transition before the moment', () => {
|
|
// Solla spoke at 17:28 BRT (20:28 UTC) — Gilberto→Pompeo at 17:13 BRT.
|
|
expect(resolvePresidingOfficer(transitions, Date.parse('2023-02-07T20:28:00Z'))).toBe(
|
|
'Pompeo de Mattos',
|
|
)
|
|
// After Bohn→Pompeo (21:13 BRT), Pompeo presided again.
|
|
expect(resolvePresidingOfficer(transitions, Date.parse('2023-02-08T00:16:00Z'))).toBe(
|
|
'Pompeo de Mattos',
|
|
)
|
|
})
|
|
|
|
it('returns the outgoing officer before the first transition', () => {
|
|
expect(resolvePresidingOfficer(transitions, Date.parse('2023-02-07T19:00:00Z'))).toBe(
|
|
'Gilberto Nascimento',
|
|
)
|
|
})
|
|
|
|
it('returns null without transitions or a usable moment', () => {
|
|
expect(resolvePresidingOfficer([], 1675801680000)).toBeNull()
|
|
expect(resolvePresidingOfficer(transitions, Number.NaN)).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('normalizeTranscription', () => {
|
|
it('accepts the OpenAI-compat segments shape', () => {
|
|
expect(
|
|
normalizeTranscription({
|
|
text: 'olá mundo',
|
|
language: 'pt',
|
|
duration: 12.5,
|
|
segments: [
|
|
{ start: 0, end: 2.7, text: ' olá' },
|
|
{ start: 2.91, end: 4.1, text: ' mundo' },
|
|
],
|
|
}),
|
|
).toEqual({
|
|
text: 'olá mundo',
|
|
language: 'pt',
|
|
duration: 12.5,
|
|
segments: [
|
|
{ start: 0, end: 2.7, text: 'olá' },
|
|
{ start: 2.91, end: 4.1, text: 'mundo' },
|
|
],
|
|
})
|
|
})
|
|
|
|
it('accepts the native chunks shape and drops invalid entries', () => {
|
|
const result = normalizeTranscription({
|
|
chunks: [
|
|
{ timestamp: [0, 1.5], text: 'a' },
|
|
{ timestamp: [null, null], text: 'lixo' },
|
|
{ timestamp: [2, 3], text: '' },
|
|
],
|
|
language: 'pt',
|
|
})
|
|
expect(result.segments).toEqual([{ start: 0, end: 1.5, text: 'a' }])
|
|
expect(result.text).toBe('a')
|
|
})
|
|
|
|
it('degrades on malformed input', () => {
|
|
expect(normalizeTranscription(null)).toEqual({
|
|
text: '',
|
|
language: null,
|
|
duration: null,
|
|
segments: [],
|
|
})
|
|
})
|
|
|
|
it('keeps a missing duration as null instead of 0', () => {
|
|
expect(normalizeTranscription({ duration: null, segments: [] }).duration).toBeNull()
|
|
expect(normalizeTranscription({ duration: '', segments: [] }).duration).toBeNull()
|
|
expect(normalizeTranscription({ duration: 12, segments: [] }).duration).toBe(12)
|
|
})
|
|
|
|
it('prefers segments over chunks when both are present', () => {
|
|
const result = normalizeTranscription({
|
|
segments: [{ start: 0, end: 1, text: 'seg' }],
|
|
chunks: [{ timestamp: [2, 3], text: 'chunk' }],
|
|
})
|
|
expect(result.segments).toEqual([{ start: 0, end: 1, text: 'seg' }])
|
|
})
|
|
})
|