mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-18 03:30:39 +09:00
feat(costs): add billing, quota, and budget control plane
This commit is contained in:
parent
656b4659fc
commit
76e6cc08a6
91 changed files with 22406 additions and 769 deletions
|
|
@ -1,14 +1,9 @@
|
|||
import express from "express";
|
||||
import request from "supertest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { costRoutes } from "../routes/costs.js";
|
||||
import { errorHandler } from "../middleware/index.js";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// parseDateRange — tested via the route handler since it's a private function
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Minimal db stub — just enough for costService() not to throw at construction
|
||||
function makeDb(overrides: Record<string, unknown> = {}) {
|
||||
const selectChain = {
|
||||
from: vi.fn().mockReturnThis(),
|
||||
|
|
@ -17,9 +12,10 @@ function makeDb(overrides: Record<string, unknown> = {}) {
|
|||
innerJoin: vi.fn().mockReturnThis(),
|
||||
groupBy: vi.fn().mockReturnThis(),
|
||||
orderBy: vi.fn().mockReturnThis(),
|
||||
limit: vi.fn().mockReturnThis(),
|
||||
then: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
// Make it thenable so Drizzle query chains resolve to []
|
||||
|
||||
const thenableChain = Object.assign(Promise.resolve([]), selectChain);
|
||||
|
||||
return {
|
||||
|
|
@ -43,17 +39,40 @@ const mockAgentService = vi.hoisted(() => ({
|
|||
}));
|
||||
const mockLogActivity = vi.hoisted(() => vi.fn());
|
||||
const mockFetchAllQuotaWindows = vi.hoisted(() => vi.fn());
|
||||
const mockCostService = vi.hoisted(() => ({
|
||||
createEvent: vi.fn(),
|
||||
summary: vi.fn().mockResolvedValue({ spendCents: 0 }),
|
||||
byAgent: vi.fn().mockResolvedValue([]),
|
||||
byAgentModel: vi.fn().mockResolvedValue([]),
|
||||
byProvider: vi.fn().mockResolvedValue([]),
|
||||
byBiller: vi.fn().mockResolvedValue([]),
|
||||
windowSpend: vi.fn().mockResolvedValue([]),
|
||||
byProject: vi.fn().mockResolvedValue([]),
|
||||
}));
|
||||
const mockFinanceService = vi.hoisted(() => ({
|
||||
createEvent: vi.fn(),
|
||||
summary: vi.fn().mockResolvedValue({ debitCents: 0, creditCents: 0, netCents: 0, estimatedDebitCents: 0, eventCount: 0 }),
|
||||
byBiller: vi.fn().mockResolvedValue([]),
|
||||
byKind: vi.fn().mockResolvedValue([]),
|
||||
list: vi.fn().mockResolvedValue([]),
|
||||
}));
|
||||
const mockBudgetService = vi.hoisted(() => ({
|
||||
overview: vi.fn().mockResolvedValue({
|
||||
companyId: "company-1",
|
||||
policies: [],
|
||||
activeIncidents: [],
|
||||
pausedAgentCount: 0,
|
||||
pausedProjectCount: 0,
|
||||
pendingApprovalCount: 0,
|
||||
}),
|
||||
upsertPolicy: vi.fn(),
|
||||
resolveIncident: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../services/index.js", () => ({
|
||||
costService: () => ({
|
||||
createEvent: vi.fn(),
|
||||
summary: vi.fn().mockResolvedValue({ spendCents: 0 }),
|
||||
byAgent: vi.fn().mockResolvedValue([]),
|
||||
byAgentModel: vi.fn().mockResolvedValue([]),
|
||||
byProvider: vi.fn().mockResolvedValue([]),
|
||||
windowSpend: vi.fn().mockResolvedValue([]),
|
||||
byProject: vi.fn().mockResolvedValue([]),
|
||||
}),
|
||||
budgetService: () => mockBudgetService,
|
||||
costService: () => mockCostService,
|
||||
financeService: () => mockFinanceService,
|
||||
companyService: () => mockCompanyService,
|
||||
agentService: () => mockAgentService,
|
||||
logActivity: mockLogActivity,
|
||||
|
|
@ -75,8 +94,12 @@ function createApp() {
|
|||
return app;
|
||||
}
|
||||
|
||||
describe("parseDateRange — date validation via route", () => {
|
||||
it("accepts valid ISO date strings and passes them to the service", async () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("cost routes", () => {
|
||||
it("accepts valid ISO date strings and passes them to cost summary routes", async () => {
|
||||
const app = createApp();
|
||||
const res = await request(app)
|
||||
.get("/api/companies/company-1/costs/summary")
|
||||
|
|
@ -102,138 +125,30 @@ describe("parseDateRange — date validation via route", () => {
|
|||
expect(res.body.error).toMatch(/invalid 'to' date/i);
|
||||
});
|
||||
|
||||
it("treats missing 'from' and 'to' as no range (passes undefined to service)", async () => {
|
||||
it("returns finance summary rows for valid requests", async () => {
|
||||
const app = createApp();
|
||||
const res = await request(app).get("/api/companies/company-1/costs/summary");
|
||||
const res = await request(app)
|
||||
.get("/api/companies/company-1/costs/finance-summary")
|
||||
.query({ from: "2026-02-01T00:00:00.000Z", to: "2026-02-28T23:59:59.999Z" });
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// byProvider pro-rata subscription split — pure math, no DB needed
|
||||
// ---------------------------------------------------------------------------
|
||||
// The split logic operates on arrays returned by DB queries.
|
||||
// We test it by calling the actual costService with a mock DB that yields
|
||||
// controlled query results and verifying the output proportions.
|
||||
|
||||
import { costService } from "../services/index.js";
|
||||
|
||||
describe("byProvider — pro-rata subscription attribution", () => {
|
||||
it("splits subscription counts proportionally by token share", async () => {
|
||||
// Two models: modelA has 75% of tokens, modelB has 25%.
|
||||
// Total subscription runs = 100, sub input tokens = 1000, sub output tokens = 400.
|
||||
// Expected: modelA gets 75% of each, modelB gets 25%.
|
||||
|
||||
// We bypass the DB by directly exercising the accumulator math.
|
||||
// Inline the accumulation logic from costs.ts to verify the arithmetic is correct.
|
||||
const costRows = [
|
||||
{ provider: "anthropic", model: "claude-sonnet", costCents: 300, inputTokens: 600, outputTokens: 150 },
|
||||
{ provider: "anthropic", model: "claude-haiku", costCents: 100, inputTokens: 200, outputTokens: 50 },
|
||||
];
|
||||
const subscriptionTotals = {
|
||||
apiRunCount: 20,
|
||||
subscriptionRunCount: 100,
|
||||
subscriptionInputTokens: 1000,
|
||||
subscriptionOutputTokens: 400,
|
||||
};
|
||||
|
||||
const totalTokens = costRows.reduce((s, r) => s + r.inputTokens + r.outputTokens, 0);
|
||||
// totalTokens = (600+150) + (200+50) = 750 + 250 = 1000
|
||||
|
||||
const result = costRows.map((row) => {
|
||||
const rowTokens = row.inputTokens + row.outputTokens;
|
||||
const share = totalTokens > 0 ? rowTokens / totalTokens : 0;
|
||||
return {
|
||||
...row,
|
||||
apiRunCount: Math.round(subscriptionTotals.apiRunCount * share),
|
||||
subscriptionRunCount: Math.round(subscriptionTotals.subscriptionRunCount * share),
|
||||
subscriptionInputTokens: Math.round(subscriptionTotals.subscriptionInputTokens * share),
|
||||
subscriptionOutputTokens: Math.round(subscriptionTotals.subscriptionOutputTokens * share),
|
||||
};
|
||||
});
|
||||
|
||||
// modelA: 750/1000 = 75%
|
||||
expect(result[0]!.subscriptionRunCount).toBe(75); // 100 * 0.75
|
||||
expect(result[0]!.subscriptionInputTokens).toBe(750); // 1000 * 0.75
|
||||
expect(result[0]!.subscriptionOutputTokens).toBe(300); // 400 * 0.75
|
||||
expect(result[0]!.apiRunCount).toBe(15); // 20 * 0.75
|
||||
|
||||
// modelB: 250/1000 = 25%
|
||||
expect(result[1]!.subscriptionRunCount).toBe(25); // 100 * 0.25
|
||||
expect(result[1]!.subscriptionInputTokens).toBe(250); // 1000 * 0.25
|
||||
expect(result[1]!.subscriptionOutputTokens).toBe(100); // 400 * 0.25
|
||||
expect(result[1]!.apiRunCount).toBe(5); // 20 * 0.25
|
||||
});
|
||||
|
||||
it("assigns share=0 to all rows when totalTokens is zero (avoids divide-by-zero)", () => {
|
||||
const costRows = [
|
||||
{ provider: "anthropic", model: "claude-sonnet", costCents: 0, inputTokens: 0, outputTokens: 0 },
|
||||
{ provider: "openai", model: "gpt-5", costCents: 0, inputTokens: 0, outputTokens: 0 },
|
||||
];
|
||||
const subscriptionTotals = { apiRunCount: 10, subscriptionRunCount: 5, subscriptionInputTokens: 100, subscriptionOutputTokens: 50 };
|
||||
const totalTokens = 0;
|
||||
|
||||
const result = costRows.map((row) => {
|
||||
const rowTokens = row.inputTokens + row.outputTokens;
|
||||
const share = totalTokens > 0 ? rowTokens / totalTokens : 0;
|
||||
return {
|
||||
subscriptionRunCount: Math.round(subscriptionTotals.subscriptionRunCount * share),
|
||||
subscriptionInputTokens: Math.round(subscriptionTotals.subscriptionInputTokens * share),
|
||||
};
|
||||
});
|
||||
|
||||
expect(result[0]!.subscriptionRunCount).toBe(0);
|
||||
expect(result[0]!.subscriptionInputTokens).toBe(0);
|
||||
expect(result[1]!.subscriptionRunCount).toBe(0);
|
||||
expect(result[1]!.subscriptionInputTokens).toBe(0);
|
||||
});
|
||||
|
||||
it("attribution rounds to nearest integer (no fractional run counts)", () => {
|
||||
// 3 models, 10 runs to split — rounding may not sum to exactly 10, that's expected
|
||||
const costRows = [
|
||||
{ inputTokens: 1, outputTokens: 0 }, // 1/3
|
||||
{ inputTokens: 1, outputTokens: 0 }, // 1/3
|
||||
{ inputTokens: 1, outputTokens: 0 }, // 1/3
|
||||
];
|
||||
const totalTokens = 3;
|
||||
const subscriptionRunCount = 10;
|
||||
|
||||
const result = costRows.map((row) => {
|
||||
const share = row.inputTokens / totalTokens;
|
||||
return Math.round(subscriptionRunCount * share);
|
||||
});
|
||||
|
||||
// Each should be Math.round(10/3) = Math.round(3.33) = 3
|
||||
expect(result).toEqual([3, 3, 3]);
|
||||
for (const count of result) {
|
||||
expect(Number.isInteger(count)).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// windowSpend — verify shape of rolling window results
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe("windowSpend — rolling window labels and hours", () => {
|
||||
it("returns results for the three standard windows (5h, 24h, 7d)", async () => {
|
||||
// The windowSpend method computes three rolling windows internally.
|
||||
// We verify the expected window labels exist in a real call by checking
|
||||
// the service contract shape. Since we're not connecting to a DB here,
|
||||
// we verify the window definitions directly from service source by
|
||||
// exercising the label computation inline.
|
||||
|
||||
const windows = [
|
||||
{ label: "5h", hours: 5 },
|
||||
{ label: "24h", hours: 24 },
|
||||
{ label: "7d", hours: 168 },
|
||||
] as const;
|
||||
|
||||
// All three standard windows must be present
|
||||
expect(windows.map((w) => w.label)).toEqual(["5h", "24h", "7d"]);
|
||||
// Hours must match expected durations
|
||||
expect(windows[0]!.hours).toBe(5);
|
||||
expect(windows[1]!.hours).toBe(24);
|
||||
expect(windows[2]!.hours).toBe(168); // 7 * 24
|
||||
expect(mockFinanceService.summary).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns 400 for invalid finance event list limits", async () => {
|
||||
const app = createApp();
|
||||
const res = await request(app)
|
||||
.get("/api/companies/company-1/costs/finance-events")
|
||||
.query({ limit: "0" });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/invalid 'limit'/i);
|
||||
});
|
||||
|
||||
it("accepts valid finance event list limits", async () => {
|
||||
const app = createApp();
|
||||
const res = await request(app)
|
||||
.get("/api/companies/company-1/costs/finance-events")
|
||||
.query({ limit: "25" });
|
||||
expect(res.status).toBe(200);
|
||||
expect(mockFinanceService.list).toHaveBeenCalledWith("company-1", undefined, 25);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue