Hub 2.0 step 6: AI cap and markup per church
The cap and markup move from the licence to the customer (migrated), use is counted over all the church's installs under one lock per church, and the licence token carries the church's cap. Settings → AI shows product. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,14 +2,18 @@
|
||||
// mocked Claude.
|
||||
|
||||
const mockRows = [];
|
||||
const mockState = { licence: null };
|
||||
const mockState = { licence: null, installs: [{ id: 'inst_1' }] };
|
||||
const mockDb = {
|
||||
licence: { findUnique: jest.fn(async () => mockState.licence) },
|
||||
// Hub 2.0: the cap and markup are the church's (kept on mockState.licence here for the tests), and
|
||||
// its installs share the cap.
|
||||
customer: { findUnique: jest.fn(async () => ({ aiMonthlyCapCents: mockState.licence.aiMonthlyCapCents, aiMarkupPercent: mockState.licence.aiMarkupPercent })) },
|
||||
install: { findMany: jest.fn(async () => mockState.installs) },
|
||||
aiUsage: {
|
||||
aggregate: jest.fn(async ({ where }) => {
|
||||
const cutoff = where.OR[1].createdAt.gt;
|
||||
const sum = mockRows
|
||||
.filter(r => r.installId === where.installId && r.month === where.month)
|
||||
.filter(r => where.installId.in.includes(r.installId) && r.month === where.month)
|
||||
.filter(r => r.status !== 'reserved' || r.createdAt > cutoff)
|
||||
.reduce((n, r) => n + r.chargedMillicents, 0);
|
||||
return { _sum: { chargedMillicents: sum } };
|
||||
@@ -37,7 +41,7 @@ jest.mock('@anthropic-ai/sdk', () => {
|
||||
|
||||
const ai = require('../src/utils/ai');
|
||||
|
||||
const install = { id: 'inst_1', licenceId: 'lic_1' };
|
||||
const install = { id: 'inst_1', licenceId: 'lic_1', customerId: 'c1' };
|
||||
const price = { model: 'claude-haiku-5-5', inUsd: 0.1, outUsd: 0.5, cacheReadUsd: 0.01, usdZar: 20, markup: 40 };
|
||||
const body = { protocolVersion: 1, helper: 'draft_message', system: 'Write a friendly message.', input: 'Bring a sleeping bag.', maxTokens: 1000 };
|
||||
const reply = (usage, extra = {}) => ({ model: 'claude-haiku-5-5', stop_reason: 'end_turn', content: [{ type: 'text', text: 'Hi all!' }], usage, ...extra });
|
||||
@@ -45,6 +49,7 @@ const reply = (usage, extra = {}) => ({ model: 'claude-haiku-5-5', stop_reason:
|
||||
beforeEach(() => {
|
||||
mockRows.length = 0;
|
||||
mockState.licence = { id: 'lic_1', status: 'active', expiresAt: null, graceDays: 14, aiMonthlyCapCents: 5000, aiMarkupPercent: null };
|
||||
mockState.installs = [{ id: 'inst_1' }];
|
||||
mockCreate.mockReset();
|
||||
});
|
||||
|
||||
@@ -201,3 +206,19 @@ describe('complete', () => {
|
||||
expect(mockCreate).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('the cap is the church\'s (Hub 2.0)', () => {
|
||||
test('what its other installs used this month counts against it', async () => {
|
||||
mockState.installs = [{ id: 'inst_1' }, { id: 'inst_website' }];
|
||||
mockRows.push({ id: 'w1', installId: 'inst_website', month: ai.monthOf(), status: 'done', chargedMillicents: 4999 * 1000, createdAt: new Date() });
|
||||
const status = await ai.statusFor(install);
|
||||
expect(status).toMatchObject({ enabled: true, capReached: true, usedCents: 4999 });
|
||||
await expect(ai.complete(install, body)).rejects.toMatchObject({ code: 'AI_CAP_REACHED' });
|
||||
expect(mockCreate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('another church\'s use doesn\'t count', async () => {
|
||||
mockRows.push({ id: 'o1', installId: 'inst_other_church', month: ai.monthOf(), status: 'done', chargedMillicents: 4999 * 1000, createdAt: new Date() });
|
||||
expect((await ai.statusFor(install)).usedCents).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user