Files
superbrain/internal/entitlements/entitlements_test.go
T
Andrew S Erwin 3b11ac83fc Notifications phase 2 (Gitea #10): share triggers, targeted people, Signal, managed-by footer
- share_received trigger: scanner fires when a bookmark, tag or collection
  is shared with the rule owner (same dedupe discipline; sharer name and
  item title in the message)
- config.targetUserIds on rules: same-tenant targets (<=20, validated at
  write time); each targeted Notification row carries recipientId and is
  delivered through the RECIPIENT's own enabled channel of the rule's kind,
  gated by the recipient's tenant plan (no channel => suppressed, visible
  in history); history lists rows you own or were targeted on
- signal channel kind: signal-cli REST bridge (POST {restUrl}/v2/send with
  account query param and recipients array); account encrypted + masked
  like every other credential; netguard-dialed like every webhook
- 'Managed by David's Superbrain.' footer is plan DATA: entitlements
  ManagedFooter (free hosted plan only; managed_footer override key),
  appended at delivery time - paying plans, core and self-hosted never
  carry it
- catalog: signal joins standard/premium/core/selfhosted channel lists
- migration 019 + schema.sql (trigger/kind CHECKs, Notification.recipientId)
- UI: trigger option, same-tenant target picker via /api/users/search,
  signal channel fields, targeted-count badge on rules
- tests: share/target/footer/signal legs against fake webhook, fake SMTP
  and a fake signal-cli bridge; entitlements footer assertions
2026-11-09 17:42:38 -06:00

143 lines
5.0 KiB
Go

package entitlements
import "testing"
func TestCatalogAndFailClosed(t *testing.T) {
// Every shipped plan resolves with sane floors.
for _, p := range []Plan{PlanFree, PlanStandard, PlanPremium, PlanCore, PlanSelfHosted} {
l := For(p)
if l.AIMode == "" || l.NotificationChannels == nil || l.Connectors == nil {
t.Fatalf("plan %s resolved incompletely: %+v", p, l)
}
if !ValidPlan(string(p)) {
t.Fatalf("ValidPlan(%s) = false", p)
}
}
if ValidPlan("enterprise") {
t.Fatal("unknown plan must not validate")
}
// Unknown plans fail closed to the free catalog, never open.
unknown := For(Plan("enterprise"))
free := For(PlanFree)
if unknown.StorageBytes != free.StorageBytes || unknown.BrainLinks != free.BrainLinks {
t.Fatalf("unknown plan must resolve to free limits: %+v", unknown)
}
// Catalog slices are copies: callers cannot mutate the shipped matrix.
a := For(PlanFree)
a.Connectors[0] = "hacked"
if For(PlanFree).Connectors[0] == "hacked" {
t.Fatal("For() leaked the catalog slice")
}
// Plan ordering sanity: premium strictly exceeds standard on the two
// paid differentiators, core/selfhosted are unlimited.
if For(PlanPremium).StorageBytes <= For(PlanStandard).StorageBytes {
t.Fatal("premium storage must exceed standard")
}
if For(PlanStandard).BrainLinks != 10 || For(PlanFree).BrainLinks != 2 {
t.Fatal("brain-link counts drifted from the plan matrix")
}
if For(PlanCore).BrainLinks != Unlimited || For(PlanSelfHosted).StorageBytes != Unlimited {
t.Fatal("core/selfhosted must be unlimited")
}
}
func TestManagedFooter(t *testing.T) {
if !For(PlanFree).ManagedFooter {
t.Fatal("free plan must carry the managed-by footer")
}
for _, p := range []Plan{PlanStandard, PlanPremium, PlanCore, PlanSelfHosted} {
if For(p).ManagedFooter {
t.Fatalf("plan %s must not carry the footer", p)
}
}
l := ApplyOverrides(For(PlanFree), map[string]string{KeyManagedFooter: "false"})
if l.ManagedFooter {
t.Fatal("managed_footer override did not apply")
}
}
func TestDefaultPlanForTier(t *testing.T) {
if DefaultPlanForTier("core") != PlanCore {
t.Fatal("core tier must derive the core plan")
}
if DefaultPlanForTier("multi") != PlanFree {
t.Fatal("multi tier must derive the free plan (billing upgrades later)")
}
if DefaultPlanForTier("") != PlanSelfHosted || DefaultPlanForTier("bogus") != PlanSelfHosted {
t.Fatal("unset/unknown tier must derive selfhosted")
}
if AutoPlan(PlanFree) || AutoPlan(PlanStandard) || AutoPlan(PlanPremium) {
t.Fatal("sold/billing-owned plans must never be auto-adjusted")
}
if !AutoPlan(PlanCore) || !AutoPlan(PlanSelfHosted) {
t.Fatal("deployment-derived plans must be adjustable")
}
}
func TestAllowListsAndChannels(t *testing.T) {
free := For(PlanFree)
if !free.AllowConnector("url") || free.AllowConnector("gmail") {
t.Fatal("free connector allowlist wrong")
}
if !free.AllowChannel("email") || free.AllowChannel("telegram") {
t.Fatal("free channel allowlist wrong")
}
premium := For(PlanPremium)
if !premium.AllowChannel("telegram") || !premium.AllowConnector("oidc") {
t.Fatal("premium allowlists wrong")
}
if !For(PlanCore).AllowConnector("mcp-core") || For(PlanPremium).AllowConnector("mcp-core") {
t.Fatal("mcp-core must be a core-plan exclusive")
}
// Matching is case/space tolerant.
if !premium.AllowChannel(" Telegram ") {
t.Fatal("channel match must normalize")
}
}
func TestApplyOverrides(t *testing.T) {
base := For(PlanFree)
got := ApplyOverrides(base, map[string]string{
KeyStorageBytes: "10737418240",
KeyBrainLinks: "-1",
KeySeats: "5",
KeyNotificationChannels: " Email , Matrix ",
KeyConnectors: "url,gmail",
KeyAIMode: "credits",
"key_from_the_future": "whatever",
})
if got.StorageBytes != 10*1024*1024*1024 || got.BrainLinks != Unlimited || got.Seats != 5 {
t.Fatalf("numeric overrides not applied: %+v", got)
}
if len(got.NotificationChannels) != 2 || got.NotificationChannels[0] != "email" || got.NotificationChannels[1] != "matrix" {
t.Fatalf("channel override not normalized: %v", got.NotificationChannels)
}
if got.AIMode != "credits" {
t.Fatal("ai_mode override not applied")
}
// Bad values are ignored — a malformed row can never widen limits.
bad := ApplyOverrides(base, map[string]string{
KeyStorageBytes: "lots",
KeyBrainLinks: "-5", // only -1 is a legal unlimited sentinel
KeyAIMode: "root",
})
if bad.StorageBytes != base.StorageBytes || bad.BrainLinks != base.BrainLinks || bad.AIMode != base.AIMode {
t.Fatalf("invalid overrides must be ignored: %+v", bad)
}
if !ValidOverrideKey(KeyStorageBytes) || ValidOverrideKey("nope") {
t.Fatal("ValidOverrideKey out of sync with the key constants")
}
}
func TestWithinQuota(t *testing.T) {
if !WithinQuota(0, 1, Unlimited) || !WithinQuota(1<<40, 1<<40, Unlimited) {
t.Fatal("unlimited must always pass")
}
if !WithinQuota(4, 1, 5) || WithinQuota(5, 1, 5) {
t.Fatal("boundary: used+delta <= limit")
}
if !WithinQuota(-3, 1, 1) {
t.Fatal("negative used is treated as 0")
}
}