remora-kit

RemoraKit, shared dive computer engine (Buhlmann ZHL-16C + GF) and SwiftUI components for Open Remora and Open Merou
git clone https://git.schurger.org/remora-kit.git
Log | Files | Refs | README | LICENSE

LogbookTests.swift (5666B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import Foundation
      3 import Testing
      4 @testable import RemoraDeco
      5 
      6 /// Dive simulated at 1 Hz: descent at 18 m/min, bottom time, ascent at 9 m/min, safety stop.
      7 func simulateDive(depth: Double, bottomMinutes: Int, start: Date, logbook: Logbook,
      8                   config: DiveComputer.Configuration = DiveComputer.Configuration()) -> DiveLog {
      9     let scale = DepthScale(water: config.water)
     10     var r = DiveRecorder(configuration: config, surfacePressure: scale.surfacePressure,
     11                          start: start, logbook: logbook)
     12     var t = 0.0, d = 0.0
     13     func tick() { t += 1; r.record(time: t, pressure: scale.pressure(atDepth: d), temperature: 18 - d / 4) }
     14     r.record(time: 0, pressure: scale.surfacePressure, temperature: 20)
     15     while d < depth { d = min(depth, d + 18.0 / 60); tick() }
     16     for _ in 0..<(bottomMinutes * 60) { tick() }
     17     while d > 5 { d = max(5, d - 9.0 / 60); tick() }
     18     for _ in 0..<180 { tick() }
     19     while d > 0 { d = max(0, d - 9.0 / 60); tick() }
     20     return r.finish()
     21 }
     22 
     23 @Suite("Logbook")
     24 struct LogbookTests {
     25     let start = Date(timeIntervalSince1970: 1_800_000_000)
     26 
     27     @Test func recordsProfileForTheGraph() throws {
     28         let dive = simulateDive(depth: 18, bottomMinutes: 20, start: start, logbook: Logbook())
     29         #expect(abs(dive.maxDepth - 18) < 1e-6)
     30         #expect(dive.samples.first?.time == 0)
     31         #expect(dive.samples.last?.depth == 0)
     32         // One sample every 2 s: a 26 min dive → ~780 samples
     33         #expect((770...790).contains(dive.samples.count))
     34         #expect(zip(dive.samples, dive.samples.dropFirst()).allSatisfy { $1.time > $0.time })
     35         #expect(abs(dive.duration / 60 - 26.0) < 0.1)
     36         #expect((14...18).contains(dive.averageDepth))
     37         #expect(dive.minTemperature == 13.5)
     38         #expect(!dive.hadDecompression)
     39         #expect(dive.events.isEmpty)
     40         #expect(dive.violations.isEmpty)
     41         #expect(dive.surfaceIntervalBefore == nil)
     42     }
     43 
     44     @Test func repetitiveDiveInheritsTissues() throws {
     45         var book = Logbook()
     46         let first = simulateDive(depth: 25, bottomMinutes: 20, start: start, logbook: book)
     47         book.add(first)
     48         let second = simulateDive(depth: 18, bottomMinutes: 10,
     49                                   start: first.end.addingTimeInterval(60 * 60), logbook: book)
     50         book.add(second)
     51         #expect(book.dives.map(\.number) == [1, 2])
     52         #expect(abs((book.dives[1].surfaceIntervalBefore ?? 0) - 60) < 0.1)
     53         // Shorter no-decompression limit than the same dive done first
     54         let fresh = simulateDive(depth: 18, bottomMinutes: 10, start: start, logbook: Logbook())
     55         let ndlRepeat = try #require(second.samples.first { abs($0.depth - 18) < 1e-6 }?.noDecompressionLimit)
     56         let ndlFresh = try #require(fresh.samples.first { abs($0.depth - 18) < 1e-6 }?.noDecompressionLimit)
     57         #expect(ndlRepeat < ndlFresh)
     58     }
     59 
     60     @Test func decoDiveIsMarkedAndAlarmsLogged() {
     61         let dive = simulateDive(depth: 40, bottomMinutes: 20, start: start, logbook: Logbook())
     62         #expect(dive.hadDecompression)
     63         // The simulation ascends without doing the stops: ceiling violated, logged in the events.
     64         #expect(dive.violations.contains { $0.warning == .ceilingViolated })
     65     }
     66 
     67     @Test func logbookRoundTripsThroughJSON() throws {
     68         var book = Logbook()
     69         var dive = simulateDive(depth: 12, bottomMinutes: 5, start: start, logbook: book)
     70         dive.site = "Lac Memphrémagog"
     71         book.add(dive)
     72         let data = try JSONEncoder().encode(book)
     73         let back = try JSONDecoder().decode(Logbook.self, from: data)
     74         #expect(back == book)
     75     }
     76 
     77     @Test func noFlyFollowsDANRuleAndModel() {
     78         var book = Logbook()
     79         let single = simulateDive(depth: 18, bottomMinutes: 20, start: start, logbook: book)
     80         book.add(single)
     81         let rightAfter = SurfaceAdvice.noFlyHours(logbook: book, at: single.end, surfacePressure: standardSurfacePressure)
     82         #expect(abs(rightAfter - 12) < 0.01)
     83         var decoBook = Logbook()
     84         decoBook.add(simulateDive(depth: 40, bottomMinutes: 20, start: start, logbook: decoBook))
     85         let afterDeco = SurfaceAdvice.noFlyHours(logbook: decoBook, at: decoBook.dives[0].end,
     86                                                  surfacePressure: standardSurfacePressure)
     87         #expect(afterDeco >= 24)
     88         let mountain = SurfaceAdvice.hoursBeforeAltitude(2500, logbook: decoBook, at: decoBook.dives[0].end,
     89                                                          surfacePressure: standardSurfacePressure)
     90         print("After 40 m × 20 min: flying in \(afterDeco) h, 2500 m pass in \(mountain) h")
     91         #expect(mountain > 0)
     92     }
     93 
     94     @Test func uddfExport() {
     95         var book = Logbook()
     96         var dive = simulateDive(depth: 18, bottomMinutes: 10, start: start, logbook: book)
     97         dive.notes = "Épave <test> & essai"
     98         book.add(dive)
     99         let xml = UDDF.export(book.dives)
    100         #expect(xml.contains("<uddf xmlns=\"http://www.streit.cc/uddf/3.2/\""))
    101         #expect(xml.contains("<name>Open Rémora</name>"))
    102         #expect(UDDF.export(book.dives, generatorName: "Open Mérou").contains("<name>Open Mérou</name>"))
    103         #expect(xml.contains("<mix id=\"mix1\"><name>Air</name><o2>0.210</o2>"))
    104         #expect(xml.contains("<greatestdepth>18.00</greatestdepth>"))
    105         #expect(xml.contains("Épave &lt;test&gt; &amp; essai"))
    106         #expect(xml.components(separatedBy: "<waypoint>").count - 1 == book.dives[0].samples.count)
    107         // Well-formed XML
    108         #expect((try? XMLDocument(xmlString: xml)) != nil)
    109     }
    110 }