Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/MIDIMapCodableTests.swift
T
josh 1ba622a611 Fix CC section: stop switch actions leaking in; add board-level target
The MIDI Mapping sheet's two sections both keyed their ForEach rows on the
enumeration offset (0,1,2…) and the Expression section mixed a bare header
Text with a ForEach. SwiftUI's List reused structurally identical row views
across the sections, so Program-Change switch rows (e.g. "Board 1 · Solo",
"Board 1 · Overdrive · Bypass") leaked into the Expression (CC) section and
their Learn button armed a Program Change instead of a Control Change.

- Rebuild both sections from precomputed row models with globally-unique,
  section-namespaced string ids and a single ForEach each; no bare headers.
- CC target is now an enum: `.pedalControl` (a knob/slider) or `.boardLevel`
  (the board fader). Board master level is now CC-assignable, shown as
  "<Board> · Level".
- CC rows are labelled "<Pedal> · <Control>" (e.g. "Overdrive · Drive").
- Stale-binding + steal-on-reassign now key off an address matcher on the
  target enum.

Tests updated for the enum target; added a board-level CC test. 319 green;
both app targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 12:25:59 -07:00

61 lines
2.2 KiB
Swift

// 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: .pedalControl(
boardIndex: 0, pedalIndex: 1, controlIndex: 0,
pedalNameHint: "Volume", controlLabelHint: "Position"
)
),
MIDIMap.CCBinding(
controller: 11,
channel: 0,
target: .boardLevel(boardIndex: 0, boardLabelHint: "Board 1")
),
]
)
}
@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")
}
}