Tests/Unit/ReportSummaryTests.swift (13109B)
1import Foundation
2import Testing
3
4@testable import Reportage
5
6@MainActor
7@Suite("NarrativeBuilder")
8struct NarrativeBuilderTests {
9
10 private static let lookup = SummaryLookup(
11 tokenTexts: [
12 "read": "Reading", "coffee": "Drinking coffee", "netflix": "Watching Netflix",
13 "library": "Library", "home": "Home", "bottle": "Blue bottle", "sam": "Sam", "kai": "Kai",
14 "tanaka": "田中さん", "lee": "Sam Lee", "taro": "田中 太郎", "yamada": "山田花子",
15 "office": "Office", "run": "Running",
16 ],
17 narrativeForms: ["yamada": "Hanako", "office": "my desk", "run": "going for a run"],
18 directoryPlaces: ["bottle"],
19 questionRules: NarrativeRule.builtIn.merging(
20 ["activity": .activities, "people": .people, "place": .place], uniquingKeysWith: { $1 })
21 )
22
23 private static func response(_ questionID: String, _ value: ResponseValue?) -> Response {
24 Response(questionID: questionID, prompt: "Prompt \(questionID)?", value: value)
25 }
26
27 private static func build(_ responses: [Response]) -> (narrative: String?, details: [AnswerDetail]) {
28 NarrativeBuilder.build(responses: responses, lookup: lookup)
29 }
30
31 @Test("Activities, place and people make one clause, with later activities lowercased")
32 func mainClause() {
33 let (narrative, details) = Self.build([
34 Self.response("activity", .tokens(["read", "coffee", "netflix"])),
35 Self.response("place", .place(PlaceAnswer(tokenID: "library"))),
36 Self.response("people", .tokens(["sam", "kai"])),
37 ])
38 #expect(narrative == "Reading, drinking coffee and watching Netflix at the library, with Sam and Kai.")
39 #expect(details.isEmpty)
40 }
41
42 @Test("People read as their name before the first space, unless written in CJK script")
43 func people() {
44 let narrative = Self.build([Self.response("people", .tokens(["sam", "lee", "tanaka", "taro"]))])
45 .narrative
46 #expect(narrative == "With Sam, Sam, 田中さん and 田中 太郎.")
47 }
48
49 @Test("Tokens merged into one another are named once")
50 func mergedTokens() {
51 var lookup = Self.lookup
52 lookup.tokenTexts["sammy"] = "Sam Lee"
53 lookup.mergedIDs["sammy"] = "lee"
54 let responses = [Self.response("people", .tokens(["sammy", "kai", "lee"]))]
55 #expect(NarrativeBuilder.build(responses: responses, lookup: lookup).narrative == "With Sam and Kai.")
56 #expect(NarrativeBuilder.describe(responses[0].value!, lookup: lookup) == "Sam Lee and Kai")
57 }
58
59 @Test("A narrative form wins over the rules for people, places and activities")
60 func narrativeForms() {
61 let (narrative, details) = Self.build([
62 Self.response("activity", .tokens(["read", "run"])),
63 Self.response("place", .place(PlaceAnswer(tokenID: "office"))),
64 Self.response("people", .tokens(["yamada"])),
65 ])
66 #expect(narrative == "Reading and going for a run at my desk, with Hanako.")
67 #expect(details.isEmpty)
68 }
69
70 @Test("Answers outside the narrative keep the token's text")
71 func formsOnlyInNarrative() {
72 var lookup = Self.lookup
73 lookup.questionRules["people"] = nil
74 let details = NarrativeBuilder.build(
75 responses: [Self.response("people", .tokens(["yamada", "lee"]))], lookup: lookup
76 ).details
77 #expect(details.map(\.answer) == ["山田花子 and Sam Lee"])
78 }
79
80 @Test("A place from a directory is shown as written, and others are phrased")
81 func places() {
82 let home = Self.build([Self.response("place", .place(PlaceAnswer(tokenID: "home")))]).narrative
83 #expect(home == "At home.")
84 let bottle = Self.build([
85 Self.response("activity", .tokens(["read"])),
86 Self.response("place", .place(PlaceAnswer(tokenID: "bottle"))),
87 ]).narrative
88 #expect(bottle == "Reading at Blue bottle.")
89 }
90
91 @Test("Built-in rules phrase each kind of answer, in survey order")
92 func builtInRules() {
93 let (narrative, _) = Self.build([
94 Self.response("reportage-default-working", .options([ResponseValue.noOption])),
95 Self.response("activity", .tokens(["read"])),
96 Self.response("reportage-default-mood", .options(["Great"])),
97 Self.response("reportage-default-coffee", .number(1)),
98 Self.response("reportage-default-learned", .text("The neighbours are nice people.")),
99 ])
100 #expect(
101 narrative
102 == "Not working. Reading. Feeling great. 1 coffee. Learned: 'The neighbours are nice people.'")
103 }
104
105 @Test("Answers follow the questions' current order, not the order they were given in")
106 func currentOrder() {
107 var lookup = Self.lookup
108 lookup.questionOrder = ["activity": 0, "place": 1, "reportage-default-working": 2]
109 let (narrative, _) = NarrativeBuilder.build(
110 responses: [
111 Self.response("reportage-default-working", .options([ResponseValue.yesOption])),
112 Self.response("activity", .tokens(["read"])),
113 Self.response("place", .place(PlaceAnswer(tokenID: "library"))),
114 Self.response("reportage-default-mood", .options(["Good"])),
115 ],
116 lookup: lookup)
117
118 #expect(narrative == "Reading at the library. Working. Feeling good.")
119 }
120
121 @Test("Answers to questions hidden from the timeline are left out")
122 func hidden() {
123 var lookup = Self.lookup
124 lookup.hiddenQuestions = ["reportage-default-working", "custom"]
125 let (narrative, details) = NarrativeBuilder.build(
126 responses: [
127 Self.response("reportage-default-working", .options([ResponseValue.yesOption])),
128 Self.response("activity", .tokens(["read"])),
129 Self.response("custom", .options([ResponseValue.yesOption])),
130 ],
131 lookup: lookup)
132 #expect(narrative == "Reading.")
133 #expect(details.isEmpty)
134 }
135
136 @Test("Reporter's built-in questions read like the defaults that ask the same things")
137 func reporterRules() {
138 let (narrative, details) = Self.build([
139 Self.response("default-question-1", .options(["Poorly"])),
140 Self.response("default-question-2", .options([ResponseValue.yesOption])),
141 Self.response("default-question-6", .number(2)),
142 ])
143 #expect(narrative == "Slept poorly. Working. 2 coffees.")
144 #expect(details.isEmpty)
145 }
146
147 @Test("Counts use the plural noun for anything but one")
148 func counts() {
149 let narrative = Self.build([Self.response("reportage-default-coffee", .number(2))]).narrative
150 #expect(narrative == "2 coffees.")
151 }
152
153 @Test("A note without final punctuation gets a full stop after the quotation")
154 func unendedNote() {
155 let narrative = Self.build([Self.response("reportage-default-learned", .text("Tide tables"))]).narrative
156 #expect(narrative == "Learned: 'Tide tables'.")
157 }
158
159 @Test("A token question with a phrase reads as a sentence of its own")
160 func tokenTemplate() {
161 var lookup = Self.lookup
162 lookup.tokenTexts["tired"] = "Tired"
163 lookup.tokenTexts["hungry"] = "Hungry"
164 lookup.questionRules["feeling"] = .template(format: "Feeling %@")
165 let (narrative, details) = NarrativeBuilder.build(
166 responses: [
167 Self.response("activity", .tokens(["read"])),
168 Self.response("feeling", .tokens(["tired", "hungry"])),
169 ],
170 lookup: lookup)
171 #expect(narrative == "Reading. Feeling tired and hungry.")
172 #expect(details.isEmpty)
173 }
174
175 @Test("A rule left blank lists the answer as question and answer")
176 func blankRules() {
177 var lookup = Self.lookup
178 lookup.questionRules["working"] = .phrases(yes: "Working", no: " ")
179 lookup.questionRules["mood"] = .template(format: "")
180 lookup.questionRules["coffee"] = .count(singular: "coffee", plural: "")
181 lookup.questionRules["learned"] = .quote(lead: "")
182 let (narrative, details) = NarrativeBuilder.build(
183 responses: [
184 Self.response("working", .options([ResponseValue.noOption])),
185 Self.response("mood", .options(["Good"])),
186 Self.response("coffee", .number(2)),
187 Self.response("learned", .text("Tide tables")),
188 Self.response("working", .options([ResponseValue.yesOption])),
189 ],
190 lookup: lookup)
191 #expect(narrative == "Working.")
192 #expect(details.map(\.answer) == ["No", "Good", "2", "Tide tables"])
193 }
194
195 @Test("Answers without a rule are listed as question and answer")
196 func details() {
197 let (narrative, details) = Self.build([
198 Self.response("custom", .options([ResponseValue.yesOption])),
199 Self.response("unknown", .number(7)),
200 Self.response("activity", nil),
201 ])
202 #expect(narrative == nil)
203 #expect(details.map(\.answer) == ["Yes", "7"])
204 }
205}
206
207@MainActor
208@Suite("InlineCasing")
209struct InlineCasingTests {
210
211 @Test(
212 "Only ordinary first words are lowercased",
213 arguments: [
214 ("Drinking coffee", "drinking coffee"),
215 ("Travelling on a train", "travelling on a train"),
216 ("Traveling on a train", "traveling on a train"),
217 ("Playing games", "playing games"),
218 ("Watching Netflix", "watching Netflix"),
219 ("Studying Japanese", "studying Japanese"),
220 ("TV", "TV"),
221 ("iPhone repair", "iPhone repair"),
222 ("Paris trip", "Paris trip"),
223 ("散歩", "散歩"),
224 ]
225 )
226 func inline(text: String, expected: String) {
227 #expect(InlineCasing.inline(text) == expected)
228 }
229}
230
231@MainActor
232@Suite("PlacePhrase")
233struct PlacePhraseTests {
234
235 @Test(
236 "Ordinary places are lowercased, with 'the' unless they read without it, and names are kept",
237 arguments: [
238 ("Home", "home"),
239 ("Work", "work"),
240 ("Library", "the library"),
241 ("Gym", "the gym"),
242 ("Coffee shop", "the coffee shop"),
243 ("The beach", "the beach"),
244 ("Central Library", "Central Library"),
245 ("IKEA", "IKEA"),
246 ("Mum's", "Mum's"),
247 ("Gate 3", "Gate 3"),
248 ("Kichijōji", "Kichijōji"),
249 ("吉祥寺駅", "吉祥寺駅"),
250 ]
251 )
252 func phrase(text: String, expected: String) {
253 #expect(PlacePhrase.phrase(for: text) == expected)
254 }
255}
256
257@MainActor
258@Suite("ReportSummary")
259struct ReportSummaryTests {
260
261 @Test("The context line names the city with its region")
262 func contextLine() {
263 var context = Context()
264 context.location = LocationContext(
265 coordinate: Coordinate(latitude: 35.7, longitude: 139.58),
266 placemark: Placemark(name: "1-2-3", subLocality: "Kichijōji")
267 )
268 #expect(ReportSummary.contextLine(context) == "Kichijōji")
269
270 context.location?.placemark?.locality = "Musashino"
271 #expect(ReportSummary.contextLine(context) == "Musashino")
272
273
274
275 context.location?.placemark?.administrativeArea = "Tokyo"
276 #expect(ReportSummary.contextLine(context) == "Musashino, Tokyo")
277 context.location?.placemark?.administrativeArea = "Musashino"
278 #expect(ReportSummary.contextLine(context) == "Musashino")
279 context.location?.placemark?.administrativeArea = "Tokyo"
280
281 context.location?.placemark?.cityWithContext = "Suginami, Tokyo"
282 #expect(ReportSummary.contextLine(context) == "Suginami, Tokyo")
283 context.location?.placemark?.cityWithContext = nil
284
285 context.weather = WeatherContext(source: .darkSky, condition: "Cloudy", temperature: 18.2)
286 let line = ReportSummary.contextLine(context)
287 #expect(line?.hasPrefix("Musashino, Tokyo · ") == true)
288 #expect(line?.hasSuffix(" Cloudy") == true)
289 }
290
291 @Test("Health adds steps, or sleep on a wake report, with Reporter's steps standing in")
292 func healthReading() {
293 var context = Context()
294 context.motion = MotionContext(steps: 1200)
295 #expect(ReportSummary.contextLine(context) == "1,200 steps")
296
297 let health = HealthContext(
298 activity: ActivityHealth(steps: 4321),
299 sleep: SleepHealth(start: .now, end: .now, asleep: 7 * 3600 + 12 * 60 + 20))
300 #expect(ReportSummary.contextLine(context, health: health, kind: .random) == "4,321 steps")
301 #expect(ReportSummary.contextLine(context, health: health, kind: .wake) == "7h 12m asleep")
302
303 context.motion = MotionContext(steps: 0)
304 #expect(ReportSummary.contextLine(context) == nil)
305 }
306}