2022-04-12 04:12:07 -07:00
|
|
|
//
|
2022-12-20 23:37:08 -08:00
|
|
|
// RootTests.swift
|
2022-04-12 04:12:07 -07:00
|
|
|
// secantTests
|
|
|
|
//
|
|
|
|
// Created by Lukáš Korba on 12.04.2022.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
import ComposableArchitecture
|
2023-05-10 09:52:56 -07:00
|
|
|
import ZcashLightClientKit
|
2023-05-29 05:38:53 -07:00
|
|
|
import FileManager
|
2023-05-31 00:49:37 -07:00
|
|
|
import DatabaseFiles
|
2023-05-30 23:29:32 -07:00
|
|
|
import ZcashSDKEnvironment
|
2023-06-01 07:05:35 -07:00
|
|
|
import WalletStorage
|
2023-06-09 01:29:01 -07:00
|
|
|
import Root
|
2023-05-29 05:38:53 -07:00
|
|
|
@testable import secant_testnet
|
2022-04-12 04:12:07 -07:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
@MainActor
|
2022-12-20 23:37:08 -08:00
|
|
|
class RootTests: XCTestCase {
|
2022-04-12 04:12:07 -07:00
|
|
|
func testWalletInitializationState_Uninitialized() throws {
|
2022-12-07 04:32:06 -08:00
|
|
|
let walletState = RootReducer.walletInitializationState(
|
2022-11-17 03:25:55 -08:00
|
|
|
databaseFiles: .noOp,
|
2023-06-09 01:29:01 -07:00
|
|
|
walletStorage: .noOp,
|
|
|
|
zcashNetwork: ZcashNetworkBuilder.network(for: .testnet)
|
2022-04-12 04:12:07 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
XCTAssertEqual(walletState, .uninitialized)
|
|
|
|
}
|
|
|
|
|
2022-04-14 04:12:09 -07:00
|
|
|
func testWalletInitializationState_FilesPresentKeysMissing() throws {
|
2022-11-17 03:25:55 -08:00
|
|
|
let wfmMock = FileManagerClient(
|
|
|
|
url: { _, _, _, _ in .emptyURL },
|
2022-04-12 04:12:07 -07:00
|
|
|
fileExists: { _ in return true },
|
|
|
|
removeItem: { _ in }
|
|
|
|
)
|
|
|
|
|
2022-12-07 04:32:06 -08:00
|
|
|
let walletState = RootReducer.walletInitializationState(
|
2022-04-12 04:12:07 -07:00
|
|
|
databaseFiles: .live(databaseFiles: DatabaseFiles(fileManager: wfmMock)),
|
2023-06-09 01:29:01 -07:00
|
|
|
walletStorage: .noOp,
|
|
|
|
zcashNetwork: ZcashNetworkBuilder.network(for: .testnet)
|
2022-04-12 04:12:07 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
XCTAssertEqual(walletState, .keysMissing)
|
|
|
|
}
|
|
|
|
|
2022-04-14 04:12:09 -07:00
|
|
|
func testWalletInitializationState_FilesMissingKeysMissing() throws {
|
2022-11-17 03:25:55 -08:00
|
|
|
let wfmMock = FileManagerClient(
|
|
|
|
url: { _, _, _, _ in .emptyURL },
|
2022-04-14 04:12:09 -07:00
|
|
|
fileExists: { _ in return false },
|
|
|
|
removeItem: { _ in }
|
|
|
|
)
|
|
|
|
|
2022-12-07 04:32:06 -08:00
|
|
|
let walletState = RootReducer.walletInitializationState(
|
2022-04-14 04:12:09 -07:00
|
|
|
databaseFiles: .live(databaseFiles: DatabaseFiles(fileManager: wfmMock)),
|
2023-06-09 01:29:01 -07:00
|
|
|
walletStorage: .noOp,
|
|
|
|
zcashNetwork: ZcashNetworkBuilder.network(for: .testnet)
|
2022-04-14 04:12:09 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
XCTAssertEqual(walletState, .uninitialized)
|
|
|
|
}
|
2022-12-20 23:37:08 -08:00
|
|
|
|
|
|
|
func testWalletInitializationState_FilesMissing() throws {
|
|
|
|
let wfmMock = FileManagerClient(
|
|
|
|
url: { _, _, _, _ in .emptyURL },
|
|
|
|
fileExists: { _ in return false },
|
|
|
|
removeItem: { _ in }
|
|
|
|
)
|
|
|
|
|
|
|
|
var walletStorage = WalletStorageClient.noOp
|
|
|
|
walletStorage.areKeysPresent = { true }
|
|
|
|
|
|
|
|
let walletState = RootReducer.walletInitializationState(
|
|
|
|
databaseFiles: .live(databaseFiles: DatabaseFiles(fileManager: wfmMock)),
|
2023-06-09 01:29:01 -07:00
|
|
|
walletStorage: walletStorage,
|
|
|
|
zcashNetwork: ZcashNetworkBuilder.network(for: .testnet)
|
2022-12-20 23:37:08 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
XCTAssertEqual(walletState, .filesMissing)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWalletInitializationState_Initialized() throws {
|
|
|
|
let wfmMock = FileManagerClient(
|
|
|
|
url: { _, _, _, _ in .emptyURL },
|
|
|
|
fileExists: { _ in return true },
|
|
|
|
removeItem: { _ in }
|
|
|
|
)
|
2022-04-14 04:12:09 -07:00
|
|
|
|
2022-12-20 23:37:08 -08:00
|
|
|
var walletStorage = WalletStorageClient.noOp
|
|
|
|
walletStorage.areKeysPresent = { true }
|
|
|
|
|
|
|
|
let walletState = RootReducer.walletInitializationState(
|
|
|
|
databaseFiles: .live(databaseFiles: DatabaseFiles(fileManager: wfmMock)),
|
2023-06-09 01:29:01 -07:00
|
|
|
walletStorage: walletStorage,
|
|
|
|
zcashNetwork: ZcashNetworkBuilder.network(for: .testnet)
|
2022-12-20 23:37:08 -08:00
|
|
|
)
|
2022-04-12 04:12:07 -07:00
|
|
|
|
2022-12-20 23:37:08 -08:00
|
|
|
XCTAssertEqual(walletState, .initialized)
|
|
|
|
}
|
2022-04-12 04:12:07 -07:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
@MainActor func testRespondToWalletInitializationState_Uninitialized() async throws {
|
2022-04-12 04:12:07 -07:00
|
|
|
let store = TestStore(
|
2023-11-16 07:10:24 -08:00
|
|
|
initialState: .initial
|
|
|
|
) {
|
2024-02-14 05:41:21 -08:00
|
|
|
RootReducer()
|
2023-11-16 07:10:24 -08:00
|
|
|
}
|
2023-02-28 09:02:31 -08:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
store.dependencies.mainQueue = .immediate
|
|
|
|
store.dependencies.crashReporter = .noOp
|
|
|
|
store.dependencies.walletStorage = .noOp
|
|
|
|
store.dependencies.databaseFiles = .noOp
|
2022-11-17 03:25:55 -08:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
await store.send(.initialization(.respondToWalletInitializationState(.uninitialized)))
|
2022-04-12 04:12:07 -07:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
await store.receive(.destination(.updateDestination(.onboarding))) { state in
|
2023-03-21 03:03:42 -07:00
|
|
|
state.destinationState.destination = .onboarding
|
|
|
|
state.appInitializationState = .uninitialized
|
2022-04-12 04:12:07 -07:00
|
|
|
}
|
2023-11-16 07:10:24 -08:00
|
|
|
|
|
|
|
await store.finish()
|
2022-04-12 04:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
func testRespondToWalletInitializationState_KeysMissing() async throws {
|
2022-04-12 04:12:07 -07:00
|
|
|
let store = TestStore(
|
2023-11-16 07:10:24 -08:00
|
|
|
initialState: .initial
|
|
|
|
) {
|
2024-02-14 05:41:21 -08:00
|
|
|
RootReducer()
|
2023-11-16 07:10:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
store.dependencies.mainQueue = .immediate
|
|
|
|
store.dependencies.crashReporter = .noOp
|
|
|
|
store.dependencies.walletStorage = .noOp
|
|
|
|
store.dependencies.databaseFiles = .noOp
|
|
|
|
|
|
|
|
await store.send(.initialization(.respondToWalletInitializationState(.keysMissing))) { state in
|
2022-04-12 04:12:07 -07:00
|
|
|
state.appInitializationState = .keysMissing
|
2024-03-12 07:34:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
await store.receive(.destination(.updateDestination(.onboarding))) { state in
|
|
|
|
state.destinationState.internalDestination = .onboarding
|
|
|
|
state.destinationState.previousDestination = .welcome
|
2022-04-12 04:12:07 -07:00
|
|
|
}
|
2023-11-16 07:10:24 -08:00
|
|
|
|
|
|
|
await store.finish()
|
2022-04-12 04:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
func testRespondToWalletInitializationState_FilesMissing() async throws {
|
2023-03-02 00:02:24 -08:00
|
|
|
let walletStorageError: Error = "export failed"
|
2023-05-10 09:52:56 -07:00
|
|
|
let zcashError = ZcashError.unknown(walletStorageError)
|
2023-03-02 00:02:24 -08:00
|
|
|
|
2022-04-12 04:12:07 -07:00
|
|
|
let store = TestStore(
|
2023-11-16 07:10:24 -08:00
|
|
|
initialState: .initial
|
|
|
|
) {
|
2024-02-14 05:41:21 -08:00
|
|
|
RootReducer()
|
2023-11-16 07:10:24 -08:00
|
|
|
}
|
2023-03-21 03:03:42 -07:00
|
|
|
|
|
|
|
store.dependencies.walletStorage = .noOp
|
2024-02-05 08:02:26 -08:00
|
|
|
store.dependencies.walletStorage.exportWallet = { throw zcashError }
|
2024-05-31 06:32:06 -07:00
|
|
|
store.dependencies.walletStatusPanel = .noOp
|
2024-04-15 05:00:39 -07:00
|
|
|
store.dependencies.userDefaults = .noOp
|
2022-11-17 03:25:55 -08:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
await store.send(.initialization(.respondToWalletInitializationState(.filesMissing))) { state in
|
2022-04-12 04:12:07 -07:00
|
|
|
state.appInitializationState = .filesMissing
|
2024-02-05 08:02:26 -08:00
|
|
|
state.isRestoringWallet = true
|
2022-04-12 04:12:07 -07:00
|
|
|
}
|
2023-03-23 08:10:18 -07:00
|
|
|
|
2024-02-05 08:02:26 -08:00
|
|
|
await store.receive(.initialization(.initializeSDK(.restoreWallet)))
|
|
|
|
|
2024-06-13 01:34:32 -07:00
|
|
|
await store.receive(.initialization(.checkBackupPhraseValidation))
|
|
|
|
|
2024-02-05 08:02:26 -08:00
|
|
|
await store.receive(.initialization(.initializationFailed(zcashError))) { state in
|
|
|
|
state.appInitializationState = .failed
|
|
|
|
state.alert = AlertState.initializationFailed(zcashError)
|
|
|
|
}
|
2024-03-06 07:12:23 -08:00
|
|
|
|
|
|
|
await store.receive(.initialization(.initializationFailed(zcashError)))
|
2023-11-16 07:10:24 -08:00
|
|
|
|
|
|
|
await store.finish()
|
2022-04-12 04:12:07 -07:00
|
|
|
}
|
2022-11-22 02:32:48 -08:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
func testRespondToWalletInitializationState_Initialized() async throws {
|
2023-03-02 00:02:24 -08:00
|
|
|
let walletStorageError: Error = "export failed"
|
2023-05-10 09:52:56 -07:00
|
|
|
let zcashError = ZcashError.unknown(walletStorageError)
|
|
|
|
|
2022-04-12 04:12:07 -07:00
|
|
|
let store = TestStore(
|
2023-11-16 07:10:24 -08:00
|
|
|
initialState: .initial
|
|
|
|
) {
|
2024-02-14 05:41:21 -08:00
|
|
|
RootReducer()
|
2023-11-16 07:10:24 -08:00
|
|
|
}
|
2023-03-21 03:03:42 -07:00
|
|
|
|
|
|
|
store.dependencies.walletStorage = .noOp
|
|
|
|
store.dependencies.walletStorage.exportWallet = { throw walletStorageError }
|
2024-04-15 05:00:39 -07:00
|
|
|
store.dependencies.userDefaults = .noOp
|
2022-11-22 02:32:48 -08:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
await store.send(.initialization(.respondToWalletInitializationState(.initialized)))
|
2022-11-22 02:32:48 -08:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
await store.receive(.initialization(.initializeSDK(.existingWallet)))
|
2023-03-23 08:10:18 -07:00
|
|
|
|
2024-03-06 07:12:23 -08:00
|
|
|
await store.receive(.initialization(.checkBackupPhraseValidation))
|
2023-03-30 22:25:13 -07:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
await store.receive(.initialization(.initializationFailed(zcashError))) { state in
|
2024-03-06 07:12:23 -08:00
|
|
|
state.appInitializationState = .failed
|
2023-06-06 02:30:58 -07:00
|
|
|
state.alert = AlertState.initializationFailed(zcashError)
|
2023-03-02 00:02:24 -08:00
|
|
|
}
|
2023-11-16 07:10:24 -08:00
|
|
|
|
2024-03-06 07:12:23 -08:00
|
|
|
await store.receive(.initialization(.initializationFailed(zcashError)))
|
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
await store.finish()
|
2022-04-12 04:12:07 -07:00
|
|
|
}
|
2023-10-13 07:36:50 -07:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
func testInitializationSuccessfullyDone() async throws {
|
2023-10-13 07:36:50 -07:00
|
|
|
let store = TestStore(
|
2023-11-16 07:10:24 -08:00
|
|
|
initialState: .initial
|
|
|
|
) {
|
2024-02-14 05:41:21 -08:00
|
|
|
RootReducer()
|
2023-11-16 07:10:24 -08:00
|
|
|
}
|
2023-10-13 07:36:50 -07:00
|
|
|
|
2024-01-08 05:14:48 -08:00
|
|
|
store.dependencies.mainQueue = .immediate
|
|
|
|
store.dependencies.sdkSynchronizer = .noOp
|
2024-06-13 01:34:32 -07:00
|
|
|
store.dependencies.autolockHandler = .noOp
|
2024-01-08 05:14:48 -08:00
|
|
|
|
2023-10-13 07:36:50 -07:00
|
|
|
// swiftlint:disable line_length
|
2023-11-16 07:10:24 -08:00
|
|
|
let uAddress = try UnifiedAddress(
|
|
|
|
encoding: "utest1zkkkjfxkamagznjr6ayemffj2d2gacdwpzcyw669pvg06xevzqslpmm27zjsctlkstl2vsw62xrjktmzqcu4yu9zdhdxqz3kafa4j2q85y6mv74rzjcgjg8c0ytrg7dwyzwtgnuc76h", network: .testnet
|
2023-10-13 07:36:50 -07:00
|
|
|
)
|
2024-01-08 05:14:48 -08:00
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
await store.send(.initialization(.initializationSuccessfullyDone(uAddress))) { state in
|
2023-10-13 07:36:50 -07:00
|
|
|
state.tabsState.addressDetailsState.uAddress = uAddress
|
|
|
|
}
|
2024-01-08 05:14:48 -08:00
|
|
|
|
|
|
|
await store.receive(.initialization(.registerForSynchronizersUpdate))
|
|
|
|
|
2024-06-13 01:34:32 -07:00
|
|
|
await store.send(.cancelAllRunningEffects)
|
|
|
|
|
2023-11-16 07:10:24 -08:00
|
|
|
await store.finish()
|
2023-10-13 07:36:50 -07:00
|
|
|
}
|
2022-04-12 04:12:07 -07:00
|
|
|
}
|