Files
token-circles/worker/test/mcp-tools-write.test.ts
Komediruzecki b3dfcd38ff feat(encryption): seal notes, learned patterns and tag-rule criteria
Migration 0033 extends field encryption to the free text beside the
ledger: notes on accounts, savings and retirement goals, housing and
portfolio holdings, loan prepayment notes (owned through their loan),
the patterns auto-categorization learns, and tag-rule criteria. Every
write goes through sealForInsert or sealedUpdate, and every read that
reaches a client, a match or a parse goes through openRows: the
account, goal, housing, holding and loan routes, the accounts export,
the MCP goals read and tag-rule upsert, backup export and restore, and
the backfill, which finds a prepayment's owner through its loan.

A learned pattern was found by `pattern = ?`, which cannot match sealed
text. With a key the profile's mappings are opened and compared in JS,
exactly and lowest id first, as the query found them. Tag-rule criteria
are opened before they are parsed, so listTagRules and autoApplyTagRules
now take the keyring.

Edits leave out a field the body left out (sealedUpdate drops undefined
values). A retirement-goal or housing edit that omitted a field no
longer fails with a 500, and a holding edit without notes no longer
writes the stored value back, which with a key would have sealed the
ciphertext a second time.

Names stay plaintext; the plan doc records why, and what else remains.
2026-09-11 10:18:29 +02:00

250 lines
8.4 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 { openedRows } from './helpers/sealed';
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, token_version, plan) VALUES (?, 'w@example.com', 'pbkdf2$100000$x$y', 'password', 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);
});
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',
},
],
})
);
// Matched after opening: in a keyed run the stored description is ciphertext.
const rows = await openedRows<{ account_id: number | null; description: string }>(
'transactions',
USER_ID,
'SELECT account_id, description, text_enc FROM transactions WHERE profile_id = ?',
PROFILE_ID
);
const row = rows.find((r) => r.description === 'Linked');
expect(row?.account_id).toBe(account.id);
});
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, token_version, plan) VALUES (9699, 'o@example.com', 'pbkdf2$100000$x$y', 'password', 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);
});
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);
// Opened: in a keyed run the stored criteria are ciphertext.
const [rule] = await openedRows<{ name: string; criteria: string }>(
'tag_rules',
USER_ID,
'SELECT name, criteria, text_enc FROM tag_rules WHERE id = ?',
out.ruleId
);
expect(rule?.name).toBe('Streaming services');
expect(JSON.parse(rule!.criteria).descriptionContains).toContain('netflix');
});
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');
}
});
});