All files / generators committee-helpers.ts

95.45% Statements 42/44
85.1% Branches 40/47
100% Functions 4/4
100% Lines 37/37

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                        8x                           6x 6x 5x     5x 3x 6x 6x 6x 6x 6x 1x 2x 2x 2x 1x     3x 3x   1x 1x                     5x 5x 5x     5x 3x         3x   1x 1x                     6x 6x 6x     6x 4x 4x 6x         1x 1x      
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Generators/CommitteeHelpers
 * @description Pure helpers for applying MCP committee data to a CommitteeData object.
 * Stateless functions — no I/O or MCP calls; only JSON parsing + data assignment.
 */
 
import type { CommitteeData, MCPToolResult } from '../types/index.js';
 
/** Featured committees to include in committee reports */
export const FEATURED_COMMITTEES = ['ENVI', 'ECON', 'AFET', 'LIBE', 'AGRI'] as const;
 
/**
 * Apply committee info from MCP result to the data object
 *
 * @param result - MCP tool result
 * @param data - Committee data to populate
 * @param abbreviation - Fallback abbreviation
 */
export function applyCommitteeInfo(
  result: MCPToolResult,
  data: CommitteeData,
  abbreviation: string
): void {
  try {
    if (!result?.content?.[0]) return;
    const parsed = JSON.parse(result.content[0].text) as {
      committee?: { name?: string; abbreviation?: string; chair?: string; memberCount?: unknown };
    };
    if (!parsed.committee) return;
    data.name = parsed.committee.name ?? data.name;
    data.abbreviation = parsed.committee.abbreviation ?? abbreviation;
    data.chair = parsed.committee.chair ?? 'N/A';
    const memberCountRaw = parsed.committee.memberCount;
    let memberCount = 0;
    if (typeof memberCountRaw === 'number' && Number.isFinite(memberCountRaw)) {
      memberCount = memberCountRaw;
    } else Eif (typeof memberCountRaw === 'string') {
      const parsedNumber = Number(memberCountRaw);
      if (Number.isFinite(parsedNumber)) {
        memberCount = parsedNumber;
      }
    }
    data.members = memberCount;
    console.log(`  ✅ Committee info: ${data.name} (${data.members} members)`);
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    console.warn('  ⚠️ Failed to parse committee info:', message);
  }
}
 
/**
 * Apply documents from MCP result to the data object
 *
 * @param result - MCP tool result
 * @param data - Committee data to populate
 */
export function applyDocuments(result: MCPToolResult, data: CommitteeData): void {
  try {
    Iif (!result?.content?.[0]) return;
    const parsed = JSON.parse(result.content[0].text) as {
      documents?: Array<{ title?: string; type?: string; documentType?: string; date?: string }>;
    };
    if (!parsed.documents || parsed.documents.length === 0) return;
    data.documents = parsed.documents.map((d) => ({
      title: d.title ?? 'Untitled Document',
      type: d.type ?? d.documentType ?? 'Document',
      date: d.date ?? '',
    }));
    console.log(`  ✅ Fetched ${data.documents.length} documents from MCP`);
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    console.warn('  ⚠️ Failed to parse documents:', message);
  }
}
 
/**
 * Apply effectiveness metrics from MCP result to the data object
 *
 * @param result - MCP tool result
 * @param data - Committee data to populate
 */
export function applyEffectiveness(result: MCPToolResult, data: CommitteeData): void {
  try {
    Iif (!result?.content?.[0]) return;
    const parsed = JSON.parse(result.content[0].text) as {
      effectiveness?: { overallScore?: unknown; rank?: string };
    };
    if (!parsed.effectiveness) return;
    const score = parsed.effectiveness.overallScore;
    const rank = parsed.effectiveness.rank ?? '';
    data.effectiveness =
      typeof score === 'number' && Number.isFinite(score)
        ? `Score: ${score.toFixed(1)} ${rank}`.trim()
        : null;
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    console.warn('  ⚠️ Failed to parse effectiveness:', message);
  }
}