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 @@ -37,6 +37,18 @@ private const val SKIP_MS = 30_000L
internal fun vaultPlayerDataSourceFactory(store: VaultStore, fileId: ByteArray): VaultDataSource.Factory =
VaultDataSource.Factory { store.openReader(fileId) }

/**
* Pure skip-forward target computation, extracted so it's unit-testable
* without a real ExoPlayer. [durationMs] is `C.TIME_UNSET` (negative) while
* the duration isn't known yet — clamping against it would collapse the
* target to 0 instead of leaving the skip a no-op-sized nudge forward.
*/
internal fun skipForwardTarget(currentPositionMs: Long, durationMs: Long, skipMs: Long): Long {
val target = currentPositionMs + skipMs
val clamped = if (durationMs > 0) target.coerceAtMost(durationMs) else target
return clamped.coerceAtLeast(0)
}

data class VaultPlayerUiState(
val title: String = "",
val isLoading: Boolean = true,
Expand Down Expand Up @@ -162,7 +174,7 @@ class VaultPlayerViewModel @Inject constructor(
}

fun onSkipForward() {
player?.let { exo -> exo.seekTo((exo.currentPosition + SKIP_MS).coerceAtMost(exo.duration.coerceAtLeast(0))) }
player?.let { exo -> exo.seekTo(skipForwardTarget(exo.currentPosition, exo.duration, SKIP_MS)) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package xyz.libravault.feature.vault

import androidx.media3.common.C
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

/**
* Regression guard: [skipForwardTarget] used to clamp the skip target against
* `exo.duration.coerceAtLeast(0)`, which collapsed the target to 0 (the very
* start of the book) whenever the duration wasn't known yet
* ([C.TIME_UNSET] is negative), instead of leaving the skip a plain forward
* nudge.
*/
class VaultPlayerSkipForwardTargetTest {

@Test
fun `skips forward by the full amount when duration is unknown`() {
assertEquals(30_000L, skipForwardTarget(currentPositionMs = 0L, durationMs = C.TIME_UNSET, skipMs = 30_000L))
assertEquals(45_000L, skipForwardTarget(currentPositionMs = 15_000L, durationMs = C.TIME_UNSET, skipMs = 30_000L))
}

@Test
fun `clamps to the known duration`() {
assertEquals(100_000L, skipForwardTarget(currentPositionMs = 90_000L, durationMs = 100_000L, skipMs = 30_000L))
}

@Test
fun `never seeks to a negative position`() {
assertEquals(0L, skipForwardTarget(currentPositionMs = -10_000L, durationMs = C.TIME_UNSET, skipMs = 5_000L))
}
}
Loading