Skip to content

Strengthen anomaly detector tests for score normalization and threshold-boundary behavior - #46636

Merged
pelikhan merged 2 commits into
mainfrom
copilot/testify-expert-improve-test-quality-yet-again
Jul 19, 2026
Merged

Strengthen anomaly detector tests for score normalization and threshold-boundary behavior#46636
pelikhan merged 2 commits into
mainfrom
copilot/testify-expert-improve-test-quality-yet-again

Conversation

Copilot AI commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

This updates pkg/agentdrain/anomaly_test.go to close high-value coverage gaps called out in the issue: score normalization bounds through Analyze, constructor threshold behavior verification through runtime paths, and clearer intent around stateful event analysis tests.

  • Score normalization guard via production path

    • Added TestAnalyze_ScoreAlwaysNormalized to assert AnomalyScore stays within [0,1] for both isNew=true and isNew=false when anomaly signals are maximized.
  • Threshold boundaries validated through Analyze (not just constructor storage)

    • Extended TestNewAnomalyDetector_ThresholdBoundaries success cases to verify:
      • similarity == threshold is not flagged low,
      • cluster.Size == rareThreshold is flagged rare,
      • values just below similarity threshold behave as expected.
  • TestBuildReason parallelization + explicit pointer guard

    • Added top-level t.Parallel().
    • Added require.NotNil before calling buildReason in subtests to make pointer assumptions explicit.
  • Stateful test intent clarified

    • Tightened TestAnalyzeEvent commentary to explicitly mark it as intentionally sequential/stateful integration-style behavior.
func TestAnalyze_ScoreAlwaysNormalized(t *testing.T) {
	t.Parallel()
	d, err := NewAnomalyDetector(1.0, 100)
	require.NoError(t, err)

	result := &MatchResult{ClusterID: 1, Similarity: 0.0}
	cluster := &Cluster{ID: 1, Size: 1}
	for _, isNew := range []bool{true, false} {
		report := d.Analyze(result, isNew, cluster)
		require.NotNil(t, report)
		assert.LessOrEqual(t, report.AnomalyScore, 1.0)
		assert.GreaterOrEqual(t, report.AnomalyScore, 0.0)
	}
}

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve test quality in pkg/agentdrain/anomaly_test.go Strengthen anomaly detector tests for score normalization and threshold-boundary behavior Jul 19, 2026
Copilot AI requested a review from pelikhan July 19, 2026 19:05
@pelikhan
pelikhan marked this pull request as ready for review July 19, 2026 20:14
Copilot AI review requested due to automatic review settings July 19, 2026 20:14
@pelikhan
pelikhan merged commit 35709a9 into main Jul 19, 2026
@pelikhan
pelikhan deleted the copilot/testify-expert-improve-test-quality-yet-again branch July 19, 2026 20:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Strengthens anomaly detector tests around threshold boundaries, normalized scores, parallelization, and stateful test intent.

Changes:

  • Verifies constructor thresholds through Analyze.
  • Adds score-bound checks for maximal anomaly signals.
  • Clarifies and improves test execution assumptions.
Show a summary per file
File Description
pkg/agentdrain/anomaly_test.go Expands anomaly detector boundary and normalization coverage.

Review details

Tip

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

  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Medium

@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.82.14

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