| new file mode 100644 |
| index 000000000..866201d7f |
| --- /dev/null |
| +++ b/internal/console/protection_test.go |
| @@ -0,0 +1,222 @@ |
| +package console |
| + |
| +import ( |
| + "reflect" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/usage" |
| +) |
| + |
| +const ( |
| + prHost = "shop.example.com" |
| + prDay = "2026-11-05" |
| +) |
| + |
| +var prNow = time.Date(2026, 11, 5, 13, 0, 0, 0, time.UTC) |
| + |
| +// prSrc is a root box's day with the given tallies, keyed as the edge keys them. |
| +func prSrc(box, date string, tallies ...usage.GuardTally) edgeDaySource { |
| + g := map[string]usage.GuardTally{} |
| + for _, t := range tallies { |
| + g[usage.GuardKey(t.Host, t.Account, t.App)] = t |
| + } |
| + return edgeDaySource{Day: usage.EdgeDay{V: 1, Box: box, Date: date, Guard: g}} |
| +} |
| + |
| +func prTally(acct, app string, hours map[string]usage.GuardHour, groups map[string]int64) usage.GuardTally { |
| + return usage.GuardTally{Host: prHost, Account: acct, App: app, Hours: hours, Groups: groups} |
| +} |
| + |
| +func TestProtectionDatesWholeDays(t *testing.T) { |
| + got := protectionDates(7, prNow) |
| + if len(got) != 7 || got[0] != "2026-10-30" || got[6] != "2026-11-05" { |
| + t.Fatalf("7 days = %v", got) |
| + } |
| + if odd := protectionDates(9, prNow); !reflect.DeepEqual(odd, got) { |
| + t.Fatalf("9 days = %v, want the 7-day window", odd) |
| + } |
| + if thirty := protectionDates(30, prNow); len(thirty) != 30 || thirty[0] != "2026-10-07" || thirty[29] != prDay { |
| + t.Fatalf("30 days = %v", thirty) |
| + } |
| + // A zone offset does not move the window: it is UTC dates. |
| + late := time.Date(2026, 11, 5, 23, 30, 0, 0, time.FixedZone("west", -5*3600)) |
| + if got := protectionDates(7, late); got[6] != "2026-11-06" { |
| + t.Fatalf("last date = %s, want 2026-11-06", got[6]) |
| + } |
| +} |
| + |
| +func TestSumProtectionTotals(t *testing.T) { |
| + srcs := []edgeDaySource{ |
| + prSrc("a", prDay, prTally("bab", "shop", map[string]usage.GuardHour{"09": {Checked: 100, Watch: 6}}, map[string]int64{"probe/wordpress": 5})), |
| + prSrc("b", prDay, prTally("bab", "shop", map[string]usage.GuardHour{"10": {Checked: 50, Watch: 2, Block: 1}}, map[string]int64{"hard/secrets": 4})), |
| + } |
| + got := sumProtection(srcs, protectionDates(7, prNow), "bab", "shop") |
| + if got.Checked != 150 || got.Watch != 8 || got.Block != 1 { |
| + t.Fatalf("totals = %d/%d/%d, want 150/8/1", got.Checked, got.Watch, got.Block) |
| + } |
| + if want := map[string]int64{"probe/wordpress": 5, "hard/secrets": 4}; !reflect.DeepEqual(got.Groups, want) { |
| + t.Fatalf("groups = %v, want %v", got.Groups, want) |
| + } |
| + if got.From != "2026-10-30" || got.To != prDay || len(got.Days) != 7 { |
| + t.Fatalf("window = %s..%s, %d days", got.From, got.To, len(got.Days)) |
| + } |
| + if d := got.Days[6]; d.Date != prDay || d.Checked != 150 || d.Watch != 8 || d.Block != 1 { |
| + t.Fatalf("last day = %+v", d) |
| + } |
| + for _, d := range got.Days[:6] { |
| + if d.Checked != 0 || d.Watch != 0 || d.Block != 0 { |
| + t.Fatalf("day %s = %+v, want zeros", d.Date, d) |
| + } |
| + } |
| +} |
| + |
| +func TestSumProtectionIgnoresDaysOutsideWindow(t *testing.T) { |
| + srcs := []edgeDaySource{ |
| + prSrc("a", "2026-10-29", prTally("bab", "shop", map[string]usage.GuardHour{"09": {Checked: 9, Block: 9}}, nil)), |
| + prSrc("a", prDay, prTally("bab", "shop", map[string]usage.GuardHour{"09": {Checked: 3}}, nil)), |
| + } |
| + if got := sumProtection(srcs, protectionDates(7, prNow), "bab", "shop"); got.Checked != 3 || got.Block != 0 { |
| + t.Fatalf("totals = %d/%d, want 3/0", got.Checked, got.Block) |
| + } |
| +} |
| + |
| +func TestSumProtectionNothingCaught(t *testing.T) { |
| + srcs := []edgeDaySource{prSrc("a", prDay, prTally("bab", "shop", map[string]usage.GuardHour{"09": {Checked: 40}, "10": {Checked: 2}}, nil))} |
| + got := sumProtection(srcs, protectionDates(7, prNow), "bab", "shop") |
| + if got.Checked != 42 || got.Watch != 0 || got.Block != 0 { |
| + t.Fatalf("totals = %d/%d/%d, want 42/0/0", got.Checked, got.Watch, got.Block) |
| + } |
| + if got.Groups == nil || len(got.Groups) != 0 { |
| + t.Fatalf("groups = %#v, want the empty map", got.Groups) |
| + } |
| +} |
| + |
| +func TestSumProtectionNoTallies(t *testing.T) { |
| + src := edgeDaySource{Day: usage.EdgeDay{V: 1, Box: "a", Date: prDay, Hours: map[string]map[string]usage.EdgeRow{ |
| + "09": {prHost: {Account: "bab", App: "shop", Requests: 500}}, |
| + }}} |
| + got := sumProtection([]edgeDaySource{src}, protectionDates(7, prNow), "bab", "shop") |
| + if got.Checked != 0 || got.Watch != 0 || got.Block != 0 { |
| + t.Fatalf("totals = %d/%d/%d, want zeros", got.Checked, got.Watch, got.Block) |
| + } |
| + if len(got.Days) != 7 { |
| + t.Fatalf("days = %d, want 7", len(got.Days)) |
| + } |
| + for _, d := range got.Days { |
| + if d.Checked != 0 || d.Watch != 0 || d.Block != 0 { |
| + t.Fatalf("day %s = %+v, want zeros", d.Date, d) |
| + } |
| + } |
| + if got.Groups == nil || len(got.Groups) != 0 { |
| + t.Fatalf("groups = %#v, want the empty map", got.Groups) |
| + } |
| +} |
| + |
| +func TestSumProtectionOwnerChange(t *testing.T) { |
| + // The host changed hands in hour 12: both owners' tallies hold that hour. |
| + srcs := []edgeDaySource{prSrc("a", prDay, |
| + prTally("bab", "shop", map[string]usage.GuardHour{"12": {Checked: 10, Watch: 3, Block: 1}}, map[string]int64{"probe/wordpress": 4}), |
| + prTally("bdk", "shop", map[string]usage.GuardHour{"12": {Checked: 20, Watch: 7, Block: 2}}, map[string]int64{"hard/secrets": 9}), |
| + )} |
| + dates := protectionDates(7, prNow) |
| + for _, c := range []struct { |
| + acct string |
| + checked, watch, block int64 |
| + groups map[string]int64 |
| + }{ |
| + {"bab", 10, 3, 1, map[string]int64{"probe/wordpress": 4}}, |
| + {"bdk", 20, 7, 2, map[string]int64{"hard/secrets": 9}}, |
| + } { |
| + got := sumProtection(srcs, dates, c.acct, "shop") |
| + if got.Checked != c.checked || got.Watch != c.watch || got.Block != c.block { |
| + t.Fatalf("%s totals = %d/%d/%d", c.acct, got.Checked, got.Watch, got.Block) |
| + } |
| + if !reflect.DeepEqual(got.Groups, c.groups) { |
| + t.Fatalf("%s groups = %v, want %v", c.acct, got.Groups, c.groups) |
| + } |
| + var groupTotal int64 |
| + for _, n := range got.Groups { |
| + groupTotal += n |
| + } |
| + if caught := got.Watch + got.Block; caught != groupTotal { |
| + t.Fatalf("%s caught %d, group total %d", c.acct, caught, groupTotal) |
| + } |
| + } |
| +} |
| + |
| +func TestSumProtectionFleetVouchesOnlyForOwner(t *testing.T) { |
| + tally := prTally("bab", "shop", map[string]usage.GuardHour{"09": {Checked: 5, Block: 1}}, map[string]int64{"scanner": 1}) |
| + dates := protectionDates(7, prNow) |
| + for _, c := range []struct { |
| + name string |
| + owner string |
| + want int64 |
| + }{{"owner", "bab", 5}, {"other owner", "bdk", 0}, {"ownerless", "", 0}} { |
| + src := prSrc("a", prDay, tally) |
| + src.Fleet, src.FleetAccount = "f1", c.owner |
| + if got := sumProtection([]edgeDaySource{src}, dates, "bab", "shop"); got.Checked != c.want { |
| + t.Fatalf("%s: checked = %d, want %d", c.name, got.Checked, c.want) |
| + } |
| + all, rows := sumProtectionAll([]edgeDaySource{src}, dates) |
| + if all.Checked != c.want || (c.want == 0) != (len(rows) == 0) { |
| + t.Fatalf("%s: all checked = %d, %d rows", c.name, all.Checked, len(rows)) |
| + } |
| + } |
| +} |
| + |
| +func TestSumProtectionAllSortsApps(t *testing.T) { |
| + mk := func(acct, app string, watch, block int64) usage.GuardTally { |
| + return usage.GuardTally{Host: app + ".example.com", Account: acct, App: app, |
| + Hours: map[string]usage.GuardHour{"09": {Checked: 100, Watch: watch, Block: block}}, Groups: map[string]int64{"scanner": watch + block}} |
| + } |
| + srcs := []edgeDaySource{ |
| + prSrc("a", prDay, mk("bab", "quiet", 0, 0), mk("bab", "mid", 3, 2), mk("bdk", "loud", 8, 4)), |
| + prSrc("b", prDay, mk("bab", "mid", 0, 0)), |
| + } |
| + sum, rows := sumProtectionAll(srcs, protectionDates(7, prNow)) |
| + var got []string |
| + for _, r := range rows { |
| + got = append(got, r.Account+"/"+r.App) |
| + } |
| + if want := []string{"bdk/loud", "bab/mid", "bab/quiet"}; !reflect.DeepEqual(got, want) { |
| + t.Fatalf("order = %v, want %v", got, want) |
| + } |
| + if rows[0].Watch != 8 || rows[0].Block != 4 || rows[0].Checked != 100 { |
| + t.Fatalf("top row = %+v", rows[0]) |
| + } |
| + if rows[1].Checked != 200 { |
| + t.Fatalf("mid row sums both boxes: %+v", rows[1]) |
| + } |
| + if sum.Checked != 400 || sum.Watch != 11 || sum.Block != 6 { |
| + t.Fatalf("summary = %d/%d/%d, want 400/11/6", sum.Checked, sum.Watch, sum.Block) |
| + } |
| + if sum.Groups["scanner"] != 17 || len(sum.Days) != 7 || sum.Days[6].Checked != 400 { |
| + t.Fatalf("summary groups/days = %v / %+v", sum.Groups, sum.Days) |
| + } |
| +} |
| + |
| +func TestSumProtectionAllTiesBreakByAccountThenApp(t *testing.T) { |
| + mk := func(acct, app string) usage.GuardTally { |
| + return usage.GuardTally{Host: app + ".example.com", Account: acct, App: app, |
| + Hours: map[string]usage.GuardHour{"09": {Watch: 2}}} |
| + } |
| + srcs := []edgeDaySource{prSrc("a", prDay, mk("bdk", "aaa"), mk("bab", "zzz"), mk("bab", "mmm"))} |
| + _, rows := sumProtectionAll(srcs, protectionDates(7, prNow)) |
| + var got []string |
| + for _, r := range rows { |
| + got = append(got, r.Account+"/"+r.App) |
| + } |
| + if want := []string{"bab/mmm", "bab/zzz", "bdk/aaa"}; !reflect.DeepEqual(got, want) { |
| + t.Fatalf("order = %v, want %v", got, want) |
| + } |
| +} |
| + |
| +func TestSumProtectionAllEmptyAccountIsOps(t *testing.T) { |
| + // A tally with no account is the ops account's, as a route's is. |
| + srcs := []edgeDaySource{prSrc("a", prDay, prTally("", "shop", map[string]usage.GuardHour{"09": {Checked: 4}}, nil))} |
| + sum, rows := sumProtectionAll(srcs, protectionDates(7, prNow)) |
| + if sum.Checked != 4 || len(rows) != 1 || rows[0].App != "shop" { |
| + t.Fatalf("summary %d, rows %+v", sum.Checked, rows) |
| + } |
| +} |