| index 25bb16042..0aa9b743e 100644 |
| --- a/internal/usage/edge_test.go |
| +++ b/internal/usage/edge_test.go |
| @@ -316,3 +316,80 @@ func TestEdgeDayOldShapeParses(t *testing.T) { |
| t.Fatalf("json = %s, want no guard key", out) |
| } |
| } |
| + |
| +// The storage merge, not just the in-memory fold: an existing day object |
| +// (rows plus a guard tally one name short of the group cap) is read back, |
| +// merged with a flush that has a delta with no groups and one carrying an |
| +// incoming "other" count, and written; the stored day is what's asserted. |
| +func TestUpsertEdgeDayMergesGuardIntoStoredDay(t *testing.T) { |
| + const host = "shop.example.com" |
| + s := release.NewDirStore(t.TempDir()) |
| + ctx := context.Background() |
| + at := time.Date(2026, 8, 11, 9, 10, 0, 0, time.UTC) |
| + key := GuardKey(host, "bab", "shop") |
| + quietKey := GuardKey("quiet.example.com", "bdk", "web") |
| + |
| + // Seed: 30 named groups, so exactly one more name still fits. |
| + seed := map[string]int64{} |
| + for i := 0; i < GuardGroupCap-2; i++ { |
| + seed[fmt.Sprintf("g%02d", i)] = 1 |
| + } |
| + first := EdgeFlush{ |
| + Rows: map[string]EdgeRow{host: {Account: "bab", App: "shop", Requests: 7, BytesOut: 70}}, |
| + Guard: map[string]GuardDelta{key: guardDelta(host, "bab", "shop", 7, 1, 0, seed)}, |
| + } |
| + if err := UpsertEdgeDay(ctx, s, "box1", at, first); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + // Second flush, next hour: a new name (fits), another new name (overflows |
| + // to other), an incoming "other" count, and a second tally with no groups. |
| + second := EdgeFlush{ |
| + Guard: map[string]GuardDelta{ |
| + key: guardDelta(host, "bab", "shop", 5, 3, 2, map[string]int64{ |
| + "new-a": 4, "new-b": 6, "g00": 2, GuardGroupOther: 5, |
| + }), |
| + quietKey: guardDelta("quiet.example.com", "bdk", "web", 9, 0, 0, nil), |
| + }, |
| + } |
| + if err := UpsertEdgeDay(ctx, s, "box1", at.Add(time.Hour), second); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + raw, _, err := s.Get(ctx, EdgeDayKey("box1", "2026-08-11")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var day EdgeDay |
| + if err := json.Unmarshal(raw, &day); err != nil { |
| + t.Fatal(err) |
| + } |
| + if row := day.Hours["09"][host]; row.Requests != 7 || row.BytesOut != 70 { |
| + t.Fatalf("seeded row = %+v, want it kept", row) |
| + } |
| + g := day.Guard[key] |
| + if g.Host != host || g.Account != "bab" || g.App != "shop" { |
| + t.Fatalf("identity = %+v", g) |
| + } |
| + if got, want := g.Hours["09"], (GuardHour{Checked: 7, Watch: 1}); got != want { |
| + t.Fatalf("hour 09 = %+v, want %+v", got, want) |
| + } |
| + if got, want := g.Hours["10"], (GuardHour{Checked: 5, Watch: 3, Block: 2}); got != want { |
| + t.Fatalf("hour 10 = %+v, want %+v", got, want) |
| + } |
| + // 30 seeded + new-a fill the 31 named slots; new-b (6) and the incoming |
| + // other (5) land in other; g00 sums. |
| + if len(g.Groups) != GuardGroupCap { |
| + t.Fatalf("groups = %d names, want %d: %v", len(g.Groups), GuardGroupCap, g.Groups) |
| + } |
| + if g.Groups["new-a"] != 4 || g.Groups["new-b"] != 0 || g.Groups[GuardGroupOther] != 11 || g.Groups["g00"] != 3 { |
| + t.Fatalf("groups = %v", g.Groups) |
| + } |
| + q := day.Guard[quietKey] |
| + if q.Hours["10"] != (GuardHour{Checked: 9}) || len(q.Groups) != 0 { |
| + t.Fatalf("quiet tally = %+v, want checked 9 and no groups", q) |
| + } |
| + if strings.Contains(string(raw), `"groups":null`) || strings.Contains(string(raw), `"groups":{}`) { |
| + t.Fatalf("stored day has an empty groups key: %s", raw) |
| + } |
| +} |