feat: working on tests for pressure estimation feature to resolve filter drop bugs

This commit is contained in:
2024-06-18 11:02:59 -04:00
parent 3503f3bbe2
commit 710811878d
3 changed files with 216 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ import SharedModels
import Styleguide
import SwiftUI
#warning("Revert rated static pressures section.")
@Reducer
public struct EquipmentSettingsForm {

View File

@@ -17,7 +17,7 @@ public struct FlaggedMeasurementsList: Sendable {
@ObservableState
@dynamicMemberLookup
public struct State: Equatable {
public struct State: Equatable, Sendable {
@Presents public var destination: Destination.State?
@Shared var sharedSettings: SharedPressureEstimationState
@@ -130,16 +130,38 @@ public struct FlaggedMeasurementsList: Sendable {
)
}
state.destination = nil
return handleEstimationForm(form: form, state: state)
return .run { [state] send in
do {
guard let flaggedEstimation = try await self._handleEstimationForm(form: form, state: state)
else { return }
await send(.receive(.success(.estimatedFlaggedMeasurement(flaggedEstimation))))
} catch {
await send(.receive(.failure(error)))
}
}
}
case .editButtonTapped(id: _):
return .none
case .onAppear:
guard let equipmentMeasurement = state.sharedSettings.equipmentMeasurement else {
return .none
guard state.sharedSettings.flaggedEquipmentMeasurement == nil
else { return .none }
guard let equipmentMeasurement = state.sharedSettings.equipmentMeasurement
else {
return .fail(
"""
Equipment measurement is not set, skipping generating flagged measurement for existing
equipment.
This is considered an application logic error.
""",
logger: logger
)
}
if state.sharedSettings.budgets == nil {
state.sharedSettings.budgets = .init(
equipmentType: state.sharedSettings.equipmentMeasurement!.equipmentType,
@@ -159,6 +181,42 @@ public struct FlaggedMeasurementsList: Sendable {
.ifLet(\.$destination, action: \.destination)
}
enum EstimationFormFailure: Error {
case invalidState
}
func _handleEstimationForm(form: EstimationForm.State, state: State) async throws -> SharedPressureEstimationState.FlaggedEstimationContainer? {
guard let equipmentMeasurement = state.sharedSettings.equipmentMeasurement,
let budgets = state.sharedSettings.budgets
else {
throw EstimationFormFailure.invalidState
}
guard let estimationState = ensureHasChanges(
formState: form,
flaggedEstimations: state.sharedSettings.flaggedEstimations
) else {
logger.debug("No changes found, not generating a new flagged estimation measurement.")
return nil
}
let flaggedMeasurement = try await flaggedMeasurement(
airflow: estimationState.cfm.airflow,
budgets: budgets,
equipmentMeasurement: equipmentMeasurement,
filterPressureDrop: parseFilterPressureDrop(formState: form, sharedSettings: state.sharedSettings),
ratedStaticPresures: state.sharedSettings.ratedStaticPressures,
tons: form.coolingCapacity
)
return SharedPressureEstimationState.FlaggedEstimationContainer(
id: form.id ?? uuid(),
estimationState: estimationState,
flaggedMeasurement: flaggedMeasurement
)
}
#warning("This is making it hard to test, perhaps don't return an effect here.")
private func handleEstimationForm(form: EstimationForm.State, state: State) -> Effect<Action> {
guard let equipmentMeasurement = state.sharedSettings.equipmentMeasurement,
let budgets = state.sharedSettings.budgets
@@ -179,7 +237,6 @@ public struct FlaggedMeasurementsList: Sendable {
flaggedEstimations: state.sharedSettings.flaggedEstimations
) else {
logger.debug("No changes found, not generating a new flagged estimation measurement.")
print("No changes found, not generating a new flagged estimation measurement.")
return .none
}