(t *testing.T)
| 1355 | } |
| 1356 | |
| 1357 | func TestDeleteOldAIBridgeRecords(t *testing.T) { |
| 1358 | t.Parallel() |
| 1359 | |
| 1360 | now := time.Date(2025, 1, 15, 7, 30, 0, 0, time.UTC) |
| 1361 | retentionPeriod := 30 * 24 * time.Hour // 30 days |
| 1362 | afterThreshold := now.Add(-retentionPeriod).Add(-24 * time.Hour) // 31 days ago (older than threshold) |
| 1363 | beforeThreshold := now.Add(-15 * 24 * time.Hour) // 15 days ago (newer than threshold) |
| 1364 | closeBeforeThreshold := now.Add(-retentionPeriod).Add(24 * time.Hour) // 29 days ago |
| 1365 | |
| 1366 | type testFixtures struct { |
| 1367 | oldInterception database.AIBridgeInterception |
| 1368 | oldInterceptionWithRelated database.AIBridgeInterception |
| 1369 | recentInterception database.AIBridgeInterception |
| 1370 | nearThresholdInterception database.AIBridgeInterception |
| 1371 | } |
| 1372 | |
| 1373 | testCases := []struct { |
| 1374 | name string |
| 1375 | retention time.Duration |
| 1376 | verify func(t *testing.T, ctx context.Context, db database.Store, fixtures testFixtures) |
| 1377 | }{ |
| 1378 | { |
| 1379 | name: "RetentionEnabled", |
| 1380 | retention: retentionPeriod, |
| 1381 | verify: func(t *testing.T, ctx context.Context, db database.Store, fixtures testFixtures) { |
| 1382 | t.Helper() |
| 1383 | |
| 1384 | interceptions, err := db.GetAIBridgeInterceptions(ctx) |
| 1385 | require.NoError(t, err) |
| 1386 | require.Len(t, interceptions, 2, "expected 2 interceptions remaining") |
| 1387 | |
| 1388 | interceptionIDs := make([]uuid.UUID, len(interceptions)) |
| 1389 | for i, interception := range interceptions { |
| 1390 | interceptionIDs[i] = interception.ID |
| 1391 | } |
| 1392 | |
| 1393 | require.NotContains(t, interceptionIDs, fixtures.oldInterception.ID, "old interception should be deleted") |
| 1394 | require.NotContains(t, interceptionIDs, fixtures.oldInterceptionWithRelated.ID, "old interception with related records should be deleted") |
| 1395 | require.Contains(t, interceptionIDs, fixtures.recentInterception.ID, "recent interception should be kept") |
| 1396 | require.Contains(t, interceptionIDs, fixtures.nearThresholdInterception.ID, "near threshold interception should be kept") |
| 1397 | |
| 1398 | // Verify related records were deleted for old interception. |
| 1399 | oldTokenUsages, err := db.GetAIBridgeTokenUsagesByInterceptionID(ctx, fixtures.oldInterceptionWithRelated.ID) |
| 1400 | require.NoError(t, err) |
| 1401 | require.Empty(t, oldTokenUsages, "old token usages should be deleted") |
| 1402 | |
| 1403 | oldUserPrompts, err := db.GetAIBridgeUserPromptsByInterceptionID(ctx, fixtures.oldInterceptionWithRelated.ID) |
| 1404 | require.NoError(t, err) |
| 1405 | require.Empty(t, oldUserPrompts, "old user prompts should be deleted") |
| 1406 | |
| 1407 | oldToolUsages, err := db.GetAIBridgeToolUsagesByInterceptionID(ctx, fixtures.oldInterceptionWithRelated.ID) |
| 1408 | require.NoError(t, err) |
| 1409 | require.Empty(t, oldToolUsages, "old tool usages should be deleted") |
| 1410 | |
| 1411 | // Verify related records were NOT deleted for near-threshold interception. |
| 1412 | newTokenUsages, err := db.GetAIBridgeTokenUsagesByInterceptionID(ctx, fixtures.nearThresholdInterception.ID) |
| 1413 | require.NoError(t, err) |
| 1414 | require.Len(t, newTokenUsages, 1, "near threshold token usages should not be deleted") |
nothing calls this directly
no test coverage detected