test: fix config tests and add Sora/User component tests
Some checks failed
CI / test (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
Security Scan / backend-security (push) Has been cancelled
Security Scan / frontend-security (push) Has been cancelled

- Fix config_test.go viper isolation by creating empty config file in temp dir
- Fix TestLoadForcedCodexInstructionsTemplate path handling for Windows
- Add SoraGeneratePage.spec.ts with comprehensive tests for Sora generation
- Add UserEditModal.spec.ts with tests for user edit modal
- Update sora_handler_test.go with additional field tests
This commit is contained in:
User
2026-04-16 10:35:54 +08:00
parent 2d59b9ebfc
commit 7fa795e6a4
4 changed files with 931 additions and 7 deletions

View File

@@ -21,7 +21,6 @@ func TestSoraHandler_ListGenerations(t *testing.T) {
handler.ListGenerations(c)
// ListGenerations 返回空列表,不需要依赖
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), "items")
}
@@ -31,7 +30,6 @@ func TestSoraHandler_ClearUserStorage_InvalidUserID(t *testing.T) {
handler := &SoraHandler{}
// 只测试无法解析为 int64 的情况
testCases := []struct {
name string
userID string
@@ -125,9 +123,28 @@ func TestSoraGenerationAdminResponse_Fields(t *testing.T) {
assert.Equal(t, "completed", resp.Status)
assert.Equal(t, "s3", resp.StorageType)
assert.Equal(t, int64(1024*1024*10), resp.FileSizeBytes)
assert.NotNil(t, resp.CompletedAt)
}
func TestSoraGenerationAdminResponse_NilCompletedAt(t *testing.T) {
resp := SoraGenerationAdminResponse{
ID: 1,
UserID: 100,
Username: "testuser",
Email: "test@example.com",
Model: "sora2",
Prompt: "A beautiful sunset",
MediaType: "video",
Status: "pending",
StorageType: "upstream",
CreatedAt: "2024-01-01T10:00:00Z",
CompletedAt: nil,
}
assert.Equal(t, "pending", resp.Status)
assert.Nil(t, resp.CompletedAt)
}
// TestNewSoraHandler tests the constructor
func TestNewSoraHandler(t *testing.T) {
handler := NewSoraHandler(nil, nil, nil)
assert.NotNil(t, handler)
@@ -136,7 +153,6 @@ func TestNewSoraHandler(t *testing.T) {
assert.Nil(t, handler.userRepo)
}
// Test helper: verify service.User has Sora fields
func TestUser_SoraFields(t *testing.T) {
user := &service.User{
ID: 1,
@@ -150,7 +166,6 @@ func TestUser_SoraFields(t *testing.T) {
assert.Equal(t, int64(1*1024*1024*1024), user.SoraStorageUsedBytes)
}
// Test helper: verify service.QuotaInfo fields
func TestQuotaInfo_Fields(t *testing.T) {
quota := &service.QuotaInfo{
QuotaBytes: 10 * 1024 * 1024 * 1024,
@@ -163,3 +178,117 @@ func TestQuotaInfo_Fields(t *testing.T) {
assert.Equal(t, int64(1*1024*1024*1024), quota.UsedBytes)
assert.Equal(t, "user", quota.QuotaSource)
}
func TestSoraSystemStatsResponse_JSON(t *testing.T) {
resp := SoraSystemStatsResponse{
TotalUsers: 10,
TotalGenerations: 100,
TotalStorageBytes: 1024,
ActiveGenerations: 5,
ByStatus: map[string]int64{"completed": 80},
ByModel: map[string]int64{"sora2": 50},
}
// Verify JSON tags by checking field values
assert.Equal(t, int64(10), resp.TotalUsers)
assert.Equal(t, int64(100), resp.TotalGenerations)
assert.Equal(t, int64(1024), resp.TotalStorageBytes)
assert.Equal(t, int64(5), resp.ActiveGenerations)
}
func TestSoraUserStatsResponse_JSON(t *testing.T) {
resp := SoraUserStatsResponse{
UserID: 1,
Username: "testuser",
Email: "test@example.com",
QuotaBytes: 1024,
UsedBytes: 512,
AvailableBytes: 512,
QuotaSource: "user",
GenerationsCount: 10,
ActiveCount: 2,
TotalFileSizeBytes: 1024,
}
// Verify all fields
assert.Equal(t, int64(1), resp.UserID)
assert.Equal(t, "testuser", resp.Username)
assert.Equal(t, "test@example.com", resp.Email)
assert.Equal(t, int64(1024), resp.QuotaBytes)
assert.Equal(t, int64(512), resp.UsedBytes)
assert.Equal(t, int64(512), resp.AvailableBytes)
assert.Equal(t, "user", resp.QuotaSource)
assert.Equal(t, int64(10), resp.GenerationsCount)
assert.Equal(t, int64(2), resp.ActiveCount)
assert.Equal(t, int64(1024), resp.TotalFileSizeBytes)
}
func TestSoraSystemStatsResponse_EmptyMaps(t *testing.T) {
resp := SoraSystemStatsResponse{
TotalUsers: 0,
TotalGenerations: 0,
TotalStorageBytes: 0,
ActiveGenerations: 0,
ByStatus: map[string]int64{},
ByModel: map[string]int64{},
}
assert.Equal(t, int64(0), resp.TotalUsers)
assert.Equal(t, int64(0), resp.TotalGenerations)
assert.Equal(t, int64(0), resp.TotalStorageBytes)
assert.Equal(t, int64(0), resp.ActiveGenerations)
assert.NotNil(t, resp.ByStatus)
assert.NotNil(t, resp.ByModel)
}
func TestSoraUserStatsResponse_QuotaSources(t *testing.T) {
sources := []string{"user", "group", "system", "unlimited"}
for _, source := range sources {
resp := SoraUserStatsResponse{
UserID: 1,
QuotaSource: source,
}
assert.Equal(t, source, resp.QuotaSource)
}
}
func TestSoraGenerationAdminResponse_Statuses(t *testing.T) {
statuses := []string{"pending", "generating", "completed", "failed", "cancelled"}
for _, status := range statuses {
resp := SoraGenerationAdminResponse{
ID: 1,
Status: status,
}
assert.Equal(t, status, resp.Status)
}
}
func TestSoraGenerationAdminResponse_MediaTypes(t *testing.T) {
mediaTypes := []string{"video", "image"}
for _, mt := range mediaTypes {
resp := SoraGenerationAdminResponse{
ID: 1,
MediaType: mt,
}
assert.Equal(t, mt, resp.MediaType)
}
}
func TestSoraGenerationAdminResponse_StorageTypes(t *testing.T) {
storageTypes := []string{"s3", "upstream"}
for _, st := range storageTypes {
resp := SoraGenerationAdminResponse{
ID: 1,
StorageType: st,
}
assert.Equal(t, st, resp.StorageType)
}
}