All files / generators breaking-content.ts

100% Statements 39/39
97.22% Branches 35/36
100% Functions 8/8
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261                                      8x                       6x 4x 4x     4x           4x                                 6x 2x 2x     2x           2x                                 6x 2x 2x     2x           2x                                               63x 63x 6x 6x                                                                             63x 63x 63x                 63x   63x                 63x                 63x                 63x                 63x 63x 63x   63x               63x             63x                         63x                              
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Generators/BreakingContent
 * @description Pure functions for building breaking-news article HTML,
 * including optional structured intelligence briefing sections derived
 * from typed MCP intelligence data.
 */
 
import { escapeHTML } from '../utils/file-utils.js';
import { getLocalizedString, EDITORIAL_STRINGS, BREAKING_STRINGS } from '../constants/languages.js';
import type {
  VotingAnomalyIntelligence,
  CoalitionIntelligence,
  MEPInfluenceScore,
} from '../types/index.js';
 
/** Maximum characters to display from raw MCP intelligence data */
const MAX_DATA_CHARS = 2000;
 
// ─── Private section builders ────────────────────────────────────────────────
 
/**
 * Build intelligence briefing section HTML from structured anomaly data
 *
 * @param anomalies - Structured voting anomaly intelligence items
 * @param lang - Language code for localized strings
 * @returns HTML section string or empty string
 */
function buildAnomalyAlertSection(anomalies: VotingAnomalyIntelligence[], lang: string): string {
  if (anomalies.length === 0) return '';
  const strings = getLocalizedString(BREAKING_STRINGS, lang);
  const items = anomalies
    .map(
      (a) =>
        `<li class="anomaly-${escapeHTML(a.significance)}">` +
        `<strong>${escapeHTML(a.description)}</strong> — ` +
        `${escapeHTML(a.implication)} ` +
        `(deviation: ${escapeHTML(String(a.deviationPercentage))}%)</li>`
    )
    .join('\n            ');
  return `
        <section class="anomaly-alert">
          <h3>${escapeHTML(strings.votingAnomalyAlert)}</h3>
          <ul>
            ${items}
          </ul>
        </section>`;
}
 
/**
 * Build coalition dynamics section HTML from structured coalition data
 *
 * @param coalitions - Structured coalition intelligence items
 * @param lang - Language code for localized strings
 * @returns HTML section string or empty string
 */
function buildCoalitionDynamicsSection(coalitions: CoalitionIntelligence[], lang: string): string {
  if (coalitions.length === 0) return '';
  const strings = getLocalizedString(BREAKING_STRINGS, lang);
  const items = coalitions
    .map(
      (c) =>
        `<li class="coalition-${escapeHTML(c.riskLevel)}">` +
        `${escapeHTML(c.groups.join(', '))} — ` +
        `cohesion: ${escapeHTML(String(Math.round(c.cohesionScore * 100)))}% ` +
        `(${escapeHTML(c.alignmentTrend)})</li>`
    )
    .join('\n            ');
  return `
        <section class="coalition-dynamics">
          <h3>${escapeHTML(strings.coalitionDynamicsSection)}</h3>
          <ul>
            ${items}
          </ul>
        </section>`;
}
 
/**
 * Build key parliamentary players section HTML from structured MEP influence data
 *
 * @param mepScores - Structured MEP influence score items
 * @param lang - Language code for localized strings
 * @returns HTML section string or empty string
 */
function buildKeyPlayersIntelSection(mepScores: MEPInfluenceScore[], lang: string): string {
  if (mepScores.length === 0) return '';
  const strings = getLocalizedString(BREAKING_STRINGS, lang);
  const items = mepScores
    .map(
      (m) =>
        `<li class="mep-score">` +
        `<strong>${escapeHTML(m.mepName)}</strong> — ` +
        `score: ${escapeHTML(String(Math.round(m.overallScore)))} ` +
        `${m.rank ? `(${escapeHTML(m.rank)})` : ''}</li>`
    )
    .join('\n            ');
  return `
        <section class="key-players-intel">
          <h3>${escapeHTML(strings.keyPlayers)}</h3>
          <ul>
            ${items}
          </ul>
        </section>`;
}
 
/**
 * Build intelligence briefing section HTML from all structured sources
 *
 * @param anomalies - Structured voting anomaly intelligence items
 * @param coalitions - Structured coalition intelligence items
 * @param mepScores - Structured MEP influence score items
 * @param lang - Language code for localized strings
 * @returns HTML section string or empty string
 */
function buildIntelligenceBriefingSection(
  anomalies: VotingAnomalyIntelligence[],
  coalitions: CoalitionIntelligence[],
  mepScores: MEPInfluenceScore[],
  lang: string
): string {
  const hasIntel = anomalies.length > 0 || coalitions.length > 0 || mepScores.length > 0;
  if (!hasIntel) return '';
  const strings = getLocalizedString(BREAKING_STRINGS, lang);
  return `
        <section class="intelligence-briefing">
          <h2>${escapeHTML(strings.intelligenceBriefing)}</h2>
          ${buildAnomalyAlertSection(anomalies, lang)}
          ${buildCoalitionDynamicsSection(coalitions, lang)}
          ${buildKeyPlayersIntelSection(mepScores, lang)}
        </section>`;
}
 
// ─── Exported function ────────────────────────────────────────────────────────
 
/**
 * Build breaking news article HTML content.
 * Accepts both raw MCP string data (rendered as narrative blocks) and optional
 * structured intelligence data (rendered as formatted HTML sections).
 * When no data is provided, returns a placeholder notice.
 *
 * @param date - Current date string for the article
 * @param anomalyRaw - Raw anomaly data from MCP
 * @param coalitionRaw - Raw coalition dynamics data from MCP
 * @param reportRaw - Raw analytical report from MCP
 * @param influenceRaw - Raw MEP influence data from MCP
 * @param lang - Language code for localized editorial strings (default: 'en')
 * @param anomalies - Optional structured voting anomaly intelligence items
 * @param coalitions - Optional structured coalition intelligence items
 * @param mepScores - Optional structured MEP influence score items
 * @returns Full article HTML content string
 */
export function buildBreakingNewsContent(
  date: string,
  anomalyRaw: string,
  coalitionRaw: string,
  reportRaw: string,
  influenceRaw: string,
  lang = 'en',
  anomalies: VotingAnomalyIntelligence[] = [],
  coalitions: CoalitionIntelligence[] = [],
  mepScores: MEPInfluenceScore[] = []
): string {
  const editorial = getLocalizedString(EDITORIAL_STRINGS, lang);
  const strings = getLocalizedString(BREAKING_STRINGS, lang);
  const hasData = Boolean(
    anomalyRaw ||
    coalitionRaw ||
    reportRaw ||
    influenceRaw ||
    anomalies.length ||
    coalitions.length ||
    mepScores.length
  );
  const timestamp = new Date().toISOString();
 
  const anomalySection = anomalyRaw
    ? `
        <section class="analysis">
          <h2>${escapeHTML(strings.votingAnomalyIntel)}</h2>
          <p class="source-attribution">${escapeHTML(editorial.sourceAttribution)}:</p>
          <p class="data-narrative">${escapeHTML(anomalyRaw.slice(0, MAX_DATA_CHARS))}</p>
        </section>`
    : '';
 
  const coalitionSection = coalitionRaw
    ? `
        <section class="coalition-impact">
          <h2>${escapeHTML(strings.coalitionDynamics)}</h2>
          <p class="source-attribution">${escapeHTML(editorial.sourceAttribution)}:</p>
          <p class="data-narrative">${escapeHTML(coalitionRaw.slice(0, MAX_DATA_CHARS))}</p>
        </section>`
    : '';
 
  const reportSection = reportRaw
    ? `
        <section class="context">
          <h2>${escapeHTML(strings.analyticalReport)}</h2>
          <p class="source-attribution">${escapeHTML(editorial.analysisNote)}:</p>
          <p class="data-narrative">${escapeHTML(reportRaw.slice(0, MAX_DATA_CHARS))}</p>
        </section>`
    : '';
 
  const keyPlayersSection = influenceRaw
    ? `
        <section class="key-players">
          <h2>${escapeHTML(strings.keyMEPInfluence)}</h2>
          <p class="source-attribution">${escapeHTML(editorial.sourceAttribution)}:</p>
          <p class="data-narrative">${escapeHTML(influenceRaw.slice(0, MAX_DATA_CHARS))}</p>
        </section>`
    : '';
 
  const context = escapeHTML(editorial.parliamentaryContext);
  const finding = escapeHTML(editorial.keyTakeaway).toLowerCase();
  const attribution = escapeHTML(editorial.sourceAttribution).toLowerCase();
 
  const whyThisMattersSection = hasData
    ? `
        <section class="why-this-matters">
          <h2>${escapeHTML(editorial.whyThisMatters)}</h2>
          <p>${context}: ${finding} — ${attribution}.</p>
        </section>`
    : '';
 
  const intelligenceBriefing = buildIntelligenceBriefingSection(
    anomalies,
    coalitions,
    mepScores,
    lang
  );
 
  const placeholderNotice = !hasData
    ? `
        <div class="notice">
          <p><strong>Note:</strong> ${escapeHTML(strings.placeholderNotice)}</p>
        </div>
        <section class="lede">
          <p>${escapeHTML(strings.placeholderLede)}</p>
        </section>`
    : `
        <section class="lede">
          <p>${escapeHTML(strings.lede)} as of ${escapeHTML(date)}.</p>
        </section>`;
 
  return `
        <div class="article-content">
          <section class="breaking-banner">
            <p class="breaking-timestamp">${escapeHTML(strings.breakingBanner)} — ${escapeHTML(timestamp)}</p>
          </section>
          ${placeholderNotice}
          ${intelligenceBriefing}
          ${anomalySection}
          ${coalitionSection}
          ${reportSection}
          ${keyPlayersSection}
          ${whyThisMattersSection}
        </div>
      `;
}