mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
Brings in580168e8and the six PRs merged on it: #628 and #623 (tests), #622, #621, #624 and #625. The branch already carried #621's first commit (3b265179), #622's first commit (50841aaf) and #624's and #625's commits. Main's reviewed versions win, and the branch's later work stays on top of them. - #621's review commit: the Accounts page takes the badge's colour and the card's icon from core/accountTypes.ts, the constellation and the edit dialog read the type through knownAccountType, and an unknown type opens as a disabled "Other account" option. The branch's credit work moves into those maps: accountTypeTone gives credit 'expense', accountTypeIcon gives it the card and cash the banknotes, the constellation keeps its "Credit card" entry and drops the dead "investment" one, and the edit dialog offers Credit Card. accountForm.ts imports main's storedAccountType and the branch's takesCreditLine. - #622's review commit is dev-changelog only, and main's entry replaces the branch's copy. The achievements code is the branch's, which sums through baseAmount wherever #622 does. - #624 and #625: the onboarding CSS and spec are main's plus the branch's later commits. The clash: the branch makes credit a type, and #621's tests on main use credit as the type a build does not know. After the merge a credit account shows as a credit card on every display #621 reads through knownAccountType (badge word, colour, icon, constellation, Transactions select, edit dialog), and the import and MCP create_account take credit, as the account form does. #621's tests that need an unknown type use sock_drawer, which no build will have; the branch's own switch from credit to loan stays in the tests that are not #621's. New checks: a credit card on each display (accountTypes, accountsPageTypes, accountConstellation, transactionsAccountTypeWord, the edit dialog in accountForm), an import contract scenario in both runtimes and MCP create_account. Changelogs: main's entries stay where main has them, and the branch's stay under [Unreleased]. The branch's credit entries now say that the import and the MCP server take a credit card.
410 lines
16 KiB
TypeScript
410 lines
16 KiB
TypeScript
/**
|
|
* The write tools: append plus curate. There is no update or delete of arbitrary records here
|
|
* by design -- an agent acting on its own analysis should be able to add and to categorize,
|
|
* and mistakes it makes should be additive and reversible.
|
|
*/
|
|
import { env, SELF } from 'cloudflare:test';
|
|
import { beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
|
import { mintApiToken } from '../src/apitoken';
|
|
import { ACCOUNT_TYPES, DEFAULT_ACCOUNT_TYPE } from '../../shared/accountSchema';
|
|
import { BUDGET_MESSAGES } from '../../shared/budgetSchema';
|
|
import { defaultTagColor, TAG_MESSAGES } from '../../shared/tagSchema';
|
|
|
|
const USER_ID = 9600;
|
|
const PROFILE_ID = 9601;
|
|
let secret = '';
|
|
let readOnly = '';
|
|
|
|
async function call(
|
|
name: string,
|
|
args: Record<string, unknown> = {},
|
|
token = secret
|
|
): Promise<any> {
|
|
const res = await SELF.fetch('https://api.example.com/mcp', {
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
jsonrpc: '2.0',
|
|
id: 1,
|
|
method: 'tools/call',
|
|
params: { name, arguments: args },
|
|
}),
|
|
});
|
|
return ((await res.json()) as any).result;
|
|
}
|
|
|
|
const unwrap = (r: any): any => {
|
|
if (r?.isError) throw new Error(r.content[0].text);
|
|
return r.structuredContent;
|
|
};
|
|
|
|
beforeAll(async () => {
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO users (id, email, password_hash, auth_provider, email_verified, token_version, plan) VALUES (?, 'w@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 (?, 'W', ?)")
|
|
.bind(PROFILE_ID, USER_ID)
|
|
.run();
|
|
secret = (
|
|
await mintApiToken(env.DB, USER_ID, {
|
|
name: 'w',
|
|
scopes: ['read', 'write'],
|
|
defaultProfileId: PROFILE_ID,
|
|
})
|
|
).secret;
|
|
readOnly = (
|
|
await mintApiToken(env.DB, USER_ID, {
|
|
name: 'ro',
|
|
scopes: ['read'],
|
|
defaultProfileId: PROFILE_ID,
|
|
})
|
|
).secret;
|
|
});
|
|
|
|
describe('write tools', () => {
|
|
beforeEach(async () => {
|
|
await env.DB.prepare('DELETE FROM transactions WHERE profile_id = ?').bind(PROFILE_ID).run();
|
|
await env.DB.prepare('DELETE FROM tag_rules WHERE profile_id = ?').bind(PROFILE_ID).run();
|
|
await env.DB.prepare('DELETE FROM budgets WHERE profile_id = ?').bind(PROFILE_ID).run();
|
|
});
|
|
|
|
it('create_account then create_transactions, with duplicates reported', async () => {
|
|
const account = unwrap(
|
|
await call('create_account', { name: 'Savings', type: 'savings', currency: 'EUR' })
|
|
);
|
|
expect(account.id).toBeGreaterThan(0);
|
|
|
|
const rows = [
|
|
{
|
|
date: '2026-03-01',
|
|
description: 'Book',
|
|
amount: -12.5,
|
|
type: 'expense',
|
|
accountName: 'Savings',
|
|
},
|
|
{
|
|
date: '2026-03-02',
|
|
description: 'Coffee',
|
|
amount: -3.2,
|
|
type: 'expense',
|
|
accountName: 'Savings',
|
|
},
|
|
];
|
|
const first = unwrap(await call('create_transactions', { transactions: rows }));
|
|
expect(first.imported).toBe(2);
|
|
|
|
const again = unwrap(await call('create_transactions', { transactions: rows }));
|
|
expect(again.imported).toBe(0);
|
|
expect(again.duplicates).toBe(2);
|
|
|
|
const count = await env.DB.prepare(
|
|
'SELECT COUNT(*) AS n FROM transactions WHERE profile_id = ?'
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.first<{ n: number }>();
|
|
expect(count?.n).toBe(2);
|
|
});
|
|
|
|
// The tool stored any string as the type: one the app does not have failed the app's account
|
|
// list, every account with it.
|
|
it('create_account refuses a type the app does not have, and stores nothing', async () => {
|
|
for (const type of ['sock_drawer', 'loan']) {
|
|
const result = await call('create_account', { name: `Card ${type}`, type, currency: 'EUR' });
|
|
expect(result.isError, `type "${type}"`).toBe(true);
|
|
expect(result.content[0].text).toContain('type');
|
|
}
|
|
const count = await env.DB.prepare(
|
|
"SELECT COUNT(*) AS n FROM accounts WHERE profile_id = ? AND name LIKE 'Card%'"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.first<{ n: number }>();
|
|
expect(count?.n).toBe(0);
|
|
|
|
const plain = unwrap(await call('create_account', { name: 'Card plain', currency: 'EUR' }));
|
|
const stored = await env.DB.prepare('SELECT type FROM accounts WHERE id = ?')
|
|
.bind(plain.id)
|
|
.first<{ type: string }>();
|
|
expect(stored?.type).toBe(DEFAULT_ACCOUNT_TYPE);
|
|
});
|
|
|
|
it('create_account takes a credit card, as the Accounts form does', async () => {
|
|
const card = unwrap(
|
|
await call('create_account', { name: 'Visa', type: 'credit', currency: 'EUR' })
|
|
);
|
|
const stored = await env.DB.prepare('SELECT type FROM accounts WHERE id = ?')
|
|
.bind(card.id)
|
|
.first<{ type: string }>();
|
|
expect(stored?.type).toBe('credit');
|
|
});
|
|
|
|
it('create_account offers the types the app has, and names only those', async () => {
|
|
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/list' }),
|
|
});
|
|
const tools = ((await res.json()) as any).result.tools as any[];
|
|
const tool = tools.find((t) => t.name === 'create_account');
|
|
const type = tool.inputSchema.properties.type;
|
|
expect(type.enum).toEqual([...ACCOUNT_TYPES]);
|
|
// Each says what every type is, "giro (a current account)", and names no other.
|
|
for (const said of [tool.description, type.description] as string[]) {
|
|
expect([...said.matchAll(/(\w+) \(/g)].map((match) => match[1])).toEqual([...ACCOUNT_TYPES]);
|
|
}
|
|
});
|
|
|
|
it('create_transactions attaches rows to the named account', async () => {
|
|
const account = unwrap(await call('create_account', { name: 'Current', currency: 'EUR' }));
|
|
unwrap(
|
|
await call('create_transactions', {
|
|
transactions: [
|
|
{
|
|
date: '2026-03-05',
|
|
description: 'Linked',
|
|
amount: -5,
|
|
type: 'expense',
|
|
accountName: 'Current',
|
|
},
|
|
],
|
|
})
|
|
);
|
|
const row = await env.DB.prepare(
|
|
"SELECT account_id FROM transactions WHERE profile_id = ? AND description = 'Linked'"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.first<{ account_id: number | null }>();
|
|
expect(row?.account_id).toBe(account.id);
|
|
});
|
|
|
|
it('create_transactions refuses an account that is not there, and adds nothing', async () => {
|
|
unwrap(await call('create_account', { name: 'Wallet', currency: 'EUR' }));
|
|
const row = (accountName: string, description: string) => ({
|
|
date: '2026-03-06',
|
|
description,
|
|
amount: -5,
|
|
type: 'expense',
|
|
accountName,
|
|
});
|
|
|
|
const result = await call('create_transactions', {
|
|
transactions: [row('wallet ', 'Known'), row('Revolut', 'Unknown'), row('N26', 'Other')],
|
|
});
|
|
|
|
expect(result.isError).toBe(true);
|
|
const said: string = result.content[0].text;
|
|
expect(said).toContain('This profile has no account named "Revolut" or "N26".');
|
|
expect(said).toContain('"Wallet"');
|
|
expect(said).toContain('create_account');
|
|
const count = await env.DB.prepare(
|
|
'SELECT COUNT(*) AS n FROM transactions WHERE profile_id = ?'
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.first<{ n: number }>();
|
|
expect(count?.n).toBe(0);
|
|
});
|
|
|
|
it('categorize_transactions updates only the named ids in this profile', async () => {
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO categories (id, name, type, profile_id) VALUES (96020, 'Books', 'expense', ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
await env.DB.prepare(
|
|
"INSERT INTO transactions (id, date, description, amount, type, currency, profile_id) VALUES (96001, '2026-03-01', 'Book', -12.5, 'expense', 'EUR', ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
await env.DB.prepare(
|
|
"INSERT INTO transactions (id, date, description, amount, type, currency, profile_id) VALUES (96002, '2026-03-02', 'Coffee', -3.2, 'expense', 'EUR', ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
|
|
const out = unwrap(
|
|
await call('categorize_transactions', { transactionIds: [96001], categoryId: 96020 })
|
|
);
|
|
expect(out.updated).toBe(1);
|
|
|
|
const book = await env.DB.prepare(
|
|
'SELECT category_id FROM transactions WHERE id = 96001'
|
|
).first<{ category_id: number | null }>();
|
|
const coffee = await env.DB.prepare(
|
|
'SELECT category_id FROM transactions WHERE id = 96002'
|
|
).first<{ category_id: number | null }>();
|
|
expect(book?.category_id).toBe(96020);
|
|
expect(coffee?.category_id).toBeNull();
|
|
});
|
|
|
|
it('categorize_transactions refuses a category from another profile', async () => {
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO users (id, email, password_hash, auth_provider, email_verified, token_version, plan) VALUES (9699, 'o@example.com', 'pbkdf2$100000$x$y', 'password', 1, 1, 'advanced')"
|
|
).run();
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO profiles (id, name, user_id) VALUES (9699, 'Other', 9699)"
|
|
).run();
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO categories (id, name, type, profile_id) VALUES (96999, 'Foreign', 'expense', 9699)"
|
|
).run();
|
|
await env.DB.prepare(
|
|
"INSERT INTO transactions (id, date, description, amount, type, currency, profile_id) VALUES (96003, '2026-03-01', 'x', -1, 'expense', 'EUR', ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
|
|
const result = await call('categorize_transactions', {
|
|
transactionIds: [96003],
|
|
categoryId: 96999,
|
|
});
|
|
expect(result.isError).toBe(true);
|
|
const row = await env.DB.prepare(
|
|
'SELECT category_id FROM transactions WHERE id = 96003'
|
|
).first<{ category_id: number | null }>();
|
|
expect(row?.category_id).toBeNull();
|
|
});
|
|
|
|
it('upsert_budget creates then updates in place', async () => {
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO categories (id, name, type, profile_id) VALUES (96021, 'Food', 'expense', ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
const created = unwrap(
|
|
await call('upsert_budget', { categoryId: 96021, amount: 300, startDate: '2026-03-01' })
|
|
);
|
|
expect(created.created).toBe(true);
|
|
|
|
const updated = unwrap(
|
|
await call('upsert_budget', { categoryId: 96021, amount: 350, startDate: '2026-03-01' })
|
|
);
|
|
expect(updated.created).toBe(false);
|
|
expect(updated.id).toBe(created.id);
|
|
|
|
const row = await env.DB.prepare('SELECT amount FROM budgets WHERE id = ?')
|
|
.bind(created.id)
|
|
.first<{ amount: number }>();
|
|
expect(row?.amount).toBe(350);
|
|
});
|
|
|
|
// The tool took any positive number and any date-shaped text, so it stored 12.345 and
|
|
// 2026-02-30, refused a budget of zero, and answered another profile's category with a 403
|
|
// where the app answers a 400 at the category. It runs the app's checks now.
|
|
it('upsert_budget checks a budget as the app does', async () => {
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO categories (id, name, type, profile_id) VALUES (96021, 'Food', 'expense', ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
const refused = async (args: Record<string, unknown>): Promise<string> => {
|
|
const result = await call('upsert_budget', args);
|
|
expect(result.isError, JSON.stringify(args)).toBe(true);
|
|
return result.content[0].text;
|
|
};
|
|
const food = { categoryId: 96021, startDate: '2026-03-01' };
|
|
|
|
expect(await refused({ ...food, amount: 12.345 })).toBe(BUDGET_MESSAGES.amountCents);
|
|
expect(await refused({ ...food, amount: -5 })).toBe(BUDGET_MESSAGES.amountNegative);
|
|
expect(await refused({ ...food, amount: 10, startDate: '2026-02-30' })).toBe(
|
|
BUDGET_MESSAGES.startDate
|
|
);
|
|
expect(await refused({ ...food, categoryId: 96099, amount: 10 })).toBe(
|
|
BUDGET_MESSAGES.category
|
|
);
|
|
const stored = await env.DB.prepare('SELECT COUNT(*) AS n FROM budgets WHERE profile_id = ?')
|
|
.bind(PROFILE_ID)
|
|
.first<{ n: number }>();
|
|
expect(stored?.n).toBe(0);
|
|
|
|
expect(unwrap(await call('upsert_budget', { ...food, amount: 0 }))).toMatchObject({
|
|
created: true,
|
|
amount: 0,
|
|
});
|
|
});
|
|
|
|
// Its month is the start date's month: a budget that starts on the 15th is March's budget, and
|
|
// setting March's again changes it. Matched on the exact date, a second budget was added.
|
|
it('upsert_budget changes the budget its month has, whatever day that one starts', async () => {
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO categories (id, name, type, profile_id) VALUES (96021, 'Food', 'expense', ?)"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.run();
|
|
const created = unwrap(
|
|
await call('upsert_budget', { categoryId: 96021, amount: 300, startDate: '2026-03-15' })
|
|
);
|
|
const updated = unwrap(
|
|
await call('upsert_budget', { categoryId: 96021, amount: 350, startDate: '2026-03-01' })
|
|
);
|
|
expect(updated).toMatchObject({ id: created.id, created: false, amount: 350 });
|
|
const march = await env.DB.prepare(
|
|
"SELECT id, amount, start_date FROM budgets WHERE profile_id = ? AND start_date >= '2026-03-01' AND start_date < '2026-04-01'"
|
|
)
|
|
.bind(PROFILE_ID)
|
|
.all();
|
|
expect(march.results).toEqual([{ id: created.id, amount: 350, start_date: '2026-03-15' }]);
|
|
});
|
|
|
|
it('upsert_tag_rule creates the tag if it does not exist', async () => {
|
|
const out = unwrap(
|
|
await call('upsert_tag_rule', {
|
|
tagName: 'subscriptions',
|
|
name: 'Streaming services',
|
|
criteria: { descriptionContains: ['netflix', 'spotify'] },
|
|
})
|
|
);
|
|
expect(out.tagId).toBeGreaterThan(0);
|
|
const rule = await env.DB.prepare('SELECT name, criteria FROM tag_rules WHERE id = ?')
|
|
.bind(out.ruleId)
|
|
.first<{ name: string; criteria: string }>();
|
|
expect(rule?.name).toBe('Streaming services');
|
|
expect(JSON.parse(rule!.criteria).descriptionContains).toContain('netflix');
|
|
});
|
|
|
|
// It wrote tags past the tag rules: a padded name was a second tag, a new one took no colour
|
|
// from the palette, and a name of 100 characters was stored where the Tags page stops at 50.
|
|
it('upsert_tag_rule finds a tag in any case or padding, and creates one by the tag rules', async () => {
|
|
await env.DB.prepare('DELETE FROM tags WHERE profile_id = ?').bind(PROFILE_ID).run();
|
|
const travel = { descriptionContains: ['rail'] };
|
|
|
|
const first = unwrap(
|
|
await call('upsert_tag_rule', { tagName: 'Travel', name: 'Trains', criteria: travel })
|
|
);
|
|
const twin = unwrap(
|
|
await call('upsert_tag_rule', { tagName: ' travel ', name: 'Planes', criteria: travel })
|
|
);
|
|
|
|
expect(twin.tagId).toBe(first.tagId);
|
|
const tags = await env.DB.prepare('SELECT name, color FROM tags WHERE profile_id = ?')
|
|
.bind(PROFILE_ID)
|
|
.all();
|
|
expect(tags.results).toEqual([{ name: 'Travel', color: defaultTagColor(0) }]);
|
|
|
|
const long = await call('upsert_tag_rule', {
|
|
tagName: 'Weekend trips to the seaside and the mountains in summer',
|
|
name: 'Long',
|
|
criteria: travel,
|
|
});
|
|
expect(long.isError).toBe(true);
|
|
expect(long.content[0].text).toBe(TAG_MESSAGES.nameLength);
|
|
const count = await env.DB.prepare('SELECT COUNT(*) AS n FROM tags WHERE profile_id = ?')
|
|
.bind(PROFILE_ID)
|
|
.first<{ n: number }>();
|
|
expect(count?.n).toBe(1);
|
|
});
|
|
|
|
it('every write tool refuses a read-only token', async () => {
|
|
for (const name of [
|
|
'create_transactions',
|
|
'create_account',
|
|
'categorize_transactions',
|
|
'upsert_tag_rule',
|
|
'upsert_budget',
|
|
]) {
|
|
const result = await call(name, {}, readOnly);
|
|
expect(result.isError, `${name} accepted a read-only token`).toBe(true);
|
|
expect(result.content[0].text).toContain('write');
|
|
}
|
|
});
|
|
});
|