import XCTest
@testable import MastermindPOCCore

final class AudioModelTests: XCTestCase {
    func testPCMFrameStoresSidecarCompatibleAudioMetadata() {
        let data = Data([0x00, 0x00, 0xff, 0x7f])
        let timestamp = Date(timeIntervalSince1970: 42)

        let frame = PCMFrame(
            source: .microphone,
            sampleRate: 16_000,
            channels: 1,
            pcmS16LE: data,
            timestamp: timestamp
        )

        XCTAssertEqual(frame.source, .microphone)
        XCTAssertEqual(frame.sampleRate, 16_000)
        XCTAssertEqual(frame.channels, 1)
        XCTAssertEqual(frame.pcmS16LE, data)
        XCTAssertEqual(frame.timestamp, timestamp)
    }

    func testPCM16LEClampsAndEncodesLittleEndianSamples() {
        let encoded = PCM16LE.encode(samples: [-2.0, -1.0, 0.0, 0.5, 2.0])

        XCTAssertEqual(
            Array(encoded),
            [
                0x00, 0x80,
                0x00, 0x80,
                0x00, 0x00,
                0x00, 0x40,
                0xff, 0x7f,
            ]
        )
    }

    func testAudioStatsCountMicrophoneAndSystemFramesSeparately() {
        var stats = AudioPipelineStats()

        stats.recordFrame(source: .microphone, byteCount: 320)
        stats.recordFrame(source: .system, byteCount: 640)
        stats.recordFrame(source: .microphone, byteCount: 160)

        XCTAssertEqual(stats.microphoneFrames, 2)
        XCTAssertEqual(stats.microphoneBytes, 480)
        XCTAssertEqual(stats.systemFrames, 1)
        XCTAssertEqual(stats.systemBytes, 640)
    }
}
