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:
@@ -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,
|
||||
|
||||
257
pkg/coredata/tracker_pattern_promote_test.go
Normal file
257
pkg/coredata/tracker_pattern_promote_test.go
Normal file
@@ -0,0 +1,257 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
)
|
||||
|
||||
// trackerPatternFixture bootstraps the parent rows that a tracker
|
||||
// pattern's FKs require: organization, cookie banner, and a normal
|
||||
// cookie category.
|
||||
type trackerPatternFixture struct {
|
||||
scope *coredata.Scope
|
||||
organizationID gid.GID
|
||||
cookieBannerID gid.GID
|
||||
cookieCategoryID gid.GID
|
||||
}
|
||||
|
||||
func seedTrackerPatternFixture(t *testing.T, ctx context.Context, client *pg.Client) trackerPatternFixture {
|
||||
t.Helper()
|
||||
|
||||
tenantID := gid.NewTenantID()
|
||||
scope := coredata.NewScope(tenantID)
|
||||
organizationID := gid.New(tenantID, coredata.OrganizationEntityType)
|
||||
cookieBannerID := gid.New(tenantID, coredata.CookieBannerEntityType)
|
||||
cookieCategoryID := gid.New(tenantID, coredata.CookieCategoryEntityType)
|
||||
now := time.Now().UTC()
|
||||
|
||||
require.NoError(t, client.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
|
||||
org := &coredata.Organization{
|
||||
ID: organizationID,
|
||||
TenantID: tenantID,
|
||||
Name: "TrackerPattern Promote Test Org",
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := org.Insert(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
banner := &coredata.CookieBanner{
|
||||
ID: cookieBannerID,
|
||||
OrganizationID: organizationID,
|
||||
Name: "TrackerPattern Promote Test Banner",
|
||||
Origin: "https://promote-test.example.com",
|
||||
State: coredata.CookieBannerStateActive,
|
||||
CookiePolicyURL: "https://promote-test.example.com/cookies",
|
||||
ConsentExpiryDays: 180,
|
||||
ShowBranding: false,
|
||||
DefaultLanguage: "en",
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := banner.Insert(ctx, tx, scope); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
category := &coredata.CookieCategory{
|
||||
ID: cookieCategoryID,
|
||||
OrganizationID: organizationID,
|
||||
CookieBannerID: cookieBannerID,
|
||||
Name: "Analytics",
|
||||
Slug: "analytics",
|
||||
Description: "",
|
||||
Kind: coredata.CookieCategoryKindNormal,
|
||||
Rank: 1,
|
||||
GCMConsentTypes: []string{},
|
||||
PostHogConsent: false,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := category.Insert(ctx, tx, scope); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}))
|
||||
|
||||
t.Cleanup(func() {
|
||||
_ = client.WithTx(context.Background(), func(ctx context.Context, tx pg.Tx) error {
|
||||
if _, err := tx.Exec(ctx, `DELETE FROM tracker_patterns WHERE cookie_banner_id = $1`, cookieBannerID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(ctx, `DELETE FROM cookie_categories WHERE cookie_banner_id = $1`, cookieBannerID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(ctx, `DELETE FROM cookie_banners WHERE id = $1`, cookieBannerID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(ctx, `DELETE FROM organizations WHERE id = $1`, organizationID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
})
|
||||
|
||||
return trackerPatternFixture{
|
||||
scope: scope,
|
||||
organizationID: organizationID,
|
||||
cookieBannerID: cookieBannerID,
|
||||
cookieCategoryID: cookieCategoryID,
|
||||
}
|
||||
}
|
||||
|
||||
func seedTrackerPattern(
|
||||
t *testing.T,
|
||||
ctx context.Context,
|
||||
client *pg.Client,
|
||||
fx trackerPatternFixture,
|
||||
pattern string,
|
||||
matchType coredata.TrackerPatternMatchType,
|
||||
source coredata.CookieSource,
|
||||
) *coredata.TrackerPattern {
|
||||
t.Helper()
|
||||
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
maxAge := 3600
|
||||
tp := &coredata.TrackerPattern{
|
||||
ID: gid.New(fx.scope.GetTenantID(), coredata.TrackerPatternEntityType),
|
||||
OrganizationID: fx.organizationID,
|
||||
CookieBannerID: fx.cookieBannerID,
|
||||
CookieCategoryID: fx.cookieCategoryID,
|
||||
TrackerType: coredata.TrackerTypeCookie,
|
||||
Pattern: pattern,
|
||||
MatchType: matchType,
|
||||
DisplayName: pattern,
|
||||
Description: "",
|
||||
MaxAgeSeconds: &maxAge,
|
||||
Source: &source,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
require.NoError(t, client.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
|
||||
return tp.Insert(ctx, tx, fx.scope)
|
||||
}))
|
||||
|
||||
return tp
|
||||
}
|
||||
|
||||
func TestTrackerPattern_PromoteSource_OverwritesSource(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := newTestPgClient(t)
|
||||
ctx := context.Background()
|
||||
fx := seedTrackerPatternFixture(t, ctx, client)
|
||||
|
||||
tp := seedTrackerPattern(
|
||||
t,
|
||||
ctx,
|
||||
client,
|
||||
fx,
|
||||
"*_session",
|
||||
coredata.TrackerPatternMatchTypeGlob,
|
||||
coredata.CookieSourcePreExisting,
|
||||
)
|
||||
|
||||
bumpedAt := time.Now().UTC().Add(time.Hour).Truncate(time.Microsecond)
|
||||
|
||||
require.NoError(t, client.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
|
||||
return tp.PromoteSource(ctx, tx, fx.scope, coredata.CookieSourceScript, bumpedAt)
|
||||
}))
|
||||
|
||||
require.NotNil(t, tp.Source)
|
||||
assert.Equal(t, coredata.CookieSourceScript, *tp.Source, "receiver must reflect the new source")
|
||||
assert.True(t, tp.UpdatedAt.Equal(bumpedAt), "receiver must reflect the new updated_at")
|
||||
|
||||
loaded := &coredata.TrackerPattern{}
|
||||
require.NoError(t, client.WithConn(ctx, func(ctx context.Context, conn pg.Querier) error {
|
||||
return loaded.LoadByID(ctx, conn, fx.scope, tp.ID)
|
||||
}))
|
||||
|
||||
require.NotNil(t, loaded.Source)
|
||||
assert.Equal(t, coredata.CookieSourceScript, *loaded.Source, "DB row must reflect the promoted source")
|
||||
assert.True(t, loaded.UpdatedAt.Equal(bumpedAt), "DB row must reflect the new updated_at")
|
||||
}
|
||||
|
||||
func TestTrackerPattern_PromoteSource_OnlyTouchesSourceAndUpdatedAt(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := newTestPgClient(t)
|
||||
ctx := context.Background()
|
||||
fx := seedTrackerPatternFixture(t, ctx, client)
|
||||
|
||||
tp := seedTrackerPattern(
|
||||
t,
|
||||
ctx,
|
||||
client,
|
||||
fx,
|
||||
"*_token",
|
||||
coredata.TrackerPatternMatchTypeGlob,
|
||||
coredata.CookieSourcePreExisting,
|
||||
)
|
||||
|
||||
originalCategory := tp.CookieCategoryID
|
||||
originalDisplay := tp.DisplayName
|
||||
originalMaxAge := tp.MaxAgeSeconds
|
||||
originalExcluded := tp.Excluded
|
||||
originalDescription := tp.Description
|
||||
|
||||
bumpedAt := time.Now().UTC().Add(2 * time.Hour).Truncate(time.Microsecond)
|
||||
|
||||
require.NoError(t, client.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
|
||||
return tp.PromoteSource(ctx, tx, fx.scope, coredata.CookieSourceExtension, bumpedAt)
|
||||
}))
|
||||
|
||||
loaded := &coredata.TrackerPattern{}
|
||||
require.NoError(t, client.WithConn(ctx, func(ctx context.Context, conn pg.Querier) error {
|
||||
return loaded.LoadByID(ctx, conn, fx.scope, tp.ID)
|
||||
}))
|
||||
|
||||
assert.Equal(t, originalCategory, loaded.CookieCategoryID, "category must be untouched")
|
||||
assert.Equal(t, originalDisplay, loaded.DisplayName, "display_name must be untouched")
|
||||
assert.Equal(t, originalMaxAge, loaded.MaxAgeSeconds, "max_age_seconds must be untouched")
|
||||
assert.Equal(t, originalExcluded, loaded.Excluded, "excluded must be untouched")
|
||||
assert.Equal(t, originalDescription, loaded.Description, "description must be untouched")
|
||||
}
|
||||
|
||||
func TestTrackerPattern_PromoteSource_NotFoundForMissingRow(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := newTestPgClient(t)
|
||||
ctx := context.Background()
|
||||
fx := seedTrackerPatternFixture(t, ctx, client)
|
||||
|
||||
tp := &coredata.TrackerPattern{ID: gid.New(fx.scope.GetTenantID(), coredata.TrackerPatternEntityType)}
|
||||
|
||||
err := client.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
|
||||
return tp.PromoteSource(ctx, tx, fx.scope, coredata.CookieSourceScript, time.Now().UTC())
|
||||
})
|
||||
|
||||
assert.ErrorIs(t, err, coredata.ErrResourceNotFound)
|
||||
}
|
||||
Reference in New Issue
Block a user