2022-06-03 11:21:51 -07:00
|
|
|
//
|
|
|
|
// AppInitializationTests.swift
|
|
|
|
// secantTests
|
|
|
|
//
|
|
|
|
// Created by Lukáš Korba on 31.05.2022.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import secant_testnet
|
|
|
|
import ComposableArchitecture
|
|
|
|
|
|
|
|
class AppInitializationTests: XCTestCase {
|
2023-01-02 07:18:58 -08:00
|
|
|
/// This integration test starts with finishing the app launch and triggering bunch of initialization procedures.
|
2022-11-22 02:32:48 -08:00
|
|
|
@MainActor func testDidFinishLaunching_to_InitializedWallet() async throws {
|
2022-06-03 11:21:51 -07:00
|
|
|
// setup the store and environment to be fully mocked
|
2023-02-13 11:32:50 -08:00
|
|
|
let recoveryPhrase = RecoveryPhrase(words: try MnemonicClient.mock.randomMnemonicWords().map { $0.redacted })
|
2022-11-22 02:32:48 -08:00
|
|
|
|
2022-11-04 23:11:25 -07:00
|
|
|
let phraseValidationState = RecoveryPhraseValidationFlowReducer.State(
|
2022-06-03 11:21:51 -07:00
|
|
|
phrase: recoveryPhrase,
|
|
|
|
missingIndices: [2, 0, 3, 5],
|
|
|
|
missingWordChips: [
|
2023-02-13 11:32:50 -08:00
|
|
|
.unassigned(word: "voice".redacted),
|
2022-06-03 11:21:51 -07:00
|
|
|
.empty,
|
2023-02-13 11:32:50 -08:00
|
|
|
.unassigned(word: "survey".redacted),
|
|
|
|
.unassigned(word: "spread".redacted)
|
2022-06-03 11:21:51 -07:00
|
|
|
],
|
|
|
|
validationWords: [
|
2023-02-13 11:32:50 -08:00
|
|
|
.init(groupIndex: 2, word: "dizzy".redacted)
|
2022-06-03 11:21:51 -07:00
|
|
|
],
|
2022-12-01 06:31:30 -08:00
|
|
|
destination: nil
|
2022-06-03 11:21:51 -07:00
|
|
|
)
|
2022-11-22 02:32:48 -08:00
|
|
|
|
2022-11-17 03:25:55 -08:00
|
|
|
let recoveryPhraseRandomizer = RecoveryPhraseRandomizerClient(
|
2022-06-03 11:21:51 -07:00
|
|
|
random: { _ in
|
|
|
|
let missingIndices = [2, 0, 3, 5]
|
|
|
|
let missingWordChipKind = [
|
|
|
|
PhraseChip.Kind.unassigned(
|
2023-02-13 11:32:50 -08:00
|
|
|
word: "voice".redacted,
|
2022-11-22 02:32:48 -08:00
|
|
|
color: Asset.Colors.Buttons.activeButton.color
|
2022-06-03 11:21:51 -07:00
|
|
|
),
|
|
|
|
PhraseChip.Kind.empty,
|
|
|
|
PhraseChip.Kind.unassigned(
|
2023-02-13 11:32:50 -08:00
|
|
|
word: "survey".redacted,
|
2022-11-22 02:32:48 -08:00
|
|
|
color: Asset.Colors.Buttons.activeButton.color
|
2022-06-03 11:21:51 -07:00
|
|
|
),
|
|
|
|
PhraseChip.Kind.unassigned(
|
2023-02-13 11:32:50 -08:00
|
|
|
word: "spread".redacted,
|
2022-11-22 02:32:48 -08:00
|
|
|
color: Asset.Colors.Buttons.activeButton.color
|
2022-06-03 11:21:51 -07:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2022-11-04 23:11:25 -07:00
|
|
|
return RecoveryPhraseValidationFlowReducer.State(
|
2022-06-03 11:21:51 -07:00
|
|
|
phrase: recoveryPhrase,
|
|
|
|
missingIndices: missingIndices,
|
|
|
|
missingWordChips: missingWordChipKind,
|
|
|
|
validationWords: [
|
|
|
|
ValidationWord(
|
|
|
|
groupIndex: 2,
|
2023-02-13 11:32:50 -08:00
|
|
|
word: "dizzy".redacted
|
2022-06-03 11:21:51 -07:00
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2022-11-22 02:32:48 -08:00
|
|
|
|
2023-03-01 10:33:58 -08:00
|
|
|
var defaultRawFlags = WalletConfig.default.flags
|
|
|
|
defaultRawFlags[.testBackupPhraseFlow] = true
|
|
|
|
let walletConfig = WalletConfig(flags: defaultRawFlags)
|
|
|
|
|
2022-12-07 04:32:06 -08:00
|
|
|
let appState = RootReducer.State(
|
2023-03-02 06:24:32 -08:00
|
|
|
debugState: .placeholder,
|
2022-12-07 05:47:42 -08:00
|
|
|
destinationState: .placeholder,
|
2023-03-07 05:01:22 -08:00
|
|
|
exportLogsState: .placeholder,
|
2022-06-03 11:21:51 -07:00
|
|
|
homeState: .placeholder,
|
|
|
|
onboardingState: .init(
|
2023-02-27 04:55:47 -08:00
|
|
|
walletConfig: .default,
|
2022-06-03 11:21:51 -07:00
|
|
|
importWalletState: .placeholder
|
|
|
|
),
|
|
|
|
phraseValidationState: phraseValidationState,
|
2022-11-04 23:11:25 -07:00
|
|
|
phraseDisplayState: RecoveryPhraseDisplayReducer.State(
|
2022-06-03 11:21:51 -07:00
|
|
|
phrase: recoveryPhrase
|
|
|
|
),
|
|
|
|
sandboxState: .placeholder,
|
2023-03-01 10:33:58 -08:00
|
|
|
walletConfig: walletConfig,
|
2022-06-03 11:21:51 -07:00
|
|
|
welcomeState: .placeholder
|
|
|
|
)
|
2022-11-22 02:32:48 -08:00
|
|
|
|
2022-06-03 11:21:51 -07:00
|
|
|
let store = TestStore(
|
|
|
|
initialState: appState,
|
2022-12-07 04:32:06 -08:00
|
|
|
reducer: RootReducer()
|
2023-02-27 04:55:47 -08:00
|
|
|
)
|
|
|
|
|
2023-02-28 09:02:31 -08:00
|
|
|
let testQueue = DispatchQueue.test
|
|
|
|
|
2023-02-27 04:55:47 -08:00
|
|
|
store.dependencies.databaseFiles = .noOp
|
|
|
|
store.dependencies.databaseFiles.areDbFilesPresentFor = { _ in true }
|
|
|
|
store.dependencies.derivationTool = .liveValue
|
2023-02-28 09:02:31 -08:00
|
|
|
store.dependencies.mainQueue = .immediate// testQueue.eraseToAnyScheduler()
|
2023-02-27 04:55:47 -08:00
|
|
|
store.dependencies.mnemonic = .mock
|
|
|
|
store.dependencies.randomRecoveryPhrase = recoveryPhraseRandomizer
|
|
|
|
store.dependencies.walletStorage.exportWallet = { .placeholder }
|
|
|
|
store.dependencies.walletStorage.areKeysPresent = { true }
|
|
|
|
store.dependencies.walletConfigProvider = .noOp
|
2022-11-14 10:36:51 -08:00
|
|
|
|
2022-06-03 11:21:51 -07:00
|
|
|
// Root of the test, the app finished the launch process and triggers the checks and initializations.
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.send(.initialization(.appDelegate(.didFinishLaunching)))
|
2022-11-22 02:32:48 -08:00
|
|
|
|
2023-02-28 09:02:31 -08:00
|
|
|
await testQueue.advance(by: 0.02)
|
|
|
|
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.receive(.initialization(.checkWalletConfig))
|
2023-02-28 09:02:31 -08:00
|
|
|
|
|
|
|
await store.receive(.walletConfigLoaded(WalletConfig.default))
|
2023-02-27 04:55:47 -08:00
|
|
|
|
|
|
|
await store.receive(.initialization(.initialSetups))
|
2022-11-22 02:32:48 -08:00
|
|
|
|
2023-02-28 09:02:31 -08:00
|
|
|
await testQueue.advance(by: 0.02)
|
|
|
|
|
2023-02-15 13:18:18 -08:00
|
|
|
await store.receive(.initialization(.configureCrashReporter))
|
2022-06-03 11:21:51 -07:00
|
|
|
|
2023-02-15 13:18:18 -08:00
|
|
|
await store.receive(.initialization(.checkWalletInitialization))
|
2022-06-03 11:21:51 -07:00
|
|
|
|
2023-02-15 13:18:18 -08:00
|
|
|
await store.receive(.initialization(.respondToWalletInitializationState(.initialized)))
|
|
|
|
|
2023-02-28 09:02:31 -08:00
|
|
|
await testQueue.advance(by: 3.00)
|
|
|
|
|
2022-12-07 05:47:42 -08:00
|
|
|
await store.receive(.initialization(.initializeSDK)) { state in
|
2022-11-22 02:32:48 -08:00
|
|
|
state.storedWallet = .placeholder
|
2022-06-03 11:21:51 -07:00
|
|
|
}
|
2023-02-27 04:55:47 -08:00
|
|
|
|
2022-12-07 05:47:42 -08:00
|
|
|
await store.receive(.initialization(.checkBackupPhraseValidation)) { state in
|
2022-06-03 11:21:51 -07:00
|
|
|
state.appInitializationState = .initialized
|
|
|
|
}
|
|
|
|
|
2022-12-07 05:47:42 -08:00
|
|
|
await store.receive(.destination(.updateDestination(.phraseDisplay))) { state in
|
|
|
|
state.destinationState.previousDestination = .welcome
|
|
|
|
state.destinationState.internalDestination = .phraseDisplay
|
2022-06-03 11:21:51 -07:00
|
|
|
}
|
2023-02-27 04:55:47 -08:00
|
|
|
|
|
|
|
await store.finish()
|
2022-06-03 11:21:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Integration test validating the side effects work together properly when no wallet is stored but database files are present.
|
2023-02-27 04:55:47 -08:00
|
|
|
@MainActor func testDidFinishLaunching_to_KeysMissing() async throws {
|
2022-06-03 11:21:51 -07:00
|
|
|
let store = TestStore(
|
|
|
|
initialState: .placeholder,
|
2022-12-07 04:32:06 -08:00
|
|
|
reducer: RootReducer()
|
2023-02-27 04:55:47 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
store.dependencies.databaseFiles = .noOp
|
|
|
|
store.dependencies.databaseFiles.areDbFilesPresentFor = { _ in true }
|
|
|
|
store.dependencies.walletStorage = .noOp
|
|
|
|
store.dependencies.mainQueue = .immediate
|
|
|
|
store.dependencies.walletConfigProvider = .noOp
|
2022-11-17 03:25:55 -08:00
|
|
|
|
2022-06-03 11:21:51 -07:00
|
|
|
// Root of the test, the app finished the launch process and triggers the checks and initializations.
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.send(.initialization(.appDelegate(.didFinishLaunching)))
|
|
|
|
|
|
|
|
await store.receive(.initialization(.checkWalletConfig))
|
2022-06-03 11:21:51 -07:00
|
|
|
|
2023-02-28 09:02:31 -08:00
|
|
|
await store.receive(.walletConfigLoaded(WalletConfig.default))
|
|
|
|
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.receive(.initialization(.initialSetups))
|
2023-02-15 13:18:18 -08:00
|
|
|
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.receive(.initialization(.configureCrashReporter))
|
|
|
|
|
|
|
|
await store.receive(.initialization(.checkWalletInitialization))
|
2022-06-03 11:21:51 -07:00
|
|
|
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.receive(.initialization(.respondToWalletInitializationState(.keysMissing))) { state in
|
2022-06-03 11:21:51 -07:00
|
|
|
state.appInitializationState = .keysMissing
|
2023-03-02 00:02:24 -08:00
|
|
|
state.alert = AlertState(
|
|
|
|
title: TextState("Wallet initialisation failed."),
|
|
|
|
message: TextState("App initialisation state: keysMissing."),
|
|
|
|
dismissButton: .default(TextState("Ok"), action: .send(.dismissAlert))
|
|
|
|
)
|
2022-06-03 11:21:51 -07:00
|
|
|
}
|
2023-02-27 04:55:47 -08:00
|
|
|
|
|
|
|
await store.finish()
|
2022-06-03 11:21:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Integration test validating the side effects work together properly when no wallet is stored and no database files are present.
|
2023-02-27 04:55:47 -08:00
|
|
|
@MainActor func testDidFinishLaunching_to_Uninitialized() async throws {
|
2022-06-03 11:21:51 -07:00
|
|
|
let store = TestStore(
|
|
|
|
initialState: .placeholder,
|
2022-12-07 04:32:06 -08:00
|
|
|
reducer: RootReducer()
|
2023-02-27 04:55:47 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
store.dependencies.databaseFiles = .noOp
|
|
|
|
store.dependencies.mainQueue = .immediate
|
|
|
|
store.dependencies.walletStorage = .noOp
|
|
|
|
store.dependencies.walletConfigProvider = .noOp
|
2022-11-17 03:25:55 -08:00
|
|
|
|
2022-06-03 11:21:51 -07:00
|
|
|
// Root of the test, the app finished the launch process and triggers the checks and initializations.
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.send(.initialization(.appDelegate(.didFinishLaunching)))
|
2023-02-15 13:18:18 -08:00
|
|
|
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.receive(.initialization(.checkWalletConfig))
|
2022-06-03 11:21:51 -07:00
|
|
|
|
2023-02-28 09:02:31 -08:00
|
|
|
await store.receive(.walletConfigLoaded(WalletConfig.default))
|
|
|
|
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.receive(.initialization(.initialSetups))
|
|
|
|
|
|
|
|
await store.receive(.initialization(.configureCrashReporter))
|
2023-02-15 13:18:18 -08:00
|
|
|
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.receive(.initialization(.checkWalletInitialization))
|
|
|
|
|
|
|
|
await store.receive(.initialization(.respondToWalletInitializationState(.uninitialized)))
|
2022-06-03 11:21:51 -07:00
|
|
|
|
2023-02-27 04:55:47 -08:00
|
|
|
await store.receive(.destination(.updateDestination(.onboarding))) { state in
|
2022-12-07 05:47:42 -08:00
|
|
|
state.destinationState.previousDestination = .welcome
|
|
|
|
state.destinationState.internalDestination = .onboarding
|
2022-06-03 11:21:51 -07:00
|
|
|
}
|
2023-02-27 04:55:47 -08:00
|
|
|
|
|
|
|
await store.finish()
|
2022-06-03 11:21:51 -07:00
|
|
|
}
|
|
|
|
}
|