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.
212 lines
7.7 KiB
TypeScript
212 lines
7.7 KiB
TypeScript
/**
|
|
* A backup carries the profiles' saved import sources, and a restore puts them back.
|
|
*
|
|
* Neither runtime wrote them to the file, so a restore lost every connected sheet. Now the file
|
|
* carries them as GET /api/import-sources answers them (config, mapping and category types as
|
|
* objects), and the restore:
|
|
*
|
|
* - puts each under its restored profile, with its default account moved to the restored copy,
|
|
* or none when the file does not carry that account;
|
|
* - takes the JSON columns as objects or as text, as a file from either runtime has them;
|
|
* - puts a source on a schedule our machines run (on open, daily) back on it only when the plan
|
|
* includes automated imports, and on manual otherwise, as the import-sources routes would;
|
|
* - restores a kind or a schedule there is no such thing as as a sheet on manual, which the
|
|
* person can open and fix, as local-first does.
|
|
*/
|
|
import { env, SELF } from 'cloudflare:test';
|
|
import { beforeEach, describe, expect, it } from 'vitest';
|
|
import { sessionCookie } from './helpers/session';
|
|
|
|
const USER_ID = 6410;
|
|
const CURRENT = 64100;
|
|
const SHEET = 'https://docs.google.com/spreadsheets/d/e/2PACX-backup-test/pub?output=csv';
|
|
let cookie = '';
|
|
|
|
beforeEach(async () => {
|
|
for (const t of ['import_sources', 'accounts', 'settings', 'rate_limits', 'profiles']) {
|
|
await env.DB.prepare(`DELETE FROM ${t}`).run();
|
|
}
|
|
await env.DB.prepare('DELETE FROM users WHERE id = ?').bind(USER_ID).run();
|
|
await env.DB.batch([
|
|
env.DB.prepare(
|
|
"INSERT INTO users (id, email, auth_provider, token_version, plan) VALUES (?, 'sources-backup@example.com', 'password', 1, 'free')"
|
|
).bind(USER_ID),
|
|
env.DB.prepare("INSERT INTO profiles (id, user_id, name) VALUES (?, ?, 'Current')").bind(
|
|
CURRENT,
|
|
USER_ID
|
|
),
|
|
env.DB.prepare(
|
|
"INSERT INTO import_sources (profile_id, kind, label, config, schedule) VALUES (?, 'google_sheet', 'Old sheet', '{}', 'manual')"
|
|
).bind(CURRENT),
|
|
]);
|
|
cookie = (await sessionCookie(USER_ID, 'password', env)).split(';')[0];
|
|
});
|
|
|
|
function source(over: Record<string, unknown> = {}): Record<string, unknown> {
|
|
return {
|
|
id: 7,
|
|
profile_id: 1,
|
|
kind: 'google_sheet',
|
|
label: 'Bank ledger',
|
|
config: { url: SHEET, sheetName: 'Sheet1' },
|
|
mapping: { date: 'Date', amount: 'Amount' },
|
|
category_types: null,
|
|
default_account_id: 3,
|
|
schedule: 'manual',
|
|
last_synced_at: '2026-02-01T08:00:00.000Z',
|
|
last_cursor: null,
|
|
...over,
|
|
};
|
|
}
|
|
|
|
function backup(sources: Record<string, unknown>[]): Record<string, unknown> {
|
|
return {
|
|
version: '3.0.0',
|
|
profiles: [{ id: 1, name: 'Restored', created_at: '2026-01-01' }],
|
|
accounts: [{ id: 3, profile_id: 1, name: 'Everyday', type: 'giro', currency: 'EUR' }],
|
|
importSources: sources,
|
|
};
|
|
}
|
|
|
|
function send(method: string, path: string, payload?: unknown): Promise<Response> {
|
|
return SELF.fetch(`https://example.com${path}`, {
|
|
method,
|
|
headers: {
|
|
Cookie: cookie,
|
|
'Content-Type': 'application/json',
|
|
'X-Profile-Id': String(CURRENT),
|
|
},
|
|
body: payload === undefined ? undefined : JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
interface SourceRow {
|
|
profile_id: number;
|
|
label: string;
|
|
config: string;
|
|
mapping: string | null;
|
|
category_types: string | null;
|
|
default_account_id: number | null;
|
|
schedule: string;
|
|
last_synced_at: string | null;
|
|
}
|
|
|
|
async function stored(): Promise<SourceRow[]> {
|
|
const { results } = await env.DB.prepare(
|
|
`SELECT s.profile_id, s.label, s.config, s.mapping, s.category_types, s.default_account_id,
|
|
s.schedule, s.last_synced_at
|
|
FROM import_sources s JOIN profiles p ON p.id = s.profile_id
|
|
WHERE p.user_id = ? ORDER BY s.id`
|
|
)
|
|
.bind(USER_ID)
|
|
.all<SourceRow>();
|
|
return results;
|
|
}
|
|
|
|
async function restoredIds(): Promise<{ profile: number; account: number }> {
|
|
const profile = await env.DB.prepare('SELECT id FROM profiles WHERE user_id = ?')
|
|
.bind(USER_ID)
|
|
.first<{ id: number }>();
|
|
const account = await env.DB.prepare('SELECT id FROM accounts WHERE profile_id = ?')
|
|
.bind(profile!.id)
|
|
.first<{ id: number }>();
|
|
return { profile: profile!.id, account: account!.id };
|
|
}
|
|
|
|
describe('a backup and its import sources', () => {
|
|
it('carries each source as the list answers it', async () => {
|
|
await env.DB.prepare(
|
|
`INSERT INTO import_sources (profile_id, kind, label, config, mapping, schedule)
|
|
VALUES (?, 'google_sheet', 'Bank ledger', ?, ?, 'manual')`
|
|
)
|
|
.bind(CURRENT, JSON.stringify({ url: SHEET }), JSON.stringify({ date: 'Date' }))
|
|
.run();
|
|
const res = await send('GET', '/api/export');
|
|
expect(res.status).toBe(200);
|
|
const file = (await res.json()) as { importSources: Record<string, unknown>[] };
|
|
expect(file.importSources).toEqual([
|
|
expect.objectContaining({ label: 'Old sheet', config: {}, mapping: null }),
|
|
expect.objectContaining({
|
|
profile_id: CURRENT,
|
|
label: 'Bank ledger',
|
|
config: { url: SHEET },
|
|
mapping: { date: 'Date' },
|
|
category_types: null,
|
|
schedule: 'manual',
|
|
}),
|
|
]);
|
|
});
|
|
|
|
it('restores a source under its profile, with its default account and its sync time', async () => {
|
|
const res = await send('POST', '/api/import', backup([source()]));
|
|
expect(res.status).toBe(200);
|
|
const ids = await restoredIds();
|
|
const [restored, ...rest] = await stored();
|
|
expect(rest).toEqual([]);
|
|
expect(restored).toEqual({
|
|
profile_id: ids.profile,
|
|
label: 'Bank ledger',
|
|
config: JSON.stringify({ url: SHEET, sheetName: 'Sheet1' }),
|
|
mapping: JSON.stringify({ date: 'Date', amount: 'Amount' }),
|
|
category_types: null,
|
|
default_account_id: ids.account,
|
|
schedule: 'manual',
|
|
last_synced_at: '2026-02-01T08:00:00.000Z',
|
|
});
|
|
});
|
|
|
|
it('reads the JSON columns as text too, and drops a default account the file does not carry', async () => {
|
|
const res = await send(
|
|
'POST',
|
|
'/api/import',
|
|
backup([
|
|
source({
|
|
config: JSON.stringify({ url: SHEET }),
|
|
mapping: JSON.stringify({ date: 'Date' }),
|
|
default_account_id: 99,
|
|
}),
|
|
])
|
|
);
|
|
expect(res.status).toBe(200);
|
|
expect(await stored()).toEqual([
|
|
expect.objectContaining({
|
|
config: JSON.stringify({ url: SHEET }),
|
|
mapping: JSON.stringify({ date: 'Date' }),
|
|
default_account_id: null,
|
|
}),
|
|
]);
|
|
});
|
|
|
|
it('puts a daily source back on manual when the plan has no automated imports', async () => {
|
|
const res = await send('POST', '/api/import', backup([source({ schedule: 'daily' })]));
|
|
expect(res.status).toBe(200);
|
|
expect((await stored()).map((s) => s.schedule)).toEqual(['manual']);
|
|
});
|
|
|
|
it('keeps a daily source daily when the plan has automated imports', async () => {
|
|
await env.DB.prepare("UPDATE users SET plan = 'advanced' WHERE id = ?").bind(USER_ID).run();
|
|
const res = await send('POST', '/api/import', backup([source({ schedule: 'daily' })]));
|
|
expect(res.status).toBe(200);
|
|
expect((await stored()).map((s) => s.schedule)).toEqual(['daily']);
|
|
});
|
|
|
|
it('restores a kind or a schedule there is no such thing as as a sheet on manual', async () => {
|
|
const res = await send(
|
|
'POST',
|
|
'/api/import',
|
|
backup([source({ kind: 'ftp', label: 'Odd kind' }), source({ id: 8, schedule: 'hourly' })])
|
|
);
|
|
expect(res.status).toBe(200);
|
|
const { results } = await env.DB.prepare(
|
|
`SELECT s.label, s.kind, s.schedule FROM import_sources s JOIN profiles p ON p.id = s.profile_id
|
|
WHERE p.user_id = ? ORDER BY s.id`
|
|
)
|
|
.bind(USER_ID)
|
|
.all();
|
|
expect(results).toEqual([
|
|
{ label: 'Odd kind', kind: 'google_sheet', schedule: 'manual' },
|
|
{ label: 'Bank ledger', kind: 'google_sheet', schedule: 'manual' },
|
|
]);
|
|
});
|
|
});
|