package console import ( "encoding/json" "io" "math" "net/http" "testing" "github.com/carlosframework/platform/internal/economics" "github.com/carlosframework/platform/internal/usage" ) // The report is the hop subset priced at the rate card's egress rate and // stated as absorbed; share is against egress; accounts sort by backhaul // desc, sqid asc on ties; no egress means no share, never a division. func TestBackhaulReportMaths(t *testing.T) { v := &EconomicsView{ Period: "mtd", CoveredDays: 3, PeriodDays: 10, Scenario: economics.Scenario{RateCard: economics.RateCard{EgressGBUSD: 0.09, InterRegionGBUSD: 0.02}}, Names: map[string]string{"bab": "ops"}, Meters: map[string]map[string]float64{ "bab": {usage.MeterEgressGB: 10, usage.MeterBackhaulGB: 2.5, usage.MeterBackhaulRequests1k: 7}, "bae": {usage.MeterEgressGB: 4, usage.MeterBackhaulGB: 2.5}, "bad": {usage.MeterEgressGB: 0}, }, } rep := buildBackhaulReport(v) if rep.EgressGB != 14 || rep.BackhaulGB != 5 || rep.BackhaulReq1k != 7 { t.Fatalf("totals = %+v", rep) } if math.Abs(rep.ShareOfEgress-5.0/14) > 1e-9 || math.Abs(rep.AbsorbedUSD-0.10) > 1e-9 || rep.InterRegionGBUSD != 0.02 { t.Fatalf("share/absorbed = %v/%v", rep.ShareOfEgress, rep.AbsorbedUSD) } // bad has egress but no backhaul: counted in the totals, no row. if len(rep.Accounts) != 2 || rep.Accounts[0].Account != "bab" || rep.Accounts[1].Account != "bae" { t.Fatalf("order = %+v", rep.Accounts) } if rep.Accounts[0].Name != "ops" || math.Abs(rep.Accounts[0].ShareOfEgress-0.25) > 1e-9 || math.Abs(rep.Accounts[1].ShareOfEgress-0.625) > 1e-9 || math.Abs(rep.Accounts[0].AbsorbedUSD-0.05) > 1e-9 { t.Fatalf("accounts = %+v", rep.Accounts) } } func getBearer(t *testing.T, u, bearer string) (int, []byte) { t.Helper() req, err := http.NewRequest(http.MethodGet, u, nil) if err != nil { t.Fatal(err) } if bearer != "" { req.Header.Set("Authorization", "Bearer "+bearer) } resp, err := http.DefaultClient.Do(req) if err != nil { t.Fatal(err) } defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) return resp.StatusCode, body } // Operator only, with the bill door's posture: anyone else sees the same // bare 404 an unknown route gets. func TestAPIEconomicsBackhaulGate(t *testing.T) { fx := newConsoleFixture(t) token := cliToken(t, fx, "paul@keymail.dev", "code-apibackhaul") ghostStatus, ghostBody := getBearer(t, fx.ts.URL+"/this-api-route-does-not-exist", token) // No token: the CLI door's own 401 with its login hint (cliUser), as // every /api door answers; a token without the operator bit gets the // ghost 404 — the bit itself must not be probeable. if status, _ := getBearer(t, fx.ts.URL+"/api/economics/backhaul", ""); status != http.StatusUnauthorized { t.Fatalf("unauthenticated = %d; want 401", status) } if status, body := getBearer(t, fx.ts.URL+"/api/economics/backhaul", token); status != ghostStatus || string(body) != string(ghostBody) { t.Fatalf("non-operator = %d %q; want the ghost 404", status, body) } if err := fx.st.SetOperator("paul@keymail.dev", true); err != nil { t.Fatal(err) } status, body := getBearer(t, fx.ts.URL+"/api/economics/backhaul?period=mtd", token) if status != http.StatusOK { t.Fatalf("operator = %d %q", status, body) } var rep backhaulReport if err := json.Unmarshal(body, &rep); err != nil { t.Fatalf("unreadable: %v: %s", err, body) } if rep.Period != "mtd" || rep.Accounts == nil { t.Fatalf("report = %+v", rep) } } // The whole "priced nowhere" claim in one assertion: an account prices // identically with and without the backhaul meters present. func TestBackhaulMetersNeverPriceAnAccount(t *testing.T) { sc := economics.DefaultScenario() base := map[string]float64{usage.MeterEgressGB: 40, usage.MeterRequests1k: 900, usage.MeterMemGBHours: 800, usage.MeterAppMonths: 3} with := map[string]float64{} for k, v := range base { with[k] = v } with[usage.MeterBackhaulGB] = 25 with[usage.MeterBackhaulRequests1k] = 500 a := economics.PriceAccount(sc, "bab", base, 12.5) b := economics.PriceAccount(sc, "bab", with, 12.5) if a != b { t.Fatalf("backhaul meters changed an account's price:\n%+v\n%+v", a, b) } // And with nothing else metered: the meters must not even count as // usage (FreeTierPos), the one generic meter walk PriceAccount has. none := economics.PriceAccount(sc, "bab", map[string]float64{}, 0) only := economics.PriceAccount(sc, "bab", map[string]float64{usage.MeterBackhaulGB: 25, usage.MeterBackhaulRequests1k: 500}, 0) if none != only { t.Fatalf("backhaul-only meters changed an otherwise idle account:\n%+v\n%+v", none, only) } }