package comm import ( "testing" "time" ) func TestMemoryRelativeRange(t *testing.T) { loc := time.UTC // 2026-09-05 是周六 now := time.Date(2026, 9, 5, 12, 0, 0, 0, loc) cases := []struct{ kind, start, end string }{ {"today", "2026-09-05", "2026-09-05"}, {"yesterday", "2026-09-04", "2026-09-04"}, // ISO 周:周一起,所以本周是 08-31(周一) ~ 09-06(周日) {"this_week", "2026-08-31", "2026-09-06"}, {"last_week", "2026-08-24", "2026-08-30"}, {"this_month", "2026-09-01", "2026-09-30"}, {"last_month", "2026-08-01", "2026-08-31"}, {"next_7_days", "2026-09-05", "2026-09-12"}, {"last_7_days", "2026-08-29", "2026-09-05"}, } for _, c := range cases { s, e, err := MemoryRelativeRange(c.kind, now) if err != nil { t.Errorf("%s: 报错 %v", c.kind, err) continue } if s != c.start || e != c.end { t.Errorf("%s: 期望 %s~%s 得到 %s~%s", c.kind, c.start, c.end, s, e) } } } func TestMemoryRelativeRange_Unknown(t *testing.T) { if _, _, err := MemoryRelativeRange("last_fortnight", time.Now()); err == nil { t.Error("不认识的范围必须报错,不能静默返回一个区间") } } func TestNormalizeMemoryCategories(t *testing.T) { got, bad := NormalizeMemoryCategories([]string{"todo", "TODO", " idea ", "nonsense", ""}) if bad != "nonsense" { t.Errorf("应报出第一个非法分类 nonsense,得到 %q", bad) } // 大小写与空格都要归一 want := []string{"todo", "todo", "idea"} if len(got) != len(want) { t.Fatalf("期望 %d 个合法分类,得到 %d: %v", len(want), len(got), got) } for i := range want { if got[i] != want[i] { t.Errorf("第 %d 个:期望 %s 得到 %s", i, want[i], got[i]) } } } func TestNormalizeMemoryCategories_AllValid(t *testing.T) { _, bad := NormalizeMemoryCategories([]string{"todo", "alarm", "idea", "expense"}) if bad != "" { t.Errorf("全部合法时不该报错,却得到 %q", bad) } }