// MIDIMapReindexTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation import LyrebirdSynth import LyrebirdUI @testable import LyrebirdShoeGaze /// MIDI bindings address pedals/boards by stable UUID, so they follow their target through /// structural edits with no reindexing. These tests assert the identity round-trips through the /// edit API's capture → rebuild, and that a binding resolves to (and only to) its pedal. @MainActor @Suite("MIDIMap identity") struct MIDIMapReindexTests { private func makeRig(_ pedals: [String]) throws -> (ShoeGaze, MIDIController, URL, PedalBoard) { let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256) let sg: ShoeGaze = ShoeGaze(engine: engine) let board: PedalBoard = try sg.addBoard() for type: String in pedals { if let pedal: Pedal = PedalRegistry.makePedal(type: type) { try board.add(pedal) } } try sg.finalizeBoard(board) let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg) return (sg, controller, url, board) } // MARK: - Identity preservation across edits @Test func pedalKeepsIdThroughInsertAndMove() throws { let (sg, _, url, board): (ShoeGaze, MIDIController, URL, PedalBoard) = try makeRig(["Overdrive", "Delay", "Reverb"]) defer { try? FileManager.default.removeItem(at: url) } let reverbID: UUID = board.pedals[2].id // Insert a Fuzz at the front → Reverb slides to index 3 but keeps its id. let afterInsert: PedalBoard = try sg.insertPedal(.fuzz, at: 0, to: board) #expect(afterInsert.pedals.count == 4) #expect(afterInsert.pedals[3].id == reverbID, "Reverb keeps its id after an insert before it") // Move it to the front → still the same id. let afterMove: PedalBoard = try sg.movePedal(from: 3, to: 0, in: afterInsert) #expect(afterMove.pedals[0].id == reverbID, "Reverb keeps its id after a move") } @Test func pedalIdSurvivesSaveLoadRoundTrip() throws { let (sg, _, url, board): (ShoeGaze, MIDIController, URL, PedalBoard) = try makeRig(["Overdrive", "Reverb"]) defer { try? FileManager.default.removeItem(at: url) } let ids: [UUID] = board.pedals.map { $0.id } // Full Codable round-trip (what save/load to disk does). let data: Data = try JSONEncoder().encode(sg.captureState()) let state: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data) let (engine2, url2): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256) defer { try? FileManager.default.removeItem(at: url2) } let sg2: ShoeGaze = ShoeGaze(engine: engine2) try sg2.loadRig(from: state) #expect(sg2.boards.first?.pedals.map { $0.id } == ids, "pedal ids persist across save/load") } @Test func boardKeepsIdThroughReorder() throws { let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256) defer { try? FileManager.default.removeItem(at: url) } let sg: ShoeGaze = ShoeGaze(engine: engine) let b0: PedalBoard = try sg.addBoard(); try sg.finalizeBoard(b0) let b1: PedalBoard = try sg.addBoard(); try sg.finalizeBoard(b1) let id0: UUID = b0.id let id1: UUID = b1.id sg.moveBoard(b1, to: 0) #expect(sg.boards[0].id == id1) #expect(sg.boards[1].id == id0) } // MARK: - Binding follows the pedal (end-to-end via the controller) @Test func bypassBindingFollowsPedalAfterInsert() throws { let (sg, controller, url, board): (ShoeGaze, MIDIController, URL, PedalBoard) = try makeRig(["Overdrive", "Reverb"]) defer { try? FileManager.default.removeItem(at: url) } withExtendedLifetime(sg) { // Map program 5 to the Reverb's bypass (pedal index 1). let reverbID: UUID = board.pedals[1].id controller.armSwitchLearn(.pedalBypass(pedalID: reverbID)) controller.ingestForTesting(.programChange(channel: 0, program: 5)) // learn // Insert a Fuzz at the front — Reverb moves to index 2, keeps its id. let edited: PedalBoard = (try? sg.insertPedal(.fuzz, at: 0, to: board)) ?? board let reverb: Pedal = edited.pedals.first { $0.id == reverbID }! #expect(edited.pedals.firstIndex { $0.id == reverbID } == 2) // Firing the binding must still toggle the Reverb (not whatever is at the old index). #expect(reverb.$bypass.value == 0.0) controller.ingestForTesting(.programChange(channel: 0, program: 5)) #expect(abs(reverb.$bypass.value - 1.0) < 1e-9, "binding follows the pedal, not the index") } } @Test func bindingToRemovedPedalIsInert() throws { let (sg, controller, url, board): (ShoeGaze, MIDIController, URL, PedalBoard) = try makeRig(["Overdrive", "Reverb"]) defer { try? FileManager.default.removeItem(at: url) } withExtendedLifetime(sg) { let overdriveID: UUID = board.pedals[0].id controller.armSwitchLearn(.pedalBypass(pedalID: overdriveID)) controller.ingestForTesting(.programChange(channel: 0, program: 5)) // learn // Remove the mapped Overdrive. Firing the binding must not crash or touch the Reverb. let edited: PedalBoard = (try? sg.removePedal(at: 0, from: board)) ?? board let reverbBypassBefore: Double = edited.pedals[0].$bypass.value controller.ingestForTesting(.programChange(channel: 0, program: 5)) #expect(edited.pedals[0].$bypass.value == reverbBypassBefore, "a binding to a removed pedal is inert — it must not fall through to another pedal") } } @Test func sameTypePedalsBindIndependently() throws { // The case the positional model got wrong: two identical-type pedals, each with its own // binding, resolve independently by id even after reordering. let (sg, controller, url, board): (ShoeGaze, MIDIController, URL, PedalBoard) = try makeRig(["Delay", "Delay"]) defer { try? FileManager.default.removeItem(at: url) } withExtendedLifetime(sg) { let firstDelayID: UUID = board.pedals[0].id controller.armSwitchLearn(.pedalBypass(pedalID: firstDelayID)) controller.ingestForTesting(.programChange(channel: 0, program: 5)) // learn // Swap the two Delays. let edited: PedalBoard = (try? sg.movePedal(from: 0, to: 1, in: board)) ?? board let firstDelay: Pedal = edited.pedals.first { $0.id == firstDelayID }! let otherDelay: Pedal = edited.pedals.first { $0.id != firstDelayID }! controller.ingestForTesting(.programChange(channel: 0, program: 5)) #expect(abs(firstDelay.$bypass.value - 1.0) < 1e-9, "binding drives the originally-mapped Delay") #expect(otherDelay.$bypass.value == 0.0, "the other same-type Delay is untouched") } } }