refactor(discovery): validator accepts *Bucket, skips bucket checks when nil

This commit is contained in:
2026-04-18 14:43:07 +02:00
parent 310673940e
commit 20176dd51f
3 changed files with 23 additions and 12 deletions

View File

@@ -224,7 +224,7 @@ func (s *Service) processBucketResponse(ctx context.Context, b Bucket, resp Pass
// Semantic validation — catches agent self-contradictions that the
// schema alone cannot. Errors drop the market; warnings would be
// appended to hinweis (none defined yet at Pass 0 scope).
issues := ValidateForInsert(dm, b)
issues := ValidateForInsert(dm, &b)
if HasErrors(issues) {
slog.InfoContext(ctx, "validation failed; skipping market",
"markt", m.MarktName, "stadt", m.Stadt, "issues", formatIssues(issues))

View File

@@ -36,15 +36,15 @@ type Issue struct {
// from, before it hits the queue. Current checks:
//
// - bundesland_mismatch: m.Bundesland does not equal b.Region (CH "Kanton X"
// prefix is normalized).
// prefix is normalized). Skipped when b is nil.
// - status_hinweis_inconsistent: AgentStatus=="bestaetigt" AND hinweis
// mentions "vorjahr" — the agent contradicted itself.
//
// Returns nil when clean.
func ValidateForInsert(m DiscoveredMarket, b Bucket) []Issue {
func ValidateForInsert(m DiscoveredMarket, b *Bucket) []Issue {
var issues []Issue
if m.Bundesland != "" && !regionsEqual(m.Bundesland, b.Region) {
if b != nil && m.Bundesland != "" && !regionsEqual(m.Bundesland, b.Region) {
issues = append(issues, Issue{
Severity: SeverityError,
Code: "bundesland_mismatch",

View File

@@ -8,27 +8,27 @@ func TestValidateForInsert(t *testing.T) {
tests := []struct {
name string
m DiscoveredMarket
b Bucket
b *Bucket
wantCodes []string
wantErrors bool
}{
{
name: "clean",
m: DiscoveredMarket{Bundesland: "Bayern", AgentStatus: "bestaetigt"},
b: baseBucket,
b: &baseBucket,
wantCodes: nil,
},
{
name: "bundesland mismatch",
m: DiscoveredMarket{Bundesland: "Baden-Württemberg", AgentStatus: "bestaetigt"},
b: baseBucket,
b: &baseBucket,
wantCodes: []string{"bundesland_mismatch"},
wantErrors: true,
},
{
name: "bundesland empty is not an error",
m: DiscoveredMarket{Bundesland: "", AgentStatus: "bestaetigt"},
b: baseBucket,
b: &baseBucket,
wantCodes: nil,
},
{
@@ -38,7 +38,7 @@ func TestValidateForInsert(t *testing.T) {
AgentStatus: "bestaetigt",
Hinweis: "Termin aus Vorjahr, noch nicht bestaetigt",
},
b: baseBucket,
b: &baseBucket,
wantCodes: []string{"status_hinweis_inconsistent"},
wantErrors: true,
},
@@ -49,13 +49,13 @@ func TestValidateForInsert(t *testing.T) {
AgentStatus: "vorjahr_unbestaetigt",
Hinweis: "Aus dem Vorjahr uebernommen",
},
b: baseBucket,
b: &baseBucket,
wantCodes: nil,
},
{
name: "kanton prefix is normalized when bucket is just the kanton name",
m: DiscoveredMarket{Bundesland: "Kanton Zürich", AgentStatus: "bestaetigt"},
b: Bucket{Region: "Zürich"},
b: &Bucket{Region: "Zürich"},
wantCodes: nil,
},
{
@@ -65,10 +65,21 @@ func TestValidateForInsert(t *testing.T) {
AgentStatus: "bestaetigt",
Hinweis: "Termin aus dem Vorjahr uebernommen",
},
b: baseBucket,
b: &baseBucket,
wantCodes: []string{"bundesland_mismatch", "status_hinweis_inconsistent"},
wantErrors: true,
},
{
name: "nil bucket skips bundesland_mismatch check",
m: DiscoveredMarket{
MarktName: "Testmarkt",
Stadt: "Testdorf",
Bundesland: "Bayern",
AgentStatus: "bestaetigt",
},
b: nil,
wantCodes: nil,
},
}
for _, tc := range tests {