-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeatmapWidgetSnapshotFactoryTests.swift
More file actions
230 lines (208 loc) · 10.1 KB
/
Copy pathHeatmapWidgetSnapshotFactoryTests.swift
File metadata and controls
230 lines (208 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// HeatmapWidgetSnapshotFactoryTests.swift
// DevLogWidgetCoreTests
//
// Created by opfic on 4/17/26.
//
import Foundation
import Testing
import DevLogCore
@testable import DevLogWidgetCore
struct HeatmapWidgetSnapshotFactoryTests {
@Test("Heatmap 위젯 스냅샷은 이번 분기 기준 월과 일별 count를 만든다")
func heatmap_위젯_스냅샷은_이번_분기_기준_월과_일별_count를_만든다() throws {
let calendar = Calendar(identifier: .gregorian)
let quarterStart = try #require(calendar.date(from: DateComponents(year: 2026, month: 4, day: 1)))
let mayStart = try #require(calendar.date(from: DateComponents(year: 2026, month: 5, day: 1)))
let juneStart = try #require(calendar.date(from: DateComponents(year: 2026, month: 6, day: 1)))
let aprilThirdDate = calendar.date(from: DateComponents(year: 2026, month: 4, day: 3))!
let mayFirstDate = calendar.date(from: DateComponents(year: 2026, month: 5, day: 1))!
let aprilFifteenthDate = calendar.date(from: DateComponents(year: 2026, month: 4, day: 15))!
let factory = HeatmapWidgetSnapshotFactory(calendar: calendar)
let snapshot = factory.makeSnapshot(
createdTodos: [
makeTodo(
id: "todo-created-apr-03",
createdAt: try #require(calendar.date(from: DateComponents(year: 2026, month: 4, day: 3)))
),
makeTodo(
id: "todo-created-mar-31",
createdAt: try #require(calendar.date(from: DateComponents(year: 2026, month: 3, day: 31)))
)
],
completedTodos: [
makeTodo(
id: "todo-completed-apr-03",
createdAt: quarterStart,
completedAt: aprilThirdDate
),
makeTodo(
id: "todo-completed-may-01",
createdAt: quarterStart,
completedAt: mayFirstDate
)
],
deletedTodos: [
makeTodo(
id: "todo-deleted-apr-15",
createdAt: quarterStart,
deletedAt: aprilFifteenthDate
)
],
selectedActivityKinds: [.created, .completed],
quarterStart: quarterStart,
now: quarterStart
)
#expect(snapshot.quarterStart == quarterStart)
#expect(snapshot.selectedActivityKindRawValues == ["created", "completed"])
#expect(snapshot.maxCount == 2)
#expect(snapshot.months.count == 3)
#expect(snapshot.months.map(\.monthStart) == [
quarterStart,
mayStart,
juneStart
])
#expect(snapshot.months[0].weeks.count == 5)
#expect(snapshot.months.flatMap(\.weeks).flatMap(\.days).filter(\.isVisible).count == 91)
let aprilThird = try #require(day(for: DateComponents(year: 2026, month: 4, day: 3), in: snapshot, calendar: calendar))
#expect(aprilThird.createdCount == 1)
#expect(aprilThird.completedCount == 1)
#expect(aprilThird.deletedCount == 0)
#expect(aprilThird.isVisible)
let aprilFifteenth = try #require(day(for: DateComponents(year: 2026, month: 4, day: 15), in: snapshot, calendar: calendar))
#expect(aprilFifteenth.createdCount == 0)
#expect(aprilFifteenth.completedCount == 0)
#expect(aprilFifteenth.deletedCount == 1)
let mayFirst = try #require(day(for: DateComponents(year: 2026, month: 5, day: 1), in: snapshot, calendar: calendar))
#expect(mayFirst.completedCount == 1)
}
@Test("Heatmap 위젯 스냅샷 maxCount는 선택된 activity kind만 기준으로 계산한다")
func heatmap_위젯_스냅샷_maxCount는_선택된_activity_kind만_기준으로_계산한다() throws {
let calendar = Calendar(identifier: .gregorian)
let quarterStart = try #require(calendar.date(from: DateComponents(year: 2026, month: 4, day: 1)))
let targetDate = try #require(calendar.date(from: DateComponents(year: 2026, month: 4, day: 10)))
let factory = HeatmapWidgetSnapshotFactory(calendar: calendar)
let snapshot = factory.makeSnapshot(
createdTodos: [
makeTodo(id: "created-1", createdAt: targetDate),
makeTodo(id: "created-2", createdAt: targetDate)
],
completedTodos: [],
deletedTodos: [
makeTodo(
id: "deleted-1",
createdAt: quarterStart,
deletedAt: targetDate
),
makeTodo(
id: "deleted-2",
createdAt: quarterStart,
deletedAt: targetDate
),
makeTodo(
id: "deleted-3",
createdAt: quarterStart,
deletedAt: targetDate
)
],
selectedActivityKinds: [.deleted],
quarterStart: quarterStart,
now: quarterStart
)
#expect(snapshot.selectedActivityKindRawValues == ["deleted"])
#expect(snapshot.maxCount == 3)
let targetDay = try #require(day(for: DateComponents(year: 2026, month: 4, day: 10), in: snapshot, calendar: calendar))
#expect(targetDay.createdCount == 2)
#expect(targetDay.deletedCount == 3)
}
@Test("Heatmap 위젯 스냅샷은 분기 시작일은 포함하고 다음 분기 시작일은 제외한다")
func heatmap_위젯_스냅샷은_분기_시작일은_포함하고_다음_분기_시작일은_제외한다() throws {
let calendar = Calendar(identifier: .gregorian)
let quarterStart = try #require(calendar.date(from: DateComponents(year: 2026, month: 4, day: 1)))
let nextQuarterStart = try #require(calendar.date(from: DateComponents(year: 2026, month: 7, day: 1)))
let quarterLastDate = try #require(calendar.date(byAdding: .second, value: -1, to: nextQuarterStart))
let factory = HeatmapWidgetSnapshotFactory(calendar: calendar)
let snapshot = factory.makeSnapshot(
createdTodos: [
makeTodo(id: "created-quarter-start", createdAt: quarterStart),
makeTodo(id: "created-next-quarter-start", createdAt: nextQuarterStart)
],
completedTodos: [
makeTodo(
id: "completed-quarter-last-date",
createdAt: quarterStart,
completedAt: quarterLastDate
)
],
deletedTodos: [],
selectedActivityKinds: [.created, .completed],
quarterStart: quarterStart,
now: quarterStart
)
let aprilFirst = try #require(day(for: DateComponents(year: 2026, month: 4, day: 1), in: snapshot, calendar: calendar))
let juneThirtieth = try #require(day(for: DateComponents(year: 2026, month: 6, day: 30), in: snapshot, calendar: calendar))
#expect(aprilFirst.createdCount == 1)
#expect(juneThirtieth.completedCount == 1)
#expect(day(for: DateComponents(year: 2026, month: 7, day: 1), in: snapshot, calendar: calendar) == nil)
#expect(snapshot.maxCount == 1)
}
@Test("Heatmap 위젯 스냅샷은 Q4 분기를 다음 해 1월 전까지 만든다")
func heatmap_위젯_스냅샷은_q4_분기를_다음_해_1월_전까지_만든다() throws {
let calendar = Calendar(identifier: .gregorian)
let q4Date = try #require(calendar.date(from: DateComponents(year: 2026, month: 11, day: 10)))
let octoberStart = try #require(calendar.date(from: DateComponents(year: 2026, month: 10, day: 1)))
let novemberStart = try #require(calendar.date(from: DateComponents(year: 2026, month: 11, day: 1)))
let decemberStart = try #require(calendar.date(from: DateComponents(year: 2026, month: 12, day: 1)))
let decemberLastDate = try #require(calendar.date(from: DateComponents(year: 2026, month: 12, day: 31)))
let nextYearStart = try #require(calendar.date(from: DateComponents(year: 2027, month: 1, day: 1)))
let factory = HeatmapWidgetSnapshotFactory(calendar: calendar)
let snapshot = factory.makeSnapshot(
createdTodos: [
makeTodo(id: "created-december-last-date", createdAt: decemberLastDate),
makeTodo(id: "created-next-year-start", createdAt: nextYearStart)
],
completedTodos: [],
deletedTodos: [],
selectedActivityKinds: [.created],
quarterStart: q4Date,
now: q4Date
)
let decemberLastDay = try #require(day(for: DateComponents(year: 2026, month: 12, day: 31), in: snapshot, calendar: calendar))
#expect(snapshot.quarterStart == octoberStart)
#expect(snapshot.months.map(\.monthStart) == [octoberStart, novemberStart, decemberStart])
#expect(decemberLastDay.createdCount == 1)
#expect(day(for: DateComponents(year: 2027, month: 1, day: 1), in: snapshot, calendar: calendar) == nil)
#expect(snapshot.maxCount == 1)
}
private func day(
for components: DateComponents,
in snapshot: HeatmapWidgetSnapshot,
calendar: Calendar
) -> WidgetHeatmapDaySnapshot? {
guard let date = calendar.date(from: components) else { return nil }
let targetDate = calendar.startOfDay(for: date)
return snapshot.months
.flatMap(\.weeks)
.flatMap(\.days)
.first { day in
day.isVisible && calendar.isDate(day.date, inSameDayAs: targetDate)
}
}
private func makeTodo(
id: String,
createdAt: Date,
completedAt: Date? = nil,
deletedAt: Date? = nil
) -> WidgetTodoSnapshot {
WidgetTodoSnapshot(
id: id,
number: 1,
title: id,
isPinned: false,
createdAt: createdAt,
completedAt: completedAt,
deletedAt: deletedAt,
dueDate: nil
)
}
}