package memory

import (
	"testing"
	"time"
)

func TestRankProfiles(t *testing.T) {
	now := time.Date(2026, 6, 22, 12, 0, 0, 0, time.UTC)
	rows := []Profile{
		{Key: "高重要近期", Value: "a", Importance: 9, LastSeenAt: now.AddDate(0, 0, -1)},
		{Key: "低重要久远", Value: "b", Importance: 2, LastSeenAt: now.AddDate(0, 0, -60)},
		{Key: "中等", Value: "c", Importance: 5, LastSeenAt: now.AddDate(0, 0, -10)},
	}
	ranked := rankProfiles(rows, now, 0)
	if ranked[0].Key != "高重要近期" || ranked[2].Key != "低重要久远" {
		t.Errorf("应按 Score 降序：高重要近期 > 中等 > 低重要久远，得 %s/%s/%s", ranked[0].Key, ranked[1].Key, ranked[2].Key)
	}
	// 截断 top-N
	if got := rankProfiles(rows, now, 2); len(got) != 2 || got[0].Key != "高重要近期" {
		t.Errorf("top-2 截断错: %d 条 首=%s", len(got), got[0].Key)
	}
	// 原切片不被改动（rankProfiles 应 copy）
	if rows[0].Key != "高重要近期" {
		t.Error("rankProfiles 不应修改入参顺序")
	}
}

func TestRecencyDecay(t *testing.T) {
	now := time.Date(2026, 6, 22, 12, 0, 0, 0, time.UTC)
	if recencyScore(now, time.Time{}) != 1.0 {
		t.Error("无 last_seen 应视为新鲜=1")
	}
	fresh := recencyScore(now, now.AddDate(0, 0, -1))
	old := recencyScore(now, now.AddDate(0, 0, -30))
	if !(fresh > old && old > 0) {
		t.Errorf("越久越低且 >0: fresh=%v old=%v", fresh, old)
	}
}

func TestProfileScore_DefaultImportance(t *testing.T) {
	now := time.Now()
	// importance=0（旧/未评分）应按兜底 5 计，而不是 0（否则被不公平遗忘）。
	p := Profile{Importance: 0, LastSeenAt: now}
	got := profileScore(p, now)
	want := wRecency*1.0 + wImportance*(defaultImportance/10)
	if got != want {
		t.Errorf("未评分行应用兜底 importance: got %v want %v", got, want)
	}
}
