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

@@ -491,6 +491,52 @@ ON CONFLICT (cookie_banner_id, tracker_type, pattern, COALESCE(max_age_seconds,
return result.RowsAffected() > 0, nil
}
// PromoteSource overwrites the row's source column with newSource and
// refreshes updated_at. The caller is responsible for ranking
// newSource against the existing value before invoking this method —
// see shouldPromoteSource in pkg/cookiebanner. Returns ErrResourceNotFound
// if no row with the receiver's ID exists in scope.
func (tp *TrackerPattern) PromoteSource(
ctx context.Context,
tx pg.Tx,
scope Scoper,
newSource CookieSource,
updatedAt time.Time,
) error {
q := `
UPDATE tracker_patterns
SET
source = @source,
updated_at = @updated_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": tp.ID,
"source": newSource,
"updated_at": updatedAt,
}
maps.Copy(args, scope.SQLArguments())
result, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot promote tracker pattern source: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
tp.Source = &newSource
tp.UpdatedAt = updatedAt
return nil
}
func (tp *TrackerPattern) Update(
ctx context.Context,
tx pg.Tx,