// MIDIMapMigrationTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation import LyrebirdSynth @testable import LyrebirdShoeGaze @MainActor @Suite("Legacy MIDI map migration") struct MIDIMapMigrationTests { /// Two boards; board 0 has two pedals, board 1 has one — with fixed ids to assert against. private func sampleBoards() -> [BoardState] { let od: PedalState = PedalState(type: "Overdrive", knobValues: [], bypass: 0) let rv: PedalState = PedalState(type: "Reverb", knobValues: [], bypass: 0) let dl: PedalState = PedalState(type: "Delay", knobValues: [], bypass: 0) return [ BoardState(label: "Board 1", pedals: [od, rv]), BoardState(label: "Board 2", pedals: [dl]), ] } // MARK: - Unit: positional → id conversion @Test func migratesPositionalBindingsToIds() { let boards: [BoardState] = sampleBoards() let legacy: LegacyMIDIMap = LegacyMIDIMap( switches: [ LegacyMIDIMap.LegacySwitchBinding(program: 1, channel: nil, action: .rigBypass, displayNameHint: nil), LegacyMIDIMap.LegacySwitchBinding(program: 2, channel: nil, action: .boardBypass(boardIndex: 1), displayNameHint: "Board 2"), LegacyMIDIMap.LegacySwitchBinding(program: 3, channel: nil, action: .pedalBypass(boardIndex: 0, pedalIndex: 1), displayNameHint: "Reverb"), ], ccs: [ LegacyMIDIMap.LegacyCCBinding(controller: 7, channel: nil, target: .pedalControl(boardIndex: 0, pedalIndex: 0, controlIndex: 2, pedalNameHint: "Overdrive", controlLabelHint: "Level"), inverted: false), LegacyMIDIMap.LegacyCCBinding(controller: 11, channel: nil, target: .boardLevel(boardIndex: 1, boardLabelHint: "Board 2"), inverted: false), ] ) let migrated: MIDIMap = legacy.migrated(boards: boards) #expect(migrated.switches[0].action == .rigBypass) #expect(migrated.switches[1].action == .boardBypass(boardID: boards[1].id)) #expect(migrated.switches[2].action == .pedalBypass(pedalID: boards[0].pedals[1].id)) #expect(migrated.ccs[0].target == .pedalControl( pedalID: boards[0].pedals[0].id, controlIndex: 2, pedalNameHint: "Overdrive", controlLabelHint: "Level")) #expect(migrated.ccs[1].target == .boardLevel(boardID: boards[1].id, boardLabelHint: "Board 2")) } @Test func dropsOutOfRangeBindings() { let boards: [BoardState] = sampleBoards() let legacy: LegacyMIDIMap = LegacyMIDIMap( switches: [ LegacyMIDIMap.LegacySwitchBinding(program: 1, channel: nil, action: .pedalBypass(boardIndex: 0, pedalIndex: 9), displayNameHint: nil), LegacyMIDIMap.LegacySwitchBinding(program: 2, channel: nil, action: .boardSolo(boardIndex: 5), displayNameHint: nil), ], ccs: [] ) let migrated: MIDIMap = legacy.migrated(boards: boards) #expect(migrated.switches.isEmpty, "bindings addressing a non-existent board/pedal are dropped") } // MARK: - Integration: legacy JSON decodes through ShoeGazeState @Test func legacyFormatJSONDecodesAndMigrates() throws { let boards: [BoardState] = sampleBoards() let legacy: LegacyMIDIMap = LegacyMIDIMap( switches: [ LegacyMIDIMap.LegacySwitchBinding(program: 3, channel: 0, action: .pedalBypass(boardIndex: 0, pedalIndex: 1), displayNameHint: "Reverb"), ], ccs: [] ) // Build the raw rig JSON with the *legacy* midiMap encoding spliced in — exactly what an // old saved rig on disk looks like (boards may or may not carry ids; midiMap is positional). var dict: [String: Any] = [ "version": presetFormatVersion, "boards": try jsonObject(boards), "midiMap": try jsonObject(legacy), ] // Sanity: the spliced midiMap is the positional shape, not the id shape. #expect(((dict["midiMap"] as? [String: Any])?["switches"] as? [Any])?.isEmpty == false) let data: Data = try JSONSerialization.data(withJSONObject: dict) let state: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data) let map: MIDIMap = try #require(state.midiMap, "legacy midiMap should migrate, not drop to nil") #expect(map.switches.count == 1) // The board ids in `state.boards` are what migration used — assert the binding points at // the Reverb (board 0, pedal 1) by that board's decoded pedal id. #expect(map.switches[0].action == .pedalBypass(pedalID: state.boards[0].pedals[1].id)) _ = dict.removeValue(forKey: "midiMap") } /// Encodes a `Codable` value and re-reads it as a JSON object for splicing. private func jsonObject(_ value: T) throws -> Any { let data: Data = try JSONEncoder().encode(value) return try JSONSerialization.jsonObject(with: data) } }