1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
import { describe, it, expect } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { quotaChargeForCall } from "../lib/ai-cost-tracker";
const USER = "11111111-1111-1111-1111-111111111111";
describe("quotaChargeForCall", () => {
it("charges the owner for input plus output tokens", () => {
expect(
quotaChargeForCall({ ownerUserId: USER, inputTokens: 900, outputTokens: 100 })
).toEqual({ userId: USER, tokens: 1000 });
});
it("charges nobody when the call has no owner", () => {
expect(
quotaChargeForCall({ ownerUserId: null, inputTokens: 500, outputTokens: 500 })
).toBeNull();
expect(
quotaChargeForCall({ ownerUserId: "", inputTokens: 500, outputTokens: 500 })
).toBeNull();
expect(
quotaChargeForCall({ inputTokens: 500, outputTokens: 500 })
).toBeNull();
});
it("skips calls that consumed nothing", () => {
expect(
quotaChargeForCall({ ownerUserId: USER, inputTokens: 0, outputTokens: 0 })
).toBeNull();
});
it("never charges a negative or fractional amount", () => {
expect(
quotaChargeForCall({ ownerUserId: USER, inputTokens: -5000, outputTokens: 10 })
).toEqual({ userId: USER, tokens: 10 });
expect(
quotaChargeForCall({ ownerUserId: USER, inputTokens: -50, outputTokens: -50 })
).toBeNull();
expect(
quotaChargeForCall({ ownerUserId: USER, inputTokens: 1.9, outputTokens: 0.9 })
).toEqual({ userId: USER, tokens: 1 });
});
it("tolerates missing token fields", () => {
expect(
quotaChargeForCall({
ownerUserId: USER,
inputTokens: undefined as unknown as number,
outputTokens: 7,
})
).toEqual({ userId: USER, tokens: 7 });
});
});
function source(...rel: string[]): string {
const raw = readFileSync(join(import.meta.dir, "..", ...rel), "utf8");
const stripped = raw
.split("\n")
.filter((l) => !l.trim().startsWith("//") && !l.trim().startsWith("*"))
.join("\n");
expect(stripped.length).toBeGreaterThan(0);
return stripped;
}
function slice(src: string, startAnchor: string, endAnchor: string): string {
const start = src.indexOf(startAnchor);
expect(start).toBeGreaterThan(-1);
const end = src.indexOf(endAnchor, start + startAnchor.length);
expect(end).toBeGreaterThan(start);
const body = src.slice(start, end);
expect(body.length).toBeGreaterThan(0);
return body;
}
describe("recordAiCost meters the quota", () => {
const src = source("lib", "ai-cost-tracker.ts");
it("awaits the meter — referencing it is not calling it", () => {
const body = slice(
src,
"export async function recordAiCost",
"export async function summarize"
);
expect(body).toContain("await meterAiQuota(args)");
});
it("meters before the ledger insert, so a ledger failure cannot skip it", () => {
const body = slice(
src,
"export async function recordAiCost",
"export async function summarize"
);
expect(body.indexOf("await meterAiQuota(args)")).toBeLessThan(
body.indexOf("insert(aiCostEvents)")
);
});
it("bumps the column the enforcement path actually reads", () => {
expect(src).toContain("bumpUsage");
expect(src).toContain("aiTokensUsedThisMonth");
});
it("invalidates the cached verdict after bumping", () => {
const meter = slice(src, "async function meterAiQuota", "export interface");
expect(meter).toContain("invalidateQuotaCache");
expect(meter.indexOf("bumpUsage")).toBeLessThan(
meter.indexOf("invalidateQuotaCache")
);
});
});
describe("the quota gate rolls the monthly cycle", () => {
const src = source("lib", "billing.ts");
it("resets an expired cycle before reading usage", () => {
const body = slice(
src,
"export async function checkAiQuotaCached",
"export function invalidateQuotaCache"
);
expect(body).toContain("resetIfCycleExpired");
expect(body.indexOf("resetIfCycleExpired")).toBeLessThan(
body.indexOf("getUserQuota(userId)")
);
});
it("still fails open on billing errors", () => {
const body = slice(
src,
"export async function checkAiQuotaCached",
"export function invalidateQuotaCache"
);
expect(body).toContain("allowed: true");
});
});
describe("AI entry points still assert the quota", () => {
for (const file of ["ai-review.ts", "ai-review-trio.ts", "ai-ci-healer.ts"]) {
it(`${file} calls assertAiQuota`, () => {
expect(source("lib", file)).toContain("assertAiQuota(");
});
}
});
|