All files / scripts validate-article-seo.js

68.66% Statements 103/150
52.22% Branches 47/90
92.85% Functions 13/14
69.56% Lines 96/138

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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535                                                                                                                                                                                                                1x                   2x 2x 2x                                   1x 1x   1x 1x                   1x 1x                 1x                 1x                                 2x 2x                 2x 2x               2x 2x               2x 2x               2x 2x               2x                                                                             24x 20x 14x 14x   6x 2x   4x       2x 2x 2x 1x                                                       6x 6x 6x 6x 6x                             1x 1x 1x 1x 1x 1x   1x 1x                                                                   1x   1x 1x 1x 1x 1x 1x 1x 1x                       1x 1x 1x             1x             1x               1x 5x 5x   1x 1x 1x   1x 2x 2x       2x 1x 1x   1x           1x 1x 1x   1x 1x   1x 1x                             1x     1x         1x   1x 1x     1x       1x 1x   1x                         1x 1x 1x 1x         1x     1x                                                
#!/usr/bin/env node
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module scripts/validate-article-seo
 * @description Hard CI gate for resolved `<title>` / `<meta description>`
 * metadata that the deterministic article generator would emit for every
 * executive brief under `analysis/daily/`. Companion to
 * `validate-manifest-seo.js`: where that script audits the per-language
 * `(title, description)` pairs committed *in manifest.json*, this one
 * audits the **resolved output** of `resolveArticleMetadata()` — the same
 * code path used by `npm run generate-article:all`.
 *
 * Why a separate gate? `manifest.json` is the Stage-B *input* contract.
 * The Stage-D *output* contract is the resolved entry returned by
 * `resolveArticleMetadata`, which threads through editorial-highlight
 * extraction, BLUF-summary derivation, contextual title composition
 * (including `— Run N` run-qualifier for same-date/same-articleType
 * republishes), and CJK-aware length budgets. Bugs in any of those
 * downstream layers can ship a degraded `<head>` even when manifest.json
 * is clean.
 *
 * Gates applied per resolved entry (English only by default — the per-
 * language outputs are validated by `validate-manifest-seo.js`):
 *
 *   1. **title-empty**       — `entry.title` must be a non-empty string.
 *   2. **title-length**      — effective length in `[TITLE_MIN_LENGTH,
 *      TITLE_MAX_LENGTH]` after CJK 2× weighting.
 *   3. **title-ellipsis**    — must not end with `…` or `...` (mid-
 *      sentence truncation regression).
 *   4. **description-empty** — `entry.description` must be a non-empty
 *      string.
 *   5. **description-length** — effective length in `[DESCRIPTION_MIN_LENGTH,
 *      DESCRIPTION_MAX_LENGTH]` after CJK 2× weighting.
 *   6. **description-ellipsis** — must not end with `…` / `...`.
 *   7. **forbidden-prefix**  — title/description must not start with a
 *      Stage-B preamble label (`Run:`, `Purpose:`, `BLUF:`, …).
 *   8. **leaky-runid**       — title/description must not contain
 *      internal run-id tokens or "analysis run" jargon.
 *   9. **title-uniqueness**  — when ≥2 runs share the same `(date,
 *      articleType)`, their resolved titles must differ (typically via
 *      the `— Run N` qualifier produced by `composeContextualTitle`).
 *  10. **title-ellipsis-cut** — title rejected by resolver predicate
 *      `looksLikeEllipsisCut` (trailing `…` / `...`). Fires alongside
 *      `title-ellipsis` for backwards compatibility.
 *  11. **title-doc-id**      — title is a bare adopted-text doc-ID
 *      (`TA-NN-YYYY-NNNN`), never an editorial headline.
 *  12. **title-section-header** — title is a bold-prose section header
 *      from `executive-brief.md` (`Strategic significance`,
 *      `Threat Level`, `Key Assumptions Check`, …).
 *  13. **title-sentence-fragment** — title is a complete sentence
 *      (single trailing period, ≥4 words) leaked from a BLUF / lede
 *      paragraph rather than a noun-phrase headline.
 *  14. **description-leaky-section-header** — description starts with
 *      a bold-prose section header label (lead phrase before `.:`).
 *
 * Gates 10–14 ship as `severity: 'advisory'` — they are surfaced in
 * the JSON report's `totals.advisories` / `totals.byGateAdvisory` and
 * printed with a ⚠️  prefix, but they do NOT count toward the
 * fail-count or exit code. This lets the validator catch resolver
 * regressions immediately without breaking CI on legacy articles that
 * pre-date the resolver-tightening work. Promote to failure-class in a
 * follow-up once the legacy data is regenerated.
 *
 * The process exits with code 1 if any failure-class violations exist
 * (unless `--no-fail` is passed for advisory mode).
 *
 * Invocation:
 *   node scripts/validate-article-seo.js \
 *     [--repo-root <path>] \
 *     [--paths <runDir>...]      # validate specific runDirs only
 *     [--lang en]                # default 'en'
 *     [--report <path>]          # write JSON report; default stdout
 *     [--no-fail]                # exit 0 even when violations found
 *     [--quiet]                  # suppress per-run logging
 */
 
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
 
import { ALL_LANGUAGES } from './constants/language-core.js';
import { discoverAnalysisRuns } from './aggregator/generator/discovery.js';
import { resolveRunSeo } from './dump-article-seo.js';
import {
  DESCRIPTION_MAX_LENGTH,
  DESCRIPTION_MIN_LENGTH,
  FORBIDDEN_PATTERNS,
  FORBIDDEN_PREFIXES,
  FORBIDDEN_SUBSTRINGS,
  TITLE_MAX_LENGTH,
  TITLE_MIN_LENGTH,
  detectForbiddenPrefix,
  detectLeakyRunIdOrJargon,
  effectiveTextLength,
} from './validate-manifest-seo.js';
import { aggregateByKey } from './validate-brief-translations.js';
import {
  findTitleRejectionReason,
  looksLikeSectionHeader,
} from './aggregator/metadata/title-rejection.js';
 
/** Trailing-ellipsis detector: literal `…` or three ASCII periods. */
export const TRAILING_ELLIPSIS_RE = /(?:…|\.{3})\s*$/u;
 
/**
 * Detect whether a value ends with an ellipsis. Returns the matching
 * fragment for the violation message, or null when the value is clean.
 *
 * @param {string} value
 * @returns {string | null}
 */
export function detectTrailingEllipsis(value) {
  Iif (typeof value !== 'string') return null;
  const m = TRAILING_ELLIPSIS_RE.exec(value);
  return m ? m[0] : null;
}
 
/**
 * Resolve a single run through the same path as the article generator
 * and apply the SEO gates to the resolved English entry. Pushes
 * violations into the accumulator.
 *
 * @param {object} ctx
 * @param {string} ctx.runDir
 * @param {string} ctx.repoRoot
 * @param {string} ctx.lang
 * @param {Array<object>} ctx.violations
 * @returns {ReturnType<typeof resolveRunSeo> | null} The resolved
 *   record (or null if resolution threw — in which case a `resolve`
 *   gate violation has been pushed).
 */
function validateOneRun(ctx) {
  const { runDir, repoRoot, lang, violations } = ctx;
  const runDirRel = path.relative(repoRoot, runDir).split(path.sep).join('/');
  let record;
  try {
    record = resolveRunSeo({ runDir, repoRoot, lang });
  } catch (err) {
    violations.push({
      runDir: runDirRel,
      lang,
      gate: 'resolve',
      message: `resolveRunSeo failed: ${err.message}`,
    });
    return null;
  }
  const { entry } = record;
  applyFieldGates({
    runDirRel,
    lang,
    kind: 'title',
    value: entry.title,
    minLen: TITLE_MIN_LENGTH,
    maxLen: TITLE_MAX_LENGTH,
    violations,
  });
  applyFieldGates({
    runDirRel,
    lang,
    kind: 'description',
    value: entry.description,
    minLen: DESCRIPTION_MIN_LENGTH,
    maxLen: DESCRIPTION_MAX_LENGTH,
    violations,
  });
  return record;
}
 
/**
 * Apply the empty / length / ellipsis / forbidden-prefix / leaky-runid
 * gates to a single (kind, value) projection.
 *
 * @param {object} ctx
 * @param {string} ctx.runDirRel
 * @param {string} ctx.lang
 * @param {'title' | 'description'} ctx.kind
 * @param {string} ctx.value
 * @param {number} ctx.minLen
 * @param {number} ctx.maxLen
 * @param {Array<object>} ctx.violations
 */
function applyFieldGates(ctx) {
  const { runDirRel, lang, kind, value, minLen, maxLen, violations } = ctx;
  Iif (typeof value !== 'string' || value.trim().length === 0) {
    violations.push({
      runDir: runDirRel,
      lang,
      gate: `${kind}-empty`,
      message: `${kind} resolved to empty / non-string value`,
    });
    return;
  }
  const length = effectiveTextLength(value);
  Iif (length < minLen || length > maxLen) {
    violations.push({
      runDir: runDirRel,
      lang,
      gate: `${kind}-length`,
      message: `${kind} has effective length ${length}; expected ${minLen}–${maxLen}`,
    });
  }
  const ellipsis = detectTrailingEllipsis(value);
  Iif (ellipsis) {
    violations.push({
      runDir: runDirRel,
      lang,
      gate: `${kind}-ellipsis`,
      message: `${kind} ends with "${ellipsis}" — mid-sentence truncation is forbidden`,
    });
  }
  const prefix = detectForbiddenPrefix(value);
  Iif (prefix) {
    violations.push({
      runDir: runDirRel,
      lang,
      gate: 'forbidden-prefix',
      message: `${kind} begins with reserved Stage-B label "${prefix}"`,
    });
  }
  const leaked = detectLeakyRunIdOrJargon(value);
  Iif (leaked) {
    violations.push({
      runDir: runDirRel,
      lang,
      gate: 'leaky-runid',
      message: `${kind} contains internal token "${leaked}"`,
    });
  }
  applyKindSpecificRejectionGates(ctx);
}
 
/**
 * Apply the resolver-aligned title-rejection predicates from
 * `src/aggregator/metadata/title-rejection.ts`. Keeping validator and
 * resolver in lock-step ensures the same regression that the resolver
 * already rejects can never sneak through into a shipped `<title>` or
 * `<meta description>`.
 *
 * Emitted gates (deferred PR #2163 follow-up):
 *
 *   - `title-ellipsis-cut`            — trailing `…` / `...`
 *   - `title-doc-id`                  — bare `TA-NN-YYYY-NNNN` doc-ID
 *   - `title-section-header`          — bold-prose section header
 *   - `title-sentence-fragment`       — leaked complete sentence
 *   - `description-leaky-section-header` — description starts with a
 *     bold-prose section header label (`Strategic significance: …`,
 *     `Threat Level: …`, …)
 *
 * @param {object} ctx
 * @param {string} ctx.runDirRel
 * @param {string} ctx.lang
 * @param {'title' | 'description'} ctx.kind
 * @param {string} ctx.value
 * @param {Array<object>} ctx.violations
 */
/**
 * Evaluate the resolver-aligned rejection gates for a single
 * `(kind, value)` pair and return the gate names that fire. Pure,
 * dependency-free, used by both {@link applyKindSpecificRejectionGates}
 * and the unit tests to keep validator and resolver in lock-step.
 *
 * @param {'title' | 'description'} kind
 * @param {string} value
 * @returns {string[]} Zero or more gate names (e.g. `title-doc-id`,
 *   `description-leaky-section-header`).
 */
export function evaluateResolverRejectionGates(kind, value) {
  if (typeof value !== 'string' || value.length === 0) return [];
  if (kind === 'title') {
    const reason = findTitleRejectionReason(value);
    return reason ? [`title-${reason}`] : [];
  }
  if (kind === 'description' && detectDescriptionLeadSectionHeader(value)) {
    return ['description-leaky-section-header'];
  }
  return [];
}
 
function applyKindSpecificRejectionGates(ctx) {
  const { runDirRel, lang, kind, value, violations } = ctx;
  const gates = evaluateResolverRejectionGates(kind, value);
  for (const gate of gates) {
    violations.push({
      runDir: runDirRel,
      lang,
      gate,
      // The 5 resolver-aligned gates ship as `advisory` so the validator
      // can surface them in the report (and unit tests can lock the
      // signal) without immediately failing CI on the 25 legacy
      // articles that pre-date the resolver-tightening work. Promote to
      // failure-class in a follow-up once those runs are regenerated.
      severity: 'advisory',
      message:
        gate === 'description-leaky-section-header'
          ? `description starts with a bold-prose section header: "${value}"`
          : `title rejected by resolver predicate (${gate.slice('title-'.length)}): "${value}"`,
    });
  }
}
 
/**
 * `true` when a description's lead phrase (first sentence or first
 * bold-label segment) is a denylisted section header. Splits on the
 * first sentence terminator (`.`, `:`, `…`, `?`, `!`) and asks the
 * canonical {@link looksLikeSectionHeader} predicate.
 *
 * @param {string} value
 * @returns {boolean}
 */
function detectDescriptionLeadSectionHeader(value) {
  const trimmed = value.trim();
  Iif (!trimmed) return false;
  const m = /^([^.:!?\u2026]+)/u.exec(trimmed);
  const lead = (m ? m[1] : trimmed).trim();
  return looksLikeSectionHeader(lead);
}
 
/**
 * After resolving every run, detect duplicate titles within the same
 * `(date, articleType)` collision group. The `— Run N` qualifier from
 * `composeContextualTitle` is the contracted differentiator for
 * same-date/same-articleType republishes; missing it is a
 * uniqueness-gate failure.
 *
 * @param {Array<{record: ReturnType<typeof resolveRunSeo>, lang: string}>} resolvedList
 * @param {Array<object>} violations
 */
function detectDuplicateTitles(resolvedList, violations) {
  /** @type {Map<string, Array<{record: any, lang: string}>>} */
  const groups = new Map();
  for (const item of resolvedList) {
    const key = `${item.record.date}|${item.record.articleType}`;
    const bucket = groups.get(key);
    Iif (bucket) bucket.push(item);
    else groups.set(key, [item]);
  }
  for (const [key, items] of groups.entries()) {
    Eif (items.length < 2) continue;
    /** @type {Map<string, string[]>} */
    const byTitle = new Map();
    for (const it of items) {
      const t = it.record.entry.title;
      const bucket = byTitle.get(t);
      if (bucket) bucket.push(it.record.runDirRel);
      else byTitle.set(t, [it.record.runDirRel]);
    }
    for (const [title, dirs] of byTitle.entries()) {
      if (dirs.length < 2) continue;
      violations.push({
        runDir: dirs.join(', '),
        lang: items[0].lang,
        gate: 'title-uniqueness',
        affectedRuns: dirs,
        message:
          `${dirs.length} runs in collision group "${key}" share the ` +
          `resolved title "${title}" — composeContextualTitle must ` +
          `append a "— Run N" qualifier (or equivalent) so the per-run ` +
          `articles are distinguishable in SERPs and social cards`,
      });
    }
  }
}
 
/**
 * Run validation across a list of run directories.
 *
 * @param {string[]} runDirs
 * @param {string} repoRoot
 * @param {{ quiet?: boolean, lang?: string }} options
 */
export function runValidation(runDirs, repoRoot, { quiet = false, lang = 'en' } = {}) {
  const allViolations = [];
  /** @type {Array<{record: ReturnType<typeof resolveRunSeo>, lang: string}>} */
  const resolved = [];
  for (const runDir of runDirs) {
    const before = allViolations.length;
    const record = validateOneRun({ runDir, repoRoot, lang, violations: allViolations });
    const runDirRel = path.relative(repoRoot, runDir).split(path.sep).join('/');
    Eif (record) resolved.push({ record, lang });
    const added = allViolations.length - before;
    Iif (!quiet) {
      if (added > 0) {
        for (const entry of allViolations.slice(before)) {
          const prefix = entry.severity === 'advisory' ? '⚠️ ' : '❌';
          const stream = entry.severity === 'advisory' ? process.stdout : process.stderr;
          stream.write(`${prefix} ${runDirRel} [${entry.gate}] ${entry.message}\n`);
        }
      } else {
        process.stdout.write(`✅ ${runDirRel}\n`);
      }
    }
  }
  const beforeUniq = allViolations.length;
  detectDuplicateTitles(resolved, allViolations);
  Iif (!quiet) {
    for (const entry of allViolations.slice(beforeUniq)) {
      const prefix = entry.severity === 'advisory' ? '⚠️ ' : '❌';
      const stream = entry.severity === 'advisory' ? process.stdout : process.stderr;
      stream.write(`${prefix} ${entry.runDir} [${entry.gate}] ${entry.message}\n`);
    }
  }
  return allViolations;
}
 
/**
 * Parse CLI argv. Exported for unit tests.
 */
export function parseArgs(argv) {
  const opts = {
    repoRoot: process.cwd(),
    paths: [],
    lang: 'en',
    report: null,
    fail: true,
    quiet: false,
  };
  for (let i = 0; i < argv.length; i += 1) {
    const arg = argv[i];
    switch (arg) {
      case '--repo-root':
        opts.repoRoot = argv[i + 1];
        i += 1;
        break;
      case '--paths':
        while (i + 1 < argv.length) {
          const next = argv[i + 1];
          Iif (next === '--') {
            i += 1;
            break;
          }
          if (next.startsWith('--')) break;
          opts.paths.push(next);
          i += 1;
        }
        break;
      case '--lang':
        opts.lang = argv[i + 1];
        i += 1;
        break;
      case '--report':
        opts.report = argv[i + 1];
        i += 1;
        break;
      case '--no-fail':
        opts.fail = false;
        break;
      case '--quiet':
        opts.quiet = true;
        break;
      case '--help':
      case '-h':
        process.stdout.write(
          'Usage: validate-article-seo.js [--repo-root <path>] ' +
            '[--paths <runDir>... [--]] [--lang <code>] [--report <path>] [--no-fail] [--quiet]\n'
        );
        process.exit(0);
        break;
      default:
        if (arg.startsWith('--')) {
          throw new Error(`Unknown flag: ${arg}`);
        }
    }
  }
  Iif (!ALL_LANGUAGES.includes(opts.lang)) {
    throw new Error(`Unsupported --lang "${opts.lang}"`);
  }
  return opts;
}
 
/** Main entry point. */
export function main(argv) {
  const opts = parseArgs(argv);
  const runDirs =
    opts.paths.length > 0
      ? opts.paths.map((p) => path.resolve(opts.repoRoot, p))
      : discoverAnalysisRuns(opts.repoRoot).map((r) => r.runDir);
 
  const violations = runValidation(runDirs, opts.repoRoot, {
    quiet: opts.quiet,
    lang: opts.lang,
  });
  const failingViolations = violations.filter((v) => v.severity !== 'advisory');
  const advisoryViolations = violations.filter((v) => v.severity === 'advisory');
 
  const report = {
    generatedAt: new Date().toISOString(),
    lang: opts.lang,
    totals: {
      runsChecked: runDirs.length,
      violations: failingViolations.length,
      advisories: advisoryViolations.length,
      byGate: aggregateByKey(failingViolations, 'gate'),
      byGateAdvisory: aggregateByKey(advisoryViolations, 'gate'),
    },
    violations,
  };
 
  const json = `${JSON.stringify(report, null, 2)}\n`;
  if (opts.report) {
    fs.mkdirSync(path.dirname(opts.report), { recursive: true });
    fs.writeFileSync(opts.report, json);
  } else Eif (!opts.quiet) {
    process.stdout.write(json);
  }
 
  Iif (failingViolations.length > 0 && opts.fail) {
    process.exit(1);
  }
  return report;
}
 
/* c8 ignore start */
if (import.meta.url === `file://${process.argv[1]}`) {
  try {
    main(process.argv.slice(2));
  } catch (err) {
    process.stderr.write(`validate-article-seo: ${err.message}\n`);
    process.exit(1);
  }
}
/* c8 ignore stop */
 
// Re-export for tests
export {
  FORBIDDEN_PATTERNS,
  FORBIDDEN_PREFIXES,
  FORBIDDEN_SUBSTRINGS,
  TITLE_MAX_LENGTH,
  TITLE_MIN_LENGTH,
  DESCRIPTION_MAX_LENGTH,
  DESCRIPTION_MIN_LENGTH,
};