Stabilize unit and UI tests for SportsTime

This commit is contained in:
treyt
2026-02-18 13:00:15 -06:00
parent 1488be7c1f
commit 20ac1a7e59
49 changed files with 432 additions and 325 deletions

View File

@@ -51,7 +51,7 @@ struct AnySportTests {
// MARK: - Test Data
private var calendar: Calendar { Calendar.current }
private var calendar: Calendar { TestClock.calendar }
private func date(month: Int) -> Date {
calendar.date(from: DateComponents(year: 2026, month: month, day: 15))!

View File

@@ -35,7 +35,7 @@ struct DynamicSportTests {
)
}
private var calendar: Calendar { Calendar.current }
private var calendar: Calendar { TestClock.calendar }
private func date(month: Int) -> Date {
calendar.date(from: DateComponents(year: 2026, month: month, day: 15))!

View File

@@ -32,7 +32,7 @@ struct GameTests {
@Test("gameDate returns start of day for dateTime")
func gameDate_returnsStartOfDay() {
let calendar = Calendar.current
let calendar = TestClock.calendar
// Game at 7:05 PM
let dateTime = calendar.date(from: DateComponents(
@@ -54,7 +54,7 @@ struct GameTests {
@Test("gameDate is same for games on same calendar day")
func gameDate_sameDay() {
let calendar = Calendar.current
let calendar = TestClock.calendar
// Morning game
let morningTime = calendar.date(from: DateComponents(
@@ -76,7 +76,7 @@ struct GameTests {
@Test("gameDate differs for games on different calendar days")
func gameDate_differentDays() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let day1 = calendar.date(from: DateComponents(
year: 2026, month: 6, day: 15, hour: 19
@@ -95,7 +95,7 @@ struct GameTests {
@Test("startTime is alias for dateTime")
func startTime_isAliasForDateTime() {
let dateTime = Date()
let dateTime = TestClock.now
let game = makeGame(dateTime: dateTime)
#expect(game.startTime == game.dateTime)
@@ -105,7 +105,7 @@ struct GameTests {
@Test("equality based on id only")
func equality_basedOnId() {
let dateTime = Date()
let dateTime = TestClock.now
let game1 = Game(
id: "game1",
@@ -135,7 +135,7 @@ struct GameTests {
@Test("inequality when ids differ")
func inequality_differentIds() {
let dateTime = Date()
let dateTime = TestClock.now
let game1 = Game(
id: "game1",
@@ -166,7 +166,7 @@ struct GameTests {
@Test("Invariant: gameDate is always at midnight")
func invariant_gameDateAtMidnight() {
let calendar = Calendar.current
let calendar = TestClock.calendar
// Test various times throughout the day
let times = [0, 6, 12, 18, 23].map { hour in
@@ -185,7 +185,7 @@ struct GameTests {
@Test("Invariant: startTime equals dateTime")
func invariant_startTimeEqualsDateTime() {
for _ in 0..<10 {
let dateTime = Date().addingTimeInterval(Double.random(in: -86400...86400))
let dateTime = TestClock.now.addingTimeInterval(Double.random(in: -86400...86400))
let game = makeGame(dateTime: dateTime)
#expect(game.startTime == game.dateTime)
}
@@ -195,7 +195,7 @@ struct GameTests {
@Test("Property: gameDate is in same calendar day as dateTime")
func property_gameDateSameCalendarDay() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let dateTime = calendar.date(from: DateComponents(
year: 2026, month: 7, day: 4, hour: 19, minute: 5

View File

@@ -344,7 +344,7 @@ struct StadiumVisitStatusTests {
@Test("isVisited: true for visited status")
func isVisited_true() {
let visit = makeVisitSummary(date: Date())
let visit = makeVisitSummary(date: TestClock.now)
let status = StadiumVisitStatus.visited(visits: [visit])
#expect(status.isVisited == true)
@@ -362,9 +362,9 @@ struct StadiumVisitStatusTests {
@Test("visitCount: returns count of visits")
func visitCount_multiple() {
let visits = [
makeVisitSummary(date: Date()),
makeVisitSummary(date: Date()),
makeVisitSummary(date: Date()),
makeVisitSummary(date: TestClock.now),
makeVisitSummary(date: TestClock.now),
makeVisitSummary(date: TestClock.now),
]
let status = StadiumVisitStatus.visited(visits: visits)
@@ -382,7 +382,7 @@ struct StadiumVisitStatusTests {
@Test("latestVisit: returns visit with max date")
func latestVisit_maxDate() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let date1 = calendar.date(from: DateComponents(year: 2025, month: 1, day: 1))!
let date2 = calendar.date(from: DateComponents(year: 2025, month: 6, day: 15))!
let date3 = calendar.date(from: DateComponents(year: 2025, month: 3, day: 10))!
@@ -408,7 +408,7 @@ struct StadiumVisitStatusTests {
@Test("firstVisit: returns visit with min date")
func firstVisit_minDate() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let date1 = calendar.date(from: DateComponents(year: 2025, month: 1, day: 1))!
let date2 = calendar.date(from: DateComponents(year: 2025, month: 6, day: 15))!
let date3 = calendar.date(from: DateComponents(year: 2025, month: 3, day: 10))!
@@ -469,7 +469,7 @@ struct VisitSummaryTests {
capacity: 40000,
sport: .mlb
),
visitDate: Date(),
visitDate: TestClock.now,
visitType: .game,
sport: .mlb,
homeTeamName: homeTeam,

View File

@@ -67,7 +67,7 @@ struct SportTests {
@Test("MLB: isInSeason returns true for months 3-10")
func mlb_isInSeason_normalRange() {
let calendar = Calendar.current
let calendar = TestClock.calendar
// In season: March through October
for month in 3...10 {
@@ -86,7 +86,7 @@ struct SportTests {
@Test("NBA: isInSeason returns true for months 10-12 and 1-6 (wrap-around)")
func nba_isInSeason_wrapAround() {
let calendar = Calendar.current
let calendar = TestClock.calendar
// In season: October through June (wraps)
let inSeasonMonths = [10, 11, 12, 1, 2, 3, 4, 5, 6]
@@ -104,7 +104,7 @@ struct SportTests {
@Test("NFL: isInSeason returns true for months 9-12 and 1-2 (wrap-around)")
func nfl_isInSeason_wrapAround() {
let calendar = Calendar.current
let calendar = TestClock.calendar
// In season: September through February (wraps)
let inSeasonMonths = [9, 10, 11, 12, 1, 2]
@@ -124,7 +124,7 @@ struct SportTests {
@Test("isInSeason boundary: first and last day of season month")
func isInSeason_boundaryDays() {
let calendar = Calendar.current
let calendar = TestClock.calendar
// MLB: First day of March (in season)
let marchFirst = calendar.date(from: DateComponents(year: 2026, month: 3, day: 1))!

View File

@@ -24,8 +24,8 @@ struct TripPollTests {
preferences: TripPreferences(
planningMode: .dateRange,
sports: [.mlb],
startDate: Date(),
endDate: Date().addingTimeInterval(86400 * 7)
startDate: TestClock.now,
endDate: TestClock.now.addingTimeInterval(86400 * 7)
),
stops: stops
)
@@ -96,7 +96,7 @@ struct TripPollTests {
@Test("computeTripHash: different trips produce different hashes")
func computeTripHash_differentTrips() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let date1 = calendar.date(from: DateComponents(year: 2026, month: 6, day: 15))!
let date2 = calendar.date(from: DateComponents(year: 2026, month: 6, day: 16))!
@@ -266,8 +266,8 @@ struct PollResultsTests {
preferences: TripPreferences(
planningMode: .dateRange,
sports: [.mlb],
startDate: Date(),
endDate: Date().addingTimeInterval(86400 * 7)
startDate: TestClock.now,
endDate: TestClock.now.addingTimeInterval(86400 * 7)
)
)
}

View File

@@ -50,8 +50,8 @@ struct TripPreferencesTests {
@Test("effectiveTripDuration: uses tripDuration when set")
func effectiveTripDuration_explicit() {
let prefs = TripPreferences(
startDate: Date(),
endDate: Date().addingTimeInterval(86400 * 14),
startDate: TestClock.now,
endDate: TestClock.now.addingTimeInterval(86400 * 14),
tripDuration: 5
)
@@ -60,7 +60,7 @@ struct TripPreferencesTests {
@Test("effectiveTripDuration: calculates from date range when tripDuration is nil")
func effectiveTripDuration_calculated() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let startDate = calendar.date(from: DateComponents(year: 2026, month: 6, day: 15))!
let endDate = calendar.date(from: DateComponents(year: 2026, month: 6, day: 22))!
@@ -75,7 +75,7 @@ struct TripPreferencesTests {
@Test("effectiveTripDuration: minimum is 1")
func effectiveTripDuration_minimum() {
let date = Date()
let date = TestClock.now
let prefs = TripPreferences(
startDate: date,
endDate: date,

View File

@@ -33,7 +33,7 @@ struct TripStopTests {
@Test("stayDuration: same day arrival and departure returns 1")
func stayDuration_sameDay() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let arrivalDate = calendar.date(from: DateComponents(year: 2026, month: 6, day: 15))!
let departureDate = calendar.date(from: DateComponents(year: 2026, month: 6, day: 15))!
@@ -44,7 +44,7 @@ struct TripStopTests {
@Test("stayDuration: 2-day stay returns 2")
func stayDuration_twoDays() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let arrivalDate = calendar.date(from: DateComponents(year: 2026, month: 6, day: 15))!
let departureDate = calendar.date(from: DateComponents(year: 2026, month: 6, day: 16))!
@@ -55,7 +55,7 @@ struct TripStopTests {
@Test("stayDuration: week-long stay")
func stayDuration_weekLong() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let arrivalDate = calendar.date(from: DateComponents(year: 2026, month: 6, day: 15))!
let departureDate = calendar.date(from: DateComponents(year: 2026, month: 6, day: 22))!
@@ -66,7 +66,7 @@ struct TripStopTests {
@Test("stayDuration: minimum is 1 even if dates are reversed")
func stayDuration_minimumIsOne() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let arrivalDate = calendar.date(from: DateComponents(year: 2026, month: 6, day: 20))!
let departureDate = calendar.date(from: DateComponents(year: 2026, month: 6, day: 15))!
@@ -79,7 +79,7 @@ struct TripStopTests {
@Test("hasGames: true when games array is non-empty")
func hasGames_true() {
let now = Date()
let now = TestClock.now
let stop = makeStop(arrivalDate: now, departureDate: now, games: ["game1", "game2"])
#expect(stop.hasGames == true)
@@ -87,7 +87,7 @@ struct TripStopTests {
@Test("hasGames: false when games array is empty")
func hasGames_false() {
let now = Date()
let now = TestClock.now
let stop = makeStop(arrivalDate: now, departureDate: now, games: [])
#expect(stop.hasGames == false)
@@ -97,7 +97,7 @@ struct TripStopTests {
@Test("formattedDateRange: single date for 1-day stay")
func formattedDateRange_singleDay() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let date = calendar.date(from: DateComponents(year: 2026, month: 6, day: 15))!
let stop = makeStop(arrivalDate: date, departureDate: date)
@@ -108,7 +108,7 @@ struct TripStopTests {
@Test("formattedDateRange: range for multi-day stay")
func formattedDateRange_multiDay() {
let calendar = Calendar.current
let calendar = TestClock.calendar
let arrival = calendar.date(from: DateComponents(year: 2026, month: 6, day: 15))!
let departure = calendar.date(from: DateComponents(year: 2026, month: 6, day: 18))!
@@ -126,8 +126,8 @@ struct TripStopTests {
stopNumber: 1,
city: "Boston",
state: "MA",
arrivalDate: Date(),
departureDate: Date()
arrivalDate: TestClock.now,
departureDate: TestClock.now
)
#expect(stop.locationDescription == "Boston, MA")
@@ -137,7 +137,7 @@ struct TripStopTests {
@Test("Invariant: stayDuration >= 1")
func invariant_stayDurationAtLeastOne() {
let calendar = Calendar.current
let calendar = TestClock.calendar
// Test various date combinations
let testCases: [(arrival: DateComponents, departure: DateComponents)] = [
@@ -158,7 +158,7 @@ struct TripStopTests {
@Test("Invariant: hasGames equals !games.isEmpty")
func invariant_hasGamesConsistent() {
let now = Date()
let now = TestClock.now
let stopWithGames = makeStop(arrivalDate: now, departureDate: now, games: ["game1"])
#expect(stopWithGames.hasGames == !stopWithGames.games.isEmpty)
@@ -171,7 +171,7 @@ struct TripStopTests {
@Test("Property: isRestDay defaults to false")
func property_isRestDayDefault() {
let now = Date()
let now = TestClock.now
let stop = makeStop(arrivalDate: now, departureDate: now)
#expect(stop.isRestDay == false)
@@ -183,8 +183,8 @@ struct TripStopTests {
stopNumber: 1,
city: "City",
state: "ST",
arrivalDate: Date(),
departureDate: Date(),
arrivalDate: TestClock.now,
departureDate: TestClock.now,
isRestDay: true
)
@@ -198,8 +198,8 @@ struct TripStopTests {
city: "City",
state: "ST",
coordinate: nil,
arrivalDate: Date(),
departureDate: Date(),
arrivalDate: TestClock.now,
departureDate: TestClock.now,
stadium: nil,
lodging: nil,
notes: nil

View File

@@ -14,14 +14,14 @@ struct TripTests {
// MARK: - Test Data
private var calendar: Calendar { Calendar.current }
private var calendar: Calendar { TestClock.calendar }
private func makePreferences() -> TripPreferences {
TripPreferences(
planningMode: .dateRange,
sports: [.mlb],
startDate: Date(),
endDate: Date().addingTimeInterval(86400 * 7)
startDate: TestClock.now,
endDate: TestClock.now.addingTimeInterval(86400 * 7)
)
}
@@ -117,7 +117,7 @@ struct TripTests {
@Test("tripDuration: minimum is 1 day")
func tripDuration_minimumIsOne() {
let date = Date()
let date = TestClock.now
let stop = makeStop(city: "NYC", arrivalDate: date, departureDate: date)
let trip = Trip(
@@ -160,7 +160,7 @@ struct TripTests {
@Test("cities: returns deduplicated list preserving order")
func cities_deduplicatedPreservingOrder() {
let date = Date()
let date = TestClock.now
let stop1 = makeStop(city: "NYC", arrivalDate: date, departureDate: date)
let stop2 = makeStop(city: "Boston", arrivalDate: date, departureDate: date)
@@ -191,7 +191,7 @@ struct TripTests {
@Test("displayName: uses arrow separator between cities")
func displayName_arrowSeparator() {
let date = Date()
let date = TestClock.now
let stop1 = makeStop(city: "NYC", arrivalDate: date, departureDate: date)
let stop2 = makeStop(city: "Boston", arrivalDate: date, departureDate: date)
@@ -267,7 +267,7 @@ struct TripTests {
@Test("Invariant: cities has no duplicates")
func invariant_citiesNoDuplicates() {
let date = Date()
let date = TestClock.now
// Create stops with duplicate cities
let stops = [