import Testing
import Foundation
@testable import 康康

struct BenchmarkStoreTests {

    /// 每个用例独立 suite,避免 Swift Testing 并行执行时互相清空数据。
    private func freshDefaults(_ name: String) -> UserDefaults {
        let suite = "test.kk.benchmark.\(name)"
        let d = UserDefaults(suiteName: suite)!
        d.removePersistentDomain(forName: suite)
        return d
    }

    @Test func savesAndLoadsPerBackend() {
        let d = freshDefaults("savesAndLoads")
        let mnn = BenchmarkResult(backendLabel: "MNN · SME2", promptTokens: 30, genTokens: 80,
                                  prefillTokensPerSecond: 120, decodeTokensPerSecond: 25,
                                  totalSeconds: 4.2, date: .now)
        let mlx = BenchmarkResult(backendLabel: "MLX · GPU", promptTokens: 30, genTokens: 80,
                                  prefillTokensPerSecond: 300, decodeTokensPerSecond: 40,
                                  totalSeconds: 2.5, date: .now)
        BenchmarkService.save(mnn, defaults: d)
        BenchmarkService.save(mlx, defaults: d)
        let all = BenchmarkService.load(defaults: d)
        #expect(all.count == 2)
        #expect(all["MNN · SME2"]?.decodeTokensPerSecond == 25)
    }

    @Test func overwritesSameBackend() {
        let d = freshDefaults("overwrites")
        let old = BenchmarkResult(backendLabel: "MLX · GPU", promptTokens: 1, genTokens: 1,
                                  prefillTokensPerSecond: 1, decodeTokensPerSecond: 1,
                                  totalSeconds: 1, date: .now)
        var new = old
        new.decodeTokensPerSecond = 99
        BenchmarkService.save(old, defaults: d)
        BenchmarkService.save(new, defaults: d)
        #expect(BenchmarkService.load(defaults: d)["MLX · GPU"]?.decodeTokensPerSecond == 99)
    }

    @Test func loadOnEmptyReturnsEmpty() {
        #expect(BenchmarkService.load(defaults: freshDefaults("loadEmpty")).isEmpty)
    }
}
