(t *testing.T)
| 217 | } |
| 218 | |
| 219 | func TestInMemoryAuditLog_PurgeEvents(t *testing.T) { |
| 220 | auditLog := NewInMemoryAuditLog(nil) |
| 221 | defer func() { _ = auditLog.Close() }() |
| 222 | |
| 223 | now := time.Now() |
| 224 | yesterday := now.Add(-24 * time.Hour) |
| 225 | |
| 226 | // 添加旧事件和新事件 |
| 227 | _ = auditLog.LogEvent(AuditEvent{ |
| 228 | Type: AuditTypeUserLogin, |
| 229 | UserID: "user1", |
| 230 | Message: "Old login", |
| 231 | Timestamp: yesterday.Add(-1 * time.Hour), |
| 232 | }) |
| 233 | _ = auditLog.LogEvent(AuditEvent{ |
| 234 | Type: AuditTypeUserLogin, |
| 235 | UserID: "user2", |
| 236 | Message: "Recent login", |
| 237 | Timestamp: now, |
| 238 | }) |
| 239 | |
| 240 | // 清除昨天之前的事件 |
| 241 | count, err := auditLog.PurgeEvents(context.Background(), yesterday) |
| 242 | if err != nil { |
| 243 | t.Fatalf("PurgeEvents failed: %v", err) |
| 244 | } |
| 245 | |
| 246 | if count != 1 { |
| 247 | t.Errorf("expected 1 event purged, got %d", count) |
| 248 | } |
| 249 | |
| 250 | // 使用GetStatus验证剩余事件数 |
| 251 | status := auditLog.GetStatus() |
| 252 | if status.TotalEvents != 1 { |
| 253 | t.Errorf("expected 1 event remaining, got %d", status.TotalEvents) |
| 254 | } |
| 255 | |
| 256 | // 使用GetEventsByUser验证user2的事件还在 |
| 257 | events, _ := auditLog.GetEventsByUser("user2", 10) |
| 258 | if len(events) == 0 { |
| 259 | t.Error("expected user2 event to remain") |
| 260 | } else if events[0].UserID != "user2" { |
| 261 | t.Errorf("expected user2 to remain, got %s", events[0].UserID) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | func TestInMemoryAuditLog_GetStatus(t *testing.T) { |
| 266 | auditLog := NewInMemoryAuditLog(nil) |
nothing calls this directly
no test coverage detected