Files
claude-usage-widget/app/src/test/java/me/khodak/claudeusage/HistoryThrottleTest.kt
T
amir 31e18ed5e9
Build APK / build (push) Successful in 1m29s
Harden WebView nav + add test suite + fix lint for production
Security:
- LoginActivity WebView now enforces a host allow-list in
  shouldOverrideUrlLoading: only claude.ai + required SSO/CDN hosts
  (Google, Apple, Cloudflare, gstatic, recaptcha) can navigate; everything
  else is blocked. market://intent:// still blocked; about:/data: allowed.
  Device-verified: claude.ai login + Cloudflare challenge still load.

Tests (33, pure-JVM JUnit4, no device needed):
- Extracted shouldRecordHistory() pure throttle decision (regression guard
  for the empty-history-chart bug) + HistoryThrottleTest.
- UsageDataTest (mergedWith last-good/partial-union, computed props),
  PaceCalcTest, PeakHoursTest.
- Added junit:junit:4.13.2 as testImplementation only.

Build quality:
- widget_layout.xml: suppress false-positive UseAppTint lint on the widget
  refresh button (app:tint doesn't work in RemoteViews; android:tint is
  correct here) so lintDebug is clean.

Verified locally: 33 unit tests pass, lintDebug 0 errors, signed
assembleRelease OK (apksigner verified, signer identity unchanged),
emulator smoke test launches + renders without crash.
2026-06-10 11:12:02 +00:00

45 lines
1.4 KiB
Kotlin

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))
}
}