Skip to content

test(agentdrain): rewrite anomaly_test.go with testify and table-driven tests#24470

Merged
pelikhan merged 2 commits intomainfrom
copilot/testify-expert-improve-test-quality
Apr 4, 2026
Merged

test(agentdrain): rewrite anomaly_test.go with testify and table-driven tests#24470
pelikhan merged 2 commits intomainfrom
copilot/testify-expert-improve-test-quality

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 4, 2026

pkg/agentdrain/anomaly_test.go used raw t.Error/t.Fatal calls and four separate single-scenario test functions, making failures hard to read and edge cases uncovered.

Changes

  • Merged TestAnomalyDetection_NewTemplate/LowSimilarity/RareCluster/Normal into a single table-driven TestAnomalyDetector_Analyze with require/assert and descriptive messages
  • Added 5 new sub-cases covering previously untested scenarios:
    • Similarity exactly at threshold (0.4 < 0.4 → false, boundary)
    • Similarity just below threshold (0.39 < 0.4 → true, boundary)
    • Combined LowSimilarity + RareCluster (score = 0.5)
    • nil cluster (guards the if cluster != nil branch explicitly)
    • Max achievable score: IsNewTemplate + RareCluster → 0.65 (with explanation that LowSimilarity is mutually exclusive with isNew=true)
  • Converted TestAnalyzeEvent to require.NoError/require.NotNil/assert.True/assert.False with descriptive messages
  • Added exact AnomalyScore assertions (assert.InDelta) instead of just > 0 checks, pinning the weighted scoring math
// Before – opaque failure message
if report.AnomalyScore <= 0 {
    t.Errorf("expected positive anomaly score for new template, got %v", report.AnomalyScore)
}

// After – precise, self-documenting
assert.InDelta(t, 0.65, report.AnomalyScore, 1e-9, "AnomalyScore mismatch")

Copilot AI changed the title [WIP] Improve test quality for pkg/agentdrain/anomaly_test.go test(agentdrain): rewrite anomaly_test.go with testify and table-driven tests Apr 4, 2026
Copilot AI requested a review from pelikhan April 4, 2026 11:55
@github-actions github-actions bot mentioned this pull request Apr 4, 2026
@pelikhan pelikhan marked this pull request as ready for review April 4, 2026 13:08
Copilot AI review requested due to automatic review settings April 4, 2026 13:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors pkg/agentdrain/anomaly_test.go to make anomaly detection tests more readable and more complete by switching to testify assertions and consolidating scenarios into table-driven subtests.

Changes:

  • Replaced multiple single-scenario anomaly tests with a single table-driven TestAnomalyDetector_Analyze.
  • Added new sub-cases covering boundary conditions, combined flags, nil cluster handling, and score ceiling behavior.
  • Updated TestAnalyzeEvent to use require/assert for clearer failure output and stronger precondition checks.
Show a summary per file
File Description
pkg/agentdrain/anomaly_test.go Rewrites anomaly-related unit tests using testify + table-driven cases and adds additional coverage for scoring/flag edge cases.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

@pelikhan pelikhan merged commit 13aec2a into main Apr 4, 2026
67 checks passed
@pelikhan pelikhan deleted the copilot/testify-expert-improve-test-quality branch April 4, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[testify-expert] Improve Test Quality: pkg/agentdrain/anomaly_test.go

3 participants