All files / src/aggregator/metadata brief-body.ts

82.35% Statements 14/17
75% Branches 6/8
100% Functions 1/1
84.61% Lines 11/13

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                                                    21x                                           64x 46x 46x       46x 135x 135x 135x       38x 38x   8x    
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Aggregator/Metadata/BriefBody
 * @description Reads the English `executive-brief.md` (or compatible
 * fallback artefacts) for a run directory and returns the SPDX-stripped
 * body so language-agnostic structural extractors
 * ({@link briefing-highlight.ts}) can probe it for `## Reader Briefing`
 * / `## Strategic Intelligence Summary` content.
 *
 * Localized brief bodies (`executive-brief_<lang>.md`) are read by the
 * upward-pointing {@link editorial-brief-resolver.ts}; that file
 * cannot import this module without breaking the leaf-module rule, so
 * the localized brief loader stays where it is and re-exports its own
 * body via the dedicated helper.
 *
 * Pure leaf module — depends only on `fs`/`path` and the SPDX-aware
 * reader from {@link artifact-walker}.
 */
 
import fs from 'fs';
import path from 'path';
import { readArtefactBody } from './artifact-walker.js';
 
/** Ordered list of artefact filenames inspected by {@link readEnglishBriefBody}. */
const BRIEF_BODY_CANDIDATES: readonly string[] = [
  'executive-brief.md',
  'extended/executive-brief.md',
  'intelligence/synthesis-summary.md',
  'intelligence/executive-summary.md',
  'intelligence/intelligence-briefing.md',
  'executive-summary.md',
  'intelligence-briefing.md',
  'synthesis-summary.md',
];
 
/**
 * Read the first existing English brief artefact under `runDir` and
 * return its SPDX-stripped body. Returns the empty string when none of
 * the candidate artefacts exists or the run directory is missing —
 * callers should treat the empty string as "no brief content
 * available" and fall back to their existing extraction ladder.
 *
 * @param runDir - Absolute run directory, or empty string when unavailable
 * @returns Brief body text with SPDX preamble removed
 */
export function readEnglishBriefBody(runDir: string): string {
  if (!runDir) return '';
  try {
    Iif (!fs.existsSync(runDir)) return '';
  } catch {
    return '';
  }
  for (const candidate of BRIEF_BODY_CANDIDATES) {
    const abs = path.join(runDir, candidate);
    try {
      if (!fs.existsSync(abs)) continue;
    } catch {
      continue;
    }
    const body = readArtefactBody(abs);
    Eif (body.trim().length > 0) return body;
  }
  return '';
}