Classify remaining D&D producer diagnostics

This commit is contained in:
2026-08-27 16:04:32 +00:00
parent 6a1fd7bdb6
commit 480680b257
22 changed files with 202 additions and 182 deletions

View File

@@ -44,9 +44,9 @@ func TestNormalizeAcceptedFixtures(t *testing.T) {
t.Fatalf("normalized value = %#v, want %#v", result.Value, fixture.Output)
}
gotReasonCodes := make([]string, 0, len(result.Warnings))
for _, warning := range result.Warnings {
gotReasonCodes = append(gotReasonCodes, warning.ReasonCode)
gotReasonCodes := make([]string, 0, len(result.Diagnostics))
for _, diagnostic := range result.Diagnostics {
gotReasonCodes = append(gotReasonCodes, diagnostic.ReasonCode)
}
if !reflect.DeepEqual(gotReasonCodes, fixture.WarningReasonCodes) {
t.Fatalf("warning reason codes = %#v, want %#v", gotReasonCodes, fixture.WarningReasonCodes)

View File

@@ -27,7 +27,6 @@ const (
ReasonCodeSpellNameUnresolved = "spell_name_unresolved"
ReasonCodeSourceReferencesNormalized = "source_references_normalized"
ReasonCodeDuplicateSpellCastCollapsed = "duplicate_spell_cast_collapsed"
ReasonCodeWarningsOmitted = "spell_normalization_warnings_omitted"
)
var requiredCapabilities = []string{"merged"}
@@ -104,10 +103,11 @@ func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalize
value, warnings := normalizeSpellList(req.MergeOutput.Value, n.effectiveCatalog, order)
value, duplicateWarnings := collapseDuplicateSpellCasts(value, index, n.effectiveCatalog)
warnings = append(warnings, duplicateWarnings...)
return contracts.TypedNormalizeResult[dnd.SpellList]{
Value: value,
Warnings: diagnostics.LimitWarnings(warnings, "spell_casts", ReasonCodeWarningsOmitted),
}, nil
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(warnings, ReasonCodeSpellNameUnresolved)
if err != nil {
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("collect diagnostics: %w", err)
}
return contracts.TypedNormalizeResult[dnd.SpellList]{Value: value, Diagnostics: diagnosticGroups}, nil
}
func normalizeSpellList(input dnd.SpellList, catalog spellcatalog.EffectiveCatalog, order shared.SourceRefOrder) (dnd.SpellList, []contracts.Warning) {

View File

@@ -149,17 +149,10 @@ func TestNormalizeCanonicalizesNamesAndReportsUnresolvedNames(t *testing.T) {
t.Fatalf("spell[%d] = %q, want %q", index, result.Value.SpellCasts[index].Spell, want)
}
}
if len(result.Warnings) != 4 {
t.Fatalf("warnings = %#v, want four name warnings", result.Warnings)
}
if result.Warnings[0].ReasonCode != ReasonCodeSpellNameCanonicalized || result.Warnings[0].Scope != "spell_casts[0]" || !strings.Contains(result.Warnings[0].Message, "input index 0") {
t.Fatalf("first warning = %#v, want canonicalization warning", result.Warnings[0])
}
if result.Warnings[1].ReasonCode != ReasonCodeSpellNameCanonicalized || result.Warnings[2].ReasonCode != ReasonCodeSpellNameCanonicalized {
t.Fatalf("catalog spelling warnings = %#v", result.Warnings[1:3])
}
if result.Warnings[3].ReasonCode != ReasonCodeSpellNameUnresolved || result.Warnings[3].Scope != "spell_casts[3]" || strings.Contains(result.Warnings[3].Message, "Mystery\nSpell") || !strings.Contains(result.Warnings[3].Message, `Mystery\nSpell\tName`) {
t.Fatalf("unresolved warning = %#v, want quoted control characters", result.Warnings[3])
canonicalized := diagnosticByReason(result.Diagnostics, ReasonCodeSpellNameCanonicalized)
unresolved := diagnosticByReason(result.Diagnostics, ReasonCodeSpellNameUnresolved)
if len(result.Warnings) != 0 || canonicalized == nil || canonicalized.Disposition != contracts.DiagnosticDispositionObservation || canonicalized.OccurrenceCount != 3 || unresolved == nil || unresolved.Disposition != contracts.DiagnosticDispositionAdvisory || unresolved.OccurrenceCount != 1 || unresolved.Samples[0].Scope != "spell_casts[3]" || strings.Contains(unresolved.Samples[0].Message, "Mystery\nSpell") || !strings.Contains(unresolved.Samples[0].Message, `Mystery\nSpell\tName`) {
t.Fatalf("diagnostics = %#v, want grouped canonicalization and unresolved-quality diagnostics", result.Diagnostics)
}
}
@@ -175,15 +168,8 @@ func TestNormalizeLimitsWarningsWithoutChangingSpellValues(t *testing.T) {
if !reflect.DeepEqual(result.Value, input) {
t.Fatalf("normalized value = %#v, want unresolved spell values preserved", result.Value)
}
if len(result.Warnings) != diagnostics.MaxWarnings {
t.Fatalf("warning count = %d, want %d", len(result.Warnings), diagnostics.MaxWarnings)
}
if first := result.Warnings[0]; first.Scope != "spell_casts[0]" || first.ReasonCode != ReasonCodeSpellNameUnresolved {
t.Fatalf("first warning = %#v, want first input warning", first)
}
summary := result.Warnings[len(result.Warnings)-1]
if summary.Scope != "spell_casts" || summary.ReasonCode != ReasonCodeWarningsOmitted || summary.Message != "2 additional warning(s) omitted" {
t.Fatalf("warning summary = %#v", summary)
if len(result.Warnings) != 0 || len(result.Diagnostics) != 1 || result.Diagnostics[0].ReasonCode != ReasonCodeSpellNameUnresolved || result.Diagnostics[0].Disposition != contracts.DiagnosticDispositionAdvisory || result.Diagnostics[0].OccurrenceCount != len(input.SpellCasts) || len(result.Diagnostics[0].Samples) != contracts.MaxDiagnosticSamples {
t.Fatalf("diagnostics = %#v", result.Diagnostics)
}
}
@@ -198,11 +184,9 @@ func TestNormalizeBoundsUnicodeNamesAndQuotesCanonicalReplacement(t *testing.T)
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
if len(result.Warnings) != 1 || result.Warnings[0].ReasonCode != ReasonCodeSpellNameCanonicalized {
t.Fatalf("warnings = %#v, want one canonicalization warning", result.Warnings)
}
if !utf8.ValidString(result.Warnings[0].Message) || !strings.Contains(result.Warnings[0].Message, "…") || strings.Contains(result.Warnings[0].Message, longName) {
t.Fatalf("warning = %q, want valid bounded Unicode diagnostic", result.Warnings[0].Message)
diagnostic := diagnosticByReason(result.Diagnostics, ReasonCodeSpellNameCanonicalized)
if diagnostic == nil || !utf8.ValidString(diagnostic.Samples[0].Message) || !strings.Contains(diagnostic.Samples[0].Message, "…") || strings.Contains(diagnostic.Samples[0].Message, longName) {
t.Fatalf("diagnostic = %#v, want valid bounded Unicode diagnostic", diagnostic)
}
if got := result.Value.SpellCasts[0].Spell; got != longName {
t.Fatalf("canonical value = %q, want full catalog name", got)
@@ -234,8 +218,9 @@ func TestNormalizeSortsAndDeduplicatesExactSourceReferences(t *testing.T) {
if !reflect.DeepEqual(result.Value.SpellCasts[0].SourceRefs, wantRefs) {
t.Fatalf("source refs = %#v, want %#v", result.Value.SpellCasts[0].SourceRefs, wantRefs)
}
if len(result.Warnings) != 1 || result.Warnings[0].ReasonCode != ReasonCodeSourceReferencesNormalized || !strings.Contains(result.Warnings[0].Message, "original count 6") || !strings.Contains(result.Warnings[0].Message, "final count 5") || !strings.Contains(result.Warnings[0].Message, "order changed true") || !strings.Contains(result.Warnings[0].Message, "duplicates removed 1") {
t.Fatalf("warnings = %#v, want source normalization warning", result.Warnings)
diagnostic := diagnosticByReason(result.Diagnostics, ReasonCodeSourceReferencesNormalized)
if diagnostic == nil || !strings.Contains(diagnostic.Samples[0].Message, "original count 6") || !strings.Contains(diagnostic.Samples[0].Message, "final count 5") || !strings.Contains(diagnostic.Samples[0].Message, "order changed true") || !strings.Contains(diagnostic.Samples[0].Message, "duplicates removed 1") {
t.Fatalf("diagnostics = %#v, want source normalization observation", result.Diagnostics)
}
}
@@ -251,10 +236,11 @@ func TestNormalizeReportsDuplicateRemovalWithoutOrderChange(t *testing.T) {
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
if len(result.Warnings) != 1 || result.Warnings[0].ReasonCode != ReasonCodeSourceReferencesNormalized {
t.Fatalf("warnings = %#v, want one source normalization warning", result.Warnings)
diagnostic := diagnosticByReason(result.Diagnostics, ReasonCodeSourceReferencesNormalized)
if diagnostic == nil {
t.Fatalf("diagnostics = %#v, want one source normalization observation", result.Diagnostics)
}
message := result.Warnings[0].Message
message := diagnostic.Samples[0].Message
if !strings.Contains(message, "order changed false") || !strings.Contains(message, "duplicates removed 1") {
t.Fatalf("warning = %q, want duplicate-only repair without order change", message)
}
@@ -361,26 +347,11 @@ func TestNormalizeCollapsesDuplicateGroupsAfterCanonicalization(t *testing.T) {
t.Fatalf("Unicode caster output = %q, want first occurrence text unchanged", got)
}
if len(result.Warnings) != 6 {
t.Fatalf("warnings = %#v, want per-cast warnings followed by two group warnings", result.Warnings)
}
if result.Warnings[0].ReasonCode != ReasonCodeSpellNameCanonicalized || result.Warnings[0].Scope != "spell_casts[0]" {
t.Fatalf("warning[0] = %#v, want input name warning", result.Warnings[0])
}
if result.Warnings[1].ReasonCode != ReasonCodeSourceReferencesNormalized || result.Warnings[1].Scope != "spell_casts[0]" {
t.Fatalf("warning[1] = %#v, want input source warning", result.Warnings[1])
}
if result.Warnings[2].ReasonCode != ReasonCodeSpellNameCanonicalized || result.Warnings[2].Scope != "spell_casts[3]" {
t.Fatalf("warning[2] = %#v, want removed occurrence warning", result.Warnings[2])
}
if result.Warnings[3].ReasonCode != ReasonCodeSpellNameCanonicalized || result.Warnings[3].Scope != "spell_casts[4]" {
t.Fatalf("warning[3] = %#v, want removed occurrence warning", result.Warnings[3])
}
if result.Warnings[4].ReasonCode != ReasonCodeDuplicateSpellCastCollapsed || result.Warnings[4].Scope != "spell_casts[0]" || !strings.Contains(result.Warnings[4].Message, "retained input index 0") || !strings.Contains(result.Warnings[4].Message, "removed input indices [3]") {
t.Fatalf("warning[4] = %#v, want first duplicate group warning", result.Warnings[4])
}
if result.Warnings[5].ReasonCode != ReasonCodeDuplicateSpellCastCollapsed || result.Warnings[5].Scope != "spell_casts[2]" || !strings.Contains(result.Warnings[5].Message, "removed input indices [4, 5]") {
t.Fatalf("warning[5] = %#v, want second duplicate group warning", result.Warnings[5])
canonicalized := diagnosticByReason(result.Diagnostics, ReasonCodeSpellNameCanonicalized)
refsNormalized := diagnosticByReason(result.Diagnostics, ReasonCodeSourceReferencesNormalized)
collapsed := diagnosticByReason(result.Diagnostics, ReasonCodeDuplicateSpellCastCollapsed)
if len(result.Warnings) != 0 || canonicalized == nil || canonicalized.OccurrenceCount != 3 || refsNormalized == nil || refsNormalized.OccurrenceCount != 1 || collapsed == nil || collapsed.OccurrenceCount != 2 || !strings.Contains(collapsed.Samples[0].Message, "retained input index 0") {
t.Fatalf("diagnostics = %#v, want grouped normalization observations", result.Diagnostics)
}
}
@@ -451,10 +422,11 @@ func TestNormalizeBoundsDuplicateWarningIndices(t *testing.T) {
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
if len(result.Value.SpellCasts) != 1 || len(result.Warnings) != 1 || result.Warnings[0].ReasonCode != ReasonCodeDuplicateSpellCastCollapsed {
t.Fatalf("result = %#v, warnings = %#v, want one retained cast and one bounded warning", result.Value, result.Warnings)
diagnostic := diagnosticByReason(result.Diagnostics, ReasonCodeDuplicateSpellCastCollapsed)
if len(result.Value.SpellCasts) != 1 || diagnostic == nil || diagnostic.OccurrenceCount != 1 {
t.Fatalf("result = %#v, diagnostics = %#v, want one retained cast and one grouped observation", result.Value, result.Diagnostics)
}
message := result.Warnings[0].Message
message := diagnostic.Samples[0].Message
if !strings.Contains(message, "removed input indices [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]") || strings.Contains(message, ", 21]") || !strings.Contains(message, "1 additional removed input indices omitted") {
t.Fatalf("warning message = %q, want 20 displayed indices and exact omitted count", message)
}
@@ -533,6 +505,15 @@ func sourceDocument(unitCount int) *source.SourceDocument {
return &source.SourceDocument{ID: "source", Units: units}
}
func diagnosticByReason(diagnostics []contracts.ProducerDiagnostic, reason string) *contracts.ProducerDiagnostic {
for index := range diagnostics {
if diagnostics[index].ReasonCode == reason {
return &diagnostics[index]
}
}
return nil
}
func overlayReference() contracts.ReferenceSet {
return spellCatalogReference(`{"schema_version":"notarius.dnd.spell-catalog-overlay.v1","catalogs":[{"id":"campaign.example","ruleset":"dnd-5e-2014","source":{"title":"Private campaign source","version":"1","url":"file:///private-source.json","license":"private"},"spells":[{"name":"Aegis of Emberfall","aliases":["Emberfall Aegis"]}]}]}`)
}