SettingsTests.swift (3917B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import Foundation 3 import Testing 4 @testable import RemoraDeco 5 6 @Suite("Settings") 7 struct SettingsTests { 8 @Test func defaultsAreAirAndConservative() throws { 9 let c = try UserSettings().configuration() 10 #expect(c.gas == .air) 11 #expect(c.deco.gradientFactors == GradientFactors(low: 0.30, high: 0.70)) 12 #expect(c.ppO2Warning == 1.4) 13 } 14 15 @Test func nitroxPercentSetsGasAndMOD() throws { 16 var s = UserSettings() 17 s.oxygenPercent = 32 18 s.water = .salt 19 #expect(try s.configuration().gas == .nitrox(32)) 20 #expect(abs(s.maximumOperatingDepth() - 33.4) < 0.3) 21 } 22 23 @Test func customGradientFactorsFromAdvancedSettings() throws { 24 var s = UserSettings() 25 s.conservatism = .custom 26 s.customGFLowPercent = 45 27 s.customGFHighPercent = 90 28 #expect(try s.configuration().deco.gradientFactors == GradientFactors(low: 0.45, high: 0.90)) 29 s.conservatism = .medium // the advanced values are kept but no longer apply 30 #expect(try s.configuration().deco.gradientFactors == GradientFactors(low: 0.40, high: 0.80)) 31 } 32 33 @Test func invalidSettingsAreRefused() { 34 var s = UserSettings() 35 s.oxygenPercent = 50 36 s.conservatism = .custom 37 s.customGFLowPercent = 80 38 s.customGFHighPercent = 60 39 s.ppO2Max = 1.8 40 #expect(Set(s.problems) == [.oxygenOutOfRange, .gfLowAboveHigh, .ppO2OutOfRange]) 41 #expect(throws: UserSettings.Problem.self) { try s.configuration() } 42 } 43 44 @Test func roundTripsThroughJSON() throws { 45 var s = UserSettings() 46 s.oxygenPercent = 36 47 s.conservatism = .custom 48 s.customGFLowPercent = 35 49 let data = try JSONEncoder().encode(s) 50 #expect(try JSONDecoder().decode(UserSettings.self, from: data) == s) 51 } 52 53 @Test func nitroxShortensDecoAtSameProfile() throws { 54 func tts(_ o2: Int) throws -> Double { 55 var s = UserSettings(); s.oxygenPercent = o2 56 let c = try s.configuration() 57 let scale = DepthScale(water: c.water) 58 var t = TissueState.saturated() 59 t.stay(atPressure: scale.pressure(atDepth: 28), gas: c.gas, minutes: 40) 60 return AscentPlanner.plan(tissues: t, pressure: scale.pressure(atDepth: 28), gas: c.gas, 61 scale: scale, settings: c.deco).timeToSurface 62 } 63 #expect(try tts(32) < tts(21)) 64 } 65 66 @Test func ndlAlarmIsAdjustableAndCanBeOff() throws { 67 var s = UserSettings() 68 #expect(try s.configuration().lowNoDecompression == 5) 69 s.ndlAlarmMinutes = 10 70 #expect(try s.configuration().lowNoDecompression == 10) 71 72 func warns(_ minutes: Int) throws -> Bool { 73 var u = UserSettings(); u.ndlAlarmMinutes = minutes; u.water = .salt 74 var c = DiveComputer(configuration: try u.configuration(), surfacePressure: standardSurfacePressure) 75 let scale = c.scale 76 c.update(time: 0, pressure: scale.surfacePressure) 77 for t in 1...300 { c.update(time: Double(t), pressure: scale.pressure(atDepth: 30)) } // NDL ≈ 4 min 78 return c.status.warnings.contains(.lowNoDecompression) 79 } 80 #expect(try warns(5)) 81 #expect(try !warns(3)) 82 #expect(try !warns(0)) 83 } 84 85 @Test func olderSavedSettingsStillLoad() throws { 86 // Settings saved by 0.2 (without the NDL alarm): they must not be lost. 87 let old = #"{"oxygenPercent":32,"water":"salt","conservatism":"medium","customGFLowPercent":30,"customGFHighPercent":70,"ppO2Max":1.4}"# 88 let s = try JSONDecoder().decode(UserSettings.self, from: Data(old.utf8)) 89 #expect(s.oxygenPercent == 32) 90 #expect(s.water == .salt) 91 #expect(s.conservatism == .medium) 92 #expect(s.ndlAlarmMinutes == 5) 93 } 94 }