All files / scripts validate-economic-context.ts

53.43% Statements 70/131
65.3% Branches 64/98
38.88% Functions 7/18
56.3% Lines 67/119

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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472                                                                                                                                                        1x                 25x                     25x 21x     1x                                                             13x                       13x 13x               15x                               21x 21x 21x 20x 20x           20x                             20x 1x       19x 66x 19x                 15x 15x 15x   15x 15x 15x             15x     14x 14x             14x     14x 2x 1x         1x             2x       12x 2x               12x 12x 2x                     12x 10x 1x           10x 2x                 12x 2x           10x 1x           10x 10x 7x           10x 1x                           12x     15x 2x                               12x 4x 4x 4x 1x           4x 1x               12x                                                                                                                                                                                   1x         1x            
#!/usr/bin/env tsx
/**
 * @file Economic context quality gate.
 *
 * Enforces that every news article shipped through agentic workflows
 * carries real World Bank / SCB data, at least one Chart.js canvas, and
 * AI-authored commentary — never the "economic-dashboard-placeholder"
 * bullet list that the April 17 2026 committee-reports article was
 * produced with.
 *
 * Usage:
 *   npx tsx scripts/validate-economic-context.ts [--date YYYY-MM-DD] [--type committee-reports]
 *
 * Exit codes:
 *   0 — all checks pass (or nothing applicable)
 *   1 — at least one article violates the contract
 *
 * Checks per English article file (non-English are translations):
 *   1. HTML must not include `class="economic-dashboard-placeholder"`
 *      when the article's committee/domain has any indicator with an
 *      `mcpTool` binding.
 *   2. HTML must include at least one `data-chart-config=` canvas
 *      (Chart.js) when the article type requires it.
 *   3. `analysis/daily/{date}/{subfolder}/economic-data.json` must
 *      exist (unless the article type is on the exempt allow-list and
 *      the skip marker is present) and contain non-empty `dataPoints`
 *      and non-empty `commentary`.
 *   4. HTML must include a footer attribution link "Data by World Bank"
 *      (or the localized variant present in the template).
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import * as fs from 'node:fs';
import * as path from 'node:path';
import { loadEconomicContext, economicDataPath } from './data-transformers/load-economic-context.js';
 
// ---------------------------------------------------------------------------
// Coverage matrix (single source of truth: .github/aw/ECONOMIC_DATA_CONTRACT.md)
// ---------------------------------------------------------------------------
 
/**
 * Minimum expected economic charts and commentary length per article
 * type. Article types not in this matrix are exempt from the gate.
 */
export interface CoverageRule {
  /** Minimum `data-chart-config` canvases expected in the HTML. */
  minCharts: number;
  /** Minimum commentary word count (matches contract in ECONOMIC_DATA_CONTRACT.md). */
  minCommentaryWords: number;
  /** Whether the article type may opt out via `skip: true`. */
  allowSkip: boolean;
  /**
   * Whether the article MUST include a D3 Sankey / network diagram
   * (emitted as a `data-d3-sankey=` marker + `js/lib/d3.*.min.js` load).
   * Enforced for weekly / monthly reviews per the Economic Data
   * Contract (.github/aw/ECONOMIC_DATA_CONTRACT.md).
   */
  requiresD3?: boolean;
}
 
/**
 * Date (ISO `YYYY-MM-DD`) on which the Economic Data Contract v1.0 became
 * authoritative. Articles published before this date legitimately had no
 * way to comply — the contract, schema, loader, and validator all landed
 * on 2026-04-17 (see `.github/aw/ECONOMIC_DATA_CONTRACT.md` § "Version
 * history"), so the first *full* day under the contract is 2026-04-18.
 *
 * The validator and the daily audit discovery use this constant to skip
 * pre-contract articles, which would otherwise keep generating the same
 * ~80 violations in the daily audit until they age out of the 7-day
 * lookback window (see `.github/workflows/economic-context-audit.yml`).
 *
 * Keep in sync with the contract's version-history entry.
 */
export const CONTRACT_EFFECTIVE_DATE = '2026-04-18';
 
/**
 * Strict `YYYY-MM-DD` validator used before any prefix/lexicographic
 * comparison on date strings. Prevents malformed or user-controlled
 * values from flowing into patterns or filesystem glue code (see CodeQL
 * alerts #185 regex-injection and #186 no-op replace).
 */
export function isIsoDate(value: string): boolean {
  return /^\d{4}-\d{2}-\d{2}$/.test(value);
}
 
/**
 * Returns true when `date` is a strict ISO `YYYY-MM-DD` string on or
 * after `CONTRACT_EFFECTIVE_DATE`. Non-ISO / malformed inputs return
 * `false` deterministically so invalid filenames never silently pass
 * the gate. Uses literal string comparison once the shape is validated
 * — safe for ISO-8601 `YYYY-MM-DD`, which is lexicographically sortable.
 */
export function isUnderContract(date: string): boolean {
  if (!isIsoDate(date)) return false;
  return date >= CONTRACT_EFFECTIVE_DATE;
}
 
export const COVERAGE_MATRIX: Readonly<Record<string, CoverageRule>> = {
  'committee-reports':  { minCharts: 2, minCommentaryWords: 60,  allowSkip: false },
  'propositions':       { minCharts: 2, minCommentaryWords: 60,  allowSkip: false },
  'motions':            { minCharts: 1, minCommentaryWords: 40,  allowSkip: false },
  'interpellations':    { minCharts: 1, minCommentaryWords: 40,  allowSkip: false },
  'evening-analysis':   { minCharts: 1, minCommentaryWords: 40,  allowSkip: false },
  'realtime-monitor':   { minCharts: 1, minCommentaryWords: 30,  allowSkip: true  },
  'breaking':           { minCharts: 1, minCommentaryWords: 30,  allowSkip: true  },
  'week-ahead':         { minCharts: 2, minCommentaryWords: 80,  allowSkip: false },
  'month-ahead':        { minCharts: 3, minCommentaryWords: 100, allowSkip: false },
  'weekly-review':      { minCharts: 3, minCommentaryWords: 150, allowSkip: false, requiresD3: true },
  'monthly-review':     { minCharts: 4, minCommentaryWords: 200, allowSkip: false, requiresD3: true },
  'deep-inspection':    { minCharts: 1, minCommentaryWords: 40,  allowSkip: true  },
  'article-generator':  { minCharts: 1, minCommentaryWords: 40,  allowSkip: true  },
};
 
// ---------------------------------------------------------------------------
// Violation reporting
// ---------------------------------------------------------------------------
 
export interface Violation {
  articleFile: string;
  articleType: string;
  reason: string;
}
 
/**
 * Count word-ish tokens in a commentary string.
 * Exposed for the unit test.
 */
export function countWords(text: string): number {
  return text
    .trim()
    .split(/\s+/)
    .filter(Boolean)
    .length;
}
 
/**
 * Count occurrences of `data-chart-config=` (Chart.js canvases) in HTML.
 * Exposed for the unit test.
 */
export function countChartCanvases(html: string): number {
  const matches = html.match(/data-chart-config=/g);
  return matches ? matches.length : 0;
}
 
/**
 * Check whether the HTML contains a footer attribution link to World
 * Bank / SCB. Matches common phrasings across the 14 languages.
 */
export function hasAttribution(html: string): boolean {
  return /World Bank|världsbanken|verdensbank|weltbank|banco mundial|banque mondiale|wereldbank|البنك الدولي|הבנק העולמי|世界銀行|세계은행|世界银行|SCB|Statistics Sweden|Statistiska centralbyrån/i.test(html);
}
 
// ---------------------------------------------------------------------------
// Per-article validation
// ---------------------------------------------------------------------------
 
/**
 * Extract article type and date from a news filename.
 * Returns null when the file does not match the expected pattern.
 *
 * Expected pattern: `news/YYYY-MM-DD-{type-with-dashes}-{lang}.html`.
 * Type is matched against the coverage matrix keys to avoid greedy splits
 * (e.g. "committee-reports" vs "opposition-motions" vs "week-ahead").
 */
export function parseArticleFilename(filePath: string): { date: string; articleType: string } | null {
  const base = path.basename(filePath, '.html');
  const match = base.match(/^(\d{4}-\d{2}-\d{2})-(.+)-([a-z]{2})$/);
  if (!match) return null;
  const date = match[1];
  const body = match[2];
 
  // Match longest known article-type suffix in the body. Some slugs
  // differ from the contract key (e.g. `government-propositions` →
  // `propositions`, `opposition-motions` → `motions`,
  // `interpellation-debates` → `interpellations`).
  const SLUG_ALIASES: Readonly<Record<string, string>> = {
    'committee-reports': 'committee-reports',
    'government-propositions': 'propositions',
    'opposition-motions': 'motions',
    'interpellation-debates': 'interpellations',
    'evening-analysis': 'evening-analysis',
    'realtime-monitor': 'realtime-monitor',
    'breaking': 'breaking',
    'week-ahead': 'week-ahead',
    'month-ahead': 'month-ahead',
    'weekly-review': 'weekly-review',
    'monthly-review': 'monthly-review',
  };
 
  // deep-inspection is special: body starts with 'deep-inspection-'
  if (body.startsWith('deep-inspection-')) {
    return { date, articleType: 'deep-inspection' };
  }
 
  // Look for exact alias match first, then longest suffix
  for (const [slug, type] of Object.entries(SLUG_ALIASES)) {
    if (body === slug || body.endsWith(`-${slug}`)) {
      return { date, articleType: type };
    }
  }
 
  // Fallback: treat the whole body as the type (e.g. article-generator).
  return { date, articleType: body };
}
 
export function validateArticle(filePath: string, rootDir: string = process.cwd()): Violation[] {
  const violations: Violation[] = [];
  const parsed = parseArticleFilename(filePath);
  Iif (!parsed) return violations; // unknown pattern, skip silently
 
  const { date, articleType } = parsed;
  const rule = COVERAGE_MATRIX[articleType];
  Iif (!rule) return violations; // article type not covered by the contract
 
  // Pre-contract articles are exempt — the contract / schema / validator
  // only became authoritative on CONTRACT_EFFECTIVE_DATE. Enforcing on
  // older articles would be a retroactive rule change we cannot satisfy
  // without re-running the World Bank / SCB MCP queries for history, and
  // it keeps the daily audit noisy for a week after every rollout.
  if (!isUnderContract(date)) return violations;
 
  let html: string;
  try {
    html = fs.readFileSync(filePath, 'utf-8');
  } catch {
    violations.push({ articleFile: filePath, articleType, reason: 'Article HTML could not be read' });
    return violations;
  }
 
  // Load the companion economic-data.json (if any)
  const ctx = loadEconomicContext(date, articleType, rootDir);
 
  // Skip marker handling
  if (ctx?.skip) {
    if (!rule.allowSkip) {
      violations.push({
        articleFile: filePath,
        articleType,
        reason: `economic-data.json has skip=true but article type '${articleType}' is not on the exempt allow-list`,
      });
    } else if (I!ctx.skipReason) {
      violations.push({
        articleFile: filePath,
        articleType,
        reason: 'economic-data.json has skip=true without skipReason',
      });
    }
    return violations;
  }
 
  // Check 1: no placeholder leakage
  if (/class="economic-dashboard-placeholder"/.test(html)) {
    violations.push({
      articleFile: filePath,
      articleType,
      reason: 'HTML contains economic-dashboard-placeholder — agentic workflow did not supply live World Bank data',
    });
  }
 
  // Check 2: Chart.js canvases
  const chartCount = countChartCanvases(html);
  if (chartCount < rule.minCharts) {
    violations.push({
      articleFile: filePath,
      articleType,
      reason: `Expected ≥${rule.minCharts} data-chart-config canvases, found ${chartCount}`,
    });
  }
 
  // Check 2b: If the article emits any `data-chart-config` canvases, the
  // HTML MUST also load the Chart.js runtime (chart.umd.*.js) and the
  // generic initializer (chart-init.js). Without them canvases render
  // blank in the browser. See scripts/article-template/template.ts.
  if (chartCount > 0) {
    if (!/<script[^>]+chart\.umd\.[^"']+\.js/.test(html)) {
      violations.push({
        articleFile: filePath,
        articleType,
        reason: 'Article has data-chart-config canvases but does not load js/lib/chart.umd.*.js — charts would render blank',
      });
    }
    if (!/<script[^>]+chart-init\.js/.test(html)) {
      violations.push({
        articleFile: filePath,
        articleType,
        reason: 'Article has data-chart-config canvases but does not load js/chart-init.js — charts would never be instantiated',
      });
    }
  }
 
  // Check 3: economic-data.json completeness
  if (!ctx) {
    violations.push({
      articleFile: filePath,
      articleType,
      reason: `Missing or malformed ${economicDataPath(date, articleType).replace(rootDir + '/', '')}`,
    });
  } else {
    if (ctx.dataPoints.length === 0) {
      violations.push({
        articleFile: filePath,
        articleType,
        reason: 'economic-data.json has empty dataPoints[] (workflow fetched no World Bank data)',
      });
    }
    const words = countWords(ctx.commentary);
    if (words < rule.minCommentaryWords) {
      violations.push({
        articleFile: filePath,
        articleType,
        reason: `AI commentary too short — ${words} words, expected ≥${rule.minCommentaryWords}`,
      });
    }
    if (ctx.source.worldBank.length === 0 && ctx.source.scb.length === 0) {
      violations.push({
        articleFile: filePath,
        articleType,
        reason: 'economic-data.json lacks source attribution (World Bank + SCB empty)',
      });
    }
  }
 
  // Check 4: footer attribution link. The renderer does not yet emit
  // a deterministic "Data by World Bank / SCB" footer string for every
  // template, so accept structured attribution from
  // `economic-data.json.source` as a fallback source of truth. This
  // prevents false failures for articles that ship valid economic
  // data and charts but whose footer copy has not been migrated.
  const hasStructuredAttribution = Boolean(
    ctx && (ctx.source.worldBank.length > 0 || ctx.source.scb.length > 0),
  );
  if (!hasAttribution(html) && !hasStructuredAttribution) {
    violations.push({
      articleFile: filePath,
      articleType,
      reason: 'Missing "Data by World Bank / SCB" footer attribution (no structured source in economic-data.json either)',
    });
  }
 
  // Check 5: D3 Sankey requirement for high-level reviews. The renderer
  // ships two equivalent Sankey flavours:
  //   (a) An inline SVG Sankey section (class="sankey-section") emitted
  //       by scripts/data-transformers/content-generators/sankey-section.ts
  //   (b) A `data-d3-sankey=` marker rendered client-side by D3 (requires
  //       `js/lib/d3.*.min.js` to be loaded)
  // Either satisfies the coverage rule. When the D3 marker is used, the
  // matching minified script MUST be loaded or the diagram would not
  // render.
  if (rule.requiresD3) {
    const hasD3Marker = /data-d3-sankey=/.test(html);
    const hasInlineSankeySection = /class="sankey-section"|id="sankey-section"/.test(html);
    if (!hasD3Marker && !hasInlineSankeySection) {
      violations.push({
        articleFile: filePath,
        articleType,
        reason: `Article type '${articleType}' requires a Sankey / flow diagram (inline class="sankey-section" SVG or data-d3-sankey= marker)`,
      });
    }
    if (hasD3Marker && !/<script[^>]+d3\.[^"']+\.min\.js/.test(html)) {
      violations.push({
        articleFile: filePath,
        articleType,
        reason: `Article type '${articleType}' uses data-d3-sankey= but js/lib/d3.*.min.js is not loaded — diagram would not render`,
      });
    }
  }
 
  return violations;
}
 
// ---------------------------------------------------------------------------
// CLI entrypoint
// ---------------------------------------------------------------------------
 
function parseArgs(argv: string[]): { date?: string; type?: string; files?: string[] } {
  const out: { date?: string; type?: string; files?: string[] } = {};
  const files: string[] = [];
  for (let i = 0; i < argv.length; i++) {
    const a = argv[i];
    if (a === '--date') out.date = argv[++i];
    else if (a === '--type') out.type = argv[++i];
    else if (a.startsWith('--')) continue;
    else files.push(a);
  }
  if (files.length > 0) out.files = files;
  return out;
}
 
function discoverArticleFiles(opts: { date?: string; type?: string }, rootDir: string): string[] {
  const newsDir = path.join(rootDir, 'news');
  let entries: string[];
  try {
    entries = fs.readdirSync(newsDir);
  } catch {
    return [];
  }
  // NOTE: do NOT construct a RegExp from `opts.date` — it would be
  // user-controlled regex input (CodeQL regex-injection). Instead we
  // validate the shape and use a literal startsWith() check.
  const explicitDate = opts.date && isIsoDate(opts.date) ? opts.date : undefined;
  const genericDatePrefix = /^\d{4}-\d{2}-\d{2}-/;
  return entries
    .filter((f) => f.endsWith('-en.html')) // check EN only — translations inherit
    .filter((f) => !f.startsWith('index'))
    .filter((f) => (explicitDate
      ? f.startsWith(`${explicitDate}-`)
      : genericDatePrefix.test(f)))
    // Skip pre-contract articles so they neither show up as "✅" noise
    // nor trigger violations the agent cannot retroactively fix. See
    // CONTRACT_EFFECTIVE_DATE above.
    .filter((f) => {
      const m = f.match(/^(\d{4}-\d{2}-\d{2})-/);
      return m ? isUnderContract(m[1]) : true;
    })
    .filter((f) => {
      if (!opts.type) return true;
      const parsed = parseArticleFilename(path.join(newsDir, f));
      return parsed?.articleType === opts.type;
    })
    .map((f) => path.join(newsDir, f));
}
 
export async function main(argv: string[] = process.argv.slice(2), rootDir: string = process.cwd()): Promise<number> {
  const opts = parseArgs(argv);
  const files = opts.files && opts.files.length > 0
    ? opts.files
    : discoverArticleFiles({ date: opts.date, type: opts.type }, rootDir);
 
  if (files.length === 0) {
    console.log('✅ validate-economic-context: no matching articles to validate');
    return 0;
  }
 
  let totalViolations = 0;
  for (const f of files) {
    const violations = validateArticle(f, rootDir);
    if (violations.length > 0) {
      console.error(`❌ ${path.relative(rootDir, f)}`);
      for (const v of violations) {
        console.error(`   • [${v.articleType}] ${v.reason}`);
      }
      totalViolations += violations.length;
    } else {
      console.log(`✅ ${path.relative(rootDir, f)}`);
    }
  }
 
  if (totalViolations > 0) {
    console.error(`\n❌ validate-economic-context: ${totalViolations} violation(s) across ${files.length} article(s)`);
    return 1;
  }
  console.log(`\n✅ validate-economic-context: ${files.length} article(s) pass the economic context contract`);
  return 0;
}
 
// Run when invoked directly (tsx runs it as an entry module).
const isMain =
  typeof process !== 'undefined' &&
  Array.isArray(process.argv) &&
  process.argv[1] != null &&
  /validate-economic-context\.(ts|mjs|js)$/.test(process.argv[1]);
 
Iif (isMain) {
  main().then((code) => process.exit(code)).catch((err) => {
    console.error('validate-economic-context crashed:', err);
    process.exit(2);
  });
}