// MIDIMapCodableTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable import LyrebirdShoeGaze @MainActor @Suite("MIDIMap persistence") struct MIDIMapCodableTests { private func sampleMap() -> MIDIMap { MIDIMap( switches: [ MIDIMap.SwitchBinding(program: 3, channel: 0, action: .rigBypass, displayNameHint: "Rig"), MIDIMap.SwitchBinding(program: 4, action: .boardSolo(boardIndex: 1), displayNameHint: "Board 2"), ], ccs: [ MIDIMap.CCBinding( controller: 7, channel: 0, target: MIDIMap.CCTarget( boardIndex: 0, pedalIndex: 1, knobIndex: 0, pedalNameHint: "Volume", knobLabelHint: "Position" ) ), ] ) } @Test func mapRoundTrips() throws { let map: MIDIMap = sampleMap() let data: Data = try JSONEncoder().encode(map) let decoded: MIDIMap = try JSONDecoder().decode(MIDIMap.self, from: data) #expect(decoded == map) } @Test func rigStateEmbedsAndRestoresMap() throws { let state: ShoeGazeState = ShoeGazeState(boards: [], midiMap: sampleMap()) let data: Data = try JSONEncoder().encode(state) let decoded: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data) #expect(decoded.midiMap == sampleMap()) } @Test func oldRigWithoutMapDecodesToNil() throws { // A rig saved before MIDI mapping existed has no `midiMap` key at all. let legacyJSON: String = #"{"version":1,"boards":[]}"# let data: Data = Data(legacyJSON.utf8) let decoded: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data) #expect(decoded.midiMap == nil, "legacy rig JSON should decode with a nil midiMap") } }