DayByDayTests/RoutineScriptTests.swift
124 lines · 5405 bytes
17 symbols in this file
1import Foundation
2import Testing
3@testable import DayByDay
4
5struct RoutineScriptTests {
6 static var calendar: Calendar {
7 var c = Calendar(identifier: .gregorian)
8 c.timeZone = TimeZone(identifier: "UTC")!
9 return c
10 }
11
12 func date(_ y: Int, _ m: Int, _ d: Int) -> Date {
13 Self.calendar.date(from: DateComponents(year: y, month: m, day: d, hour: 9))!
14 }
15
16 func script(_ y: Int, _ m: Int, _ d: Int, name: String? = "Maya",
17 birthday: (month: Int, day: Int)? = nil, picture: DayPictureKind? = nil) -> [String] {
18 RoutineScript(date: date(y, m, d), calendar: Self.calendar, name: name,
19 birthday: birthday, picture: picture).lines.map(\.text)
20 }
21
22 @Test func fridayScript() {
23 #expect(script(2026, 10, 16) == [
24 "Hi, Maya!", "Today is Friday.", "Yesterday was Thursday.", "Tomorrow is Saturday.",
25 "It's October.", "It's Fall.",
26 ])
27 #expect(script(2026, 10, 16, name: nil).first == "Hi!")
28 let greeting = RoutineScript(date: date(2026, 10, 16), calendar: Self.calendar, name: "Maya",
29 birthday: nil, picture: nil).lines[0]
30 #expect(greeting.spoken == "Hi there!")
31 let named = RoutineScript(date: date(2026, 10, 16), calendar: Self.calendar, name: "izzy",
32 birthday: nil, picture: nil).lines[0]
33 #expect(named.text == "Hi, izzy!")
34 #expect(named.spoken == "Hi, Izzy!")
35 #expect(RoutineScript(date: date(2026, 10, 16), calendar: Self.calendar, name: "Maya",
36 birthday: nil, picture: nil).lines[1].spoken == "Today is Friday.")
37 let lines = RoutineScript(date: date(2026, 10, 16), calendar: Self.calendar, name: nil,
38 birthday: nil, picture: nil).lines
39 #expect(lines[1].symbol == DayOfWeek.friday.symbol)
40 #expect(lines[4].symbol == MonthOfYear.october.symbol)
41 #expect(lines[5].symbol == Season.fall.symbol)
42 }
43
44 @Test func birthdayMonthLine() {
45 #expect(script(2026, 10, 16, birthday: (month: 10, day: 30))[1] == "Your birthday is in October!")
46 #expect(!script(2026, 10, 16, birthday: (month: 5, day: 1)).contains { $0.contains("birthday") })
47 }
48
49 @Test func birthdayTodayBeatsMonth() {
50 let lines = script(2026, 10, 16, birthday: (month: 10, day: 16))
51 #expect(lines[1] == "Today is your birthday! Happy birthday!")
52 #expect(!lines.contains("Your birthday is in October!"))
53 }
54
55 @Test func leapBirthday() {
56 #expect(script(2027, 2, 28, birthday: (month: 2, day: 29))[1] == "Your birthday is in February!")
57 #expect(script(2028, 2, 29, birthday: (month: 2, day: 29))[1] == "Today is your birthday! Happy birthday!")
58 #expect(!script(2027, 3, 1, birthday: (month: 2, day: 29)).contains { $0.contains("birthday") })
59 }
60
61 @Test func pictureLine() {
62 let lines = script(2026, 10, 16, picture: .swim)
63 #expect(lines[1] == "Today is Friday.")
64 #expect(lines[2] == "Today is swim day!")
65 }
66
67 @Test func weekWraps() {
68 #expect(script(2026, 10, 18).contains("Yesterday was Saturday."))
69 #expect(script(2026, 10, 18)[1] == "Today is Sunday.")
70 #expect(script(2026, 10, 17).contains("Tomorrow is Sunday."))
71 }
72
73 @Test func monthEnd() {
74 let lines = script(2026, 10, 31)
75 #expect(lines.contains("Tomorrow is Sunday."))
76 #expect(lines.contains("It's October."))
77 }
78
79 /// Dates are always Gregorian, whatever calendar the device uses.
80 @Test func appCalendarIsGregorian() {
81 #expect(AppCalendar.current.identifier == .gregorian)
82 #expect(AppCalendar.current.firstWeekday == 1)
83 #expect(AppCalendar.current.timeZone == .current)
84 }
85
86 @Test func yearEnd() {
87 let lines = script(2027, 1, 1)
88 #expect(lines.contains("It's January."))
89 #expect(lines.contains("Yesterday was Thursday."))
90 #expect(lines.contains("It's Winter."))
91 }
92}
93
94struct MonthGridTests {
95 let calendar = RoutineScriptTests.calendar
96
97 @Test func octoberGrid() {
98 let g = MonthGrid(year: 2026, month: 10, calendar: calendar)
99 #expect(g.leadingBlanks == 4)
100 #expect(g.days == 31)
101 #expect(g.cells.prefix(5) == [nil, nil, nil, nil, 1])
102 #expect(g.cells.last == 31)
103 }
104
105 @Test func februaryGrids() {
106 #expect(MonthGrid(year: 2027, month: 2, calendar: calendar).days == 28)
107 #expect(MonthGrid(year: 2028, month: 2, calendar: calendar).days == 29)
108 }
109
110 @Test func neverPastCurrentMonth() {
111 let today = (year: 2026, month: 10)
112 #expect(!MonthGrid(year: 2026, month: 10, calendar: calendar).canGoLater(than: today))
113 #expect(MonthGrid(year: 2026, month: 9, calendar: calendar).canGoLater(than: today))
114 #expect(MonthGrid(year: 2025, month: 12, calendar: calendar).canGoLater(than: today))
115 #expect(!MonthGrid(year: 2027, month: 1, calendar: calendar).canGoLater(than: today))
116 }
117
118 @Test func shiftAcrossYear() {
119 let jan = MonthGrid(year: 2026, month: 12, calendar: calendar).shifted(by: 1)
120 #expect(jan.year == 2027 && jan.month == 1)
121 let nov = MonthGrid(year: 2027, month: 1, calendar: calendar).shifted(by: -2)
122 #expect(nov.year == 2026 && nov.month == 11)
123 }
124}