mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
Profiles, Settings and the Import page move onto the form kit from #602: a refused entry is said under the field that is wrong, in the same words in cloud and local-first. Each runtime now runs one set of rules from shared/ for a profile, a setting, a connected source, a learned category mapping, an uploaded file and an export. - A profile name that is blank, over 100 characters, or another profile's in other capitals is refused at name. Any profile but the last can be deleted, and the Danger Zone lists the page's profiles under their current names. - PUT /api/settings refuses a key another route owns. The base currency select is labelled and says why at the select. - Both runtimes read an uploaded file with shared/importUpload.ts: an upload is checked to be a complete file before it is read, every cell is text, and a file with no header row is refused at file. Local-first reads the file off the page's thread, with a time limit, and imports an uploaded file again. The email importer, /api/v1/import and MCP's upload use the same reader. - A backup carries each profile's connected sources and only its own settings. A restore files each retirement plan and badge record under its profile, from either runtime's file, and restores profile names that differ only in case, the later one numbered. - Clearing a profile deletes its connected sources on the Worker, and deleting an account runs the same deletes. - Applying category mappings updates the linked goals. The Worker's full backup is indented when Settings asks. The Worker's reseed-demo answers 410. - The contract suite settles 12 pins (43 to 31). Slice 4b of 6 in docs/plans/2026-10-07-form-errors.md.
156 lines
6.6 KiB
TypeScript
156 lines
6.6 KiB
TypeScript
import { IMPORT_SOURCE_MESSAGES as M } from '../../importSourceSchema';
|
|
import { expectOk, scenario } from '../types';
|
|
import type { ContractApi, Expect, Json } from '../types';
|
|
import { account } from './accounts';
|
|
|
|
const SHEET_URL = 'https://docs.google.com/spreadsheets/d/contract-sheet/edit#gid=0';
|
|
|
|
/** The body Connected Sources saves for a new sheet (features/import/ConnectedSources.tsx, addSource). */
|
|
export function sourceForm(fields: Record<string, unknown> = {}) {
|
|
return {
|
|
kind: 'google_sheet',
|
|
label: 'Bank ledger',
|
|
config: { url: SHEET_URL, sheetName: 'Sheet1' },
|
|
// The mapping is kept by header name, so it survives the sheet's columns moving.
|
|
mapping: { date: 'Date', description: 'Description', amount: 'Amount', category: 'Category' },
|
|
schedule: 'manual',
|
|
...fields,
|
|
};
|
|
}
|
|
|
|
async function addSource(api: ContractApi, expect: Expect, fields: Record<string, unknown> = {}) {
|
|
const reply = await api.post('/api/import-sources', sourceForm(fields));
|
|
expectOk(expect, reply, 'POST /api/import-sources');
|
|
expect(reply.status).toBe(201);
|
|
return reply.body;
|
|
}
|
|
|
|
async function sources(api: ContractApi, expect: Expect): Promise<Json[]> {
|
|
const reply = await api.get('/api/import-sources');
|
|
expectOk(expect, reply, 'GET /api/import-sources');
|
|
return reply.body as Json[];
|
|
}
|
|
|
|
export const importSources = [
|
|
scenario('a connected sheet is saved, read back, synced and removed', async (api, expect) => {
|
|
const saved = await addSource(api, expect);
|
|
const asSaved = {
|
|
id: expect.any(Number),
|
|
kind: 'google_sheet',
|
|
label: 'Bank ledger',
|
|
config: { url: SHEET_URL, sheetName: 'Sheet1' },
|
|
mapping: { date: 'Date', description: 'Description', amount: 'Amount', category: 'Category' },
|
|
category_types: null,
|
|
default_account_id: null,
|
|
schedule: 'manual',
|
|
last_synced_at: null,
|
|
last_cursor: null,
|
|
};
|
|
expect(saved).toMatchObject(asSaved);
|
|
const id = saved.id as number;
|
|
expect(await sources(api, expect)).toEqual([expect.objectContaining({ ...asSaved, id })]);
|
|
|
|
// After an import from it, the page stamps the sync time and nothing else.
|
|
const syncedAt = '2026-10-08T09:30:00.000Z';
|
|
const synced = await api.put(`/api/import-sources/${id}`, { last_synced_at: syncedAt });
|
|
expectOk(expect, synced, 'PUT the sync time');
|
|
expect(synced.body).toMatchObject({ ...asSaved, id, last_synced_at: syncedAt });
|
|
expect(await sources(api, expect)).toEqual([
|
|
expect.objectContaining({ ...asSaved, id, last_synced_at: syncedAt }),
|
|
]);
|
|
|
|
// A change sends only the fields it changes; the rest stay as they were.
|
|
const everyday = await account(api, expect, 'Everyday', 1000);
|
|
const changed = await api.put(`/api/import-sources/${id}`, {
|
|
label: 'Joint ledger',
|
|
category_types: { Savings: 'account' },
|
|
default_account_id: everyday,
|
|
});
|
|
expectOk(expect, changed, 'PUT the label and account');
|
|
const asChanged = {
|
|
...asSaved,
|
|
id,
|
|
label: 'Joint ledger',
|
|
category_types: { Savings: 'account' },
|
|
default_account_id: everyday,
|
|
last_synced_at: syncedAt,
|
|
};
|
|
expect(changed.body).toMatchObject(asChanged);
|
|
expect(await sources(api, expect)).toEqual([expect.objectContaining(asChanged)]);
|
|
|
|
const removed = await api.delete(`/api/import-sources/${id}`);
|
|
expectOk(expect, removed, 'DELETE the source');
|
|
expect(removed.body).toEqual({ deleted: true });
|
|
expect(await sources(api, expect)).toEqual([]);
|
|
expect((await api.delete(`/api/import-sources/${id}`)).status).toBe(404);
|
|
expect((await api.put(`/api/import-sources/${id}`, { label: 'Gone' })).status).toBe(404);
|
|
}),
|
|
|
|
scenario("another profile's connected sheet is not changed or removed", async (api, expect) => {
|
|
const { id } = await addSource(api, expect);
|
|
const other = api.other;
|
|
expect(await sources(other, expect)).toEqual([]);
|
|
expect((await other.put(`/api/import-sources/${id}`, { label: 'Theirs' })).status).toBe(404);
|
|
expect((await other.delete(`/api/import-sources/${id}`)).status).toBe(404);
|
|
expect(await sources(api, expect)).toEqual([
|
|
expect.objectContaining({ id, label: 'Bank ledger', last_synced_at: null }),
|
|
]);
|
|
}),
|
|
|
|
scenario('connected sheets are listed newest first', async (api, expect) => {
|
|
const first = (await addSource(api, expect, { label: 'First' })).id;
|
|
const second = (await addSource(api, expect, { label: 'Second' })).id;
|
|
const third = (await addSource(api, expect, { label: 'Third' })).id;
|
|
await addSource(api.other, expect, { label: 'Theirs' });
|
|
expect((await sources(api, expect)).map((s) => s.id)).toEqual([third, second, first]);
|
|
}),
|
|
|
|
scenario(
|
|
'a connected source that cannot be stored is refused at its field, and nothing is saved',
|
|
async (api, expect) => {
|
|
const theirs = await account(api.other, expect, 'Theirs', 5);
|
|
const refused = async (fields: Record<string, unknown>) => {
|
|
const reply = await api.post('/api/import-sources', sourceForm(fields));
|
|
expect(reply.status, JSON.stringify(reply.body)).toBe(400);
|
|
return reply.body.fields as Record<string, string>;
|
|
};
|
|
expect(
|
|
await refused({ kind: 'dropbox', schedule: 'hourly', label: 'x'.repeat(201) })
|
|
).toEqual({ kind: M.kind, schedule: M.schedule, label: M.labelLength });
|
|
expect(await refused({ label: 42, config: 'sheet' })).toEqual({
|
|
label: M.label,
|
|
config: M.config,
|
|
});
|
|
expect(await refused({ config: { url: 'https://example.com/not-a-sheet' } })).toEqual({
|
|
'config.url': M.url,
|
|
});
|
|
expect(await refused({ default_account_id: theirs })).toEqual({
|
|
default_account_id: M.account,
|
|
});
|
|
expect(await sources(api, expect)).toEqual([]);
|
|
|
|
// An edit is checked for the fields it sends, and only those.
|
|
const { id } = await addSource(api, expect);
|
|
const edit = (fields: Record<string, unknown>) =>
|
|
api.put(`/api/import-sources/${id}`, fields);
|
|
expect((await edit({ schedule: 'weekly' })).body.fields).toEqual({ schedule: M.schedule });
|
|
expect((await edit({ config: { sheetName: 'Sheet2' } })).body.fields).toEqual({
|
|
'config.url': M.url,
|
|
});
|
|
expect((await edit({ default_account_id: theirs })).body.fields).toEqual({
|
|
default_account_id: M.account,
|
|
});
|
|
expectOk(expect, await edit({ label: ' Renamed ' }), 'PUT a new name');
|
|
expect(await sources(api, expect)).toEqual([
|
|
expect.objectContaining({
|
|
id,
|
|
label: 'Renamed',
|
|
schedule: 'manual',
|
|
default_account_id: null,
|
|
config: { url: SHEET_URL, sheetName: 'Sheet1' },
|
|
}),
|
|
]);
|
|
}
|
|
),
|
|
];
|