| 1 | package usage |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/release" |
| 10 | ) |
| 11 | |
| 12 | func TestKeys(t *testing.T) { |
| 13 | if got := CensusDayKey("2026-08-11"); got != "usage/census/2026-08-11.json" { |
| 14 | t.Fatalf("census key = %q", got) |
| 15 | } |
| 16 | if got := StorageDayKey("2026-08-11"); got != "usage/storage/2026-08-11.json" { |
| 17 | t.Fatalf("storage key = %q", got) |
| 18 | } |
| 19 | if got := StatementKey("bab", "2026-08"); got != "usage/accounts/bab/2026-08.json" { |
| 20 | t.Fatalf("statement key = %q", got) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestAppStorageStoreProvisionedWireShapeIsAdditive(t *testing.T) { |
| 25 | raw, err := json.Marshal(AppStorage{BucketBytes: 12, StoreProvisioned: true}) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if got, want := string(raw), `{"bucket_bytes":12,"store_provisioned":true}`; got != want { |
| 30 | t.Fatalf("provisioned app storage = %s, want %s", got, want) |
| 31 | } |
| 32 | |
| 33 | // Objects written before StoreProvisioned existed remain valid and read as |
| 34 | // unprovisioned. New objects always carry the field so a consumer inspecting |
| 35 | // the raw shape can distinguish a legacy snapshot from a known zero count. |
| 36 | var legacy AppStorage |
| 37 | if err := json.Unmarshal([]byte(`{"bucket_bytes":7,"store_bytes":3}`), &legacy); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if legacy.StoreProvisioned { |
| 41 | t.Fatal("legacy app storage read as provisioned") |
| 42 | } |
| 43 | raw, err = json.Marshal(AppStorage{BucketBytes: 12}) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if got, want := string(raw), `{"bucket_bytes":12,"store_provisioned":false}`; got != want { |
| 48 | t.Fatalf("unprovisioned app storage = %s, want %s", got, want) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestCreateDayObjectIsCreateOnly(t *testing.T) { |
| 53 | s := release.NewDirStore(t.TempDir()) |
| 54 | ctx := context.Background() |
| 55 | day := CensusDay{V: 1, Date: "2026-08-11", Accounts: map[string]CensusAccount{"bab": {Apps: 2, Routes: 3}}} |
| 56 | created, err := CreateDayObject(ctx, s, CensusDayKey(day.Date), day) |
| 57 | if err != nil || !created { |
| 58 | t.Fatalf("first create = %v %v", created, err) |
| 59 | } |
| 60 | // Second console racing the same day: no error, no overwrite. |
| 61 | loser := CensusDay{V: 1, Date: "2026-08-11", Accounts: map[string]CensusAccount{"bab": {Apps: 99}}} |
| 62 | created, err = CreateDayObject(ctx, s, CensusDayKey(day.Date), loser) |
| 63 | if err != nil || created { |
| 64 | t.Fatalf("second create = %v %v", created, err) |
| 65 | } |
| 66 | raw, _, _ := s.Get(ctx, CensusDayKey(day.Date)) |
| 67 | var got CensusDay |
| 68 | if err := json.Unmarshal(raw, &got); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | if got.Accounts["bab"].Apps != 2 { |
| 72 | t.Fatalf("winner overwritten: %+v", got) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestUpsertStatementReplaces(t *testing.T) { |
| 77 | s := release.NewDirStore(t.TempDir()) |
| 78 | ctx := context.Background() |
| 79 | st := Statement{V: 1, Account: "bab", Month: "2026-08", |
| 80 | Meters: map[string]float64{MeterEgressGB: 1.5}, CoveredDays: 10, DaysInMonth: 31, |
| 81 | UpdatedAt: time.Date(2026, 8, 11, 0, 0, 0, 0, time.UTC)} |
| 82 | if err := UpsertStatement(ctx, s, st); err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | st.Meters[MeterEgressGB] = 2.5 |
| 86 | st.CoveredDays = 11 |
| 87 | if err := UpsertStatement(ctx, s, st); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | raw, _, _ := s.Get(ctx, StatementKey("bab", "2026-08")) |
| 91 | var got Statement |
| 92 | if err := json.Unmarshal(raw, &got); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if got.Meters[MeterEgressGB] != 2.5 || got.CoveredDays != 11 { |
| 96 | t.Fatalf("statement = %+v", got) |
| 97 | } |
| 98 | } |
| 99 | |