2023-08-11 16:10:23 +02:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2022-2024 New Vector Ltd.
|
2023-08-11 16:10:23 +02:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2023-08-11 16:10:23 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
import MatrixRustSDK
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
@testable import ElementX
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
class NotificationSettingsEditScreenViewModelTests: XCTestCase {
|
|
|
|
private var viewModel: NotificationSettingsEditScreenViewModelProtocol!
|
|
|
|
private var notificationSettingsProxy: NotificationSettingsProxyMock!
|
2023-08-22 14:33:39 +02:00
|
|
|
private var userSession: UserSessionProtocol!
|
2023-08-11 16:10:23 +02:00
|
|
|
|
|
|
|
private var context: NotificationSettingsEditScreenViewModelType.Context {
|
|
|
|
viewModel.context
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor override func setUpWithError() throws {
|
2024-02-27 16:22:47 +02:00
|
|
|
let clientProxy = ClientProxyMock(.init(userID: "@a:b.com"))
|
2024-05-27 14:44:15 +03:00
|
|
|
userSession = UserSessionMock(.init(clientProxy: clientProxy))
|
2023-08-11 16:10:23 +02:00
|
|
|
notificationSettingsProxy = NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration())
|
|
|
|
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReturnValue = .allMessages
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFetchSettings() async throws {
|
|
|
|
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneClosure = { isEncrypted, isOneToOne in
|
|
|
|
switch (isEncrypted, isOneToOne) {
|
|
|
|
case (_, true):
|
|
|
|
return .allMessages
|
|
|
|
case (_, _):
|
|
|
|
return .mentionsAndKeywordsOnly
|
|
|
|
}
|
|
|
|
}
|
2023-08-22 14:33:39 +02:00
|
|
|
viewModel = NotificationSettingsEditScreenViewModel(chatType: .groupChat,
|
|
|
|
userSession: userSession,
|
2023-08-11 16:10:23 +02:00
|
|
|
notificationSettingsProxy: notificationSettingsProxy)
|
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
let deferred = deferFulfillment(viewModel.context.$viewState) { state in
|
|
|
|
state.defaultMode != nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
viewModel.fetchInitialContent()
|
2023-09-26 13:28:29 +03:00
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
// `getDefaultRoomNotificationModeIsEncryptedIsOneToOne` must have been called twice (for encrypted and unencrypted group chats)
|
|
|
|
let invocations = notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReceivedInvocations
|
|
|
|
|
|
|
|
XCTAssertEqual(invocations.count, 2)
|
|
|
|
// First call for encrypted group chats
|
|
|
|
XCTAssertEqual(invocations[0].isEncrypted, true)
|
|
|
|
XCTAssertEqual(invocations[0].isOneToOne, false)
|
|
|
|
// Second call for unencrypted group chats
|
|
|
|
XCTAssertEqual(invocations[1].isEncrypted, false)
|
|
|
|
XCTAssertEqual(invocations[1].isOneToOne, false)
|
|
|
|
|
|
|
|
XCTAssertEqual(context.viewState.defaultMode, .mentionsAndKeywordsOnly)
|
|
|
|
XCTAssertNil(context.viewState.bindings.alertInfo)
|
2024-01-12 09:46:51 +01:00
|
|
|
XCTAssertFalse(context.viewState.canPushEncryptedEvents)
|
2023-11-15 15:57:17 +01:00
|
|
|
XCTAssertNotNil(context.viewState.description(for: .mentionsAndKeywordsOnly))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFetchSettingsWithCanPushEncryptedEvents() async throws {
|
|
|
|
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneClosure = { isEncrypted, isOneToOne in
|
|
|
|
switch (isEncrypted, isOneToOne) {
|
|
|
|
case (_, true):
|
|
|
|
return .allMessages
|
|
|
|
case (_, _):
|
|
|
|
return .mentionsAndKeywordsOnly
|
|
|
|
}
|
|
|
|
}
|
2024-01-12 09:46:51 +01:00
|
|
|
notificationSettingsProxy.canPushEncryptedEventsToDeviceClosure = {
|
2023-11-15 15:57:17 +01:00
|
|
|
true
|
|
|
|
}
|
|
|
|
viewModel = NotificationSettingsEditScreenViewModel(chatType: .groupChat,
|
|
|
|
userSession: userSession,
|
|
|
|
notificationSettingsProxy: notificationSettingsProxy)
|
|
|
|
|
|
|
|
let deferred = deferFulfillment(viewModel.context.$viewState) { state in
|
|
|
|
state.defaultMode != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
viewModel.fetchInitialContent()
|
|
|
|
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
// `getDefaultRoomNotificationModeIsEncryptedIsOneToOne` must have been called twice (for encrypted and unencrypted group chats)
|
|
|
|
let invocations = notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReceivedInvocations
|
|
|
|
|
|
|
|
XCTAssertEqual(invocations.count, 2)
|
|
|
|
// First call for encrypted group chats
|
|
|
|
XCTAssertEqual(invocations[0].isEncrypted, true)
|
|
|
|
XCTAssertEqual(invocations[0].isOneToOne, false)
|
|
|
|
// Second call for unencrypted group chats
|
|
|
|
XCTAssertEqual(invocations[1].isEncrypted, false)
|
|
|
|
XCTAssertEqual(invocations[1].isOneToOne, false)
|
|
|
|
|
|
|
|
XCTAssertEqual(context.viewState.defaultMode, .mentionsAndKeywordsOnly)
|
|
|
|
XCTAssertNil(context.viewState.bindings.alertInfo)
|
2024-01-12 09:46:51 +01:00
|
|
|
XCTAssertTrue(context.viewState.canPushEncryptedEvents)
|
2023-11-15 15:57:17 +01:00
|
|
|
XCTAssertNil(context.viewState.description(for: .mentionsAndKeywordsOnly))
|
2023-08-11 16:10:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSetModeAllMessages() async throws {
|
|
|
|
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReturnValue = .mentionsAndKeywordsOnly
|
2023-08-22 14:33:39 +02:00
|
|
|
viewModel = NotificationSettingsEditScreenViewModel(chatType: .groupChat,
|
|
|
|
userSession: userSession,
|
2023-08-11 16:10:23 +02:00
|
|
|
notificationSettingsProxy: notificationSettingsProxy)
|
2023-09-26 13:28:29 +03:00
|
|
|
let deferred = deferFulfillment(viewModel.context.$viewState) { state in
|
|
|
|
state.defaultMode != nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
viewModel.fetchInitialContent()
|
2023-09-26 13:28:29 +03:00
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
try await deferred.fulfill()
|
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
var deferredViewState = deferFulfillment(viewModel.context.$viewState, keyPath: \.pendingMode, transitionValues: [nil, .allMessages, nil])
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
context.send(viewAction: .setMode(.allMessages))
|
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
try await deferredViewState.fulfill()
|
2023-08-11 16:10:23 +02:00
|
|
|
|
|
|
|
// `setDefaultRoomNotificationModeIsEncryptedIsOneToOneMode` must have been called twice (for encrypted and unencrypted group chats)
|
|
|
|
let invocations = notificationSettingsProxy.setDefaultRoomNotificationModeIsEncryptedIsOneToOneModeReceivedInvocations
|
|
|
|
XCTAssertEqual(notificationSettingsProxy.setDefaultRoomNotificationModeIsEncryptedIsOneToOneModeCallsCount, 2)
|
|
|
|
// First call for encrypted group chats
|
|
|
|
XCTAssertEqual(invocations[0].isEncrypted, true)
|
|
|
|
XCTAssertEqual(invocations[0].isOneToOne, false)
|
|
|
|
XCTAssertEqual(invocations[0].mode, .allMessages)
|
|
|
|
// Second call for unencrypted group chats
|
|
|
|
XCTAssertEqual(invocations[1].isEncrypted, false)
|
|
|
|
XCTAssertEqual(invocations[1].isOneToOne, false)
|
|
|
|
XCTAssertEqual(invocations[1].mode, .allMessages)
|
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
deferredViewState = deferFulfillment(viewModel.context.$viewState,
|
|
|
|
keyPath: \.defaultMode,
|
|
|
|
transitionValues: [.allMessages])
|
|
|
|
|
|
|
|
try await deferredViewState.fulfill()
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
XCTAssertEqual(context.viewState.defaultMode, .allMessages)
|
|
|
|
XCTAssertNil(context.viewState.bindings.alertInfo)
|
|
|
|
}
|
2023-09-26 13:28:29 +03:00
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
func testSetModeMentions() async throws {
|
2023-08-22 14:33:39 +02:00
|
|
|
viewModel = NotificationSettingsEditScreenViewModel(chatType: .groupChat,
|
|
|
|
userSession: userSession,
|
2023-08-11 16:10:23 +02:00
|
|
|
notificationSettingsProxy: notificationSettingsProxy)
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
let deferred = deferFulfillment(viewModel.context.$viewState) { state in
|
|
|
|
state.defaultMode != nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
viewModel.fetchInitialContent()
|
2023-09-26 13:28:29 +03:00
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
try await deferred.fulfill()
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
var deferredViewState = deferFulfillment(viewModel.context.$viewState,
|
|
|
|
keyPath: \.pendingMode,
|
|
|
|
transitionValues: [nil, .mentionsAndKeywordsOnly, nil])
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
context.send(viewAction: .setMode(.mentionsAndKeywordsOnly))
|
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
try await deferredViewState.fulfill()
|
2023-08-11 16:10:23 +02:00
|
|
|
|
|
|
|
// `setDefaultRoomNotificationModeIsEncryptedIsOneToOneMode` must have been called twice (for encrypted and unencrypted group chats)
|
|
|
|
let invocations = notificationSettingsProxy.setDefaultRoomNotificationModeIsEncryptedIsOneToOneModeReceivedInvocations
|
|
|
|
XCTAssertEqual(notificationSettingsProxy.setDefaultRoomNotificationModeIsEncryptedIsOneToOneModeCallsCount, 2)
|
|
|
|
// First call for encrypted group chats
|
|
|
|
XCTAssertEqual(invocations[0].isEncrypted, true)
|
|
|
|
XCTAssertEqual(invocations[0].isOneToOne, false)
|
|
|
|
XCTAssertEqual(invocations[0].mode, .mentionsAndKeywordsOnly)
|
|
|
|
// Second call for unencrypted group chats
|
|
|
|
XCTAssertEqual(invocations[1].isEncrypted, false)
|
|
|
|
XCTAssertEqual(invocations[1].isOneToOne, false)
|
|
|
|
XCTAssertEqual(invocations[1].mode, .mentionsAndKeywordsOnly)
|
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
deferredViewState = deferFulfillment(viewModel.context.$viewState,
|
|
|
|
keyPath: \.defaultMode,
|
|
|
|
transitionValues: [.mentionsAndKeywordsOnly])
|
|
|
|
|
|
|
|
try await deferredViewState.fulfill()
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
XCTAssertEqual(context.viewState.defaultMode, .mentionsAndKeywordsOnly)
|
|
|
|
XCTAssertNil(context.viewState.bindings.alertInfo)
|
|
|
|
}
|
2023-09-26 13:28:29 +03:00
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
func testSetModeDirectChats() async throws {
|
|
|
|
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReturnValue = .mentionsAndKeywordsOnly
|
|
|
|
// Initialize for direct chats
|
2023-08-22 14:33:39 +02:00
|
|
|
viewModel = NotificationSettingsEditScreenViewModel(chatType: .oneToOneChat,
|
|
|
|
userSession: userSession,
|
2023-08-11 16:10:23 +02:00
|
|
|
notificationSettingsProxy: notificationSettingsProxy)
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
let deferred = deferFulfillment(viewModel.context.$viewState) { state in
|
|
|
|
state.defaultMode != nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
viewModel.fetchInitialContent()
|
2023-09-26 13:28:29 +03:00
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
try await deferred.fulfill()
|
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
let deferredViewState = deferFulfillment(viewModel.context.$viewState,
|
|
|
|
keyPath: \.pendingMode,
|
|
|
|
transitionValues: [nil, .allMessages, nil])
|
2023-08-11 16:10:23 +02:00
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
context.send(viewAction: .setMode(.allMessages))
|
|
|
|
|
|
|
|
try await deferredViewState.fulfill()
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
// `setDefaultRoomNotificationModeIsEncryptedIsOneToOneMode` must have been called twice (for encrypted and unencrypted direct chats)
|
|
|
|
let invocations = notificationSettingsProxy.setDefaultRoomNotificationModeIsEncryptedIsOneToOneModeReceivedInvocations
|
|
|
|
XCTAssertEqual(notificationSettingsProxy.setDefaultRoomNotificationModeIsEncryptedIsOneToOneModeCallsCount, 2)
|
|
|
|
// First call for encrypted direct chats
|
|
|
|
XCTAssertEqual(invocations[0].isEncrypted, true)
|
|
|
|
XCTAssertEqual(invocations[0].isOneToOne, true)
|
|
|
|
XCTAssertEqual(invocations[0].mode, .allMessages)
|
|
|
|
// Second call for unencrypted direct chats
|
|
|
|
XCTAssertEqual(invocations[1].isEncrypted, false)
|
|
|
|
XCTAssertEqual(invocations[1].isOneToOne, true)
|
|
|
|
XCTAssertEqual(invocations[1].mode, .allMessages)
|
|
|
|
}
|
2023-09-26 13:28:29 +03:00
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
func testSetModeFailure() async throws {
|
|
|
|
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReturnValue = .mentionsAndKeywordsOnly
|
2023-10-12 11:46:32 +03:00
|
|
|
notificationSettingsProxy.setDefaultRoomNotificationModeIsEncryptedIsOneToOneModeThrowableError = NotificationSettingsError.Generic(msg: "error")
|
2023-08-22 14:33:39 +02:00
|
|
|
viewModel = NotificationSettingsEditScreenViewModel(chatType: .oneToOneChat,
|
|
|
|
userSession: userSession,
|
2023-08-11 16:10:23 +02:00
|
|
|
notificationSettingsProxy: notificationSettingsProxy)
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
let deferred = deferFulfillment(viewModel.context.$viewState) { state in
|
|
|
|
state.defaultMode != nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
viewModel.fetchInitialContent()
|
2023-09-26 13:28:29 +03:00
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
try await deferred.fulfill()
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
let deferredViewState = deferFulfillment(viewModel.context.$viewState,
|
|
|
|
keyPath: \.pendingMode,
|
|
|
|
transitionValues: [nil, .allMessages, nil])
|
2023-08-11 16:10:23 +02:00
|
|
|
|
|
|
|
context.send(viewAction: .setMode(.allMessages))
|
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
try await deferredViewState.fulfill()
|
|
|
|
|
2023-08-11 16:10:23 +02:00
|
|
|
XCTAssertNotNil(context.viewState.bindings.alertInfo)
|
|
|
|
}
|
2023-09-26 13:28:29 +03:00
|
|
|
|
2023-08-22 14:33:39 +02:00
|
|
|
func testSelectRoom() async throws {
|
|
|
|
let roomID = "!roomidentifier:matrix.org"
|
|
|
|
viewModel = NotificationSettingsEditScreenViewModel(chatType: .oneToOneChat,
|
|
|
|
userSession: userSession,
|
|
|
|
notificationSettingsProxy: notificationSettingsProxy)
|
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
let deferredActions = deferFulfillment(viewModel.actions) { action in
|
|
|
|
switch action {
|
|
|
|
case .requestRoomNotificationSettingsPresentation:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:33:39 +02:00
|
|
|
context.send(viewAction: .selectRoom(roomIdentifier: roomID))
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
let sentAction = try await deferredActions.fulfill()
|
2023-08-22 14:33:39 +02:00
|
|
|
|
|
|
|
let expectedAction = NotificationSettingsEditScreenViewModelAction.requestRoomNotificationSettingsPresentation(roomID: roomID)
|
2023-09-26 13:28:29 +03:00
|
|
|
guard case let .requestRoomNotificationSettingsPresentation(roomID: receivedRoomID) = sentAction, receivedRoomID == roomID else {
|
|
|
|
XCTFail("Expected action \(expectedAction), but was \(sentAction)")
|
2023-08-22 14:33:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 16:10:23 +02:00
|
|
|
}
|