mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
A password account confirms its email before it signs in or uses the app. - Create account ends on Check your inbox, which names the address, sends the link again and says where to open it. The screen is the same whatever the address. - A password sign-in works once the address is confirmed, or in the browser that opened the account's own confirm link. Until then the right password is refused as a wrong one is, and the form offers Send the link again. - A signed-in account whose address is not confirmed sees Confirm your email instead of the app. The Worker answers its sessions and API tokens 403 EMAIL_UNCONFIRMED everywhere but me, logout, the signed-in resend, the link's finish route, and the billing portal for an account that pays. - POST /api/auth/verify-email/resend sends the link again while signed out, with limits and the captcha before any look-up. One budget of three confirm links an hour covers every way of asking for one. - Sign-up answers first, then makes the account, its profile and its first link in one batch, tried twice. Signing up again for an address waiting for its link mails a fresh link. - A sign-in code, a reset link and Google sign-in confirm the address. A passkey or two-factor does not. - /api/v1 capabilities, reminders and sheet syncs leave out an account waiting for its link. - The welcome and confirm mails say the link starts the account and where to sign in after opening it. No mail on the sign-up path has an em dash before the shared footer. No migration.
219 lines
8.7 KiB
TypeScript
219 lines
8.7 KiB
TypeScript
/**
|
|
* The read tools, and the two properties that keep them from ruining a caller's context:
|
|
* every list caps at MAX_ROWS, and summarize_spending aggregates server-side so the agent
|
|
* never pages thousands of rows to compute a sum.
|
|
*/
|
|
import { env, SELF } from 'cloudflare:test';
|
|
import { beforeAll, describe, expect, it, vi } from 'vitest';
|
|
import { mintApiToken } from '../src/apitoken';
|
|
|
|
const USER_ID = 9400;
|
|
const PROFILE_ID = 9401;
|
|
let secret = '';
|
|
|
|
async function call(name: string, args: Record<string, unknown> = {}): Promise<any> {
|
|
const res = await SELF.fetch('https://api.example.com/mcp', {
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${secret}`, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
jsonrpc: '2.0',
|
|
id: 1,
|
|
method: 'tools/call',
|
|
params: { name, arguments: args },
|
|
}),
|
|
});
|
|
const body = (await res.json()) as any;
|
|
if (body.result?.isError) throw new Error(body.result.content[0].text);
|
|
return body.result.structuredContent;
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO users (id, email, password_hash, auth_provider, email_verified, token_version, plan) VALUES (?, 'read@example.com', 'pbkdf2$100000$x$y', 'password', 1, 1, 'advanced')"
|
|
)
|
|
.bind(USER_ID)
|
|
.run();
|
|
await env.DB.prepare("INSERT OR IGNORE INTO profiles (id, name, user_id) VALUES (?, 'Read', ?)")
|
|
.bind(PROFILE_ID, USER_ID)
|
|
.run();
|
|
await env.DB.prepare(
|
|
"INSERT INTO accounts (id, name, type, currency, balance, profile_id) VALUES (94010, 'Checking', 'giro', 'EUR', 1000, ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
await env.DB.prepare(
|
|
"INSERT INTO categories (id, name, type, profile_id) VALUES (94020, 'Groceries', 'expense', ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
|
|
// Three groceries expenses in January, one in February, plus one income.
|
|
const rows: [string, string, number, string, number | null][] = [
|
|
['2026-01-05', 'Market', -20, 'expense', 94020],
|
|
['2026-01-15', 'Market', -30, 'expense', 94020],
|
|
['2026-01-25', 'Market', -50, 'expense', 94020],
|
|
['2026-02-05', 'Market', -10, 'expense', 94020],
|
|
['2026-01-01', 'Salary', 2000, 'income', null],
|
|
];
|
|
for (const [date, description, amount, type, category] of rows) {
|
|
await env.DB.prepare(
|
|
'INSERT INTO transactions (date, description, amount, type, currency, account_id, category_id, profile_id) VALUES (?, ?, ?, ?, ?, 94010, ?, ?)'
|
|
)
|
|
.bind(date, description, amount, type, 'EUR', category, PROFILE_ID)
|
|
.run();
|
|
}
|
|
|
|
secret = (
|
|
await mintApiToken(env.DB, USER_ID, {
|
|
name: 'read',
|
|
scopes: ['read'],
|
|
defaultProfileId: PROFILE_ID,
|
|
})
|
|
).secret;
|
|
});
|
|
|
|
describe('read tools', () => {
|
|
it('list_transactions filters by date, type and category', async () => {
|
|
const all = await call('list_transactions', {});
|
|
expect(all.totalCount).toBe(5);
|
|
expect(all.truncated).toBe(false);
|
|
|
|
const january = await call('list_transactions', { from: '2026-01-01', to: '2026-01-31' });
|
|
expect(january.totalCount).toBe(4);
|
|
|
|
const expenses = await call('list_transactions', { type: 'expense' });
|
|
expect(expenses.totalCount).toBe(4);
|
|
|
|
const groceries = await call('list_transactions', { categoryIds: [94020] });
|
|
expect(groceries.totalCount).toBe(4);
|
|
|
|
const search = await call('list_transactions', { search: 'Salary' });
|
|
expect(search.rows).toHaveLength(1);
|
|
expect(search.rows[0].description).toBe('Salary');
|
|
});
|
|
|
|
it('list_transactions paginates and reports truncation at the cap', async () => {
|
|
const page = await call('list_transactions', { limit: 2 });
|
|
expect(page.rows).toHaveLength(2);
|
|
expect(page.truncated).toBe(true);
|
|
expect(page.nextCursor).not.toBeNull();
|
|
|
|
const next = await call('list_transactions', { limit: 2, cursor: page.nextCursor });
|
|
expect(next.rows).toHaveLength(2);
|
|
expect(next.rows[0].id).not.toBe(page.rows[0].id);
|
|
});
|
|
|
|
it('list_transactions refuses a limit over the hard cap', async () => {
|
|
await expect(call('list_transactions', { limit: 5000 })).rejects.toThrow(/argument/i);
|
|
});
|
|
|
|
it('summarize_spending aggregates by category and by month', async () => {
|
|
const byCategory = await call('summarize_spending', {
|
|
from: '2026-01-01',
|
|
to: '2026-01-31',
|
|
groupBy: 'category',
|
|
});
|
|
const groceries = byCategory.groups.find((g: any) => g.key === 'Groceries');
|
|
expect(groceries.total).toBeCloseTo(-100);
|
|
expect(groceries.count).toBe(3);
|
|
|
|
const byMonth = await call('summarize_spending', { groupBy: 'month', type: 'expense' });
|
|
const january = byMonth.groups.find((g: any) => g.key === '2026-01');
|
|
expect(january.total).toBeCloseTo(-100);
|
|
expect(byMonth.groups.find((g: any) => g.key === '2026-02').total).toBeCloseTo(-10);
|
|
});
|
|
|
|
it('list_reference_data returns accounts, categories and tags together', async () => {
|
|
const ref = await call('list_reference_data', {});
|
|
expect(ref.accounts.map((a: any) => a.name)).toContain('Checking');
|
|
expect(ref.categories.map((x: any) => x.name)).toContain('Groceries');
|
|
expect(Array.isArray(ref.tags)).toBe(true);
|
|
});
|
|
|
|
it('get_overview reports balances and month totals', async () => {
|
|
const overview = await call('get_overview', { month: '2026-01' });
|
|
expect(overview.accounts).toHaveLength(1);
|
|
expect(overview.netWorth).toBeCloseTo(1000);
|
|
expect(overview.month.income).toBeCloseTo(2000);
|
|
expect(overview.month.expense).toBeCloseTo(-100);
|
|
});
|
|
|
|
// Upcoming means what the Dashboard's Upcoming Bills lists: the active bills that fall due from
|
|
// today through 30 days on, on the dates they fall due. The tool read the stored due date, the
|
|
// first one, which paying never moves: a monthly bill set up before today was never upcoming
|
|
// again, and a paused one with a later date still was.
|
|
it('get_overview lists the bills falling due in the next 30 days, as the Dashboard does', async () => {
|
|
vi.useFakeTimers({ toFake: ['Date'] });
|
|
vi.setSystemTime(new Date('2026-10-08T12:00:00Z'));
|
|
try {
|
|
const bills: [number, string, string, string | null, number][] = [
|
|
// Paid for October: next due 5 November.
|
|
[94040, 'Rent', '2026-01-05', '2026-10-03', 1],
|
|
// Paid for September: due on the 10th.
|
|
[94041, 'Water', '2026-01-10', '2026-09-10', 1],
|
|
// Overdue since the 3rd: the Bills page lists it as unpaid, not as upcoming.
|
|
[94042, 'Power', '2026-01-03', '2026-09-03', 1],
|
|
// Paused.
|
|
[94043, 'Gym', '2026-12-01', null, 0],
|
|
];
|
|
for (const [id, name, due, paid, active] of bills) {
|
|
await env.DB.prepare(
|
|
"INSERT INTO bills (id, name, amount, frequency, due_date, last_paid_date, is_active, profile_id) VALUES (?, ?, 10, 'monthly', ?, ?, ?, ?)"
|
|
)
|
|
.bind(id, name, due, paid, active, PROFILE_ID)
|
|
.run();
|
|
}
|
|
|
|
const overview = await call('get_overview', {});
|
|
expect(
|
|
overview.upcomingBills.map((b: any) => [b.name, b.next_due_date, b.days_until])
|
|
).toEqual([
|
|
['Water', '2026-10-10', 2],
|
|
['Rent', '2026-11-05', 28],
|
|
]);
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
// A month's budgets are the ones that start in it, as the Budgets page reads them. The tool
|
|
// answered every budget the profile ever had, each against the asked month's spending.
|
|
it("get_budgets_and_goals answers the month's budgets only", async () => {
|
|
await env.DB.prepare(
|
|
"INSERT INTO categories (id, name, type, profile_id) VALUES (94021, 'Rent', 'expense', ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
for (const [id, amount, start] of [
|
|
[94032, 500, '2026-01-01'],
|
|
[94033, 600, '2026-02-01'],
|
|
] as const) {
|
|
await env.DB.prepare(
|
|
"INSERT INTO budgets (id, category_id, amount, period, start_date, profile_id) VALUES (?, 94021, ?, 'monthly', ?, ?)"
|
|
)
|
|
.bind(id, amount, start, PROFILE_ID)
|
|
.run();
|
|
}
|
|
|
|
const rent = async (month: string) =>
|
|
(await call('get_budgets_and_goals', { month })).budgets
|
|
.filter((b: any) => b.category_id === 94021)
|
|
.map((b: any) => [b.id, b.amount]);
|
|
expect(await rent('2026-01')).toEqual([[94032, 500]]);
|
|
expect(await rent('2026-02')).toEqual([[94033, 600]]);
|
|
});
|
|
|
|
it('get_budgets_and_goals returns budgets with spend against them', async () => {
|
|
await env.DB.prepare(
|
|
"INSERT INTO budgets (id, category_id, amount, period, start_date, profile_id) VALUES (94030, 94020, 150, 'monthly', '2026-01-01', ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
const result = await call('get_budgets_and_goals', { month: '2026-01' });
|
|
const budget = result.budgets.find((b: any) => b.id === 94030);
|
|
expect(budget.amount).toBe(150);
|
|
expect(budget.spent).toBeCloseTo(100);
|
|
expect(budget.remaining).toBeCloseTo(50);
|
|
});
|
|
});
|