diff --git a/DuckDuckGoTests/AIChat/AIChatSettingsTests.swift b/DuckDuckGoTests/AIChat/AIChatSettingsTests.swift index 93667a4aa9..2b320f8fd9 100644 --- a/DuckDuckGoTests/AIChat/AIChatSettingsTests.swift +++ b/DuckDuckGoTests/AIChat/AIChatSettingsTests.swift @@ -28,12 +28,14 @@ class AIChatSettingsTests: XCTestCase { private var mockPrivacyConfigurationManager: PrivacyConfigurationManagerMock! private var mockInternalUserDecider: MockInternalUserDecider! private var mockUserDefaults: UserDefaults! + private var mockNotificationCenter: NotificationCenter! override func setUp() { super.setUp() mockPrivacyConfigurationManager = PrivacyConfigurationManagerMock() mockInternalUserDecider = MockInternalUserDecider() mockUserDefaults = UserDefaults(suiteName: "TestDefaults") + mockNotificationCenter = NotificationCenter() } override func tearDown() { @@ -41,13 +43,15 @@ class AIChatSettingsTests: XCTestCase { mockPrivacyConfigurationManager = nil mockInternalUserDecider = nil mockUserDefaults = nil + mockNotificationCenter = nil super.tearDown() } func testAIChatURLReturnsDefaultWhenRemoteSettingsMissing() { let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager, internalUserDecider: mockInternalUserDecider, - userDefaults: mockUserDefaults) + userDefaults: mockUserDefaults, + notificationCenter: mockNotificationCenter) (mockPrivacyConfigurationManager.privacyConfig as? PrivacyConfigurationMock)?.settings = [:] @@ -58,7 +62,8 @@ class AIChatSettingsTests: XCTestCase { func testAIChatURLReturnsRemoteSettingWhenAvailable() { let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager, internalUserDecider: mockInternalUserDecider, - userDefaults: mockUserDefaults) + userDefaults: mockUserDefaults, + notificationCenter: mockNotificationCenter) let remoteURL = "https://example.com/ai-chat" (mockPrivacyConfigurationManager.privacyConfig as? PrivacyConfigurationMock)?.settings = [ @@ -71,7 +76,8 @@ class AIChatSettingsTests: XCTestCase { func testIsAIChatFeatureEnabledWhenFeatureIsEnabled() { let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager, internalUserDecider: mockInternalUserDecider, - userDefaults: mockUserDefaults) + userDefaults: mockUserDefaults, + notificationCenter: mockNotificationCenter) (mockPrivacyConfigurationManager.privacyConfig as? PrivacyConfigurationMock)?.enabledFeaturesForVersions = [ .aiChat: [AppVersionProvider().appVersion() ?? ""] @@ -83,7 +89,8 @@ class AIChatSettingsTests: XCTestCase { func testIsAIChatFeatureEnabledForInternalUser() { let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager, internalUserDecider: mockInternalUserDecider, - userDefaults: mockUserDefaults) + userDefaults: mockUserDefaults, + notificationCenter: mockNotificationCenter) mockInternalUserDecider.mockIsInternalUser = true XCTAssertTrue(settings.isAIChatFeatureEnabled) @@ -92,7 +99,8 @@ class AIChatSettingsTests: XCTestCase { func testEnableAIChatBrowsingMenuUserSettings() { let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager, internalUserDecider: mockInternalUserDecider, - userDefaults: mockUserDefaults) + userDefaults: mockUserDefaults, + notificationCenter: mockNotificationCenter) (mockPrivacyConfigurationManager.privacyConfig as? PrivacyConfigurationMock)?.enabledFeaturesForVersions = [ .aiChat: [AppVersionProvider().appVersion() ?? ""] @@ -107,8 +115,37 @@ class AIChatSettingsTests: XCTestCase { settings.enableAIChatBrowsingMenuUserSettings(enable: true) XCTAssertTrue(settings.isAIChatBrowsingToolbarShortcutFeatureEnabled) } -} + func testEnableAIChatAddressBarUserSettings() { + let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager, + internalUserDecider: mockInternalUserDecider, + userDefaults: mockUserDefaults, + notificationCenter: mockNotificationCenter) + + settings.enableAIChatAddressBarUserSettings(enable: false) + XCTAssertFalse(settings.isAIChatAddressBarUserSettingsEnabled) + + settings.enableAIChatAddressBarUserSettings(enable: true) + XCTAssertTrue(settings.isAIChatAddressBarUserSettingsEnabled) + } + + func testNotificationPostedWhenSettingsChange() { + let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager, + internalUserDecider: mockInternalUserDecider, + userDefaults: mockUserDefaults, + notificationCenter: mockNotificationCenter) + + let expectation = self.expectation(description: "Notification should be posted") + + let observer = mockNotificationCenter.addObserver(forName: .aiChatSettingsChanged, object: nil, queue: nil) { _ in + expectation.fulfill() + } + + settings.enableAIChatBrowsingMenuUserSettings(enable: false) + waitForExpectations(timeout: 1, handler: nil) + mockNotificationCenter.removeObserver(observer) + } +} final private class MockInternalUserDecider: InternalUserDecider { var mockIsInternalUser: Bool = false