Cotton code
62 lines · 3.5 KB · SwiftRaw
// EVERY TYPE THE CORE NAMES, HELD TO HEALTHKIT (health-ios, 20 Nov 2026).// `Fixtures/health-types.json` is `health.types` as the core answers it// (written and checked by cotton-ffi's `health_types_fixture_is_the_cores`).// HealthKit raises, rather than failing, on an identifier it does not know,// a unit string it cannot read and statistics a type cannot give; so every// one is held here, on the simulator, before a phone meets it.import HealthKitimport XCTest@testable import CottonHealthKitfinal class HealthTypesTests: XCTestCase {    private func types() throws -> [HealthReadType] {        struct Types: Decodable { let types: [HealthReadType] }        #if SWIFT_PACKAGE        let bundle = Bundle.module        #else        let bundle = Bundle(for: Self.self)        #endif        let url = try XCTUnwrap(bundle.url(forResource: "health-types", withExtension: "json", subdirectory: "Fixtures")            ?? bundle.url(forResource: "health-types", withExtension: "json"))        return try JSONDecoder().decode(Types.self, from: Data(contentsOf: url)).types    }    /// RULE: every type the core names is one HealthKit offers on this iOS, asked for read only; a quantity's unit is one HealthKit reads for it, and its day is asked as HealthKit can give it (a sum of a cumulative type, an average or the last of a discrete one).    func testEveryTypeIsOneHealthKitCanAnswer() throws {        let types = try types()        XCTAssertGreaterThan(types.count, 100, "the whole catalogue")        for type in types {            XCTAssertNotNil(type.sampleType, "\(type.healthkit) is a type this iOS offers")            XCTAssertTrue(type.statisticsFit, "\(type.healthkit): \(type.unit ?? "") with \(type.statistics ?? "")")            XCTAssertFalse(type.asked.isEmpty, "\(type.healthkit) is asked for")            if let quantity = type.sampleType as? HKQuantityType, let unit = type.unit, !unit.isEmpty {                XCTAssertTrue(quantity.is(compatibleWith: HKUnit(from: unit)), "\(type.healthkit) reads in \(unit)")            }        }        XCTAssertEqual(Set(types.map(\.healthkit)).count, types.count, "each type once")    }    /// RULE: heart rate is read as the day's statistics and a workout's own, never as samples sent; only a type with something of its own to send keeps an anchored query.    func testHeartRateIsADayAndAWorkoutsStatistics() throws {        let heart = try XCTUnwrap(try types().first { $0.healthkit == HKQuantityTypeIdentifier.heartRate.rawValue })        XCTAssertEqual(heart.sends, ["days", "workouts"])        XCTAssertEqual(heart.statistics, "discreteAverage")        XCTAssertTrue(heart.anchored)    }    /// RULE: a type HealthKit does not know is no type, never an exception: the reader skips it.    func testAnUnknownIdentifierIsNoType() {        let unknown = HealthReadType(healthkit: "HKQuantityTypeIdentifierNothingAtAll", kind: "quantity", unit: "count", sends: ["days"], statistics: "cumulativeSum")        XCTAssertNil(unknown.sampleType)        XCTAssertFalse(unknown.statisticsFit)        XCTAssertTrue(unknown.asked.isEmpty)    }    /// RULE: the days a refold reads run from the first to the last, each once, as the core keys them.    func testDaysBetweenAreEveryDayOnce() {        XCTAssertEqual(HealthDays.between("2026-02-27", "2026-03-02"), ["2026-02-27", "2026-02-28", "2026-03-01", "2026-03-02"])        XCTAssertEqual(HealthDays.between("2026-03-02", "2026-03-02"), ["2026-03-02"])        XCTAssertEqual(HealthDays.between("2026-03-03", "2026-03-02"), [])    }}