Zum Inhalt

apple/PhysarumApp/Tests/AudioConfigImportTests.swift

import XCTest
import PhysarumEngine
@testable import Physarum

final class AudioConfigImportTests: XCTestCase {
    private func state() -> AppState {
        let tmp = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("phys-aci-\(UUID().uuidString)", isDirectory: true)
        return AppState(store: PresetStore(baseDir: tmp), audioStore: AudioPresetStore(baseDir: tmp.appendingPathComponent("audio")))
    }

    func testAudioConfigRoundTrip() throws {
        let cfg = AudioConfig(id: "audio-x", name: "Club",
                              reactive: AudioReactive(enabled: true, gain: 2, autoGain: false, routes: []))
        let data = try JSONEncoder().encode(cfg)
        let decoded = try JSONDecoder().decode(AudioConfig.self, from: data)
        XCTAssertEqual(decoded.id, cfg.id)
        XCTAssertEqual(decoded.name, cfg.name)
        XCTAssertEqual(decoded.reactive.gain, 2)
    }

    func testImportReIdsAndAddsAudioPreset() {
        let s = state()
        let incoming = AudioConfig(id: "some-other-id", name: "Loud",
                                   reactive: AudioReactive(enabled: true, gain: 3, autoGain: true, routes: []))
        let before = s.audioPresets.count
        s.importAudioConfig(incoming)
        XCTAssertEqual(s.audioPresets.count, before + 1)
        let added = s.audioPresets.last!
        XCTAssertNotEqual(added.id, "some-other-id")   // re-id'd
        XCTAssertEqual(added.name, "Loud")
        XCTAssertEqual(added.reactive.gain, 3)
    }

    /// An audio-config JSON missing a required field must throw on decode and not crash.
    func testImportMissingRequiredFieldThrows() throws {
        let cfg = AudioConfig(id: "audio-x", name: "Club",
                              reactive: AudioReactive(enabled: true, gain: 2, autoGain: false, routes: []))
        let full = try JSONEncoder().encode(cfg)
        var object = try XCTUnwrap(JSONSerialization.jsonObject(with: full) as? [String: Any])
        object.removeValue(forKey: "reactive")
        let missing = try JSONSerialization.data(withJSONObject: object)
        XCTAssertThrowsError(try JSONDecoder().decode(AudioConfig.self, from: missing))
    }
}