Files
KomediruzeckiandGitHub ceb43bb3a8 feat(forms): profiles, Settings and import say what is wrong at the field (slice 4b of 6) (#611)
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.
2026-10-09 21:18:26 +02:00

155 lines
5.7 KiB
TypeScript

/**
* POST /api/import/upload reads a file as local-first does (shared/importUpload.ts), and refuses
* it in the same words, at the `file` field the Import page sends it in.
*
* Before, the Worker said "No file uploaded" and "File too large (max 10MB)" where local-first said
* "File too large (max 10 MB)", neither named the field, and a file that is not a workbook at all
* (a broken .xlsx) was a 500. A workbook's cells came back as they were stored, so an amount was
* a number and a date its serial number (46082), where the page and the import read text: the
* preview stopped at the first number. Now every cell is text and a date cell is its day. The
* twin is frontend/src/core/storage/__tests__/importUpload.test.ts.
*/
import { env, SELF } from 'cloudflare:test';
import * as XLSX from 'xlsx';
import { beforeEach, describe, expect, it } from 'vitest';
import { IMPORT_UPLOAD_MESSAGES as M } from '../../shared/importUpload';
import { sessionCookie } from './helpers/session';
let cookie = '';
beforeEach(async () => {
for (const table of ['rate_limits', 'profiles', 'users']) {
await env.DB.prepare(`DELETE FROM ${table}`).run();
}
await env.DB.batch([
env.DB.prepare(
"INSERT INTO users (id, email, auth_provider, token_version, plan) VALUES (98, 'upload@example.com', 'password', 1, 'basic')"
),
env.DB.prepare("INSERT INTO profiles (id, user_id, name) VALUES (980, 98, 'Main')"),
]);
cookie = (await sessionCookie(98, 'password', env)).split(';')[0];
});
async function upload(
file: File | null,
sheetName?: string
): Promise<{ status: number; body: unknown }> {
const form = new FormData();
if (file) form.append('file', file);
if (sheetName) form.append('sheetName', sheetName);
const res = await SELF.fetch('https://example.com/api/import/upload', {
method: 'POST',
headers: { Cookie: cookie },
body: form,
});
return { status: res.status, body: await res.json() };
}
function workbook(): Uint8Array<ArrayBuffer> {
const book = XLSX.utils.book_new();
const march = XLSX.utils.aoa_to_sheet([
['Date', 'Description', 'Amount'],
['2026-03-01', 'Rent', -900.5],
]);
// A date as Excel keeps one: the day's serial number, shown in a date format.
march.A2 = { t: 'n', v: 46082, z: 'yyyy-mm-dd' };
XLSX.utils.book_append_sheet(book, march, 'March');
XLSX.utils.book_append_sheet(
book,
XLSX.utils.aoa_to_sheet([
['Datum', 'Omschrijving', 'Bedrag'],
['2026-04-01', 'Huur', -900],
]),
'April'
);
return new Uint8Array(XLSX.write(book, { type: 'array', bookType: 'xlsx' }) as ArrayBuffer);
}
describe('POST /api/import/upload', () => {
it('refuses no file, and a file over 10 MB, at the file', async () => {
expect(await upload(null)).toEqual({
status: 400,
body: { error: M.file, fields: { file: M.file } },
});
const big = new File([new Uint8Array(11 * 1024 * 1024)], 'big.csv', { type: 'text/csv' });
expect(await upload(big)).toEqual({
status: 413,
body: { error: M.tooLarge, fields: { file: M.tooLarge } },
});
});
it('refuses a workbook it cannot open at the file, rather than failing', async () => {
// The first bytes of a zip, as an .xlsx file starts, and then nothing a zip has.
const broken = new Uint8Array([0x50, 0x4b, 0x03, 0x04, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0]);
expect(await upload(new File([broken], 'statement.xlsx'))).toEqual({
status: 400,
body: { error: M.unreadable, fields: { file: M.unreadable } },
});
});
it("answers a workbook's cells as text, a date cell as its day", async () => {
// As numbers, the preview's duplicate check called trim() on them and stopped, and the
// import read a date's serial number as no date at all.
expect(await upload(new File([workbook()], 'statement.xlsx'))).toEqual({
status: 200,
body: {
headers: ['Date', 'Description', 'Amount'],
rows: [['2026-03-01', 'Rent', '-900.5']],
selectedSheet: 'March',
sheetNames: ['March', 'April'],
},
});
});
it('answers the sheet an upload names, with every sheet listed', async () => {
expect(await upload(new File([workbook()], 'statement.xlsx'), 'April')).toEqual({
status: 200,
body: {
headers: ['Datum', 'Omschrijving', 'Bedrag'],
rows: [['2026-04-01', 'Huur', '-900']],
selectedSheet: 'April',
sheetNames: ['March', 'April'],
},
});
});
it('refuses a file with no header row at the file', async () => {
const empty = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(empty, XLSX.utils.aoa_to_sheet([]), 'Empty');
const bytes = new Uint8Array(XLSX.write(empty, { type: 'array', bookType: 'xlsx' }));
for (const file of [
new File(['\n \n'], 'empty.csv', { type: 'text/csv' }),
new File([bytes], 'empty.xlsx'),
]) {
expect(await upload(file)).toEqual({
status: 400,
body: { error: M.empty, fields: { file: M.empty } },
});
}
});
it('refuses a workbook cut short at the file', async () => {
const whole = workbook();
expect(await upload(new File([whole.slice(0, whole.length - 30)], 'statement.xlsx'))).toEqual({
status: 400,
body: { error: M.unreadable, fields: { file: M.unreadable } },
});
});
it('answers a number cell as the import reads it: 7.534 as the number it is', async () => {
const book = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(
book,
XLSX.utils.aoa_to_sheet([
['Date', 'Amount'],
['2026-03-01', 7.534],
]),
'March'
);
const bytes = new Uint8Array(XLSX.write(book, { type: 'array', bookType: 'xlsx' }));
expect((await upload(new File([bytes], 'rates.xlsx'))).body).toMatchObject({
rows: [['2026-03-01', '7.5340']],
});
});
});