All files / chart-model/src chart-options.ts

100% Statements 19/19
100% Branches 17/17
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                                                    7351x 7350x 7351x 7351x 7351x 377346x 285x 285x 283x 283x 281x 281x   7350x 7350x    
import { normalizeChannel, normalizeObjectKey, resolveBMSBase, type BeMusicJSON } from '@be-music/chart-source';
import { hasControlFlow } from './control-flow.ts';
import { createTimingResolver, type TimingResolver } from './timing.ts';
 
export interface ChartOptionCue {
  seconds: number;
  key: string;
  /** Whole vendor payload. Absent means an undefined reference to diagnose, not an instruction to clear options. */
  declaration?: string;
}
 
export interface ChartOptionTimeline {
  /** Ordered complete #OPTION payloads. Vendor arguments may contain whitespace. */
  initial: string[];
  /** Channel-A6 references in chart-time order; equal-time entries retain normalized IR order. */
  cues: ChartOptionCue[];
}
 
/**
 * Collect the resolved chart's authored option declarations and A6 triggers without guessing vendor semantics.
 * Resolve RANDOM/SWITCH first: branch ordering can change which initial options take effect.
 */
export function buildChartOptionTimeline(
  json: BeMusicJSON,
  timing: Pick<TimingResolver, 'eventToSeconds'> = createTimingResolver(json),
): ChartOptionTimeline {
  if (hasControlFlow(json)) throw new RangeError('Resolve BMS control flow before building a chart option timeline.');
  const initial = json.bms.options !== undefined ? [...json.bms.options] : json.bms.option ? [json.bms.option] : [];
  const base = resolveBMSBase(json);
  const cues: ChartOptionCue[] = [];
  for (const event of json.events) {
    if (normalizeChannel(event.channel) !== 'A6') continue;
    const key = normalizeObjectKey(event.value, base, json.sourceFormat === 'bmson');
    if (!key || key === '00') continue;
    const seconds = timing.eventToSeconds(event);
    if (!Number.isFinite(seconds)) continue;
    const declaration = json.bms.changeOption[key];
    cues.push({ seconds, key, ...(declaration !== undefined ? { declaration } : {}) });
  }
  cues.sort((left, right) => left.seconds - right.seconds);
  return { initial, cues };
}