All files / scripts validate-cross-references.ts

96.77% Statements 60/62
76.92% Branches 20/26
100% Functions 12/12
98.11% Lines 52/53

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                                          1x                                                                               1x                 13x   13x 13x   13x                   13x 91x 28x       13x                       9x 9x 9x 9x 9x 9x                                 9x 33x   36x 33x 9x   9x 9x 9x 9x   9x   9x                                                           2x 3x     2x       2x 3x 2x 3x   2x             1x   1x 1x 1x 1x   1x 1x 2x     1x 1x 1x 1x       1x 1x 2x     1x             2x                    
/**
 * @module Validation/CrossReferenceTracking
 * @description Source verification engine enforcing multi-source journalism standards.
 * Bounded context: Quality Assurance
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import type {
  ArticleBatchItem,
  BatchValidationResult,
  CISummary,
  CrossRefValidationResult,
  RequiredToolsMap,
} from './types/validation.js';
import type { ArticleType } from './types/article.js';
 
/**
 * Required MCP tools per article type.
 */
export const REQUIRED_TOOLS_PER_TYPE: RequiredToolsMap = {
  'week-ahead': [
    'get_calendar_events',
    'search_dokument',
    'get_fragor',
    'get_interpellationer',
  ],
  'committee-reports': [
    'get_betankanden',
    'search_voteringar',
    'search_anforanden',
    'get_propositioner',
  ],
  propositions: [
    'get_propositioner',
    'search_dokument_fulltext',
    'analyze_g0v_by_department',
    'search_anforanden',
  ],
  motions: [
    'get_motioner',
    'search_dokument_fulltext',
    'analyze_g0v_by_department',
    'search_anforanden',
  ],
  'month-ahead': [
    'get_calendar_events',
    'get_betankanden',
    'get_propositioner',
    'get_motioner',
  ],
  breaking: [
    'search_voteringar',
    'get_voting_group',
    'search_anforanden',
    'search_ledamoter',
  ],
} as const;
 
/** Minimum number of distinct data sources required per article */
export const MINIMUM_SOURCES = 3;
 
/**
 * Extract cross-reference types from article content.
 *
 * @param content - Article HTML or text content
 * @returns Array of detected cross-reference category names
 */
export function extractCrossReferences(content: string | null | undefined): string[] {
  Iif (!content) return [];
 
  const references: string[] = [];
  const contentLower = content.toLowerCase();
 
  const patterns: Readonly<Record<string, RegExp>> = {
    calendar: /calendar|event|schedule|meeting/gi,
    documents: /dokument|document|bill|proposition|motion|betänkande/gi,
    votes: /vote|votering|röst|ballot/gi,
    speeches: /speech|anförande|debate|tal/gi,
    members: /ledamot|\bmp\b|member|representative/gi,
    committees: /committee|utskott|commission/gi,
    questions: /question|fråga|interpellation/gi,
  };
 
  for (const [type, pattern] of Object.entries(patterns)) {
    if (pattern.test(contentLower)) {
      references.push(type);
    }
  }
 
  return references;
}
 
/**
 * Calculate quality score for cross-referencing (0–1).
 */
function calculateScore(
  allRequired: boolean,
  minSources: boolean,
  hasReferences: boolean,
  extraCount: number,
): number {
  let score = 0;
  if (allRequired) score += 0.4;
  if (minSources) score += 0.3;
  if (hasReferences) score += 0.2;
  score += Math.min(extraCount * 0.05, 0.1);
  return Math.min(score, 1.0);
}
 
/**
 * Validate cross-references for a single article.
 *
 * @param articleType   - Type of article
 * @param articleContent - Article HTML or text content
 * @param mcpCalls      - Array of MCP calls made during generation
 * @returns Validation result with pass/fail and score
 */
export function validateCrossReferences(
  articleType: string,
  articleContent: string,
  mcpCalls: ReadonlyArray<{ readonly tool: string }> = [],
): CrossRefValidationResult {
  const requiredTools =
    REQUIRED_TOOLS_PER_TYPE[articleType as ArticleType] ?? ([] as readonly string[]);
  const usedTools = mcpCalls.map((call) => call.tool);
 
  const missingTools = requiredTools.filter((tool) => !usedTools.includes(tool));
  const extraTools = usedTools.filter((tool) => !requiredTools.includes(tool));
  const crossReferencesInText = extractCrossReferences(articleContent);
 
  const uniqueTools = [...new Set(usedTools.filter(Boolean))];
  const hasMinimumSources = uniqueTools.length >= MINIMUM_SOURCES;
  const allRequiredToolsUsed = missingTools.length === 0;
  const hasCrossReferencesInText = crossReferencesInText.length >= MINIMUM_SOURCES;
 
  const passed = allRequiredToolsUsed && hasMinimumSources && hasCrossReferencesInText;
 
  return {
    articleType,
    requiredTools: [...requiredTools],
    usedTools,
    missingTools,
    extraTools,
    crossReferencesInText,
    sourceCount: usedTools.length,
    hasMinimumSources,
    allRequiredToolsUsed,
    hasCrossReferencesInText,
    passed,
    score: calculateScore(
      allRequiredToolsUsed,
      hasMinimumSources,
      hasCrossReferencesInText,
      extraTools.length,
    ),
  };
}
 
/**
 * Validate multiple articles at once.
 *
 * @param articles - Array of article objects with type, content, mcpCalls
 * @returns Aggregated validation results
 */
export function validateArticleBatch(
  articles: ReadonlyArray<ArticleBatchItem> | null | undefined,
): BatchValidationResult {
  const results = (articles ?? []).map((article) =>
    validateCrossReferences(article.type, article.content, article.mcpCalls),
  );
 
  Iif (results.length === 0) {
    return { total: 0, passed: 0, failed: 0, avgScore: 0, passRate: 0, details: [] };
  }
 
  const total = results.length;
  const passed = results.filter((r) => r.passed).length;
  const failed = total - passed;
  const avgScore = results.reduce((sum, r) => sum + r.score, 0) / total;
 
  return { total, passed, failed, avgScore, passRate: passed / total, details: results };
}
 
/**
 * Generate a Markdown validation report.
 */
export function generateValidationReport(validation: CrossRefValidationResult): string {
  const { articleType, usedTools, missingTools, crossReferencesInText, passed, score } = validation;
 
  let report = `## Cross-Reference Validation Report\n\n`;
  report += `**Article Type**: ${articleType}\n`;
  report += `**Status**: ${passed ? '✅ PASSED' : '❌ FAILED'}\n`;
  report += `**Score**: ${(score * 100).toFixed(0)}%\n\n`;
 
  report += `### MCP Tools Used (${usedTools.length})\n`;
  for (const tool of usedTools) {
    report += `- ✅ ${tool}\n`;
  }
 
  Eif (missingTools.length > 0) {
    report += `\n### Missing Required Tools (${missingTools.length})\n`;
    for (const tool of missingTools) {
      report += `- ❌ ${tool}\n`;
    }
  }
 
  report += `\n### Cross-References in Text (${crossReferencesInText.length})\n`;
  for (const ref of crossReferencesInText) {
    report += `- ${ref}\n`;
  }
 
  return report;
}
 
/**
 * Export CI-friendly summary from batch results.
 */
export function exportCISummary(batchResults: BatchValidationResult): CISummary {
  return {
    status: batchResults.passRate === 1 ? 'success' : 'failure',
    total: batchResults.total,
    passed: batchResults.passed,
    failed: batchResults.failed,
    passRate: `${(batchResults.passRate * 100).toFixed(1)}%`,
    avgScore: `${(batchResults.avgScore * 100).toFixed(1)}%`,
    timestamp: new Date().toISOString(),
  };
}