46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"git.ilapage.cn/OPC/chorus/internal/core/model"
|
|
)
|
|
|
|
func TestSafeReturnTo(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
want string
|
|
}{
|
|
{"", "/"},
|
|
{"/", "/"},
|
|
{"/generations/12?tab=result", "/generations/12?tab=result"},
|
|
{"//example.invalid", "/"},
|
|
{"/\\example.invalid", "/"},
|
|
{"https://example.invalid", "/"},
|
|
{"/line\nbreak", "/"},
|
|
}
|
|
for _, test := range tests {
|
|
if got := safeReturnTo(test.input); got != test.want {
|
|
t.Errorf("safeReturnTo(%q) = %q, want %q", test.input, got, test.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGenerationResponseUsesUTCAndIncludesStartedAt(t *testing.T) {
|
|
location := time.FixedZone("UTC+8", 8*60*60)
|
|
created := time.Date(2026, 8, 25, 17, 0, 0, 0, location)
|
|
started := created.Add(5 * time.Second)
|
|
response := generationResponse(model.Generation{ID: 7, Status: model.StatusRunning, CreatedAt: created, StartedAt: &started})
|
|
if got := response["created_at"].(time.Time); got.Location() != time.UTC || got.Hour() != 9 {
|
|
t.Fatalf("created_at = %#v", got)
|
|
}
|
|
gotStarted, ok := response["started_at"].(*time.Time)
|
|
if !ok || gotStarted == nil || gotStarted.Location() != time.UTC || gotStarted.Hour() != 9 {
|
|
t.Fatalf("started_at = %#v", response["started_at"])
|
|
}
|
|
if response["completed_at"] != (*time.Time)(nil) {
|
|
t.Fatalf("completed_at = %#v", response["completed_at"])
|
|
}
|
|
}
|