All files / src/aggregator/run section-expansion.ts

100% Statements 15/15
100% Branches 10/10
100% Functions 2/2
100% Lines 14/14

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                                                        1057x 1057x 4277x 279x 279x 1267x   279x 38x 38x   3998x 212x 212x 212x     1057x    
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Aggregator/Run/SectionExpansion
 * @description Expands an `ArtifactSection` declaration into the concrete
 * ordered list of artifact paths drawn from the manifest/discovery set.
 */
 
import type { ArtifactSection } from '../artifact-order.js';
 
/**
 * Expand an `artifacts` entry from {@link ArtifactSection} into a list of
 * concrete artifact paths. Exact paths are kept as-is; directory prefixes
 * ending in `/` expand to every remaining `.md` under that directory
 * (lexical order), excluding files already claimed by higher-priority
 * sections.
 *
 * @param section - Canonical section descriptor from {@link ARTIFACT_SECTIONS}
 * @param available - Set of every known artifact path (run-relative)
 * @param consumed - Mutable set of paths already claimed by earlier sections
 * @returns Ordered list of artifact paths that belong to this section
 */
export function expandSectionArtifacts(
  section: ArtifactSection,
  available: Set<string>,
  consumed: Set<string>
): string[] {
  const out: string[] = [];
  for (const entry of section.artifacts) {
    if (entry.endsWith('/')) {
      const prefix = entry;
      const matching = [...available]
        .filter((p) => p.startsWith(prefix) && !consumed.has(p))
        .sort();
      for (const p of matching) {
        out.push(p);
        consumed.add(p);
      }
    } else if (available.has(entry) && !consumed.has(entry)) {
      out.push(entry);
      consumed.add(entry);
      if (section.id === 'executive-brief') break;
    }
  }
  return out;
}