package me.khodak.claudeusage

import me.khodak.claudeusage.data.PreferencesManager
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * Pure-JVM tests for the history-throttle decision extracted from PreferencesManager.recordHistory.
 * No Android Context — exercises only PreferencesManager.shouldRecordHistory.
 */
class HistoryThrottleTest {

    private val gap = PreferencesManager.MIN_HISTORY_GAP_MS

    @Test
    fun returnsTrueWhenNoPreviousPoint() {
        // First-ever reading (lastEpochMs == null) is always recorded.
        assertTrue(PreferencesManager.shouldRecordHistory(null, now = 1_000_000L, minGapMs = gap))
    }

    @Test
    fun returnsFalseWhenWithinGap() {
        val last = 1_000_000L
        // One millisecond before the gap elapses → throttled.
        val now = last + gap - 1L
        assertFalse(PreferencesManager.shouldRecordHistory(last, now, gap))
    }

    @Test
    fun returnsTrueExactlyAtGapBoundary() {
        val last = 1_000_000L
        // Exactly at the gap (>= boundary is inclusive) → recorded.
        val now = last + gap
        assertTrue(PreferencesManager.shouldRecordHistory(last, now, gap))
    }

    @Test
    fun returnsTrueAfterGap() {
        val last = 1_000_000L
        val now = last + gap + 1L
        assertTrue(PreferencesManager.shouldRecordHistory(last, now, gap))
    }
}
