import Testing
import Foundation
@testable import Music

struct ScannerServiceTests {
    // Creates a temp directory with audio and non-audio files, verifies only audio files are discovered.
    // 1. Creates a unique temporary directory.
    // 2. Creates files with audio extensions (mp3, m4a, flac, wav, aiff) and non-audio extensions (txt, png, json).
    // 3. Creates a subdirectory with a nested mp3 file to test recursive discovery.
    // 4. Calls ScannerService.discoverAudioFiles and checks that exactly 6 audio files are found.
    // 5. Verifies all discovered files have audio-only extensions.
    // 6. Cleans up the temp directory.
    @Test func discoverAudioFiles() throws {
        let tmpDir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
        try FileManager.default.createDirectory(at: tmpDir, withIntermediateDirectories: true)
        defer { try? FileManager.default.removeItem(at: tmpDir) }

        let audioFiles = ["song.mp3", "track.m4a", "audio.flac", "sound.wav", "music.aiff"]
        let nonAudioFiles = ["readme.txt", "image.png", "data.json"]

        for name in audioFiles + nonAudioFiles {
            FileManager.default.createFile(atPath: tmpDir.appendingPathComponent(name).path, contents: Data())
        }

        let subDir = tmpDir.appendingPathComponent("subfolder")
        try FileManager.default.createDirectory(at: subDir, withIntermediateDirectories: true)
        FileManager.default.createFile(atPath: subDir.appendingPathComponent("nested.mp3").path, contents: Data())

        let discovered = ScannerService.discoverAudioFiles(in: tmpDir)
        #expect(discovered.count == 6)

        let extensions = Set(discovered.map { $0.pathExtension.lowercased() })
        #expect(extensions.isSubset(of: ["mp3", "m4a", "flac", "wav", "aiff", "aac", "alac"]))
    }
}
