krz/moonrise

Night-sky companion for iPhone, Apple Watch and Vision Pro: what's up tonight, the Moon, planets, launches and missions, and a Library of the solar system. SwiftUI app with a Cloudflare Workers backend. astronomy cloudflare-workers ios night-sky space swiftui visionos watchos

Packages/MoonriseKit/Tests/FeedsTests/NOAAScalesTests.swift

6c4b28384d65e09486e0ad62f2ac9d10401cc80c
moonrise/Packages/MoonriseKit/Tests/FeedsTests/NOAAScalesTests.swift history · blame · raw

52 lines · 2745 bytes

 1import Foundation
 2import Testing
 3@testable import Feeds
 4
 5// Fixtures saved from the live SWPC feeds on 2026-10-19.
 6
 7@Test func decodesCurrentAndPastDayScales() throws {
 8    let url = try #require(Bundle.module.url(forResource: "Fixtures/noaa-scales", withExtension: "json"))
 9    let s = try NOAAScales.decode(Data(contentsOf: url))
10    #expect(s.current == NOAAScales.Levels(r: 0, s: 0, g: 0))
11    #expect(s.past24h == NOAAScales.Levels(r: 1, s: 0, g: 0))
12    #expect(s.asOf == (try Date("2026-10-19T21:13:00Z", strategy: .iso8601)))
13}
14
15@Test func scaleLevelNames() {
16    #expect(NOAAScales.levelName(0) == "none")
17    #expect(NOAAScales.levelName(3) == "strong")
18    #expect(NOAAScales.levelName(5) == "extreme")
19}
20
21@Test func decodesNewestProbabilities() throws {
22    let url = try #require(Bundle.module.url(forResource: "Fixtures/solar-probabilities", withExtension: "json"))
23    let p = try SolarProbabilities.decode(Data(contentsOf: url))
24    #expect(p.issued == (try Date("2026-10-19T00:00:00Z", strategy: .iso8601)))
25    #expect(p.days.count == 3)
26    #expect(p.days[0] == SolarProbabilities.Day(c: 85, m: 25, x: 5, proton: 5))
27}
28
29@Test func probabilitiesForADate() throws {
30    let url = try #require(Bundle.module.url(forResource: "Fixtures/solar-probabilities", withExtension: "json"))
31    let p = try SolarProbabilities.decode(Data(contentsOf: url))
32    let issued = p.issued
33    #expect(p.day(at: issued.addingTimeInterval(23 * 3600)) == p.days[0])
34    #expect(p.day(at: issued.addingTimeInterval(25 * 3600)) == p.days[1])
35    #expect(p.day(at: issued.addingTimeInterval(-3600)) == nil)
36    #expect(p.day(at: issued.addingTimeInterval(73 * 3600)) == nil)
37}
38
39@Test func snapshotRoundTripsThroughDisk() throws {
40    let file = FileManager.default.temporaryDirectory.appending(path: "sun-\(UUID().uuidString)/SunSnapshot.json")
41    let snapshot = SunSnapshot(
42        fetched: Date(timeIntervalSince1970: 1_800_000_000),
43        kp: [KpBar(start: Date(timeIntervalSince1970: 1_800_000_000), kp: 5.33, predicted: true)],
44        xray: [XRaySample(time: Date(timeIntervalSince1970: 1_800_000_060), flux: 2.4e-5)],
45        ovation: Ovation(forecastTime: Date(timeIntervalSince1970: 1_800_000_000), cells: [.init(lon: 0, lat: 65, probability: 12)]),
46        scales: NOAAScales(asOf: Date(timeIntervalSince1970: 1_800_000_000), current: .init(r: 1, s: 0, g: 2), past24h: .init(r: 2, s: 0, g: 2)),
47        probabilities: SolarProbabilities(issued: Date(timeIntervalSince1970: 1_799_971_200), days: [.init(c: 85, m: 25, x: 5, proton: 5)])
48    )
49    try snapshot.save(to: file)
50    #expect(SunSnapshot.load(from: file) == snapshot)
51    #expect(SunSnapshot.load(from: file.deletingLastPathComponent().appending(path: "missing.json")) == nil)
52}