All files / src/aggregator/runs grouping.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 2/2
100% Lines 8/8

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                                            17x                                 9x 9x 16x 16x 16x 16x   9x    
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Aggregator/Runs/Grouping
 * @description Pure grouping helpers used by the batch driver to detect
 * `(date, articleType)` collisions. Two runs that share both keys would
 * otherwise produce overlapping article filenames; the batch driver
 * disambiguates by appending the sanitised `runId` as a slug suffix.
 */
 
import type { DiscoveredRun } from './discover.js';
 
/**
 * Build a stable `"<date>|<articleType>"` collision key for a discovered
 * run. Exposed so callers can look up a run's group without re-importing
 * the grouping function.
 *
 * @param run - Discovered run
 * @returns Pipe-separated collision key
 */
export function collisionKey(run: Pick<DiscoveredRun, 'date' | 'articleType'>): string {
  return `${run.date}|${run.articleType}`;
}
 
/**
 * Group discovered runs by `(date, articleType)` so callers can decide
 * whether a collision-suffix is needed when writing articles.
 *
 * Insertion order is preserved within each bucket — the batch driver
 * iterates the original `runs` list in order and looks up its own bucket
 * by key, so the collision-suffix is appended deterministically.
 *
 * @param runs - Discovered runs
 * @returns Map from `"<date>|<articleType>"` to the runs in that group
 */
export function groupRunsForCollision(
  runs: readonly DiscoveredRun[]
): Map<string, DiscoveredRun[]> {
  const groups = new Map<string, DiscoveredRun[]>();
  for (const run of runs) {
    const key = collisionKey(run);
    const bucket = groups.get(key) ?? [];
    bucket.push(run);
    groups.set(key, bucket);
  }
  return groups;
}