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.
96 lines
3.5 KiB
TypeScript
96 lines
3.5 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import * as XLSX from 'xlsx';
|
|
import { parseAttachment } from '../src/import-email';
|
|
|
|
// Email-in (Ask 3) turns a forwarded bank-statement attachment into { headers, rows } that feed
|
|
// the shared executeImport. parseAttachment is that step; the auth/dedup/insert around it are
|
|
// covered by the secret-tag check and the existing /api/import/execute suite.
|
|
|
|
const XLSX_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
|
|
/** A one-sheet workbook of these rows, after an empty sheet when `emptyFirst`. */
|
|
function workbook(rows: unknown[][], emptyFirst = false): Uint8Array {
|
|
const wb = XLSX.utils.book_new();
|
|
if (emptyFirst) XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet([]), 'Notes');
|
|
XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet(rows), 'Statement');
|
|
return new Uint8Array(XLSX.write(wb, { type: 'array', bookType: 'xlsx' }));
|
|
}
|
|
|
|
describe('parseAttachment', () => {
|
|
it('parses a CSV attachment into headers + rows', () => {
|
|
const csv = 'Date,Description,Amount\n2026-07-01,Coffee,-4.50\n2026-07-02,Salary,1000\n';
|
|
const table = parseAttachment('statement.csv', 'text/csv', new TextEncoder().encode(csv));
|
|
expect(table).not.toBeNull();
|
|
expect(table!.headers).toEqual(['Date', 'Description', 'Amount']);
|
|
expect(table!.rows).toHaveLength(2);
|
|
expect(table!.rows[0]).toEqual(['2026-07-01', 'Coffee', '-4.50']);
|
|
});
|
|
|
|
it('parses an XLSX attachment into headers + rows', () => {
|
|
const ws = XLSX.utils.aoa_to_sheet([
|
|
['Date', 'Description', 'Amount'],
|
|
['2026-07-01', 'Coffee', -4.5],
|
|
['2026-07-02', 'Salary', 1000],
|
|
]);
|
|
const wb = XLSX.utils.book_new();
|
|
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
|
|
const bytes = new Uint8Array(XLSX.write(wb, { type: 'array', bookType: 'xlsx' }));
|
|
const table = parseAttachment(
|
|
'statement.xlsx',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
bytes
|
|
);
|
|
expect(table).not.toBeNull();
|
|
expect(table!.headers).toEqual(['Date', 'Description', 'Amount']);
|
|
expect(table!.rows).toHaveLength(2);
|
|
expect(table!.rows[0][0]).toBe('2026-07-01');
|
|
expect(table!.rows[1][1]).toBe('Salary');
|
|
});
|
|
|
|
it('reads a date cell as its day, as an uploaded file is read', () => {
|
|
const table = parseAttachment(
|
|
'statement.xlsx',
|
|
XLSX_TYPE,
|
|
workbook([
|
|
['Date', 'Description', 'Amount'],
|
|
[new Date(Date.UTC(2026, 6, 1)), 'Coffee', -4.5],
|
|
])
|
|
);
|
|
expect(table?.rows).toEqual([['2026-07-01', 'Coffee', '-4.5']]);
|
|
});
|
|
|
|
it('reads the first sheet that has a header row', () => {
|
|
const table = parseAttachment(
|
|
'statement.xlsx',
|
|
XLSX_TYPE,
|
|
workbook(
|
|
[
|
|
['Date', 'Description', 'Amount'],
|
|
['2026-07-02', 'Salary', 1000],
|
|
],
|
|
true
|
|
)
|
|
);
|
|
expect(table).toEqual({
|
|
headers: ['Date', 'Description', 'Amount'],
|
|
rows: [['2026-07-02', 'Salary', '1000']],
|
|
});
|
|
});
|
|
|
|
it('returns null for a workbook that is not a complete file', () => {
|
|
const whole = workbook([
|
|
['Date', 'Description', 'Amount'],
|
|
['2026-07-01', 'Coffee', -4.5],
|
|
]);
|
|
expect(
|
|
parseAttachment('statement.xlsx', XLSX_TYPE, whole.slice(0, whole.length - 30))
|
|
).toBeNull();
|
|
});
|
|
|
|
it('returns null for a non-tabular attachment (e.g. a PDF)', () => {
|
|
expect(
|
|
parseAttachment('statement.pdf', 'application/pdf', new Uint8Array([1, 2, 3]))
|
|
).toBeNull();
|
|
});
|
|
});
|