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
|
import Foundation
import Testing
@testable import Hutch
struct InboxViewModelTests {
@Test
func derivesRepositoryNameFromCommonPatchListSuffixes() {
#expect(InboxThreadUtilities.deriveRepositoryName(from: "hut-devel") == "hut")
#expect(InboxThreadUtilities.deriveRepositoryName(from: "git.patches") == "git")
#expect(InboxThreadUtilities.deriveRepositoryName(from: "discuss") == nil)
}
@Test
func parsesMailtoDraft() {
let draft = ThreadViewModel.mailComposeDraft(
from: "mailto:[email protected][email protected]&subject=Re:%20PATCH&body=LGTM"
)
#expect(draft?.recipients == ["[email protected]"])
#expect(draft?.ccRecipients == ["[email protected]"])
#expect(draft?.subject == "Re: PATCH")
#expect(draft?.body == "LGTM")
}
@Test
func computesLocalUnreadStateFromLastViewedMarker() {
let suiteName = "InboxViewModelTests-\(UUID().uuidString)"
let defaults = UserDefaults(suiteName: suiteName)!
defer { defaults.removePersistentDomain(forName: suiteName) }
let threadID = "list#message"
let lastActivity = Date(timeIntervalSince1970: 2_000)
#expect(InboxReadStateStore.isUnread(threadID: threadID, lastActivityAt: lastActivity, defaults: defaults))
InboxReadStateStore.markViewed(Date(timeIntervalSince1970: 1_000), for: threadID, defaults: defaults)
#expect(InboxReadStateStore.isUnread(threadID: threadID, lastActivityAt: lastActivity, defaults: defaults))
InboxReadStateStore.markViewed(Date(timeIntervalSince1970: 2_500), for: threadID, defaults: defaults)
#expect(!InboxReadStateStore.isUnread(threadID: threadID, lastActivityAt: lastActivity, defaults: defaults))
}
@Test
func normalizesThreadSubjectsForDisplay() {
let summary = InboxThreadSummary(
rootEmailID: 1,
rootMessageID: "message",
threadRootEmailIDs: [1],
threadRootMessageIDs: ["message"],
listID: 2,
listRID: "list",
listName: "hut-devel",
listOwner: Entity(canonicalName: "~owner"),
subject: "Re: Fwd: [PATCH] test: add parser ",
latestSender: Entity(canonicalName: "~sender"),
lastActivityAt: Date(timeIntervalSince1970: 2_000),
messageCount: 3,
repo: "hut",
containsPatch: true,
isUnread: true
)
#expect(summary.displaySubject == "[PATCH] test: add parser")
#expect(summary.metadataLine.contains("~sender"))
#expect(summary.metadataLine.contains("2 replies"))
}
@Test
func keepsDistinctThreadsDistinctByRootMessageID() {
let baseList = InboxMailingListReference(
id: 1,
rid: "list",
name: "hut-devel",
owner: Entity(canonicalName: "~owner")
)
let first = InboxThreadSummary(
rootEmailID: 10,
rootMessageID: "message-1",
threadRootEmailIDs: [10],
threadRootMessageIDs: ["message-1"],
listID: baseList.id,
listRID: baseList.rid,
listName: baseList.name,
listOwner: baseList.owner,
subject: "[PATCH] test",
latestSender: Entity(canonicalName: "~a"),
lastActivityAt: Date(timeIntervalSince1970: 100),
messageCount: 1,
repo: "hut",
containsPatch: true,
isUnread: true
)
let second = InboxThreadSummary(
rootEmailID: 11,
rootMessageID: "message-2",
threadRootEmailIDs: [11],
threadRootMessageIDs: ["message-2"],
listID: baseList.id,
listRID: baseList.rid,
listName: baseList.name,
listOwner: baseList.owner,
subject: "[PATCH] test",
latestSender: Entity(canonicalName: "~b"),
lastActivityAt: Date(timeIntervalSince1970: 200),
messageCount: 2,
repo: "hut",
containsPatch: true,
isUnread: true
)
#expect(first.id != second.id)
}
@Test
func mailingListThreadFilterMatchesSubjectAndSender() {
let baseList = InboxMailingListReference(
id: 1,
rid: "list",
name: "hutch-devel",
owner: Entity(canonicalName: "~owner")
)
let threads = [
InboxThreadSummary(
rootEmailID: 10,
rootMessageID: "message-1",
threadRootEmailIDs: [10],
threadRootMessageIDs: ["message-1"],
listID: baseList.id,
listRID: baseList.rid,
listName: baseList.name,
listOwner: baseList.owner,
subject: "Re: [PATCH] add search",
latestSender: Entity(canonicalName: "~alice"),
lastActivityAt: Date(timeIntervalSince1970: 100),
messageCount: 1,
repo: "hutch",
containsPatch: true,
isUnread: true
),
InboxThreadSummary(
rootEmailID: 11,
rootMessageID: "message-2",
threadRootEmailIDs: [11],
threadRootMessageIDs: ["message-2"],
listID: baseList.id,
listRID: baseList.rid,
listName: baseList.name,
listOwner: baseList.owner,
subject: "Release planning",
latestSender: Entity(canonicalName: "~bob"),
lastActivityAt: Date(timeIntervalSince1970: 200),
messageCount: 2,
repo: "hutch",
containsPatch: false,
isUnread: true
)
]
let subjectMatches = MailingListDetailViewModel.filterThreads(threads, matching: "search")
let senderMatches = MailingListDetailViewModel.filterThreads(threads, matching: "~bob")
#expect(subjectMatches.map(\.rootEmailID) == [10])
#expect(senderMatches.map(\.rootEmailID) == [11])
}
@Test
func segmentsPatchBodyAndTreatsSignatureAsPlainText() {
let body = """
From: Christian Cleberg <[email protected]>
---
test-patch.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 test-patch.txt
diff --git a/test-patch.txt b/test-patch.txt
new file mode 100644
index 0000000..c7b6eed
--- /dev/null
+++ b/test-patch.txt
@@ -0,0 +1 @@
+test Wed Mar 18 23:19:03 CDT 2026
--
2.50.1 (Apple Git-155)
"""
let segments = InboxThreadUtilities.segmentMessageBody(body, isPatch: true)
#expect(segments.count == 3)
guard case let .plainText(leadingPlainText) = segments[0] else {
Issue.record("Expected first segment to be plain text")
return
}
#expect(leadingPlainText.contains("From: Christian Cleberg <[email protected]>"))
#expect(leadingPlainText.contains("---"))
#expect(leadingPlainText.contains(" test-patch.txt | 1 +"))
#expect(leadingPlainText.contains(" 1 file changed, 1 insertion(+)"))
#expect(leadingPlainText.contains(" create mode 100644 test-patch.txt"))
guard case let .diff(diff) = segments[1] else {
Issue.record("Expected second segment to be diff")
return
}
#expect(diff.contains("diff --git a/test-patch.txt b/test-patch.txt"))
#expect(diff.contains("--- /dev/null"))
#expect(diff.contains("+++ b/test-patch.txt"))
#expect(diff.contains("+test Wed Mar 18 23:19:03 CDT 2026"))
#expect(!diff.contains("-- \n2.50.1 (Apple Git-155)"))
guard case let .plainText(trailingPlainText) = segments[2] else {
Issue.record("Expected third segment to be plain text")
return
}
#expect(trailingPlainText.contains("--"))
#expect(trailingPlainText.contains("2.50.1 (Apple Git-155)"))
}
}
|