Files
element-x-ios/UnitTests/Sources/UserSettingsTests.swift
T
Doug 93825c01ad Initial implementation of AccountSettings. (#6275)
* Make the UserPreference macro also generate an accessor for the key name.

* Initial implementation of AccountSettings.

- Add an AccountUserDefaults storage with a userID prefix on the key.
- Migrate the 2 values reset by AppSettings.resetSessionSpecificSettings to AccountSettings.

* Rename UserSettings.volatile() to UserSettings.mock()

Given it now has a userID parameter with a default value..
2026-10-06 17:52:24 +01:00

103 lines
4.5 KiB
Swift

//
// Copyright 2026 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
@testable import ElementX
import Foundation
import Testing
struct UserSettingsTests {
private let store = VolatileUserDefaults()
private let appSettings: AppSettings
private let aliceSettings: UserSettings
private let bobSettings: UserSettings
init() {
appSettings = AppSettings(store: store)
aliceSettings = appSettings.userSettings(for: "@alice:example.com")
bobSettings = appSettings.userSettings(for: "@bob:example.com")
}
@Test
func accountSettingsAreScopedToAccount() {
// Given settings for two different accounts.
#expect(aliceSettings.searchBreadcrumbs.isEmpty)
#expect(bobSettings.searchBreadcrumbs.isEmpty)
// When updating an account setting for Alice.
aliceSettings.searchBreadcrumbs = [.query("Alice")]
// Then only Alice should see the change.
#expect(aliceSettings.searchBreadcrumbs == [.query("Alice")])
#expect(bobSettings.searchBreadcrumbs.isEmpty)
}
@Test
func appSettingsAreShared() {
// Given settings for two different accounts.
#expect(!appSettings.hasSignedInBefore)
#expect(!aliceSettings.hasSignedInBefore)
#expect(!bobSettings.hasSignedInBefore)
// When updating an app setting through Alice's settings.
aliceSettings.hasSignedInBefore = true
// Then the change should be seen by the app and every account.
#expect(appSettings.hasSignedInBefore)
#expect(aliceSettings.hasSignedInBefore)
#expect(bobSettings.hasSignedInBefore)
}
@Test
func migrateAppSettingsValueToAccountSettings() throws {
// Given a store containing values saved under the keys previously used by the app settings.
// These keys are hardcoded so that renaming a property breaks this test instead of the migration.
store.set(true, forKey: "hasRunIdentityConfirmationOnboarding")
try store.set(JSONEncoder().encode([SearchBreadcrumb.query("Alice")]), forKey: "searchBreadcrumbs")
#expect(store.object(forKey: "hasRunIdentityConfirmationOnboarding") != nil)
#expect(store.object(forKey: "searchBreadcrumbs") != nil)
#expect(!aliceSettings.hasRunIdentityConfirmationOnboarding)
#expect(aliceSettings.searchBreadcrumbs.isEmpty)
// When migrating those values into Alice's account settings.
aliceSettings.migrateAppSettingsValueToAccountSettings(\.hasRunIdentityConfirmationOnboardingKey)
aliceSettings.migrateAppSettingsValueToAccountSettings(\.searchBreadcrumbsKey)
// Then the values should belong to Alice only.
#expect(aliceSettings.hasRunIdentityConfirmationOnboarding)
#expect(aliceSettings.searchBreadcrumbs == [.query("Alice")])
#expect(!bobSettings.hasRunIdentityConfirmationOnboarding)
#expect(bobSettings.searchBreadcrumbs.isEmpty)
#expect(store.object(forKey: "hasRunIdentityConfirmationOnboarding") == nil)
#expect(store.object(forKey: "searchBreadcrumbs") == nil)
}
@Test
func resetSessionSpecificSettings() {
// Given app settings and account settings for two different accounts.
appSettings.hasSignedInBefore = true
aliceSettings.hasRunIdentityConfirmationOnboarding = true
aliceSettings.searchBreadcrumbs = [.query("Alice")]
bobSettings.hasRunIdentityConfirmationOnboarding = true
bobSettings.searchBreadcrumbs = [.query("Bob")]
#expect(appSettings.hasSignedInBefore)
#expect(aliceSettings.hasRunIdentityConfirmationOnboarding)
#expect(aliceSettings.searchBreadcrumbs == [.query("Alice")])
#expect(bobSettings.hasRunIdentityConfirmationOnboarding)
#expect(bobSettings.searchBreadcrumbs == [.query("Bob")])
// When resetting Alice's session specific settings.
aliceSettings.resetSessionSpecificSettings()
// Then only Alice's account settings should be reset.
#expect(!aliceSettings.hasRunIdentityConfirmationOnboarding)
#expect(aliceSettings.searchBreadcrumbs.isEmpty)
#expect(bobSettings.hasRunIdentityConfirmationOnboarding)
#expect(bobSettings.searchBreadcrumbs == [.query("Bob")])
#expect(appSettings.hasSignedInBefore)
}
}