首页接口实现

This commit is contained in:
2026-07-21 16:57:46 +08:00
parent 83d38956ca
commit 5103c08c2b
5 changed files with 918 additions and 177 deletions

87
web/services/home_test.go Normal file
View File

@@ -0,0 +1,87 @@
package services
import (
m "platform/web/models"
"testing"
"time"
)
func TestNewHomeRange(t *testing.T) {
rng, err := newHomeRange("2026-07-20", "2026-07-21")
if err != nil {
t.Fatalf("newHomeRange returned error: %v", err)
}
if got, want := len(rng.Dates), 2; got != want {
t.Fatalf("len(Dates) = %d, want %d", got, want)
}
if got, want := rng.Dates[0], "2026-07-20"; got != want {
t.Fatalf("Dates[0] = %s, want %s", got, want)
}
if got, want := rng.Dates[1], "2026-07-21"; got != want {
t.Fatalf("Dates[1] = %s, want %s", got, want)
}
if got, want := rng.StartUTC.Format(time.RFC3339), "2026-07-19T16:00:00Z"; got != want {
t.Fatalf("StartUTC = %s, want %s", got, want)
}
if got, want := rng.EndUTC.Format(time.RFC3339), "2026-07-21T16:00:00Z"; got != want {
t.Fatalf("EndUTC = %s, want %s", got, want)
}
}
func TestNewHomeRangeRejectsInvalidRange(t *testing.T) {
_, err := newHomeRange("2026-07-21", "2026-07-20")
if err == nil {
t.Fatal("newHomeRange expected error")
}
}
func TestCalcExtractableQuota(t *testing.T) {
loc := homeLocation()
now := time.Date(2026, 7, 21, 10, 0, 0, 0, loc)
got := calcExtractable(m.ResourceModeQuota, 100, 30, 0, nil, nil, now, loc)
if want := int64(70); got != want {
t.Fatalf("calcExtractable quota = %d, want %d", got, want)
}
got = calcExtractable(m.ResourceModeQuota, 100, 130, 0, nil, nil, now, loc)
if want := int64(0); got != want {
t.Fatalf("calcExtractable exhausted quota = %d, want %d", got, want)
}
}
func TestCalcExtractableTimeCountsTodayAndExpiryDate(t *testing.T) {
loc := homeLocation()
now := time.Date(2026, 7, 21, 10, 0, 0, 0, loc)
lastAt := time.Date(2026, 7, 21, 9, 0, 0, 0, loc)
expireAt := time.Date(2026, 7, 23, 12, 0, 0, 0, loc)
got := calcExtractable(m.ResourceModeTime, 100, 0, 30, &lastAt, &expireAt, now, loc)
if want := int64(270); got != want {
t.Fatalf("calcExtractable time = %d, want %d", got, want)
}
}
func TestCalcExtractableTimeResetsDailyWhenLastAtIsNotToday(t *testing.T) {
loc := homeLocation()
now := time.Date(2026, 7, 21, 10, 0, 0, 0, loc)
lastAt := time.Date(2026, 7, 20, 23, 59, 59, 0, loc)
expireAt := time.Date(2026, 7, 21, 23, 0, 0, 0, loc)
got := calcExtractable(m.ResourceModeTime, 100, 0, 80, &lastAt, &expireAt, now, loc)
if want := int64(100); got != want {
t.Fatalf("calcExtractable time reset = %d, want %d", got, want)
}
}
func TestCalcExtractableTimeExpired(t *testing.T) {
loc := homeLocation()
now := time.Date(2026, 7, 21, 10, 0, 0, 0, loc)
expireAt := time.Date(2026, 7, 21, 9, 0, 0, 0, loc)
got := calcExtractable(m.ResourceModeTime, 100, 0, 0, nil, &expireAt, now, loc)
if want := int64(0); got != want {
t.Fatalf("calcExtractable expired time = %d, want %d", got, want)
}
}