AI status: tokens used and an estimated allowance in tokens, for churches (2.0.1)
Churches see their AI use as input/output tokens; rand stays on the Hub. The status also carries usedInputTokens/usedOutputTokens and capInputTokens/capOutputTokens (half the cap priced as input, half as output). Cents unchanged for older installs. Protocol §3b updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,11 +12,11 @@ const mockDb = {
|
||||
aiUsage: {
|
||||
aggregate: jest.fn(async ({ where }) => {
|
||||
const cutoff = where.OR[1].createdAt.gt;
|
||||
const sum = mockRows
|
||||
const rows = mockRows
|
||||
.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 } };
|
||||
.filter(r => r.status !== 'reserved' || r.createdAt > cutoff);
|
||||
const sum = k => rows.reduce((n, r) => n + (r[k] || 0), 0);
|
||||
return { _sum: { chargedMillicents: sum('chargedMillicents'), inputTokens: sum('inputTokens'), outputTokens: sum('outputTokens'), cacheReadTokens: sum('cacheReadTokens'), cacheWriteTokens: sum('cacheWriteTokens') } };
|
||||
}),
|
||||
create: jest.fn(async ({ data }) => { const row = { id: `u${mockRows.length + 1}`, createdAt: new Date(), ...data }; mockRows.push(row); return row; }),
|
||||
update: jest.fn(async ({ where, data }) => Object.assign(mockRows.find(r => r.id === where.id), data)),
|
||||
@@ -114,9 +114,17 @@ describe('pricing', () => {
|
||||
describe('status', () => {
|
||||
const now = new Date('2026-11-10T10:00:00Z');
|
||||
test('hidden for cap R0, an inactive licence or no Hub set-up', () => {
|
||||
expect(ai.statusOf({ licence: { ...mockState.licence, aiMonthlyCapCents: 0 }, price, chargedMillicents: 0, now }).enabled).toBe(false);
|
||||
expect(ai.statusOf({ licence: { ...mockState.licence, status: 'suspended' }, price, chargedMillicents: 0, now }).enabled).toBe(false);
|
||||
expect(ai.statusOf({ licence: mockState.licence, price: null, chargedMillicents: 0, now }).enabled).toBe(false);
|
||||
expect(ai.statusOf({ licence: { ...mockState.licence, aiMonthlyCapCents: 0 }, price, used: null, now }).enabled).toBe(false);
|
||||
expect(ai.statusOf({ licence: { ...mockState.licence, status: 'suspended' }, price, used: null, now }).enabled).toBe(false);
|
||||
expect(ai.statusOf({ licence: mockState.licence, price: null, used: null, now }).enabled).toBe(false);
|
||||
});
|
||||
test('the allowance as tokens: half the cap priced as input, half as output (Hub 2.0.1)', () => {
|
||||
// R50 at Haiku's $0.10 / $0.50, R20 to the dollar, 40% markup: R25 buys 8.93M input or 1.79M output.
|
||||
expect(ai.allowanceTokens(5000, price)).toEqual({ capInputTokens: 8928000, capOutputTokens: 1785000 });
|
||||
expect(ai.allowanceTokens(0, price)).toEqual({ capInputTokens: 0, capOutputTokens: 0 });
|
||||
expect(ai.allowanceTokens(5000, null)).toEqual({ capInputTokens: 0, capOutputTokens: 0 });
|
||||
const s = ai.statusOf({ licence: mockState.licence, price, used: { chargedMillicents: 0, inputTokens: 1840, outputTokens: 420 }, now });
|
||||
expect(s).toMatchObject({ usedInputTokens: 1840, usedOutputTokens: 420, capInputTokens: 8928000, capOutputTokens: 1785000 });
|
||||
});
|
||||
test('an expired licence still has AI during its grace days', () => {
|
||||
const expired = { ...mockState.licence, expiresAt: new Date('2026-11-01T00:00:00Z') };
|
||||
@@ -124,9 +132,9 @@ describe('status', () => {
|
||||
expect(ai.licenceAllows(expired, new Date('2026-11-16T00:00:00Z'))).toBe(false);
|
||||
});
|
||||
test('cap reached once there is no room for the smallest call', () => {
|
||||
const s = ai.statusOf({ licence: mockState.licence, price, chargedMillicents: 1000 * 1000, now });
|
||||
const s = ai.statusOf({ licence: mockState.licence, price, used: { chargedMillicents: 1000 * 1000 }, now });
|
||||
expect(s).toMatchObject({ enabled: true, capReached: false, capCents: 5000, usedCents: 1000, usedPercent: 20, renewsOn: '2026-12-01' });
|
||||
expect(ai.statusOf({ licence: mockState.licence, price, chargedMillicents: 5000 * 1000 - 100, now }).capReached).toBe(true);
|
||||
expect(ai.statusOf({ licence: mockState.licence, price, used: { chargedMillicents: 5000 * 1000 - 100 }, now }).capReached).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user