All files / scripts/validators/executive-brief-translations validate-translation-content.ts

98.03% Statements 50/51
76.08% Branches 35/46
100% Functions 5/5
97.87% Lines 46/47

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                                                                                  11x 11x     11x 11x 11x                 11x 11x 11x                 11x 11x 11x                 11x 11x 11x                 11x 11x 33x 23x 11x 11x                       11x 11x 22x 15x 11x 11x                       11x 2x 2x               11x 11x             11x 11x                       11x 11x                     11x 11x 2x           9x 9x 9x 9x 9x 9x 9x                 11x    
/**
 * @module scripts/validators/executive-brief-translations/validate-translation-content
 * @description Per-translation rule runner — builds the 10-item
 *              `CheckResult[]` shape used by the orchestrator and
 *              consumed by `tests/validate-executive-brief-translations.test.ts`.
 *
 *              Rule census: extracted from
 *              `scripts/validate-executive-brief-translations.ts` lines
 *              232–382. Logic is byte-identical to the original — each
 *              check pushes the same `check` identifier and `detail`
 *              string into the result array.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import { countCodeFences } from './counters/code-fences.js';
import { countHeadings } from './counters/headings.js';
import { countMermaidBlocks } from './counters/mermaid-blocks.js';
import { countTableRows } from './counters/table-rows.js';
import { countWords } from './counters/words.js';
import { extractDokIds } from './extractors/dok-ids.js';
import { extractSourceShaMarker, hasRtlMarker } from './extractors/source-sha.js';
import { extractUrls } from './extractors/urls.js';
import { findBannedEnglishPhrases } from './rules/banned-english.js';
import { RTL_LANGS, type CheckResult, type TranslationLang } from './types.js';
 
export interface ValidateTranslationOptions {
  /** Source executive-brief.md content. */
  sourceContent: string;
  /** Translation file path (used for diagnostic context only). */
  translationPath: string;
  /** Translation file content. May be empty if file doesn't exist. */
  translationContent: string;
  /** Target language code. */
  lang: TranslationLang;
  /** Optional source SHA — when supplied, the trailer must match. */
  sourceSha?: string | null;
}
 
export function validateTranslationContent(opts: ValidateTranslationOptions): CheckResult[] {
  const { sourceContent, translationContent, lang, sourceSha } = opts;
  const checks: CheckResult[] = [];
 
  // 1. Heading parity
  const srcHeadings = countHeadings(sourceContent);
  const tgtHeadings = countHeadings(translationContent);
  checks.push({
    check: 'heading-count',
    passed: srcHeadings === tgtHeadings,
    detail: srcHeadings === tgtHeadings
      ? `${tgtHeadings}`
      : `source=${srcHeadings} translation=${tgtHeadings}`,
  });
 
  // 2. Table-row parity
  const srcRows = countTableRows(sourceContent);
  const tgtRows = countTableRows(translationContent);
  checks.push({
    check: 'table-row-count',
    passed: srcRows === tgtRows,
    detail: srcRows === tgtRows
      ? `${tgtRows}`
      : `source=${srcRows} translation=${tgtRows}`,
  });
 
  // 3. Code-fence parity
  const srcFences = countCodeFences(sourceContent);
  const tgtFences = countCodeFences(translationContent);
  checks.push({
    check: 'code-fence-count',
    passed: srcFences === tgtFences,
    detail: srcFences === tgtFences
      ? `${tgtFences}`
      : `source=${srcFences} translation=${tgtFences}`,
  });
 
  // 4. Mermaid-block parity
  const srcMermaid = countMermaidBlocks(sourceContent);
  const tgtMermaid = countMermaidBlocks(translationContent);
  checks.push({
    check: 'mermaid-block-count',
    passed: srcMermaid === tgtMermaid,
    detail: srcMermaid === tgtMermaid
      ? `${tgtMermaid}`
      : `source=${srcMermaid} translation=${tgtMermaid}`,
  });
 
  // 5. dok_id preservation (set equality — no missing, no extras)
  const srcDokIds = extractDokIds(sourceContent);
  const tgtDokIds = extractDokIds(translationContent);
  const missingDokIds = [...srcDokIds].filter((id) => !tgtDokIds.has(id));
  const extraDokIds = [...tgtDokIds].filter((id) => !srcDokIds.has(id));
  const dokIdPassed = missingDokIds.length === 0 && extraDokIds.length === 0;
  checks.push({
    check: 'dok-id-preservation',
    passed: dokIdPassed,
    detail: dokIdPassed
      ? `${srcDokIds.size} preserved`
      : [
          missingDokIds.length > 0 ? `missing: ${missingDokIds.join(', ')}` : '',
          extraDokIds.length > 0 ? `extra: ${extraDokIds.join(', ')}` : '',
        ].filter(Boolean).join('; '),
  });
 
  // 6. URL preservation (set equality — no missing, no extras)
  const srcUrls = extractUrls(sourceContent);
  const tgtUrls = extractUrls(translationContent);
  const missingUrls = [...srcUrls].filter((u) => !tgtUrls.has(u));
  const extraUrls = [...tgtUrls].filter((u) => !srcUrls.has(u));
  const urlPassed = missingUrls.length === 0 && extraUrls.length === 0;
  checks.push({
    check: 'url-preservation',
    passed: urlPassed,
    detail: urlPassed
      ? `${srcUrls.size} preserved`
      : [
          missingUrls.length > 0 ? `missing ${missingUrls.length} of ${srcUrls.size}` : '',
          extraUrls.length > 0 ? `extra ${extraUrls.length} URLs injected` : '',
        ].filter(Boolean).join('; '),
  });
 
  // 7. RTL marker (ar / he only)
  if (RTL_LANGS.includes(lang)) {
    const rtlOk = hasRtlMarker(translationContent);
    checks.push({
      check: 'rtl-marker',
      passed: rtlOk,
      detail: rtlOk ? 'present' : 'missing `<!-- dir: rtl -->` marker',
    });
  }
 
  // 8. source-sha trailer
  const trailerSha = extractSourceShaMarker(translationContent);
  Iif (sourceSha === null || sourceSha === undefined) {
    checks.push({
      check: 'source-sha-marker',
      passed: trailerSha !== null,
      detail: trailerSha ? 'present' : 'missing `<!-- source-sha: -->` trailer',
    });
  } else {
    const shaMatches = trailerSha === sourceSha;
    checks.push({
      check: 'source-sha-marker',
      passed: shaMatches,
      detail: !trailerSha
        ? 'missing `<!-- source-sha: -->` trailer'
        : shaMatches
          ? 'matches source'
          : `stale (trailer=${trailerSha.slice(0, 8)} source=${sourceSha.slice(0, 8)})`,
    });
  }
 
  // 9. Banned English phrases (non-EN files)
  const banned = findBannedEnglishPhrases(translationContent);
  checks.push({
    check: 'no-banned-english',
    passed: banned.length === 0,
    detail: banned.length === 0 ? 'clean' : `found: ${banned.join(', ')}`,
  });
 
  // 10. Word-count drift (±25%) — skipped for CJK scripts (Japanese, Chinese)
  // because they do not use whitespace word boundaries and the whitespace
  // tokeniser systematically undercounts them by ~4–6×. Structural parity
  // (headings, table rows, code fences, mermaid blocks, dok_id and URL sets)
  // still guarantees the translation is not truncated or padded.
  const NON_WORDSPACE_LANGS: ReadonlyArray<TranslationLang> = ['ja', 'zh'];
  if (NON_WORDSPACE_LANGS.includes(lang)) {
    checks.push({
      check: 'word-count-drift',
      passed: true,
      detail: 'skipped (CJK script — no whitespace word boundaries)',
    });
  } else {
    const srcWords = countWords(sourceContent);
    const tgtWords = countWords(translationContent);
    const tolerance = 0.25;
    const lowerBound = Math.floor(srcWords * (1 - tolerance));
    const upperBound = Math.ceil(srcWords * (1 + tolerance));
    const inBounds = tgtWords >= lowerBound && tgtWords <= upperBound;
    checks.push({
      check: 'word-count-drift',
      passed: inBounds,
      detail: inBounds
        ? `${tgtWords} words (source=${srcWords})`
        : `${tgtWords} words outside [${lowerBound}, ${upperBound}] (source=${srcWords})`,
    });
  }
 
  return checks;
}