Files
tokens-reef/backend/internal/handler/admin/sora_handler_test.go
User 2d59b9ebfc
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
feat: add Sora admin backend and fix type inconsistencies
Backend changes:
- Add SoraHandler for admin Sora management APIs
  - GET /api/v1/admin/sora/stats - system statistics
  - GET /api/v1/admin/sora/users - user storage stats
  - GET /api/v1/admin/sora/generations - generation records
  - DELETE /api/v1/admin/sora/users/:id/storage - clear user storage
- Add sora_storage_quota_bytes to AdminUser DTO
- Add SoraStorageQuotaBytes to UpdateUserInput for admin user updates
- Add comprehensive tests for SoraHandler

Frontend changes:
- Add soraAdminAPI for Sora management
- Add sora_storage_quota_bytes and sora_storage_used_bytes to AdminUser type
- Add Sora storage quota field to UserEditModal (GB unit)
- Fix UsageLog type: add media_type, fix duration_ms to optional
- Fix AdminUsageLog type: add channel_id, billing_tier

Test fixes:
- Add window.matchMedia mock to AccountUsageCell.spec.ts
- Add tlsFingerprintProfileAPI mock to EditAccountModal.spec.ts
- Fix loadTLSProfiles function order in EditAccountModal.vue
- Fix translation key references in AccountStatusIndicator.spec.ts
2026-04-16 09:20:23 +08:00

166 lines
4.9 KiB
Go

package admin
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestSoraHandler_ListGenerations(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := &SoraHandler{}
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodGet, "/admin/sora/generations", nil)
handler.ListGenerations(c)
// ListGenerations 返回空列表,不需要依赖
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), "items")
}
func TestSoraHandler_ClearUserStorage_InvalidUserID(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := &SoraHandler{}
// 只测试无法解析为 int64 的情况
testCases := []struct {
name string
userID string
expected int
}{
{"empty string", "", http.StatusBadRequest},
{"non-numeric", "abc", http.StatusBadRequest},
{"float", "1.5", http.StatusBadRequest},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodDelete, "/admin/sora/users/"+tc.userID+"/storage", nil)
c.Params = gin.Params{{Key: "id", Value: tc.userID}}
handler.ClearUserStorage(c)
assert.Equal(t, tc.expected, w.Code)
})
}
}
func TestSoraSystemStatsResponse_Fields(t *testing.T) {
resp := SoraSystemStatsResponse{
TotalUsers: 10,
TotalGenerations: 100,
TotalStorageBytes: 1024 * 1024 * 1024,
ActiveGenerations: 5,
ByStatus: map[string]int64{"completed": 80, "failed": 20},
ByModel: map[string]int64{"sora2": 50, "sora1": 50},
}
assert.Equal(t, int64(10), resp.TotalUsers)
assert.Equal(t, int64(100), resp.TotalGenerations)
assert.Equal(t, int64(1024*1024*1024), resp.TotalStorageBytes)
assert.Equal(t, int64(5), resp.ActiveGenerations)
assert.Equal(t, int64(80), resp.ByStatus["completed"])
assert.Equal(t, int64(50), resp.ByModel["sora2"])
}
func TestSoraUserStatsResponse_Fields(t *testing.T) {
resp := SoraUserStatsResponse{
UserID: 1,
Username: "testuser",
Email: "test@example.com",
QuotaBytes: 10 * 1024 * 1024 * 1024,
UsedBytes: 1 * 1024 * 1024 * 1024,
AvailableBytes: 9 * 1024 * 1024 * 1024,
QuotaSource: "user",
GenerationsCount: 10,
ActiveCount: 2,
TotalFileSizeBytes: 1 * 1024 * 1024 * 1024,
}
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(10*1024*1024*1024), resp.QuotaBytes)
assert.Equal(t, int64(1*1024*1024*1024), resp.UsedBytes)
assert.Equal(t, "user", resp.QuotaSource)
assert.Equal(t, int64(10), resp.GenerationsCount)
assert.Equal(t, int64(2), resp.ActiveCount)
}
func TestSoraGenerationAdminResponse_Fields(t *testing.T) {
completedAt := "2024-01-01T12:00:00Z"
resp := SoraGenerationAdminResponse{
ID: 1,
UserID: 100,
Username: "testuser",
Email: "test@example.com",
Model: "sora2",
Prompt: "A beautiful sunset",
MediaType: "video",
Status: "completed",
StorageType: "s3",
MediaURL: "https://example.com/video.mp4",
FileSizeBytes: 1024 * 1024 * 10,
ErrorMessage: "",
CreatedAt: "2024-01-01T10:00:00Z",
CompletedAt: &completedAt,
}
assert.Equal(t, int64(1), resp.ID)
assert.Equal(t, int64(100), resp.UserID)
assert.Equal(t, "testuser", resp.Username)
assert.Equal(t, "sora2", resp.Model)
assert.Equal(t, "video", resp.MediaType)
assert.Equal(t, "completed", resp.Status)
assert.Equal(t, "s3", resp.StorageType)
assert.Equal(t, int64(1024*1024*10), resp.FileSizeBytes)
}
// TestNewSoraHandler tests the constructor
func TestNewSoraHandler(t *testing.T) {
handler := NewSoraHandler(nil, nil, nil)
assert.NotNil(t, handler)
assert.Nil(t, handler.soraGenService)
assert.Nil(t, handler.soraQuotaService)
assert.Nil(t, handler.userRepo)
}
// Test helper: verify service.User has Sora fields
func TestUser_SoraFields(t *testing.T) {
user := &service.User{
ID: 1,
Email: "test@example.com",
SoraStorageQuotaBytes: 10 * 1024 * 1024 * 1024,
SoraStorageUsedBytes: 1 * 1024 * 1024 * 1024,
}
assert.Equal(t, int64(1), user.ID)
assert.Equal(t, int64(10*1024*1024*1024), user.SoraStorageQuotaBytes)
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,
UsedBytes: 1 * 1024 * 1024 * 1024,
AvailableBytes: 9 * 1024 * 1024 * 1024,
QuotaSource: "user",
}
assert.Equal(t, int64(10*1024*1024*1024), quota.QuotaBytes)
assert.Equal(t, int64(1*1024*1024*1024), quota.UsedBytes)
assert.Equal(t, "user", quota.QuotaSource)
}