diff --git a/pkg/cookiebanner/worker.go b/pkg/cookiebanner/worker.go index ed1e2981a..00538afc7 100644 --- a/pkg/cookiebanner/worker.go +++ b/pkg/cookiebanner/worker.go @@ -85,12 +85,18 @@ func (h *patternAnalysisHandler) Process(ctx context.Context, banner coredata.Co func(ctx context.Context, tx pg.Tx) error { scope := coredata.NewScopeFromObjectID(banner.ID) - var patterns coredata.CookiePatterns - if err := patterns.LoadAllByCookieBannerID(ctx, tx, scope, banner.ID, nil); err != nil { - return fmt.Errorf("cannot load patterns: %w", err) + var exactPatterns coredata.CookiePatterns + if err := exactPatterns.LoadAllByCookieBannerID( + ctx, + tx, + scope, + banner.ID, + coredata.NewCookiePatternFilter(new(coredata.CookiePatternMatchTypeExact), nil, new(false)), + ); err != nil { + return fmt.Errorf("cannot load exact patterns: %w", err) } - mergeGroups := findMergeGroups(patterns, patternMergeThreshold) + mergeGroups := findMergeGroups(exactPatterns, patternMergeThreshold) merged := false for key, group := range mergeGroups { @@ -176,15 +182,8 @@ func findMergeGroups( patterns coredata.CookiePatterns, threshold int, ) map[mergeGroupKey][]*coredata.CookiePattern { - var exact []*coredata.CookiePattern - for _, p := range patterns { - if p.MatchType == coredata.CookiePatternMatchTypeExact { - exact = append(exact, p) - } - } - prefixCounts := make(map[mergeGroupKey][]*coredata.CookiePattern) - for _, p := range exact { + for _, p := range patterns { for _, pfx := range separatorPrefixes(p.Pattern) { key := mergeGroupKey{categoryID: p.CookieCategoryID, prefix: pfx} prefixCounts[key] = append(prefixCounts[key], p) @@ -297,14 +296,13 @@ func (h *patternAnalysisHandler) adoptUncategorisedPatterns( return false, fmt.Errorf("cannot load uncategorised category: %w", err) } - prefixMatchType := coredata.CookiePatternMatchTypePrefix var prefixPatterns coredata.CookiePatterns if err := prefixPatterns.LoadAllByCookieBannerID( ctx, tx, scope, banner.ID, - coredata.NewCookiePatternFilter(&prefixMatchType, nil, nil), + coredata.NewCookiePatternFilter(new(coredata.CookiePatternMatchTypePrefix), nil, new(false)), ); err != nil { return false, fmt.Errorf("cannot load prefix patterns: %w", err) } @@ -324,7 +322,7 @@ func (h *patternAnalysisHandler) adoptUncategorisedPatterns( tx, scope, banner.ID, - coredata.NewCookiePatternFilter(&exactMatchType, &uncategorised.ID, nil), + coredata.NewCookiePatternFilter(&exactMatchType, &uncategorised.ID, new(false)), ); err != nil { return false, fmt.Errorf("cannot load uncategorised exact patterns: %w", err) } diff --git a/pkg/cookiebanner/worker_test.go b/pkg/cookiebanner/worker_test.go index 22a1fc214..f6c4b3b7c 100644 --- a/pkg/cookiebanner/worker_test.go +++ b/pkg/cookiebanner/worker_test.go @@ -218,29 +218,6 @@ func TestFindMergeGroups(t *testing.T) { }, ) - t.Run( - "skips non-exact patterns", - func(t *testing.T) { - t.Parallel() - - patterns := coredata.CookiePatterns{ - makePattern("ph_phc_abc123"), - makePattern("ph_phc_def456"), - makePattern("ph_phc_ghi789"), - { - Pattern: "ph_phc_", - MatchType: coredata.CookiePatternMatchTypePrefix, - }, - } - - groups := findMergeGroups(patterns, 3) - require.Len(t, groups, 1) - - group := groups[mergeGroupKey{categoryID: gid.Nil, prefix: "ph_phc_"}] - assert.Len(t, group, 3) - }, - ) - t.Run( "leftover patterns form group under shorter prefix", func(t *testing.T) {