All files / src/generators/strategies week-ahead-strategy.ts

95.23% Statements 60/63
82.5% Branches 33/40
100% Functions 6/6
95.16% Lines 59/62

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                                                                                                                6x 6x 6x   6x 6x   6x 6x   6x       5x                             16x 16x 16x 1x 16x 1x 16x                       16x 16x 15x 16x 1x 16x 1x     16x 1x       16x 1x     15x 16x 16x 15x 15x                         23x   23x                                             6x 6x     6x       5x   5x                                 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x         13x 13x 13x                         28x 28x       28x 28x   28x     28x                     8x  
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Generators/Strategies/WeekAheadStrategy
 * @description Article strategy for the Week Ahead article type.
 * Fetches plenary sessions, committee meetings, documents, pipeline and
 * parliamentary questions then renders a forward-looking HTML article.
 */
 
import type { EuropeanParliamentMCPClient } from '../../mcp/ep-mcp-client.js';
import { ArticleCategory } from '../../types/index.js';
import type { LanguageCode, DateRange, WeekAheadData, EPFeedData } from '../../types/index.js';
import { WEEK_AHEAD_TITLES, getLocalizedString } from '../../constants/languages.js';
import { fetchWeekAheadData, fetchEPFeedData } from '../pipeline/fetch-stage.js';
import {
  buildWeekAheadContent,
  buildKeywords,
  buildWhatToWatchSection,
} from '../week-ahead-content.js';
import { buildDeepAnalysisSection } from '../deep-analysis-content.js';
import {
  buildProspectiveAnalysis,
  buildProspectiveSwot,
  buildProspectiveDashboard,
  buildProspectiveMindmap,
} from '../analysis-builders.js';
import { buildSwotSection } from '../swot-content.js';
import { buildDashboardSection } from '../dashboard-content.js';
import { buildIntelligenceMindmapSection } from '../mindmap-content.js';
import type { ArticleStrategy, ArticleData, ArticleMetadata } from './article-strategy.js';
import { pl } from '../../utils/metadata-utils.js';
 
// ─── Data payload ─────────────────────────────────────────────────────────────
 
/** Data fetched and pre-processed by {@link WeekAheadStrategy} */
export interface WeekAheadArticleData extends ArticleData {
  /** Week-ahead date range */
  readonly dateRange: DateRange;
  /** Aggregated week-ahead domain data */
  readonly weekData: WeekAheadData;
  /** SEO keywords derived from the week-ahead data */
  readonly keywords: readonly string[];
  /** EP feed data for enrichment (when available) */
  readonly feedData?: EPFeedData | undefined;
}
 
// ─── Date-range helper ────────────────────────────────────────────────────────
 
/**
 * Compute the week-ahead date range starting the day after `baseDate`.
 *
 * @param baseDate - ISO 8601 publication date (YYYY-MM-DD)
 * @returns Date range spanning the next 7 days
 */
function computeWeekAheadDateRange(baseDate: string): DateRange {
  const base = new Date(`${baseDate}T00:00:00Z`);
  const startDate = new Date(base);
  startDate.setUTCDate(base.getUTCDate() + 1);
 
  const endDate = new Date(startDate);
  endDate.setUTCDate(startDate.getUTCDate() + 7);
 
  const startParts = startDate.toISOString().split('T');
  const endParts = endDate.toISOString().split('T');
 
  Iif (!startParts[0] || !endParts[0]) {
    throw new Error('Invalid date format generated in computeWeekAheadDateRange');
  }
 
  return {
    start: startParts[0],
    end: endParts[0],
  };
}
 
// ─── Content-aware metadata helpers ───────────────────────────────────────────
 
/**
 * Build a content-aware title suffix from week-ahead data counts.
 *
 * @param weekData - Aggregated week-ahead domain data
 * @returns Short suffix for the title, or empty string
 */
function buildWeekAheadTitleSuffix(weekData: WeekAheadData): string {
  const parts: string[] = [];
  if (weekData.events.length > 0) parts.push(pl(weekData.events.length, 'Event', 'Events'));
  if (weekData.committees.length > 0)
    parts.push(pl(weekData.committees.length, 'Committee Meeting', 'Committee Meetings'));
  if (weekData.pipeline.length > 0)
    parts.push(pl(weekData.pipeline.length, 'Pipeline Item', 'Pipeline Items'));
  return parts.join(', ');
}
 
/**
 * Build a content-aware description from week-ahead data.
 * Summarises event counts, committee meetings, and key highlights.
 *
 * @param weekData - Aggregated week-ahead domain data
 * @param dateRange - Date range for the article
 * @returns SEO-friendly description (≤ 200 chars)
 */
function buildWeekAheadDescription(weekData: WeekAheadData, dateRange: DateRange): string {
  const parts: string[] = [];
  if (weekData.events.length > 0)
    parts.push(`${pl(weekData.events.length, 'scheduled event', 'scheduled events')}`);
  if (weekData.committees.length > 0)
    parts.push(`${pl(weekData.committees.length, 'committee meeting', 'committee meetings')}`);
  if (weekData.pipeline.length > 0)
    parts.push(
      `${pl(weekData.pipeline.length, 'legislative pipeline item', 'legislative pipeline items')}`
    );
  if (weekData.questions.length > 0)
    parts.push(
      `${pl(weekData.questions.length, 'parliamentary question', 'parliamentary questions')}`
    );
 
  if (parts.length === 0) {
    return `European Parliament calendar and plenary agenda for ${dateRange.start} to ${dateRange.end}.`;
  }
 
  const highlight = weekData.events[0]?.title ?? '';
  const base = `EP week ahead (${dateRange.start}–${dateRange.end}): ${parts.join(', ')}`;
  if (highlight) {
    const full = `${base}. Key: ${highlight}`;
    return full.length > 200 ? full.slice(0, 197) + '...' : full;
  }
  return base.length > 200 ? base.slice(0, 197) + '...' : base;
}
 
// ─── Strategy implementation ──────────────────────────────────────────────────
 
/**
 * Article strategy for {@link ArticleCategory.WEEK_AHEAD}.
 * Fetches plenary, committee, document, pipeline and question data then builds
 * a forward-looking preview of the upcoming parliamentary week.
 */
export class WeekAheadStrategy implements ArticleStrategy<WeekAheadArticleData> {
  readonly type = ArticleCategory.WEEK_AHEAD;
 
  readonly requiredMCPTools = [
    'get_plenary_sessions',
    'get_committee_info',
    'search_documents',
    'monitor_legislative_pipeline',
    'get_parliamentary_questions',
    'get_events',
    'get_events_feed',
    'get_plenary_documents_feed',
    'get_adopted_texts_feed',
  ] as const;
 
  /**
   * Fetch week-ahead data from MCP.
   *
   * @param client - MCP client or null
   * @param date - ISO 8601 publication date
   * @returns Populated week-ahead data payload
   */
  async fetchData(
    client: EuropeanParliamentMCPClient | null,
    date: string
  ): Promise<WeekAheadArticleData> {
    const dateRange = computeWeekAheadDateRange(date);
    console.log(`  📆 Date range: ${dateRange.start} to ${dateRange.end}`);
 
    // Fetch traditional MCP data and EP feeds in parallel
    const [weekData, feedData] = await Promise.all([
      fetchWeekAheadData(client, dateRange),
      fetchEPFeedData(client, 'one-week', dateRange),
    ]);
    const keywords = buildKeywords(weekData);
 
    return {
      date,
      dateRange,
      weekData,
      keywords,
      feedData,
    };
  }
 
  /**
   * Build the week-ahead HTML body for the specified language.
   *
   * @param data - Week-ahead data payload
   * @param lang - Target language code used for editorial strings
   * @returns Article HTML body
   */
  buildContent(data: WeekAheadArticleData, lang: LanguageCode): string {
    const base = buildWeekAheadContent(data.weekData, data.dateRange, lang);
    const watchSection = buildWhatToWatchSection(data.weekData.pipeline, [], lang);
    const analysis = buildProspectiveAnalysis(data.weekData, data.dateRange, 'week');
    const analysisSection = buildDeepAnalysisSection(analysis, lang, 'en');
    const mindmapData = buildProspectiveMindmap(data.weekData, lang);
    const mindmapSection = buildIntelligenceMindmapSection(mindmapData, lang);
    const swotData = buildProspectiveSwot(data.weekData, 'week', lang);
    const swotSection = buildSwotSection(swotData, lang);
    const dashboardData = buildProspectiveDashboard(data.weekData, 'week', lang);
    const dashboardSection = buildDashboardSection(dashboardData, lang);
    // Inject at the explicit <!-- /article-content --> marker position so the
    // section stays inside the .article-content styling scope. The marker is
    // removed from the final HTML output to avoid unnecessary bytes.
    const injection =
      (watchSection || '') + analysisSection + mindmapSection + swotSection + dashboardSection;
    Eif (injection) {
      return base.replace('<!-- /article-content -->', injection);
    }
    return base.replace('<!-- /article-content -->', '');
  }
 
  /**
   * Return language-specific metadata for the week-ahead article.
   *
   * @param data - Week-ahead data payload
   * @param lang - Target language code
   * @returns Localised metadata
   */
  getMetadata(data: WeekAheadArticleData, lang: LanguageCode): ArticleMetadata {
    const titleFn = getLocalizedString(WEEK_AHEAD_TITLES, lang);
    const { title: baseTitle, subtitle: baseSubtitle } = titleFn(
      data.dateRange.start,
      data.dateRange.end
    );
    const suffix = lang === 'en' ? buildWeekAheadTitleSuffix(data.weekData) : '';
    const title = suffix ? `${baseTitle} — ${suffix}` : baseTitle;
    const subtitle =
      lang === 'en'
        ? buildWeekAheadDescription(data.weekData, data.dateRange) || baseSubtitle
        : baseSubtitle;
    return {
      title,
      subtitle,
      keywords: data.keywords,
      category: ArticleCategory.WEEK_AHEAD,
      sources: [],
    };
  }
}
 
/** Singleton instance for use by the strategy registry */
export const weekAheadStrategy = new WeekAheadStrategy();