Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public enum WhoopLiveCapabilities {
caps.subtracting([.spo2])
}

/// SQL-safe rewrite of a comma-joined capabilities string: remove bare `spo2` tokens only.
/// SQL-safe rewrite of a comma-joined capabilities string: trim tokens and remove bare `spo2` only.
/// Does not touch other metrics. Empty result is preserved as empty (caller should not write that).
public static func stripSpo2Token(fromEncoded encoded: String) -> String {
encoded
.split(separator: ",", omittingEmptySubsequences: true)
.map(String.init)
.filter { $0 != Metric.spo2.rawValue }
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty && $0 != Metric.spo2.rawValue }
.joined(separator: ",")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ final class WhoopLiveCapabilitiesTests: XCTestCase {
)
}

func testStripSpo2TokenCanonicalizesWhitespaceBearingStoredRows() {
let cases: [(encoded: String, expected: String)] = [
// Canonical controls.
("hr,spo2,hrv", "hr,hrv"),
("hr,hrv,skinTemp", "hr,hrv,skinTemp"),
// Same logical tokens as they can appear in a restored SQLite row.
("hr, spo2,hrv", "hr,hrv"),
(" hr , spo2 , hrv ", "hr,hrv"),
("hr,,spo2, ,hrv", "hr,hrv"),
("\thr,\tspo2,\nhrv\t", "hr,hrv"),
(" hr ,hrv", "hr,hrv"),
(", hr ,, spo2 , skinTemp ,sleep ,", "hr,skinTemp,sleep"),
(" spo2 ", ""),
(" spo2 , spo2 ", ""),
]

for testCase in cases {
XCTAssertEqual(
WhoopLiveCapabilities.stripSpo2Token(fromEncoded: testCase.encoded),
testCase.expected,
"encoded=\(testCase.encoded.debugDescription)"
)
}
}

func testWithoutCalibratedSpo2() {
let raw: Set<Metric> = [.hr, .hrv, .spo2, .skinTemp]
XCTAssertEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ class WhoopLiveCapabilitiesTest {
)
}

@Test
fun stripSpo2TokenCanonicalizesWhitespaceBearingStoredRows() {
val cases = listOf(
// Canonical controls.
"hr,spo2,hrv" to "hr,hrv",
"hr,hrv,skinTemp" to "hr,hrv,skinTemp",
// Same logical tokens as they can appear in a restored SQLite row.
"hr, spo2,hrv" to "hr,hrv",
" hr , spo2 , hrv " to "hr,hrv",
"hr,,spo2, ,hrv" to "hr,hrv",
"\thr,\tspo2,\nhrv\t" to "hr,hrv",
" hr ,hrv" to "hr,hrv",
", hr ,, spo2 , skinTemp ,sleep ," to "hr,skinTemp,sleep",
" spo2 " to "",
" spo2 , spo2 " to "",
)

for ((encoded, expected) in cases) {
assertEquals(
"encoded=${encoded.quoteForFailure()}",
expected,
WhoopLiveCapabilities.stripSpo2Token(encoded),
)
}
}

private fun String.quoteForFailure(): String = "\"$this\""

@Test
fun withoutCalibratedSpo2() {
val raw = setOf(Metric.hr, Metric.hrv, Metric.spo2, Metric.skinTemp)
Expand Down