package placement

import (
	"testing"

	"haixun-backend/internal/library/websearch"
)

func TestNextRecencyCascadeWindowSkipsRanWindows(t *testing.T) {
	ran := map[int]bool{7: true}
	if got := nextRecencyCascadeWindow(ran); got != 14 {
		t.Fatalf("expected 14, got %d", got)
	}
	ran[14] = true
	if got := nextRecencyCascadeWindow(ran); got != 30 {
		t.Fatalf("expected 30, got %d", got)
	}
	ran[30] = true
	if got := nextRecencyCascadeWindow(ran); got != 0 {
		t.Fatalf("expected 0, got %d", got)
	}
}

func TestPassesPostAgeUsesRecencyWindow(t *testing.T) {
	old := timeNow().AddDate(0, 0, -10).Format("2006-01-02")
	if PassesPostAge(old, QueryRecency, 7) {
		t.Fatal("expected 10-day-old post to fail 7-day recency window")
	}
	if !PassesPostAge(old, QueryRecency, 14) {
		t.Fatal("expected 10-day-old post to pass 14-day recency window")
	}
}

func TestBuildRecencyCascadeQuery(t *testing.T) {
	st := &tagCascadeState{Tag: "洗衣精 推薦", ProductFitScore: 55}
	tq, ok := buildRecencyCascadeQuery(st, 14, websearch.ProviderBrave)
	if !ok || tq.RecencyDays != 14 || tq.Dimension != QueryRecency {
		t.Fatalf("unexpected cascade query: %+v ok=%v", tq, ok)
	}
}
