test: add comprehensive test coverage and improve code quality

- Add new test files for auth, service, and handler modules
- Improve test organization and coverage
- Refactor code for better maintainability
- Add captcha, settings, stats, and theme handler tests
- Add auth module tests (CAS, OAuth, password, SSO, state)
- Add service layer tests for auth, export, permissions, roles
- All Go tests pass (exit code 0)
- All frontend tests pass (325 tests in 59 files)
This commit is contained in:
2026-04-17 20:43:50 +08:00
parent 0d66aa0423
commit 582ad7a069
136 changed files with 19010 additions and 8544 deletions

View File

@@ -52,13 +52,13 @@ import (
// LatencyStats 延迟统计结果
type LatencyStats struct {
Samples int // 采样次数
Min time.Duration // 最小值
Max time.Duration // 最大值
Mean time.Duration // 平均值
P50 time.Duration // 中位数
P95 time.Duration // 95th 百分位
P99 time.Duration // 99th 百分位
Samples int // 采样次数
Min time.Duration // 最小值
Max time.Duration // 最大值
Mean time.Duration // 平均值
P50 time.Duration // 中位数
P95 time.Duration // 95th 百分位
P99 time.Duration // 99th 百分位
rawDurations []time.Duration // 原始数据(内部使用)
}
@@ -89,12 +89,12 @@ func (s *LatencyStats) Compute() LatencyStats {
result := LatencyStats{
Samples: n,
Min: durations[0],
Max: durations[n-1],
Mean: sum / time.Duration(n),
P50: durations[n*50/100],
P95: durations[n*95/100],
P99: durations[n*99/100],
Min: durations[0],
Max: durations[n-1],
Mean: sum / time.Duration(n),
P50: durations[n*50/100],
P95: durations[n*95/100],
P99: durations[n*99/100],
}
return result
}
@@ -442,9 +442,9 @@ func TestScale_LL_001C_CursorPagination(t *testing.T) {
idx := i + j
logs = append(logs, &domain.LoginLog{
UserID: ptrInt64(int64(idx % 1000)),
LoginType: 1,
LoginType: 1,
IP: "127.0.0.1",
Status: 1,
Status: 1,
CreatedAt: time.Now().Add(-time.Duration(idx) * time.Second),
})
}
@@ -546,9 +546,9 @@ func TestScale_OPLOG_001C_OperationLogCursorPagination(t *testing.T) {
RequestPath: fmt.Sprintf("/api/resource/%d", idx%1000),
ResponseStatus: 200,
IP: "10.0.0." + string(rune('1'+idx%9)),
UserAgent: "test-agent",
UserID: &uid,
CreatedAt: time.Now().Add(-time.Duration(idx) * time.Second),
UserAgent: "test-agent",
UserID: &uid,
CreatedAt: time.Now().Add(-time.Duration(idx) * time.Second),
})
}
db.CreateInBatches(logs, 2000)
@@ -1258,7 +1258,7 @@ func TestScale_AUTH_001_LoginFailureLogScale(t *testing.T) {
failIdx := (idx / userCount) % len(failReasons)
loginLogRepo.Create(context.Background(), &domain.LoginLog{
UserID: &users[userIdx].ID,
LoginType: int(domain.LoginTypePassword),
LoginType: int(domain.LoginTypePassword),
IP: fmt.Sprintf("10.0.%d.%d", idx%256, failIdx),
Status: 0,
FailReason: failReasons[failIdx],
@@ -1322,10 +1322,10 @@ func TestScale_OPLOG_001_OperationLogScale(t *testing.T) {
OperationType: operations[i%len(operations)],
OperationName: "TestOperation",
RequestMethod: "POST",
RequestPath: "/api/v1/test",
RequestPath: "/api/v1/test",
ResponseStatus: 200,
IP: fmt.Sprintf("192.168.%d.%d", i%256, (i*7)%256),
UserAgent: "TestAgent/1.0",
IP: fmt.Sprintf("192.168.%d.%d", i%256, (i*7)%256),
UserAgent: "TestAgent/1.0",
})
}
@@ -1651,7 +1651,6 @@ func TestScale_CONC_003_ConcurrentLoginLogWrite(t *testing.T) {
}
}
// =============================================================================
// Helper Functions
// =============================================================================