import Testing
import SwiftUI
@testable import 康康

@MainActor
struct TrendInsightCacheTests {

    private func bucket(values: [Double]) -> SeriesBucket {
        let points = values.enumerated().map { i, v in
            SeriesBucket.Point(id: "p\(i)",
                               date: Date(timeIntervalSince1970: Double(i) * 86_400),
                               value: v, status: .normal)
        }
        let line = SeriesBucket.SeriesLine(id: "glucose.fasting", seriesKey: "glucose.fasting",
                                           label: nil, color: .blue, points: points,
                                           referenceRange: 3.9...6.1)
        return SeriesBucket(id: "glucose.fasting", title: "空腹血糖", unit: "mmol/L",
                            lines: [line], latestDate: .now, kind: .monitor)
    }

    @Test func fingerprintStableForSameData() {
        let a = TrendInsightService.fingerprint(for: bucket(values: [5.2, 5.5]))
        let b = TrendInsightService.fingerprint(for: bucket(values: [5.2, 5.5]))
        #expect(a == b)
    }

    @Test func fingerprintChangesWhenDataChanges() {
        let a = TrendInsightService.fingerprint(for: bucket(values: [5.2, 5.5]))
        let b = TrendInsightService.fingerprint(for: bucket(values: [5.2, 5.5, 6.0]))
        #expect(a != b)
    }

    @Test func dataLinesFormatsDateAndValue() {
        let lines = TrendInsightService.dataLines(for: bucket(values: [5.2, 5.5]))
        #expect(lines.contains("1970-01-01 5.2"))
        #expect(lines.contains("1970-01-02 5.5"))
    }

    @Test func rangeTextRendersReference() {
        #expect(TrendInsightService.rangeText(for: bucket(values: [5.2]))
                == ",参考 3.9-6.1")
    }
}
