summaryrefslogtreecommitdiff
path: root/HutchTests
diff options
context:
space:
mode:
authorChristian Cleberg <[email protected]>2026-07-15 22:38:22 -0500
committerChristian Cleberg <[email protected]>2026-07-15 22:38:55 -0500
commit3611d941247a7163fce9c61d8f75a81ddedd90b7 (patch)
tree860872deabc628b7ecedd055a45269deb65a1844 /HutchTests
parentb9ec80716ea015de5b6b31395fdc5ff03191398c (diff)
downloadhutch-3611d941247a7163fce9c61d8f75a81ddedd90b7.tar.gz
hutch-3611d941247a7163fce9c61d8f75a81ddedd90b7.tar.bz2
hutch-3611d941247a7163fce9c61d8f75a81ddedd90b7.zip
fix: start a new account at zero unread
An absent view record read as unread, so on first sign-in every thread a list had ever carried was unread. On a busy list that is thousands of threads the user never intended to read, inflating the Home dashboard, the needs-attention snapshot, and the widget with a number that means nothing. Record a baseline when an account is activated: mail that predates it is read, mail after it is not. activate() is the single funnel for launch validation, account switching, and adding an account, so one call covers every sign-in path. Accounts that already carry read state have been in use, so they get a distantPast baseline and keep every unread thread they had — upgrading must not silently mark a real backlog as read. markUnread now records an explicit distantPast marker instead of deleting the entry. Deleting would drop the thread back to the baseline rule, so marking an old thread unread would appear to do nothing.
Diffstat (limited to 'HutchTests')
-rw-r--r--HutchTests/InboxViewModelTests.swift104
1 files changed, 104 insertions, 0 deletions
diff --git a/HutchTests/InboxViewModelTests.swift b/HutchTests/InboxViewModelTests.swift
index 7bfc199..92f380a 100644
--- a/HutchTests/InboxViewModelTests.swift
+++ b/HutchTests/InboxViewModelTests.swift
@@ -42,6 +42,110 @@ struct InboxViewModelTests {
}
@Test
+ func freshAccountTreatsExistingMailAsRead() {
+ let suiteName = "InboxViewModelTests-\(UUID().uuidString)"
+ let defaults = UserDefaults(suiteName: suiteName)!
+ defer { defaults.removePersistentDomain(forName: suiteName) }
+
+ let signIn = Date(timeIntervalSince1970: 5_000)
+ InboxReadStateStore.establishBaselineIfNeeded(now: signIn, defaults: defaults)
+
+ // Years of list history should not land on a new user as unread.
+ #expect(
+ !InboxReadStateStore.isUnread(
+ threadID: "list#old",
+ lastActivityAt: Date(timeIntervalSince1970: 4_000),
+ defaults: defaults
+ )
+ )
+ }
+
+ @Test
+ func mailArrivingAfterSignInIsUnread() {
+ let suiteName = "InboxViewModelTests-\(UUID().uuidString)"
+ let defaults = UserDefaults(suiteName: suiteName)!
+ defer { defaults.removePersistentDomain(forName: suiteName) }
+
+ InboxReadStateStore.establishBaselineIfNeeded(now: Date(timeIntervalSince1970: 5_000), defaults: defaults)
+
+ #expect(
+ InboxReadStateStore.isUnread(
+ threadID: "list#new",
+ lastActivityAt: Date(timeIntervalSince1970: 6_000),
+ defaults: defaults
+ )
+ )
+ }
+
+ @Test
+ func mailExactlyAtTheBaselineIsRead() {
+ let suiteName = "InboxViewModelTests-\(UUID().uuidString)"
+ let defaults = UserDefaults(suiteName: suiteName)!
+ defer { defaults.removePersistentDomain(forName: suiteName) }
+
+ let signIn = Date(timeIntervalSince1970: 5_000)
+ InboxReadStateStore.establishBaselineIfNeeded(now: signIn, defaults: defaults)
+
+ #expect(!InboxReadStateStore.isUnread(threadID: "list#edge", lastActivityAt: signIn, defaults: defaults))
+ }
+
+ @Test
+ func baselineIsEstablishedOnceAndNotMovedBySubsequentSignIns() {
+ let suiteName = "InboxViewModelTests-\(UUID().uuidString)"
+ let defaults = UserDefaults(suiteName: suiteName)!
+ defer { defaults.removePersistentDomain(forName: suiteName) }
+
+ InboxReadStateStore.establishBaselineIfNeeded(now: Date(timeIntervalSince1970: 5_000), defaults: defaults)
+ // A later launch must not silently mark the backlog read.
+ InboxReadStateStore.establishBaselineIfNeeded(now: Date(timeIntervalSince1970: 9_000), defaults: defaults)
+
+ #expect(
+ InboxReadStateStore.isUnread(
+ threadID: "list#since",
+ lastActivityAt: Date(timeIntervalSince1970: 6_000),
+ defaults: defaults
+ )
+ )
+ }
+
+ @Test
+ func existingAccountsKeepTheirUnreadBacklog() {
+ let suiteName = "InboxViewModelTests-\(UUID().uuidString)"
+ let defaults = UserDefaults(suiteName: suiteName)!
+ defer { defaults.removePersistentDomain(forName: suiteName) }
+
+ // An account already carrying read state has been in use, so upgrading
+ // must not retroactively mark everything it had not read as read.
+ InboxReadStateStore.markViewed(Date(timeIntervalSince1970: 1_000), for: "list#seen", defaults: defaults)
+ InboxReadStateStore.establishBaselineIfNeeded(now: Date(timeIntervalSince1970: 5_000), defaults: defaults)
+
+ #expect(
+ InboxReadStateStore.isUnread(
+ threadID: "list#unseen",
+ lastActivityAt: Date(timeIntervalSince1970: 4_000),
+ defaults: defaults
+ )
+ )
+ }
+
+ @Test
+ func markingAnOldThreadUnreadSurvivesTheBaseline() {
+ let suiteName = "InboxViewModelTests-\(UUID().uuidString)"
+ let defaults = UserDefaults(suiteName: suiteName)!
+ defer { defaults.removePersistentDomain(forName: suiteName) }
+
+ InboxReadStateStore.establishBaselineIfNeeded(now: Date(timeIntervalSince1970: 5_000), defaults: defaults)
+ let oldActivity = Date(timeIntervalSince1970: 4_000)
+
+ #expect(!InboxReadStateStore.isUnread(threadID: "list#old", lastActivityAt: oldActivity, defaults: defaults))
+
+ // Explicitly marking it unread must stick, rather than falling back to the
+ // baseline rule and reading as read again.
+ InboxReadStateStore.markUnread(for: "list#old", defaults: defaults)
+ #expect(InboxReadStateStore.isUnread(threadID: "list#old", lastActivityAt: oldActivity, defaults: defaults))
+ }
+
+ @Test
func normalizesThreadSubjectsForDisplay() {
let summary = InboxThreadSummary(
rootEmailID: 1,