package usage import ( "context" "encoding/json" "testing" "time" "github.com/carlosframework/platform/internal/release" ) func TestEdgeDayKeyAndLabels(t *testing.T) { if got := EdgeDayKey("box1", "2026-08-11"); got != "usage/edge/box1/days/2026-08-11.json" { t.Fatalf("EdgeDayKey = %q", got) } at := time.Date(2026, 8, 11, 9, 4, 0, 0, time.UTC) if got := DayOf(at); got != "2026-08-11" { t.Fatalf("DayOf = %q", got) } if got := MonthOf(at); got != "2026-08" { t.Fatalf("MonthOf = %q", got) } if got := HourLabel(at); got != "09" { t.Fatalf("HourLabel = %q", got) } } func TestUpsertEdgeDayMergesAcrossFlushes(t *testing.T) { s := release.NewDirStore(t.TempDir()) ctx := context.Background() at := time.Date(2026, 8, 11, 9, 10, 0, 0, time.UTC) d1 := map[string]EdgeRow{ "a.example.com": {Account: "bab", App: "hello", Requests: 10, BytesOut: 1000}, } if err := UpsertEdgeDay(ctx, s, "box1", at, d1); err != nil { t.Fatal(err) } // Second flush, same hour: counts must ADD, not replace. if err := UpsertEdgeDay(ctx, s, "box1", at.Add(5*time.Minute), d1); err != nil { t.Fatal(err) } // Third flush, next hour and a second host. d2 := map[string]EdgeRow{ "a.example.com": {Account: "bab", App: "hello", Requests: 1, BytesOut: 50}, "b.example.com": {Account: "ops", App: "site", Requests: 2, BytesOut: 20}, } if err := UpsertEdgeDay(ctx, s, "box1", at.Add(time.Hour), d2); 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 day.V != 1 || day.Box != "box1" || day.Date != "2026-08-11" { t.Fatalf("header = %+v", day) } h9 := day.Hours["09"]["a.example.com"] if h9.Requests != 20 || h9.BytesOut != 2000 || h9.Account != "bab" || h9.App != "hello" { t.Fatalf("hour 09 row = %+v", h9) } if day.Hours["10"]["b.example.com"].Requests != 2 { t.Fatalf("hour 10 = %+v", day.Hours["10"]) } } // The hop counters are an ADDITIVE shape change (the paid-for wire rule): // every day object already in a production bucket predates them, so one // written without them must still parse, fold and round-trip — and a // single-region box, which never sees a hop, must keep writing objects that // are byte-identical to what it wrote before the fields existed. func TestEdgeDayHopFieldsAdditive(t *testing.T) { // A day object as a pre-ruling binary wrote it: no hop keys at all. const old = `{"v":1,"box":"box1","date":"2026-08-24","hours":{"09":{"a.example.com":` + `{"account":"bab","app":"hello","requests":10,"bytes_out":1000}}}}` var day EdgeDay if err := json.Unmarshal([]byte(old), &day); err != nil { t.Fatalf("a day object written before the hop fields existed must still parse: %v", err) } row := day.Hours["09"]["a.example.com"] if row.Requests != 10 || row.BytesOut != 1000 || row.HopRequests != 0 || row.HopBytesOut != 0 { t.Fatalf("row = %+v, want the old counters and zero hop counters", row) } // And it folds a hop-bearing delta on top without losing the old counts. day.AddDeltas("09", map[string]EdgeRow{ "a.example.com": {Account: "bab", App: "hello", Requests: 3, BytesOut: 300, HopRequests: 2, HopBytesOut: 250}, }) row = day.Hours["09"]["a.example.com"] if row.Requests != 13 || row.BytesOut != 1300 || row.HopRequests != 2 || row.HopBytesOut != 250 { t.Fatalf("folded row = %+v", row) } // Round-trip: what a hop-annotating box writes, a hop-annotating box reads. raw, err := json.Marshal(day) if err != nil { t.Fatal(err) } var back EdgeDay if err := json.Unmarshal(raw, &back); err != nil { t.Fatal(err) } if got := back.Hours["09"]["a.example.com"]; got != row { t.Fatalf("round-tripped row = %+v, want %+v", got, row) } // omitempty, the single-region promise: a row with no hop traffic writes // no hop keys, so a one-box deployment's objects do not change shape. plain, err := json.Marshal(EdgeRow{Account: "bab", App: "hello", Requests: 1, BytesOut: 10}) if err != nil { t.Fatal(err) } if want := `{"account":"bab","app":"hello","requests":1,"bytes_out":10}`; string(plain) != want { t.Fatalf("hop-free row marshals as %s, want %s — omitempty keeps single-region objects unchanged", plain, want) } } // The fold accumulates the hop counters across flushes exactly as it does the // totals — they are counters in the same hour bucket, not a snapshot. func TestEdgeDayHourFoldSumsHopFields(t *testing.T) { var day EdgeDay delta := map[string]EdgeRow{ "a.example.com": {Account: "bab", App: "hello", Requests: 4, BytesOut: 400, HopRequests: 1, HopBytesOut: 100}, } day.AddDeltas("09", delta) day.AddDeltas("09", delta) day.AddDeltas("10", delta) if got := day.Hours["09"]["a.example.com"]; got.Requests != 8 || got.BytesOut != 800 || got.HopRequests != 2 || got.HopBytesOut != 200 { t.Fatalf("hour 09 row = %+v, want two flushes summed", got) } if got := day.Hours["10"]["a.example.com"]; got.HopRequests != 1 || got.HopBytesOut != 100 { t.Fatalf("hour 10 row = %+v, want the third flush in its own bucket", got) } // The subset invariant, stated where a future reader will meet it: hop // bytes live INSIDE bytes_out, so a consumer summing BytesOut has already // counted them. if got := day.Hours["09"]["a.example.com"]; got.HopBytesOut > got.BytesOut || got.HopRequests > got.Requests { t.Fatalf("row = %+v — the hop counters are a subset of the totals, never an addition", got) } } func TestUpsertEdgeDayEmptyDeltasIsNoop(t *testing.T) { s := release.NewDirStore(t.TempDir()) if err := UpsertEdgeDay(context.Background(), s, "box1", time.Now().UTC(), nil); err != nil { t.Fatal(err) } if keys, _ := s.List(context.Background(), "usage/"); len(keys) != 0 { t.Fatalf("noop wrote %v", keys) } }