package connectsrv

import (
	"testing"
	"time"
)

func TestActiveUsersCountsAndPrune(t *testing.T) {
	a := newActiveUsers()
	base := time.Date(2026, 6, 5, 12, 0, 0, 0, time.UTC)
	cur := base
	a.now = func() time.Time { return cur }

	a.seen("u1") // at base
	cur = base.Add(2 * time.Hour)
	a.seen("u2") // base+2h
	cur = base.Add(50 * time.Hour)
	a.seen("u3") // base+50h

	windows := []time.Duration{24 * time.Hour, 7 * 24 * time.Hour}

	// now = base+50h: u3 within 24h; all three within 7d.
	got := a.counts(windows)
	if got[0] != 1 || got[1] != 3 {
		t.Fatalf("counts at +50h = %v, want [1 3]", got)
	}

	// now = base+169h: u1 (age 169h) prunes past the 7d window; u2/u3 remain in 7d.
	cur = base.Add(169 * time.Hour)
	got = a.counts(windows)
	if got[0] != 0 || got[1] != 2 {
		t.Fatalf("counts at +169h = %v, want [0 2]", got)
	}
	if _, ok := a.lastSeen["u1"]; ok {
		t.Fatalf("u1 should have been pruned from the tracker")
	}
}

func TestActiveUsersIgnoresEmpty(t *testing.T) {
	a := newActiveUsers()
	a.seen("")
	if got := a.counts([]time.Duration{time.Hour}); got[0] != 0 {
		t.Fatalf("empty uid recorded: got %v", got)
	}
}
