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 @@ -4,6 +4,24 @@ import XCTest
/// Golden vectors for content-driven breath protocols (holds + Presence tempos).
final class BreathProtocolPlayerTests: XCTestCase {

func test_negative_stage_duration_clamps_and_schedules_safely() {
let stage = BreathStage(type: .inhale, durationMs: -1)
let proto = BreathProtocol(
id: "negative_duration",
title: "Negative duration",
subtitle: "",
edu: "",
mode: .playable,
category: .ans,
recommendedDurationMs: 1_000,
stages: [stage]
)

XCTAssertEqual(stage.durationMs, 0)
XCTAssertEqual(proto.cycleDurationMs, 0)
XCTAssertTrue(BreathProtocolPlayer.schedule(proto, sessionMs: 1_000).isEmpty)
}

func test_box_one_cycle_cues() {
let proto = BreathProtocolCatalog.protocolById("box_4_4_4_4")!
let cues = BreathProtocolPlayer.schedule(proto, sessionMs: 16_000)
Expand Down
10 changes: 9 additions & 1 deletion android/app/src/main/java/com/noop/analytics/BreathProtocol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ enum class BreathProtocolCategory {
BIOFEEDBACK,
}

data class BreathStage(
data class BreathStage private constructor(
val type: BreathPhase,
val durationMs: Int,
val label: String? = null,
) {
init {
require(durationMs >= 0)
}

companion object {
operator fun invoke(
type: BreathPhase,
durationMs: Int,
label: String? = null,
): BreathStage = BreathStage(type, durationMs.coerceAtLeast(0), label)
}
}

data class BreathProtocol(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ import org.junit.Test
*/
class BreathProtocolPlayerTest {

@Test
fun test_negative_stage_duration_clamps_and_schedules_safely() {
val stage = BreathStage(BreathPhase.INHALE, -1)
val proto = BreathProtocol(
id = "negative_duration",
title = "Negative duration",
subtitle = "",
edu = "",
mode = BreathProtocolMode.PLAYABLE,
category = BreathProtocolCategory.ANS,
recommendedDurationMs = 1_000,
stages = listOf(stage),
)

assertEquals(0, stage.durationMs)
assertEquals(0, proto.cycleDurationMs)
assertTrue(BreathProtocolPlayer.schedule(proto, sessionMs = 1_000).isEmpty())
}

@Test
fun test_box_one_cycle_cues() {
val proto = BreathProtocolCatalog.protocolById("box_4_4_4_4")!!
Expand Down