import { describe, expect, it } from 'vitest'; import { BEAT_TEMPLATES } from './sequencer'; import { createInitialSequencerState, sequencerReducer, } from './domain/sequencerReducer'; import { generateUniqueSoundColor } from './domain/soundColors'; import { clampPitchSemitones, getPitchLabel, INSTRUMENT_CATEGORIES, INSTRUMENT_CATALOG, INSTRUMENT_BY_ID, INSTRUMENT_CATEGORY_BY_ID, MAX_PITCH_SEMITONES, MIN_PITCH_SEMITONES, } from './domain/instruments'; import { INSTRUMENT_VOICES } from './audio/soundVoices'; import { getAlphabeticLabel, getNextBeatLetter, getNextAvailableStep, getPlaybackTickDurationMs, getLoopPositionPercent, getPointForStep, getStepFromPosition, getTotalSteps, MAX_TEMPO_BPM, MIN_TEMPO_BPM, SVG_CENTER, wrapStepPosition, TOTAL_STEPS, TEMPO_BPM, } from './sequencer'; import type { BeatPoint } from './sequencer'; import { getSoundOrbitRadius, getStackedPosition } from './domain/timeline'; describe('circular step positions', () => { it('derives playback timing from tempo and fine-grained subdivisions', () => { expect(getPlaybackTickDurationMs(120)).toBeCloseTo(60_000 / 120 / 16, 8); }); it('maps each of the 16 points back to its step', () => { for (let step = 0; step < TOTAL_STEPS; step += 1) { const point = getPointForStep(step, 180); expect(getStepFromPosition(point.x, point.y)).toBeCloseTo(step, 8); } }); it('wraps points just before the first step to the end of the loop', () => { const point = getPointForStep(TOTAL_STEPS - 1, 180); expect(getStepFromPosition(point.x, point.y)).toBeCloseTo(TOTAL_STEPS - 1, 8); }); it('maps all steps around a three-beat bar', () => { const totalSteps = getTotalSteps(3); for (let step = 0; step < totalSteps; step += 1) { const point = getPointForStep(step, 180, SVG_CENTER, totalSteps); expect(getStepFromPosition(point.x, point.y, SVG_CENTER, totalSteps)).toBeCloseTo(step, 8); } }); it('maps all steps around an eight-beat bar', () => { const totalSteps = getTotalSteps(8); for (let step = 0; step < totalSteps; step += 1) { const point = getPointForStep(step, 180, SVG_CENTER, totalSteps); expect(getStepFromPosition(point.x, point.y, SVG_CENTER, totalSteps)).toBeCloseTo(step, 8); } }); it('preserves fractional positions between the regular steps', () => { const fractionalStep = 3.375; const point = getPointForStep(fractionalStep, 180); expect(getStepFromPosition(point.x, point.y)).toBeCloseTo(fractionalStep, 8); }); it('gives each sound its own orbit and stacks repeated beats of that sound', () => { const beats: BeatPoint[] = [ { id: 'kick-a', soundId: 'kick', letter: 'a', step: 4 }, { id: 'kick-b', soundId: 'kick', letter: 'b', step: 4 }, ]; const kickPosition = getStackedPosition(beats[0], beats, TOTAL_STEPS, 180); const secondKickPosition = getStackedPosition(beats[1], beats, TOTAL_STEPS, 180); expect(kickPosition).not.toEqual(secondKickPosition); expect(getSoundOrbitRadius(0, 2)).not.toBe(getSoundOrbitRadius(1, 2)); }); it('wraps fractional positions and reports loop percentages', () => { expect(wrapStepPosition(16.25, 16)).toBeCloseTo(0.25); expect(wrapStepPosition(-0.25, 16)).toBeCloseTo(15.75); expect(getLoopPositionPercent(4, 16)).toBe(25); }); it('adds a sound at the next empty step', () => { const beats: BeatPoint[] = [ { id: 'first', soundId: 'kick', letter: 'a', step: 0 }, { id: 'second', soundId: 'hi-hat', letter: 'b', step: 1 }, ]; expect(getNextAvailableStep(beats)).toBe(2); }); it('reuses the first step when every step is occupied', () => { const beats: BeatPoint[] = Array.from({ length: TOTAL_STEPS }, (_, step) => ({ id: `beat-${step}`, soundId: 'kick', letter: getAlphabeticLabel(step).toLowerCase(), step, })); expect(getNextAvailableStep(beats)).toBe(0); }); it('reuses the first step when a three-beat bar is full', () => { const totalSteps = getTotalSteps(3); const beats: BeatPoint[] = Array.from({ length: totalSteps }, (_, step) => ({ id: `beat-${step}`, soundId: 'kick', letter: getAlphabeticLabel(step).toLowerCase(), step, })); expect(getNextAvailableStep(beats, totalSteps)).toBe(0); }); }); describe('sound colors', () => { it('generates a distinct color for each added sound', () => { const colors: string[] = []; for (let soundIndex = 0; soundIndex < 8; soundIndex += 1) { const color = generateUniqueSoundColor(colors); expect(colors).not.toContain(color); colors.push(color); } }); }); describe('instrument catalog and pitch', () => { it('places every instrument in exactly one category and has a sound voice', () => { const categorizedInstruments = INSTRUMENT_CATEGORIES.flatMap( (category) => category.instrumentIds, ); expect(new Set(categorizedInstruments).size).toBe(INSTRUMENT_CATALOG.length); expect(new Set(INSTRUMENT_CATEGORIES.map((category) => category.letter)).size) .toBe(INSTRUMENT_CATEGORIES.length); INSTRUMENT_CATALOG.forEach((instrument) => { expect(categorizedInstruments).toContain(instrument.id); expect(typeof INSTRUMENT_VOICES[instrument.id]).toBe('function'); }); }); it('keeps pitch within two octaves and names notes from C2 through C6', () => { expect(clampPitchSemitones(MIN_PITCH_SEMITONES - 1)).toBe(MIN_PITCH_SEMITONES); expect(clampPitchSemitones(MAX_PITCH_SEMITONES + 1)).toBe(MAX_PITCH_SEMITONES); expect(clampPitchSemitones(3.7)).toBe(4); expect(getPitchLabel(MIN_PITCH_SEMITONES)).toBe('C2'); expect(getPitchLabel(0)).toBe('C4'); expect(getPitchLabel(MAX_PITCH_SEMITONES)).toBe('C6'); expect(getPitchLabel(0, INSTRUMENT_BY_ID.kick.pitchBaseMidi)).toBe('F3'); }); it('assigns beat letters within their category and supports labels beyond Z', () => { const beats: BeatPoint[] = [ { id: 'kick-1', soundId: 'kick', letter: 'a', step: 0 }, { id: 'snare-1', soundId: 'snare', letter: 'b', step: 4 }, { id: 'piano-1', soundId: 'piano', letter: 'a', step: 8 }, ]; expect(getNextBeatLetter(beats, 'drums')).toBe('c'); expect(getNextBeatLetter(beats, 'keys')).toBe('b'); expect(getAlphabeticLabel(26)).toBe('AA'); expect(INSTRUMENT_CATEGORY_BY_ID.drums.letter).toBe('A'); }); }); describe('sequencer state transitions', () => { it('loads a template and removes every beat in the focused sound ring', () => { const templateBeats: BeatPoint[] = [ { id: 'kick-1', soundId: 'kick', letter: 'a', step: 0 }, { id: 'snare-1', soundId: 'snare', letter: 'b', step: 4 }, { id: 'piano-1', soundId: 'piano', letter: 'a', step: 8 }, ]; const loaded = sequencerReducer(createInitialSequencerState(), { type: 'template-loaded', beats: templateBeats, tempoBpm: 120, beatsPerBar: 4, }); expect(loaded.selectedBeatId).toBe('kick-1'); expect(loaded.focusedSoundId).toBe('kick'); const afterRingRemoval = sequencerReducer(loaded, { type: 'focused-sound-removed' }); expect(afterRingRemoval.beats).toEqual([templateBeats[1], templateBeats[2]]); expect(afterRingRemoval.selectedBeatId).toBeNull(); expect(afterRingRemoval.focusedSoundId).toBe('snare'); }); it('clears out-of-range selections when the time signature gets shorter', () => { const beats: BeatPoint[] = [ { id: 'hat-1', soundId: 'hi-hat', letter: 'a', step: 0 }, { id: 'kick-1', soundId: 'kick', letter: 'b', step: 12 }, ]; const loaded = sequencerReducer(createInitialSequencerState(), { type: 'template-loaded', beats, tempoBpm: 120, beatsPerBar: 4, }); const selected = sequencerReducer(loaded, { type: 'beat-selected', beatId: 'kick-1' }); const shortened = sequencerReducer(selected, { type: 'time-signature-changed', beatsPerBar: 2, }); expect(shortened.beats).toEqual(beats); expect(shortened.selectedBeatId).toBeNull(); expect(shortened.focusedSoundId).toBe('hi-hat'); }); it('removes one sound ring without removing other sounds in the same category', () => { const beats: BeatPoint[] = [ { id: 'kick-1', soundId: 'kick', letter: 'a', step: 0 }, { id: 'hat-1', soundId: 'hi-hat', letter: 'b', step: 4 }, { id: 'bass-1', soundId: 'bass', letter: 'a', step: 8 }, { id: 'bass-2', soundId: 'bass', letter: 'b', step: 12 }, ]; const loaded = sequencerReducer(createInitialSequencerState(), { type: 'template-loaded', beats, tempoBpm: TEMPO_BPM, beatsPerBar: 4, }); const bassSelected = sequencerReducer(loaded, { type: 'sound-selected', soundId: 'bass', }); const removed = sequencerReducer(bassSelected, { type: 'focused-sound-removed' }); expect(removed.beats).toEqual([beats[0], beats[1]]); expect(removed.focusedSoundId).toBe('kick'); expect(removed.selectedBeatId).toBeNull(); }); it('clamps tempo changes to the supported range', () => { const initial = createInitialSequencerState(); const tooSlow = sequencerReducer(initial, { type: 'tempo-changed', tempoBpm: 10 }); const tooFast = sequencerReducer(initial, { type: 'tempo-changed', tempoBpm: 300 }); expect(MIN_TEMPO_BPM).toBe(30); expect(MAX_TEMPO_BPM).toBe(260); expect(tooSlow.tempoBpm).toBe(MIN_TEMPO_BPM); expect(tooFast.tempoBpm).toBe(MAX_TEMPO_BPM); }); it('changes pitch on one selected beat and clamps it to the supported range', () => { const beats: BeatPoint[] = [ { id: 'bass-1', soundId: 'bass', letter: 'a', step: 0, pitchSemitones: 0 }, { id: 'piano-1', soundId: 'piano', letter: 'a', step: 4, pitchSemitones: 0 }, ]; const loaded = sequencerReducer(createInitialSequencerState(), { type: 'template-loaded', beats, tempoBpm: TEMPO_BPM, beatsPerBar: 4, }); const changed = sequencerReducer(loaded, { type: 'beat-pitch-changed', beatId: 'bass-1', pitchSemitones: MAX_PITCH_SEMITONES + 12, }); expect(changed.beats[0].pitchSemitones).toBe(MAX_PITCH_SEMITONES); expect(changed.beats[1].pitchSemitones).toBe(0); }); }); describe('dance templates', () => { it('keeps each beat inside its template time signature', () => { BEAT_TEMPLATES.forEach((template) => { const totalSteps = getTotalSteps(template.beatsPerBar); expect(template.tempoBpm).toBeGreaterThanOrEqual(MIN_TEMPO_BPM); expect(template.tempoBpm).toBeLessThanOrEqual(MAX_TEMPO_BPM); template.beats.forEach((beat) => { expect(beat.step).toBeGreaterThanOrEqual(0); expect(beat.step).toBeLessThan(totalSteps); }); }); }); });