TestLiveUsageTracker_SystemClassNotMetered is the load-bearing assertion for the metering gate: a system-class account (synthetic probe traffic) writes ZERO usage_events and ZERO usage_summaries, so it never accrues quota. A standard account in the same test writes both (regression guard that the ga
(t *testing.T)
| 99 | // standard account in the same test writes both (regression guard that the gate |
| 100 | // only short-circuits non-standard classes). |
| 101 | func TestLiveUsageTracker_SystemClassNotMetered(t *testing.T) { |
| 102 | pool := testutil.TestDB(t) |
| 103 | store := usage.NewStore(pool) |
| 104 | idStore := identity.NewStore(pool) |
| 105 | tracker := usage.NewUsageTracker(store) |
| 106 | ctx := context.Background() |
| 107 | |
| 108 | countEvents := func(userID string) int { |
| 109 | var n int |
| 110 | if err := pool.QueryRow(ctx, |
| 111 | `SELECT COUNT(*) FROM usage_events WHERE user_id = $1`, userID).Scan(&n); err != nil { |
| 112 | t.Fatalf("count usage_events: %v", err) |
| 113 | } |
| 114 | return n |
| 115 | } |
| 116 | |
| 117 | // system-class account: gate short-circuits, no rows written. |
| 118 | sysUser, err := idStore.CreateOrGetUser(ctx, "probe-system@example.com", "Probe System", "google-probe-system") |
| 119 | if err != nil { |
| 120 | t.Fatalf("CreateOrGetUser system: %v", err) |
| 121 | } |
| 122 | if _, err := pool.Exec(ctx, `UPDATE users SET account_class = 'system' WHERE id = $1`, sysUser.ID); err != nil { |
| 123 | t.Fatalf("set system class: %v", err) |
| 124 | } |
| 125 | |
| 126 | for _, dir := range []string{"inbound", "outbound"} { |
| 127 | allowed, err := tracker.RecordAndCheck(ctx, sysUser.ID, "agent-sys", "probe.example.com", dir) |
| 128 | if err != nil || !allowed { |
| 129 | t.Errorf("system %s RecordAndCheck: allowed=%v err=%v", dir, allowed, err) |
| 130 | } |
| 131 | } |
| 132 | if n := countEvents(sysUser.ID); n != 0 { |
| 133 | t.Errorf("system account wrote %d usage_events, want 0", n) |
| 134 | } |
| 135 | if _, err := store.GetUsageSummary(ctx, sysUser.ID, usage.CurrentDate()); !errors.Is(err, pgx.ErrNoRows) { |
| 136 | t.Errorf("system account usage_summaries lookup err = %v, want ErrNoRows (no summary written)", err) |
| 137 | } |
| 138 | |
| 139 | // standard-class account (regression): gate does NOT short-circuit. |
| 140 | stdUser, err := idStore.CreateOrGetUser(ctx, "probe-standard@example.com", "Probe Standard", "google-probe-standard") |
| 141 | if err != nil { |
| 142 | t.Fatalf("CreateOrGetUser standard: %v", err) |
| 143 | } |
| 144 | for _, dir := range []string{"inbound", "outbound"} { |
| 145 | if _, err := tracker.RecordAndCheck(ctx, stdUser.ID, "agent-std", "real.example.com", dir); err != nil { |
| 146 | t.Fatalf("standard %s RecordAndCheck: %v", dir, err) |
| 147 | } |
| 148 | } |
| 149 | if n := countEvents(stdUser.ID); n != 2 { |
| 150 | t.Errorf("standard account wrote %d usage_events, want 2", n) |
| 151 | } |
| 152 | sum, err := store.GetUsageSummary(ctx, stdUser.ID, usage.CurrentDate()) |
| 153 | if err != nil { |
| 154 | t.Fatalf("standard GetUsageSummary: %v", err) |
| 155 | } |
| 156 | if sum.TotalCount != 2 { |
| 157 | t.Errorf("standard account summary TotalCount = %d, want 2", sum.TotalCount) |
| 158 | } |
nothing calls this directly
no test coverage detected