2026-08-27 15:19:56 +00:00
|
|
|
package diagnostics
|
|
|
|
|
|
|
|
|
|
import (
|
2026-11-26 01:59:34 +00:00
|
|
|
"fmt"
|
2026-08-27 15:19:56 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCollectorCountsOccurrencesAndRetainsDistinctSamplesInOrder(t *testing.T) {
|
|
|
|
|
collector := NewCollector()
|
|
|
|
|
for _, diagnostic := range []contracts.ProducerDiagnostic{
|
|
|
|
|
advisory("one", "first"),
|
|
|
|
|
advisory("one", "first"),
|
|
|
|
|
advisory("two", "second"),
|
|
|
|
|
advisory("three", "third"),
|
|
|
|
|
advisory("four", "fourth"),
|
|
|
|
|
} {
|
|
|
|
|
if err := collector.Add(diagnostic); err != nil {
|
|
|
|
|
t.Fatalf("Add() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
diagnostics := collector.Diagnostics()
|
|
|
|
|
if len(diagnostics) != 1 {
|
|
|
|
|
t.Fatalf("group count = %d, want 1", len(diagnostics))
|
|
|
|
|
}
|
|
|
|
|
group := diagnostics[0]
|
|
|
|
|
if group.OccurrenceCount != 5 || group.OmittedSampleCount != 2 {
|
|
|
|
|
t.Fatalf("group counts = %#v, want five occurrences and two omitted samples", group)
|
|
|
|
|
}
|
|
|
|
|
if got := []string{group.Samples[0].Scope, group.Samples[1].Scope, group.Samples[2].Scope}; !equalStrings(got, []string{"one", "two", "three"}) {
|
|
|
|
|
t.Fatalf("sample order = %#v, want first three distinct samples", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-26 01:59:34 +00:00
|
|
|
func TestCollectorSeparatesGroupsAndRejectsInvalidGroups(t *testing.T) {
|
2026-08-27 15:19:56 +00:00
|
|
|
collector := NewCollector()
|
|
|
|
|
if err := collector.Add(advisory("one", "first")); err != nil {
|
|
|
|
|
t.Fatalf("Add() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
warning := advisory("two", "second")
|
|
|
|
|
warning.Disposition = contracts.DiagnosticDispositionWarning
|
|
|
|
|
warning.Category = contracts.DiagnosticCategoryFallback
|
|
|
|
|
if err := collector.Add(warning); err != nil {
|
|
|
|
|
t.Fatalf("Add() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if got := len(collector.Diagnostics()); got != 2 {
|
|
|
|
|
t.Fatalf("group count = %d, want 2", got)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
invalid := advisory("bad", "bad")
|
|
|
|
|
invalid.ReasonCode = ""
|
|
|
|
|
if err := collector.Add(invalid); err == nil {
|
|
|
|
|
t.Fatal("Add() error = nil, want invalid diagnostic error")
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-26 01:59:34 +00:00
|
|
|
many := NewCollector()
|
|
|
|
|
for index := 0; index < 400; index++ {
|
2026-08-27 15:19:56 +00:00
|
|
|
diagnostic := advisory("scope", "message")
|
2026-11-26 01:59:34 +00:00
|
|
|
diagnostic.ReasonCode = fmt.Sprintf("reason-%d", index)
|
|
|
|
|
if err := many.Add(diagnostic); err != nil {
|
2026-08-27 15:19:56 +00:00
|
|
|
t.Fatalf("Add(%d) error = %v", index, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-26 01:59:34 +00:00
|
|
|
if got := len(many.Diagnostics()); got != 400 {
|
|
|
|
|
t.Fatalf("group count = %d, want 400", got)
|
2026-08-27 15:19:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCollectorReturnsIndependentSnapshots(t *testing.T) {
|
|
|
|
|
collector := NewCollector()
|
|
|
|
|
if err := collector.Add(advisory("scope", "message")); err != nil {
|
|
|
|
|
t.Fatalf("Add() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
first := collector.Diagnostics()
|
|
|
|
|
first[0].Samples[0].Message = "changed"
|
|
|
|
|
second := collector.Diagnostics()
|
|
|
|
|
if second[0].Samples[0].Message != "message" {
|
|
|
|
|
t.Fatalf("collector snapshot changed = %#v", second)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-26 01:59:34 +00:00
|
|
|
func TestCollectorRejectsOccurrenceOverflow(t *testing.T) {
|
|
|
|
|
collector := NewCollector()
|
|
|
|
|
first := advisory("scope", "message")
|
|
|
|
|
first.OccurrenceCount = maximumInt()
|
|
|
|
|
first.OmittedSampleCount = maximumInt() - 1
|
|
|
|
|
if err := collector.Add(first); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := collector.Add(advisory("other", "message")); err == nil {
|
|
|
|
|
t.Fatal("Add() accepted occurrence count overflow")
|
|
|
|
|
}
|
|
|
|
|
if got := collector.Diagnostics(); len(got) != 1 || got[0].OccurrenceCount != maximumInt() {
|
|
|
|
|
t.Fatalf("collector changed after overflow: %#v", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 15:19:56 +00:00
|
|
|
func advisory(scope string, message string) contracts.ProducerDiagnostic {
|
|
|
|
|
return contracts.ProducerDiagnostic{
|
|
|
|
|
Disposition: contracts.DiagnosticDispositionAdvisory,
|
|
|
|
|
Category: contracts.DiagnosticCategoryDataQuality,
|
|
|
|
|
ReasonCode: "source_unrelated",
|
|
|
|
|
OccurrenceCount: 1,
|
|
|
|
|
Samples: []contracts.DiagnosticSample{{Scope: scope, Message: message}},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func equalStrings(left []string, right []string) bool {
|
|
|
|
|
if len(left) != len(right) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for index := range left {
|
|
|
|
|
if left[index] != right[index] {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|