From ccb17ef0eabd87e2bb62ae87d048b5a5687214ee Mon Sep 17 00:00:00 2001 From: Lukas Korba Date: Thu, 18 May 2023 09:25:40 +0200 Subject: [PATCH] [#1046] Implement SaplingParamsAction - SaplingParamsAction tests - added TODO for TestCoordinator reset() - SaplingParametersHandler mock added --- .../SaplingParamsActionTests.swift | 33 +++++++++++++++++++ Tests/TestUtils/Sourcery/AutoMockable.swift | 1 + .../AutoMockable.generated.swift | 25 ++++++++++++++ Tests/TestUtils/TestCoordinator.swift | 33 ++++++++++--------- 4 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 Tests/OfflineTests/CompactBlockProcessorActions/SaplingParamsActionTests.swift diff --git a/Tests/OfflineTests/CompactBlockProcessorActions/SaplingParamsActionTests.swift b/Tests/OfflineTests/CompactBlockProcessorActions/SaplingParamsActionTests.swift new file mode 100644 index 00000000..ee431750 --- /dev/null +++ b/Tests/OfflineTests/CompactBlockProcessorActions/SaplingParamsActionTests.swift @@ -0,0 +1,33 @@ +// +// SaplingParamsActionTests.swift +// +// +// Created by Lukáš Korba on 18.05.2023. +// + +import XCTest +@testable import TestUtils +@testable import ZcashLightClientKit + +final class SaplingParamsActionTests: ZcashTestCase { + func testSaplingParamsAction_NextAction() async throws { + let loggerMock = LoggerMock() + let saplingParametersHandlerMock = SaplingParametersHandlerMock() + + mockContainer.mock(type: Logger.self, isSingleton: true) { _ in loggerMock } + mockContainer.mock(type: SaplingParametersHandler.self, isSingleton: true) { _ in saplingParametersHandlerMock } + + let saplingParamsActionAction = SaplingParamsAction(container: mockContainer) + + do { + let nextContext = try await saplingParamsActionAction.run(with: .init(state: .handleSaplingParams)) { _ in } + let nextState = await nextContext.state + XCTAssertTrue( + nextState == .scanDownloaded, + "nextContext after .handleSaplingParams is expected to be .scanDownloaded but received \(nextState)" + ) + } catch { + XCTFail("testSaplingParamsAction_NextAction is not expected to fail. \(error)") + } + } +} diff --git a/Tests/TestUtils/Sourcery/AutoMockable.swift b/Tests/TestUtils/Sourcery/AutoMockable.swift index ca273b31..c9b1b101 100644 --- a/Tests/TestUtils/Sourcery/AutoMockable.swift +++ b/Tests/TestUtils/Sourcery/AutoMockable.swift @@ -17,6 +17,7 @@ extension BlockValidator { } extension LightWalletdInfo { } extension LightWalletService { } extension Logger { } +extension SaplingParametersHandler { } extension Synchronizer { } extension TransactionRepository { } extension ZcashRustBackendWelding { } diff --git a/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift b/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift index a9d1051f..b6a4f388 100644 --- a/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift +++ b/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift @@ -426,6 +426,31 @@ class LoggerMock: Logger { errorFileFunctionLineClosure?(message, file, function, line) } +} +class SaplingParametersHandlerMock: SaplingParametersHandler { + + + init( + ) { + } + + // MARK: - handleIfNeeded + + var handleIfNeededThrowableError: Error? + var handleIfNeededCallsCount = 0 + var handleIfNeededCalled: Bool { + return handleIfNeededCallsCount > 0 + } + var handleIfNeededClosure: (() async throws -> Void)? + + func handleIfNeeded() async throws { + if let error = handleIfNeededThrowableError { + throw error + } + handleIfNeededCallsCount += 1 + try await handleIfNeededClosure?() + } + } class SynchronizerMock: Synchronizer { diff --git a/Tests/TestUtils/TestCoordinator.swift b/Tests/TestUtils/TestCoordinator.swift index 5e474754..b4dd53c1 100644 --- a/Tests/TestUtils/TestCoordinator.swift +++ b/Tests/TestUtils/TestCoordinator.swift @@ -212,22 +212,23 @@ extension TestCoordinator { func reset(saplingActivation: BlockHeight, branchID: String, chainName: String) throws { Task { await self.synchronizer.blockProcessor.stop() - let config = await self.synchronizer.blockProcessor.config - - let newConfig = CompactBlockProcessor.Configuration( - alias: config.alias, - fsBlockCacheRoot: config.fsBlockCacheRoot, - dataDb: config.dataDb, - spendParamsURL: config.spendParamsURL, - outputParamsURL: config.outputParamsURL, - saplingParamsSourceURL: config.saplingParamsSourceURL, - retries: config.retries, - maxBackoffInterval: config.maxBackoffInterval, - rewindDistance: config.rewindDistance, - walletBirthdayProvider: config.walletBirthdayProvider, - saplingActivation: saplingActivation, - network: config.network - ) + // TODO: [1102] review and potentially fix/remove commented code https://github.com/zcash/ZcashLightClientKit/issues/1102 +// let config = await self.synchronizer.blockProcessor.config +// +// let newConfig = CompactBlockProcessor.Configuration( +// alias: config.alias, +// fsBlockCacheRoot: config.fsBlockCacheRoot, +// dataDb: config.dataDb, +// spendParamsURL: config.spendParamsURL, +// outputParamsURL: config.outputParamsURL, +// saplingParamsSourceURL: config.saplingParamsSourceURL, +// retries: config.retries, +// maxBackoffInterval: config.maxBackoffInterval, +// rewindDistance: config.rewindDistance, +// walletBirthdayProvider: config.walletBirthdayProvider, +// saplingActivation: saplingActivation, +// network: config.network +// ) // await self.synchronizer.blockProcessor.update(config: newConfig) }