Files
member-console/internal/entitlements/migration18_test.go
T
cgalo5758 3727ff31d8 Add entitlement set rule change ledger and preview flow
Add an append-only ledger of entitlement set rule changes with per-pool
effect rows, a preview-and-commit rule change flow, and an automatic
drain that settles deferred recomputations. Rules gain a tier reduction
policy, resource keys declare over-limit behavior, and the materializer
now lowers limits when a rule stops applying.
Add entitlement set rule change ledger and preview flow

Add an append-only ledger of entitlement set rule changes with a
preview-and-commit operator flow. Rule writes now go through an enclosed
`core.commit_rule_change` function that files an act row and one
obligation per carrying pool, with a drain workflow settling deferred
recomputations. The preview dry-runs the materializer with a rule
overlay and renders per-pool buckets, reduction-policy disclosures, and
provider over-limit consequences. Materializing transactions take a
shared advisory rendezvous that rule changes hold exclusively, enforced
by a possession assertion. Add History and Entitlement changes surfaces,
a rule-less warning on five product-selection surfaces, and a
`tier_reduction_policy` column that gates FedWiki parking.
2026-09-15 03:53:28 -05:00

61 lines
2.2 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
package entitlements_test
import (
"context"
"testing"
"git.coopcloud.tech/wiki-cafe/member-console/internal/db"
"github.com/pressly/goose/v3"
)
// Scenario: a fedwiki_sites rule that existed before migration 18 carries
// force_reduce after it, so the FedWiki quota sweep keeps parking across the
// deploy that introduces the policy column.
func TestMigration18SetsExistingFedwikiRulesToForceReduce(t *testing.T) {
database := testDB(t)
ctx := context.Background()
core := db.BaseSources()[0]
if err := goose.SetDialect("postgres"); err != nil {
t.Fatal(err)
}
goose.SetBaseFS(core.Migrations)
goose.SetTableName("goose_db_version_core")
t.Cleanup(func() {
goose.SetBaseFS(nil)
goose.SetTableName("goose_db_version")
})
if err := goose.DownTo(database, core.Dir, 17); err != nil {
t.Fatalf("roll core back to 17: %v", err)
}
var setID, ruleID string
if err := database.QueryRowContext(ctx,
`INSERT INTO core.entitlement_sets (name, is_active) VALUES ('migration 18 seed', TRUE) RETURNING set_id`).Scan(&setID); err != nil {
t.Fatalf("seed set at 17: %v", err)
}
if err := database.QueryRowContext(ctx,
`INSERT INTO core.entitlement_set_rules (set_id, rule_type, resource_key, resource_value, resource_per_unit, stacking_policy)
VALUES ($1, 'limit', 'fedwiki_sites', 5, FALSE, 'additive') RETURNING rule_id`, setID).Scan(&ruleID); err != nil {
t.Fatalf("seed rule at 17: %v", err)
}
t.Cleanup(func() {
_, _ = database.ExecContext(ctx, `DELETE FROM core.entitlement_set_rules WHERE rule_id = $1`, ruleID)
_, _ = database.ExecContext(ctx, `DELETE FROM core.entitlement_sets WHERE set_id = $1`, setID)
})
if err := goose.UpTo(database, core.Dir, 18); err != nil {
t.Fatalf("migrate core to 18: %v", err)
}
var policy string
if err := database.QueryRowContext(ctx,
`SELECT tier_reduction_policy FROM core.entitlement_set_rules WHERE rule_id = $1`, ruleID).Scan(&policy); err != nil {
t.Fatalf("read policy after 18: %v", err)
}
if policy != "force_reduce" {
t.Errorf("expected the pre-existing fedwiki_sites rule at force_reduce after migration 18, got %q", policy)
}
}