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/SkyEngineTests/EventTests.swift

31 lines · 1202 bytes

 1import Foundation
 2import Testing
 3@testable import SkyEngine
 4
 5// Rise and set times from JPL Horizons (R_T_S_ONLY=TVH, refracted, minute resolution),
 6// Chicago, searching 2026-10-17 00:00 to 2026-10-18 00:00 UT.
 7
 8let midnightUT = Date(timeIntervalSince1970: 1_792_195_200) // 2026-10-17T00:00:00Z
 9
10func ut(_ hour: Int, _ minute: Int) -> Date {
11    midnightUT.addingTimeInterval(TimeInterval(hour * 3600 + minute * 60))
12}
13
14@Test(arguments: [
15    (Body.sun, ut(12, 6), ut(23, 6)),
16    (Body.saturn, ut(22, 39), ut(10, 58)),
17    (Body.moon, ut(18, 57), ut(2, 49)),
18])
19func riseSetMatchesHorizons(_ body: Body, _ rise: Date, _ set: Date) throws {
20    let r = SkyEngine.riseSet(body, after: midnightUT, from: chicago)
21    let gotRise = try #require(r.rise)
22    let gotSet = try #require(r.set)
23    #expect(abs(gotRise.timeIntervalSince(rise)) < 120, "\(body) rise \(gotRise)")
24    #expect(abs(gotSet.timeIntervalSince(set)) < 120, "\(body) set \(gotSet)")
25}
26
27@Test func nextQuarterAfterObservationIsFirstQuarter() throws {
28    let q = try SkyEngine.nextMoonQuarter(after: observed)
29    #expect(q.quarter == .firstQuarter)
30    #expect(q.date > observed && q.date < observed.addingTimeInterval(4 * 86_400))
31}