import XCTest @testable import FuelBoardShared /// The Best Value announcement gate — shown to users who updated into the /// feature after finishing onboarding, once per announcement version. /// /// Every fact the rule depends on is injectable, so the whole truth table is /// checked here instead of driving a simulator through four combinations (which /// is how a rule like this quietly rots). final class BestValueAnnouncementTests: XCTestCase { /// Nothing set up: the case that should be announced to. private var unconfigured: FuelSavingsSettings { FuelSavingsSettings.defaults } /// Enabled with the default 45 MPG / £40, which is a valid configuration. private var configured: FuelSavingsSettings { var settings = FuelSavingsSettings.defaults settings.enabled = true return settings } func testAnnouncesToAnExistingUserWhoHasNotSeenIt() { XCTAssertTrue( FuelStore.shouldAnnounceBestValue(settings: unconfigured, hasSeen: false, onboardingDue: false, isSuppressed: false), "an existing user, nothing configured, announcement unseen: show it" ) } func testNeverAnnouncesTwice() { XCTAssertFalse(FuelStore.shouldAnnounceBestValue(settings: unconfigured, hasSeen: true, onboardingDue: false)) } func testNeverWhileOnboardingIsDue() { // A new install runs the full onboarding, which already contains this // page — showing both would be the obvious first bug. XCTAssertFalse(FuelStore.shouldAnnounceBestValue(settings: unconfigured, hasSeen: false, onboardingDue: true)) } func testNeverWhileOnboardingIsOnScreen() { XCTAssertFalse(FuelStore.shouldAnnounceBestValue(settings: unconfigured, hasSeen: false, onboardingDue: false, isSuppressed: true)) } func testNeverForSomebodyWhoAlreadyConfiguredIt() { XCTAssertFalse(FuelStore.shouldAnnounceBestValue(settings: configured, hasSeen: false, onboardingDue: false), "never announce a feature the user set up themselves") } func testStillAnnouncesWhenEnabledButNotActuallyConfigured() { // Enabled with an out-of-range MPG is NOT configured, so the // announcement still has something useful to offer. var settings = FuelSavingsSettings.defaults settings.enabled = true settings.mpg = 2 // below the validity floor XCTAssertTrue(FuelStore.shouldAnnounceBestValue(settings: settings, hasSeen: false, onboardingDue: false)) } func testMarkingSeenIsWhatStopsTheNextLaunch() { // The stored contract the gate reads. FuelStore.markBestValueIntroSeen() XCTAssertTrue(FuelStore.hasSeenBestValueIntro()) XCTAssertFalse(FuelStore.shouldAnnounceBestValue(settings: unconfigured, onboardingDue: false)) } func testTheStoredValueIsTheAnnouncementVersionNotTheAppVersion() { // Keyed on the announcement version so a bugfix release cannot // re-announce anything. Bump `currentBestValueIntroVersion` only when // Best Value changes in a way worth re-explaining — this assertion is // the reminder that doing so re-shows the page to everyone. XCTAssertEqual(FuelStore.currentBestValueIntroVersion, 1) } }