Promote glob source and trigger draft on adoption

The pattern-analysis worker dropped two signals on every run. When
InsertIfNotExists hit a pre-existing glob, the computed bestSource
was discarded by the LoadByBannerIDTypeAndPattern fallback, so the
SCRIPT > EXTENSION > PRE_EXISTING precedence advertised on
bestSource was only ever enforced at first insert. Subsequent
batches with stronger sources could not promote the glob, even
though the page-script-wins rule already lives in detected_trackers
at the row level.

Separately, adoptUncategorisedPatterns returned an adopted bool
that the worker discarded; the function moves detected trackers
from uncategorised exact patterns into categorised globs, which is
a real consent transition, but no draft banner version was created
on adoption-only runs.

Add a focused TrackerPattern.PromoteSource that only updates the
source and updated_at columns. Express the precedence as a pure-Go
shouldPromoteSource helper alongside bestSource so the rule is
unit-testable without a database. The worker now calls
InsertIfNotExists, then on conflict loads, skips when the slot is
held by an exact pattern or a user-recategorised glob, and only
calls PromoteSource when the candidate source ranks above the
existing one.

The skip branch is now documented: adoptUncategorisedPatterns is
the safety net that re-homes uncategorised exacts into the existing
glob via globMatch. Capture its adopted return value and use it
(instead of the previous over-eager consentChanged flag) to gate
ensureDraftVersionForBanner. Merging exacts into a glob in their
own category never changes visitor consent, so the prior flag
produced redundant draft versions on every non-uncategorised merge.

Cover the new pieces with three test layers: pure-unit cases for
shouldPromoteSource (precedence matrix including HTTP/nil collapse
and equal-rank no-write), DB-backed tests for PromoteSource (touch
only source + updated_at, ErrResourceNotFound for missing rows),
and end-to-end worker tests for source promotion on an existing
glob, draft-on-adoption, and the merge-only no-draft case.

Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-05-26 14:50:39 +02:00
parent bd5c527548
commit 05cbab7258
5 changed files with 957 additions and 28 deletions

View File

@@ -1051,3 +1051,103 @@ func TestDurationBucket(t *testing.T) {
)
}
}
func TestShouldPromoteSource(t *testing.T) {
t.Parallel()
script := coredata.CookieSourceScript
extension := coredata.CookieSourceExtension
preExisting := coredata.CookieSourcePreExisting
http := coredata.CookieSourceHTTP
tests := []struct {
name string
existing *coredata.CookieSource
candidate *coredata.CookieSource
want bool
}{
{
name: "nil existing promotes to SCRIPT",
existing: nil,
candidate: &script,
want: true,
},
{
name: "nil existing promotes to EXTENSION",
existing: nil,
candidate: &extension,
want: true,
},
{
name: "nil existing does not promote to PRE_EXISTING (equal rank)",
existing: nil,
candidate: &preExisting,
want: false,
},
{
name: "PRE_EXISTING promotes to SCRIPT",
existing: &preExisting,
candidate: &script,
want: true,
},
{
name: "PRE_EXISTING promotes to EXTENSION",
existing: &preExisting,
candidate: &extension,
want: true,
},
{
name: "EXTENSION promotes to SCRIPT",
existing: &extension,
candidate: &script,
want: true,
},
{
name: "SCRIPT does not promote to PRE_EXISTING",
existing: &script,
candidate: &preExisting,
want: false,
},
{
name: "SCRIPT does not promote to EXTENSION",
existing: &script,
candidate: &extension,
want: false,
},
{
name: "EXTENSION does not promote to PRE_EXISTING",
existing: &extension,
candidate: &preExisting,
want: false,
},
{
name: "SCRIPT does not promote to SCRIPT (equal rank, no write)",
existing: &script,
candidate: &script,
want: false,
},
{
name: "HTTP collapses to PRE_EXISTING rank: does not promote SCRIPT",
existing: &script,
candidate: &http,
want: false,
},
{
name: "HTTP collapses to PRE_EXISTING rank: equal to nil existing",
existing: nil,
candidate: &http,
want: false,
},
}
for _, tt := range tests {
t.Run(
tt.name,
func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, shouldPromoteSource(tt.existing, tt.candidate))
},
)
}
}