Files
MobileMusicAssistant/MobileMusicAssistantTests/MAStoreManagerTests.swift
T

86 lines
3.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Testing
import Foundation
@testable import Mobile_Music_Assistant
// MARK: - MAStoreManager Nudge Logic
@Suite("MAStoreManager Nudge Logic")
@MainActor
struct MAStoreManagerNudgeTests {
// Use isolated UserDefaults to avoid polluting real app state
private func makeStore(firstLaunch: Date, lastKeyDate: Date?) -> MAStoreManager {
let store = MAStoreManager()
let defaults = UserDefaults.standard
defaults.set(firstLaunch, forKey: "ma_firstLaunchDate")
if let last = lastKeyDate {
defaults.set(last, forKey: "ma_lastNudgeOrPurchaseDate")
} else {
defaults.removeObject(forKey: "ma_lastNudgeOrPurchaseDate")
}
return store
}
// MARK: - Initial trigger
@Test("Does not nudge before 3 days have passed")
func noNudgeBeforeThreeDays() {
let store = makeStore(firstLaunch: Date(), lastKeyDate: nil)
#expect(store.shouldShowNudge == false)
}
@Test("Does not nudge at exactly 2 days 23 hours")
func noNudgeJustBeforeThreshold() {
let almostThreeDays = Date().addingTimeInterval(-(3 * 24 * 3600 - 60))
let store = makeStore(firstLaunch: almostThreeDays, lastKeyDate: nil)
#expect(store.shouldShowNudge == false)
}
@Test("Nudges after 3 days with no prior interaction")
func nudgeAfterThreeDays() {
let threeDaysAgo = Date().addingTimeInterval(-(3 * 24 * 3600 + 1))
let store = makeStore(firstLaunch: threeDaysAgo, lastKeyDate: nil)
#expect(store.shouldShowNudge == true)
}
// MARK: - 6-month repeat
@Test("Does not nudge again within 6 months of last interaction")
func noNudgeWithinSixMonths() {
let fourMonthsAgo = Date().addingTimeInterval(-(4 * 30 * 24 * 3600))
let fiveMonthsAgo = Date().addingTimeInterval(-(5 * 30 * 24 * 3600))
let store = makeStore(firstLaunch: fiveMonthsAgo, lastKeyDate: fourMonthsAgo)
#expect(store.shouldShowNudge == false)
}
@Test("Nudges again after 6 months since last interaction")
func nudgeAfterSixMonths() {
let sevenMonthsAgo = Date().addingTimeInterval(-(7 * 30 * 24 * 3600))
let sixMonthsAgo = Date().addingTimeInterval(-(6 * 30 * 24 * 3600 + 60))
let store = makeStore(firstLaunch: sevenMonthsAgo, lastKeyDate: sixMonthsAgo)
#expect(store.shouldShowNudge == true)
}
// MARK: - recordNudgeShown
@Test("recordNudgeShown suppresses future nudge within 6 months")
func recordNudgeShownSuppressesNudge() {
let fourDaysAgo = Date().addingTimeInterval(-(4 * 24 * 3600))
let store = makeStore(firstLaunch: fourDaysAgo, lastKeyDate: nil)
#expect(store.shouldShowNudge == true)
store.recordNudgeShown()
#expect(store.shouldShowNudge == false)
}
// MARK: - firstLaunchDate persistence
@Test("firstLaunchDate is written once and not overwritten")
func firstLaunchDateNotOverwritten() {
let expectedDate = Date().addingTimeInterval(-100)
UserDefaults.standard.set(expectedDate, forKey: "ma_firstLaunchDate")
let store = MAStoreManager()
let delta = abs(store.firstLaunchDate.timeIntervalSince(expectedDate))
#expect(delta < 1.0)
}
}