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

SubsurfaceImportTests.swift (3828B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import Foundation
      3 import Testing
      4 @testable import RemoraDeco
      5 
      6 let sampleLog = """
      7 <divelog program='subsurface' version='3'>
      8 <settings>
      9 <divecomputerid model='Shearwater Perdix' deviceid='1234'/>
     10 </settings>
     11 <divesites>
     12 <site uuid='4c2e7d10' name='Carrière Morrison' gps='45.628300 -75.923900'>
     13 </site>
     14 </divesites>
     15 <dives>
     16 <trip date='2026-07-12' time='10:15:00' location='Wakefield'>
     17 <dive number='41' divesiteid='4c2e7d10' date='2026-07-12' time='10:15:00' duration='38:20 min'>
     18   <notes>Visibilité 8 m &amp; thermocline à 9 m</notes>
     19   <cylinder size='11.1 l' workpressure='207.0 bar' description='AL80' o2='32.0%' start='200.0 bar' end='70.0 bar'/>
     20   <divecomputer model='Shearwater Perdix' deviceid='1234' diveid='abcd'>
     21   <depth max='24.3 m' mean='14.1 m' />
     22   <temperature water='9.0 C' />
     23   <surface pressure='0.998 bar' />
     24   <water salinity='1000 g/l' />
     25   <sample time='0:10 min' depth='1.5 m' temp='21.0 C' ndl='99:00 min' />
     26   <sample time='2:00 min' depth='24.3 m' temp='9.0 C' ndl='18:00 min' />
     27   <sample time='25:00 min' depth='24.3 m' in_deco='1' stoptime='2:00 min' stopdepth='3.0 m' />
     28   <sample time='33:00 min' depth='3.0 m' in_deco='0' ndl='99:00 min' />
     29   <sample time='38:20 min' depth='0.0 m' />
     30   </divecomputer>
     31   <divecomputer model='Second ordinateur'>
     32   <sample time='0:10 min' depth='99.0 m' />
     33   </divecomputer>
     34 </dive>
     35 </trip>
     36 <dive number='40' date='2026-06-01' time='14:00:00' duration='45:00 min'>
     37   <location>Lac</location>
     38 </dive>
     39 </dives>
     40 </divelog>
     41 """
     42 
     43 @Suite("Subsurface import")
     44 struct SubsurfaceImportTests {
     45     @Test func parsesDivesSitesAndProfile() throws {
     46         let dives = try SubsurfaceImport.dives(from: Data(sampleLog.utf8))
     47         #expect(dives.map(\.number) == [40, 41])
     48         let d = dives[1]
     49         #expect(d.source == .subsurface)
     50         #expect(d.computerModel == "Shearwater Perdix")
     51         #expect(d.site == "Carrière Morrison")
     52         #expect(d.latitude == 45.6283)
     53         #expect(d.notes == "Visibilité 8 m & thermocline à 9 m")
     54         #expect(d.gas == .nitrox(32))
     55         #expect(d.water == .fresh)
     56         #expect(d.surfacePressure == 0.998)
     57         #expect(d.samples.count == 5)              // first dive computer only
     58         #expect(d.maxDepth == 24.3)
     59         #expect(d.duration == 38 * 60 + 20)
     60         #expect(d.minTemperature == 9)
     61         #expect(d.samples[2].temperature == 9)     // temperature carries forward
     62         #expect(d.hadDecompression)
     63         #expect(d.samples[2].stopDepth == 3 && d.samples[2].stopMinutes == 2)
     64         #expect(d.samples[3].stopDepth == nil)
     65         #expect(d.tissuesAfter == nil)
     66     }
     67 
     68     @Test func diveWithoutProfileKeepsDuration() throws {
     69         let d = try SubsurfaceImport.dives(from: Data(sampleLog.utf8))[0]
     70         #expect(d.duration == 45 * 60)
     71         #expect(d.site == "Lac")
     72     }
     73 
     74     @Test func mergeIsIdempotentAndKeepsNumbers() throws {
     75         let dives = try SubsurfaceImport.dives(from: Data(sampleLog.utf8))
     76         var book = Logbook()
     77         #expect(book.merge(dives) == 2)
     78         #expect(book.merge(try SubsurfaceImport.dives(from: Data(sampleLog.utf8))) == 0)
     79         let mine = simulateDive(depth: 12, bottomMinutes: 5,
     80                                 start: Date(timeIntervalSince1970: 1_800_000_000), logbook: book)
     81         book.add(mine)
     82         #expect(book.dives.map(\.number) == [40, 41, 42])
     83         // Tissues come from the last Open Rémora dive, not from the imported ones.
     84         #expect(book.tissues(at: book.dives[2].end.addingTimeInterval(600),
     85                              surfacePressure: standardSurfacePressure) != nil)
     86     }
     87 
     88     @Test func rejectsOtherXML() {
     89         #expect(throws: SubsurfaceImport.Failure.self) {
     90             try SubsurfaceImport.dives(from: Data("<uddf></uddf>".utf8))
     91         }
     92     }
     93 }