445 lines
19 KiB
Swift
445 lines
19 KiB
Swift
// BoardVersionTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
@testable import LyrebirdShoeGaze
|
|
|
|
@MainActor
|
|
@Suite("BoardVersion — save/load and rebuild")
|
|
struct BoardVersionTests {
|
|
|
|
// MARK: - Helpers
|
|
|
|
/// Creates a two-board test rig.
|
|
///
|
|
/// Board 0 (index 0): Overdrive → Delay
|
|
/// Board 1 (index 1): Reverb
|
|
///
|
|
/// - Returns: `(ShoeGaze, SynthesisEngine, URL)` where `URL` is the NRT audio output
|
|
/// file that must be deleted by the caller via `defer`.
|
|
private func makeTwoBoardRig() throws -> (ShoeGaze, SynthesisEngine, URL) {
|
|
let renderDuration: Double = 0.1
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: renderDuration,
|
|
numAudioBuses: 128
|
|
)
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
|
|
let board0: PedalBoard = try sg.addBoard()
|
|
try board0.add(OverdrivePedal())
|
|
try board0.add(DelayPedal())
|
|
try sg.finalizeBoard(board0)
|
|
|
|
let board1: PedalBoard = try sg.addBoard()
|
|
try board1.add(ReverbPedal())
|
|
try sg.finalizeBoard(board1)
|
|
|
|
return (sg, engine, url)
|
|
}
|
|
|
|
// MARK: - PedalBoard.captureState
|
|
|
|
@Test func captureStateReflectsCurrentPedalTypesAndLabel() throws {
|
|
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
let state: BoardState = sg.boards[0].captureState()
|
|
#expect(state.label == "Board 1", "Label should be 'Board 1'")
|
|
#expect(state.pedals.count == 2, "Board 0 should have 2 pedals")
|
|
#expect(state.pedals[0].type == "Overdrive")
|
|
#expect(state.pedals[1].type == "Delay")
|
|
}
|
|
|
|
@Test func captureStateReflectsCurrentKnobValues() throws {
|
|
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
guard let overdrive: OverdrivePedal = sg.boards[0].pedals[0] as? OverdrivePedal else {
|
|
Issue.record("Expected OverdrivePedal at index 0")
|
|
return
|
|
}
|
|
let knownDrive: Double = 18.5
|
|
overdrive.drive = knownDrive
|
|
|
|
let state: BoardState = sg.boards[0].captureState()
|
|
// drive is effectKnobs[0] → knobValues[0]
|
|
let capturedDrive: Double = state.pedals[0].knobValues[0]
|
|
#expect(abs(capturedDrive - knownDrive) < 1e-9,
|
|
"Captured drive should be \(knownDrive), got \(capturedDrive)")
|
|
}
|
|
|
|
// MARK: - rebuildBoard(_:from:)
|
|
|
|
@Test func rebuildBoardReplacesPedalsFromState() throws {
|
|
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
let board0: PedalBoard = sg.boards[0]
|
|
// Build a state that has only a Reverb pedal.
|
|
let newState: BoardState = BoardState(
|
|
label: "Rebuilt Board",
|
|
pedals: [
|
|
PedalState(type: "Reverb", knobValues: [2.5, 0.3, 1.0, 0.9], bypass: 0.0),
|
|
]
|
|
)
|
|
|
|
let rebuilt: PedalBoard = try sg.rebuildBoard(board0, from: newState)
|
|
#expect(rebuilt.pedals.count == 1, "Rebuilt board should have 1 pedal")
|
|
#expect(rebuilt.pedals[0].displayName == "Reverb",
|
|
"Rebuilt board's only pedal should be Reverb")
|
|
#expect(rebuilt.label == "Rebuilt Board")
|
|
}
|
|
|
|
@Test func rebuildBoardPreservesIndexInBoards() throws {
|
|
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
// board0 is at index 0; board1 is at index 1.
|
|
let board0: PedalBoard = sg.boards[0]
|
|
let newState: BoardState = BoardState(
|
|
label: "Replaced Board 1",
|
|
pedals: [
|
|
PedalState(type: "Fuzz", knobValues: [0.05, 3500.0, 1.0, 1.0], bypass: 0.0),
|
|
]
|
|
)
|
|
|
|
let rebuilt: PedalBoard = try sg.rebuildBoard(board0, from: newState)
|
|
// After rebuild, boards should still have 2 entries.
|
|
#expect(sg.boards.count == 2, "boards count should remain 2")
|
|
// The rebuilt board must occupy index 0.
|
|
#expect(sg.boards[0] === rebuilt, "Rebuilt board should be at index 0")
|
|
}
|
|
|
|
@Test func rebuildBoardSkipsUnknownPedalTypes() throws {
|
|
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
let board0: PedalBoard = sg.boards[0]
|
|
let newState: BoardState = BoardState(
|
|
label: "Mixed Board",
|
|
pedals: [
|
|
PedalState(type: "Overdrive", knobValues: [4.0, 4000.0, 1.0, 1.0], bypass: 0.0),
|
|
PedalState(type: "GhostPedal", knobValues: [], bypass: 0.0),
|
|
PedalState(type: "Fuzz", knobValues: [0.05, 3500.0, 1.0, 1.0], bypass: 0.0),
|
|
]
|
|
)
|
|
|
|
let rebuilt: PedalBoard = try sg.rebuildBoard(board0, from: newState)
|
|
// "GhostPedal" is unknown and must be skipped; only Overdrive and Fuzz should load.
|
|
#expect(rebuilt.pedals.count == 2, "Unknown pedal type should be skipped")
|
|
#expect(rebuilt.pedals[0].displayName == "Overdrive")
|
|
#expect(rebuilt.pedals[1].displayName == "Fuzz")
|
|
}
|
|
|
|
@Test func rebuildBoardRendersNonSilentAfterEngineStart() throws {
|
|
let renderDuration: Double = 0.2
|
|
let (engine, audioURL): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: renderDuration,
|
|
numAudioBuses: 128
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: audioURL) }
|
|
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
let board: PedalBoard = try sg.addBoard()
|
|
try board.add(OverdrivePedal())
|
|
try sg.finalizeBoard(board)
|
|
|
|
let newState: BoardState = BoardState(
|
|
label: "After Rebuild",
|
|
pedals: [
|
|
PedalState(type: "Reverb", knobValues: [2.5, 0.3, 1.0, 0.9], bypass: 0.0),
|
|
]
|
|
)
|
|
|
|
// Rebuild before engine.start() — the canonical "engine stopped" path.
|
|
let rebuilt: PedalBoard = try sg.rebuildBoard(board, from: newState)
|
|
#expect(sg.boards.count == 1)
|
|
#expect(sg.boards[0] === rebuilt)
|
|
|
|
try engine.start()
|
|
defer { engine.stop() }
|
|
|
|
// The test loop source feeds signal; the reverb+master chain should produce output.
|
|
let samples: [Float] = readStereoCAF(url: audioURL, sampleRate: engine.sampleRate)
|
|
#expect(hasNonSilentOutput(samples), "Rebuilt board should produce non-silent output")
|
|
}
|
|
|
|
// MARK: - BoardVersionStore.capture / disk round-trip
|
|
|
|
@Test func versionStoreRoundTrip() throws {
|
|
let (sg, _, audioURL): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: audioURL) }
|
|
|
|
let store: BoardVersionStore = BoardVersionStore()
|
|
store.capture(sg.boards[0], name: "Test Capture")
|
|
#expect(store.versions.count == 1)
|
|
|
|
let tempDir: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
|
defer { try? FileManager.default.removeItem(atPath: tempDir.path) }
|
|
|
|
try store.writeAllToDisk(in: tempDir)
|
|
|
|
// Load into a fresh store.
|
|
let freshStore: BoardVersionStore = BoardVersionStore()
|
|
try freshStore.loadAllFromDisk(in: tempDir)
|
|
|
|
#expect(freshStore.versions.count == 1, "Fresh store should contain one version")
|
|
let original: BoardVersion = store.versions[0]
|
|
let loaded: BoardVersion = try #require(freshStore.versions.first)
|
|
#expect(loaded == original, "Loaded version should equal the saved version")
|
|
}
|
|
|
|
@Test func versionStoreRoundTripPreservesMultipleVersions() throws {
|
|
let (sg, _, audioURL): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: audioURL) }
|
|
|
|
let store: BoardVersionStore = BoardVersionStore()
|
|
store.capture(sg.boards[0], name: "Version A")
|
|
store.capture(sg.boards[1], name: "Version B")
|
|
#expect(store.versions.count == 2)
|
|
|
|
let tempDir: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
|
defer { try? FileManager.default.removeItem(atPath: tempDir.path) }
|
|
|
|
try store.writeAllToDisk(in: tempDir)
|
|
|
|
let freshStore: BoardVersionStore = BoardVersionStore()
|
|
try freshStore.loadAllFromDisk(in: tempDir)
|
|
|
|
#expect(freshStore.versions.count == 2, "Should load both versions")
|
|
// Loaded versions are sorted by savedAt; original store insertion order may differ.
|
|
// Compare by id membership rather than order.
|
|
let originalIDs: Set<UUID> = Set(store.versions.map { $0.id })
|
|
let loadedIDs: Set<UUID> = Set(freshStore.versions.map { $0.id })
|
|
#expect(originalIDs == loadedIDs, "All version IDs should survive round-trip")
|
|
}
|
|
|
|
// MARK: - Knob value restoration via rebuildBoard
|
|
|
|
@Test func rebuildBoardRestoresKnobValues() throws {
|
|
let renderDuration: Double = 0.1
|
|
let (engine, audioURL): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: renderDuration,
|
|
numAudioBuses: 128
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: audioURL) }
|
|
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
let board: PedalBoard = try sg.addBoard()
|
|
let overdrive: OverdrivePedal = OverdrivePedal()
|
|
try board.add(overdrive)
|
|
try sg.finalizeBoard(board)
|
|
|
|
// Set a known drive value.
|
|
let knownDrive: Double = 15.0
|
|
overdrive.drive = knownDrive
|
|
|
|
// Capture the state at this point via BoardVersionStore.
|
|
let store: BoardVersionStore = BoardVersionStore()
|
|
store.capture(board, name: "Known Drive State")
|
|
let version: BoardVersion = try #require(store.versions.first)
|
|
|
|
// Mutate the knob value.
|
|
overdrive.drive = 1.0
|
|
#expect(abs(overdrive.drive - 1.0) < 1e-9)
|
|
|
|
// Rebuild from the saved version state.
|
|
let rebuilt: PedalBoard = try sg.rebuildBoard(sg.boards[0], from: version.state)
|
|
|
|
guard let restoredOverdrive: OverdrivePedal = rebuilt.pedals[0] as? OverdrivePedal else {
|
|
Issue.record("Expected OverdrivePedal after rebuildBoard")
|
|
return
|
|
}
|
|
// drive is effectKnobs[0] → knobValues[0]; applyState restores it via UIParamNode.update.
|
|
#expect(
|
|
abs(restoredOverdrive.drive - knownDrive) < 1e-9,
|
|
"drive should be \(knownDrive) after rebuild, got \(restoredOverdrive.drive)"
|
|
)
|
|
}
|
|
|
|
// MARK: - BoardVersionStore.remove
|
|
|
|
@Test func versionStoreRemoveDropsEntryByID() throws {
|
|
let (sg, _, audioURL): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: audioURL) }
|
|
|
|
let store: BoardVersionStore = BoardVersionStore()
|
|
store.capture(sg.boards[0], name: "A")
|
|
store.capture(sg.boards[1], name: "B")
|
|
#expect(store.versions.count == 2)
|
|
|
|
let toRemove: BoardVersion = try #require(store.versions.first)
|
|
store.remove(toRemove)
|
|
|
|
#expect(store.versions.count == 1, "Store should have 1 version after remove")
|
|
#expect(store.versions.first?.id != toRemove.id, "Removed version should not be present")
|
|
}
|
|
|
|
// MARK: - BoardVersionStore.loadAllFromDisk skips bad files
|
|
|
|
@Test func loadAllFromDiskSkipsCorruptFiles() throws {
|
|
let (sg, _, audioURL): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: audioURL) }
|
|
|
|
let tempDir: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
|
defer { try? FileManager.default.removeItem(atPath: tempDir.path) }
|
|
|
|
// Capture one real version using the store's public API (captures sg.boards[0]).
|
|
// `writeToDisk` uses secondsSince1970 encoding, which `loadAllFromDisk` expects.
|
|
let writeStore: BoardVersionStore = BoardVersionStore()
|
|
writeStore.capture(sg.boards[0], name: "Valid")
|
|
let validID: UUID = try #require(writeStore.versions.first?.id)
|
|
try writeStore.writeAllToDisk(in: tempDir)
|
|
|
|
// Write one corrupt JSON file into the same directory.
|
|
let corruptURL: URL = tempDir.appendingPathComponent("corrupt.json")
|
|
let corruptData: Data = Data("not valid json at all {{{".utf8)
|
|
try corruptData.write(to: corruptURL, options: .atomic)
|
|
|
|
// Write a non-JSON file that should be ignored by extension filter.
|
|
let ignoredURL: URL = tempDir.appendingPathComponent("readme.txt")
|
|
let ignoredData: Data = Data("this is not JSON".utf8)
|
|
try ignoredData.write(to: ignoredURL, options: .atomic)
|
|
|
|
let store: BoardVersionStore = BoardVersionStore()
|
|
try store.loadAllFromDisk(in: tempDir)
|
|
|
|
#expect(store.versions.count == 1, "Only the valid JSON should be loaded")
|
|
#expect(store.versions.first?.id == validID)
|
|
}
|
|
|
|
// MARK: - rebuildBoard — non-destructive on failure
|
|
|
|
/// Verifies that a successful `rebuildBoard` keeps `boards.count` stable and places
|
|
/// the rebuilt board at the correct index. Also verifies that rebuilding an unmanaged
|
|
/// board (not in `boards`) appends the new board without disturbing the managed list.
|
|
@Test func rebuildBoardPreservesCountAndIndexOnSuccess() throws {
|
|
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
// Precondition: 2 boards at indices 0 and 1.
|
|
#expect(sg.boards.count == 2)
|
|
let originalBoard1: PedalBoard = sg.boards[1]
|
|
|
|
// Rebuild board at index 0 with a single Compressor.
|
|
let board0: PedalBoard = sg.boards[0]
|
|
let state: BoardState = BoardState(
|
|
label: "Compressor Only",
|
|
pedals: [PedalState(type: "Compressor", knobValues: [0.5, 0.7, 2.0, 1.0], bypass: 0.0)]
|
|
)
|
|
let rebuilt: PedalBoard = try sg.rebuildBoard(board0, from: state)
|
|
|
|
// Count must be unchanged.
|
|
#expect(sg.boards.count == 2, "boards.count should remain 2 after successful rebuild")
|
|
// Rebuilt board occupies the original slot.
|
|
#expect(sg.boards[0] === rebuilt, "Rebuilt board should be at index 0")
|
|
// The other board is undisturbed.
|
|
#expect(sg.boards[1] === originalBoard1, "Board at index 1 should be unchanged")
|
|
// Content is correct.
|
|
#expect(rebuilt.pedals.count == 1)
|
|
#expect(rebuilt.pedals[0].displayName == "Compressor")
|
|
}
|
|
|
|
/// Verifies that when `rebuildBoard` is called with a board that is NOT in `boards`
|
|
/// (an unmanaged board), the managed list grows by one and the existing boards are
|
|
/// undisturbed.
|
|
@Test func rebuildBoardUnmanagedBoardAppendsWithoutCorruption() throws {
|
|
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
// Create a board that we do NOT pass through addBoard on this sg instance.
|
|
// We simulate an unmanaged board by capturing a reference to the second board
|
|
// and then removing it manually, leaving us with only board 0 managed.
|
|
let unmanagedBoard: PedalBoard = sg.boards[1]
|
|
sg.removeBoard(unmanagedBoard)
|
|
#expect(sg.boards.count == 1, "Should have 1 managed board after manual remove")
|
|
|
|
let state: BoardState = BoardState(
|
|
label: "Unmanaged Rebuild",
|
|
pedals: [PedalState(type: "Tremolo", knobValues: [0.4, 1.0, 0.5, 1.0], bypass: 0.0)]
|
|
)
|
|
|
|
// Pass the removed (now unmanaged) board to rebuildBoard.
|
|
let rebuilt: PedalBoard = try sg.rebuildBoard(unmanagedBoard, from: state)
|
|
|
|
// An unmanaged board has no oldIndex, so the new board is simply appended.
|
|
#expect(sg.boards.count == 2, "Unmanaged rebuild should append (count goes to 2)")
|
|
#expect(sg.boards.last === rebuilt, "Rebuilt board should be the last element")
|
|
#expect(rebuilt.pedals.count == 1)
|
|
#expect(rebuilt.pedals[0].displayName == "Tremolo")
|
|
// The original managed board is undisturbed.
|
|
#expect(sg.boards[0] !== rebuilt, "The original board at index 0 should be unchanged")
|
|
}
|
|
|
|
// MARK: - BoardVersionStore.deleteFromDisk
|
|
|
|
@Test func deleteFromDiskRemovesFile() throws {
|
|
let (sg, _, audioURL): (ShoeGaze, SynthesisEngine, URL) = try makeTwoBoardRig()
|
|
defer { try? FileManager.default.removeItem(at: audioURL) }
|
|
|
|
let tempDir: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
|
defer { try? FileManager.default.removeItem(atPath: tempDir.path) }
|
|
|
|
// Capture a version and write it to disk.
|
|
let store: BoardVersionStore = BoardVersionStore()
|
|
store.capture(sg.boards[0], name: "To Delete")
|
|
let capturedVersion: BoardVersion = try #require(store.versions.first)
|
|
try store.writeAllToDisk(in: tempDir)
|
|
|
|
// Confirm the file is present on disk.
|
|
let freshStore1: BoardVersionStore = BoardVersionStore()
|
|
try freshStore1.loadAllFromDisk(in: tempDir)
|
|
#expect(freshStore1.versions.count == 1, "File should be present before delete")
|
|
|
|
// Delete from disk and remove from memory.
|
|
try store.deleteFromDisk(capturedVersion, in: tempDir)
|
|
store.remove(capturedVersion)
|
|
#expect(store.versions.isEmpty, "In-memory store should be empty after remove")
|
|
|
|
// Reload — the deleted file must not appear.
|
|
let freshStore2: BoardVersionStore = BoardVersionStore()
|
|
try freshStore2.loadAllFromDisk(in: tempDir)
|
|
#expect(freshStore2.versions.isEmpty, "Deleted version should not appear after reload")
|
|
}
|
|
|
|
/// `deleteFromDisk` is a no-op when the file is already absent — must not throw.
|
|
@Test func deleteFromDiskIsNoOpForMissingFile() throws {
|
|
let tempDir: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
|
defer { try? FileManager.default.removeItem(atPath: tempDir.path) }
|
|
|
|
// Create a version but do NOT write it to disk.
|
|
let store: BoardVersionStore = BoardVersionStore()
|
|
let phantom: BoardVersion = BoardVersion(
|
|
id: UUID(),
|
|
name: "Never Written",
|
|
savedAt: Date(),
|
|
state: BoardState(label: "Empty", pedals: [])
|
|
)
|
|
|
|
// Should not throw even though the file was never written.
|
|
#expect(throws: Never.self) {
|
|
try store.deleteFromDisk(phantom, in: tempDir)
|
|
}
|
|
}
|
|
|
|
// MARK: - BoardVersionStore.defaultDirectory
|
|
|
|
@Test func defaultDirectoryIsNonEmpty() {
|
|
let dir: URL = BoardVersionStore.defaultDirectory()
|
|
#expect(!dir.path.isEmpty, "Default directory path should be non-empty")
|
|
#expect(dir.lastPathComponent == "ShoeGazeVersions",
|
|
"Default directory last component should be 'ShoeGazeVersions'")
|
|
}
|
|
}
|