Spaces:
Running
Running
File size: 2,023 Bytes
0545b15 fa363c5 0545b15 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import { describe, expect, it } from 'vitest';
import { buildSettlementBody, readSettlementForm, settlementFormFromQuery } from '../../../src/lib/server/forms/settlement-form';
const fd = (o: Record<string, string>) => {
const f = new FormData();
for (const [k, v] of Object.entries(o)) f.append(k, v);
return f;
};
describe('settlement form', () => {
it('reads and builds', () => {
const values = readSettlementForm(fd({ from: 'u_b', to: 'u_a', amount: '12.5', date: '2026-08-02', note: ' thanks ' }));
expect(buildSettlementBody(values, ['u_a', 'u_b'])).toEqual({ ok: true, body: { from: 'u_b', to: 'u_a', amountCents: 1250, date: '2026-08-02', note: 'thanks' } });
});
it('validates', () => {
const good = readSettlementForm(fd({ from: 'u_b', to: 'u_a', amount: '5', date: '2026-08-02' }));
const errs = (v: Partial<typeof good>) => {
const r = buildSettlementBody({ ...good, ...v }, ['u_a', 'u_b']);
return r.ok ? {} : r.errors;
};
expect(errs({ to: 'u_b' })).toHaveProperty('to');
expect(errs({ from: 'u_x' })).toHaveProperty('from');
expect(errs({ amount: '-1' })).toHaveProperty('amount');
expect(errs({ date: 'nope' })).toHaveProperty('date');
});
it('prefills from query params with a safe fallback', () => {
const q = new URLSearchParams({ from: 'u_b', to: 'u_a', amount: '90.00' });
expect(settlementFormFromQuery(q, 'u_b', '2026-08-29')).toEqual({ from: 'u_b', to: 'u_a', amount: '90.00', date: '2026-08-29', note: '' });
expect(settlementFormFromQuery(new URLSearchParams(), 'u_b', '2026-08-29')).toEqual({ from: 'u_b', to: '', amount: '', date: '2026-08-29', note: '' });
});
it('trims and truncates query-derived values', () => {
const q = new URLSearchParams({ from: ' u_b ', to: ' u_a ', amount: ` ${'9'.repeat(200)} ` });
const values = settlementFormFromQuery(q, 'u_b', '2026-08-29');
expect(values.from).toBe('u_b');
expect(values.to).toBe('u_a');
expect(values.amount).toBe('9'.repeat(64));
expect(values.amount.length).toBe(64);
});
});
|