// FactoryRigLibraryTests.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("FactoryRigLibrary") struct FactoryRigLibraryTests { // MARK: - Catalog integrity @Test func catalogIsWellFormed() { let rigs: [FactoryRig] = FactoryRigLibrary.all #expect(rigs.count >= 6, "expected a full starter slate") let ids: Set = Set(rigs.map { $0.id }) #expect(ids.count == rigs.count, "rig ids must be unique") let names: Set = Set(rigs.map { $0.name }) #expect(names.count == rigs.count, "rig names must be unique") let minBlurbLength: Int = 20 for rig: FactoryRig in rigs { #expect(!rig.name.isEmpty, "\(rig.name) has empty name") #expect(rig.blurb.count >= minBlurbLength, "\(rig.name) blurb too short") } // The slate should span more than one sonic family. let categories: Set = Set(rigs.map { $0.category }) #expect(categories.count >= 3, "starter slate should cover several categories") } @Test func blurbsAvoidTrademarkedProductNames() { // Guard against a blurb accidentally naming a real amp/pedal brand: factory rigs describe // the sound, never a trademarked product. let forbidden: [String] = [ "marshall", "fender", "vox", "mesa", "boss", "orange", "dumble", "roland", "ibanez", ] for rig: FactoryRig in FactoryRigLibrary.all { let haystack: String = (rig.name + " " + rig.blurb).lowercased() for brand: String in forbidden { #expect(!haystack.contains(brand), "\(rig.name) mentions '\(brand)'") } } } @Test func factoryDetectionRecognisesEveryRigAndRejectsOthers() { let rigs: [FactoryRig] = FactoryRigLibrary.all #expect(FactoryRigLibrary.factoryIDs.count == rigs.count) for rig: FactoryRig in rigs { #expect(FactoryRigLibrary.isFactory(rig.id), "\(rig.name) not recognised as factory") } // A freshly generated id is not a factory rig. #expect(!FactoryRigLibrary.isFactory(UUID())) } @Test func categoryLookupAndGroupingCoverEveryRigOnce() { for rig: FactoryRig in FactoryRigLibrary.all { #expect(FactoryRigLibrary.category(for: rig.id) == rig.category, "\(rig.name) category lookup mismatch") } #expect(FactoryRigLibrary.category(for: UUID()) == nil, "a random id has no category") // Each category grouping contains only its own rigs, in catalog order. for category: FactoryRigCategory in FactoryRigCategory.allCases { let rigs: [FactoryRig] = FactoryRigLibrary.rigs(in: category) #expect(rigs.allSatisfy { $0.category == category }) } // Grouping by every category partitions the catalog exactly (no rig lost or duplicated). let grouped: [FactoryRig] = FactoryRigCategory.allCases.flatMap { FactoryRigLibrary.rigs(in: $0) } #expect(grouped.count == FactoryRigLibrary.all.count, "grouping dropped or duplicated a rig") } // MARK: - Seeding entries @Test func makeEntriesMatchesCatalogAndIsStable() throws { let entries: [RigEntry] = try FactoryRigLibrary.makeEntries() let rigs: [FactoryRig] = FactoryRigLibrary.all #expect(entries.count == rigs.count, "one entry per rig") for (entry, rig): (RigEntry, FactoryRig) in zip(entries, rigs) { #expect(entry.id == rig.id, "entry id must be the rig's stable id") #expect(entry.name == rig.name) // A rig builds either plain boards or a router (or both) — never nothing. let routerStates: [ChainRouterState] = entry.state.routers ?? [] #expect(!entry.state.boards.isEmpty || !routerStates.isEmpty, "\(rig.name) built nothing") let boardPedals: Int = entry.state.boards.reduce(0) { $0 + $1.pedals.count } let routerPedals: Int = routerStates.reduce(0) { $0 + $1.boardA.count + $1.boardB.count } #expect(boardPedals + routerPedals >= 3, "\(rig.name) should build a non-trivial chain") } // Stable ids: re-seeding yields identical ids (so a re-seed overwrites in place). let again: [RigEntry] = try FactoryRigLibrary.makeEntries() #expect(entries.map { $0.id } == again.map { $0.id }) } @Test func entriesRoundTripThroughJSON() throws { let encoder: JSONEncoder = JSONEncoder() let decoder: JSONDecoder = JSONDecoder() for entry: RigEntry in try FactoryRigLibrary.makeEntries() { let data: Data = try encoder.encode(entry) let decoded: RigEntry = try decoder.decode(RigEntry.self, from: data) #expect(decoded == entry, "\(entry.name) did not survive a JSON round-trip") } } // MARK: - Load + render (full ship path) @Test func everyFactoryEntryLoadsAndRenders() throws { let renderDuration: Double = 0.2 let numAudioBuses: Int = 128 for entry: RigEntry in try FactoryRigLibrary.makeEntries() { let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let session: ShoeGaze = ShoeGaze(engine: engine) try session.loadRig(from: entry.state) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(hasNonSilentOutput(samples), "\(entry.name) rendered silence") } } }