2026-07-03 15:15:36 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Batch 4: richer export — full JSON assembly (money in dollars) and the payments
|
|
|
|
|
// export's date-range vs year filtering.
|
|
|
|
|
const test = require('node:test');
|
|
|
|
|
const assert = require('node:assert/strict');
|
|
|
|
|
const os = require('node:os');
|
|
|
|
|
const path = require('node:path');
|
|
|
|
|
const fs = require('node:fs');
|
|
|
|
|
|
|
|
|
|
const dbPath = path.join(os.tmpdir(), `bill-tracker-export-richer-${process.pid}.sqlite`);
|
|
|
|
|
process.env.DB_PATH = dbPath;
|
|
|
|
|
|
refactor(server): migrate middleware, workers, and db layer to TypeScript (.cts)
- middleware/ (5): requireAuth, securityHeaders, errorFormatter, csrf,
rateLimiter — fully typed against the shared http Req/Res/Next types.
- workers/dailyWorker — fully typed.
- db/ (4): database, subscriptionCatalogSeed, migrations/versionedMigrations,
migrations/legacyReconcileMigrations — large dynamic schema/migration infra,
converted with @ts-nocheck (typing deferred). All requires updated to .cts.
Behavior-preserving. Verified: typecheck:server 0, check:server 0, suite 226/226.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-06 11:34:08 -05:00
|
|
|
const { getDb, closeDb } = require('../db/database.cts');
|
2026-07-06 11:26:50 -05:00
|
|
|
const exportRouter = require('../routes/export.cts');
|
2026-07-03 15:15:36 -05:00
|
|
|
const { getUserExportData } = exportRouter;
|
|
|
|
|
|
|
|
|
|
let userId, billId;
|
|
|
|
|
|
|
|
|
|
function callExport(query) {
|
2026-07-06 14:23:53 -05:00
|
|
|
const layer = exportRouter.stack.find((l) => l.route?.path === '/' && l.route.methods.get);
|
2026-07-03 15:15:36 -05:00
|
|
|
const handler = layer.route.stack[0].handle;
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const headers = {};
|
|
|
|
|
const req = { query, user: { id: userId, role: 'user' } };
|
|
|
|
|
const res = {
|
|
|
|
|
statusCode: 200,
|
2026-07-06 14:23:53 -05:00
|
|
|
setHeader(k, v) {
|
|
|
|
|
headers[k] = v;
|
|
|
|
|
},
|
|
|
|
|
status(c) {
|
|
|
|
|
this.statusCode = c;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
|
|
|
|
json(d) {
|
|
|
|
|
resolve({ status: this.statusCode, headers, json: d });
|
|
|
|
|
},
|
|
|
|
|
send(body) {
|
|
|
|
|
resolve({ status: this.statusCode, headers, body });
|
|
|
|
|
},
|
2026-07-03 15:15:36 -05:00
|
|
|
};
|
|
|
|
|
handler(req, res);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test.before(() => {
|
|
|
|
|
const db = getDb();
|
2026-07-06 14:23:53 -05:00
|
|
|
userId = db
|
|
|
|
|
.prepare(
|
|
|
|
|
"INSERT INTO users (username, password_hash, role, active) VALUES ('export-user','x','user',1)",
|
|
|
|
|
)
|
|
|
|
|
.run().lastInsertRowid;
|
|
|
|
|
billId = db
|
|
|
|
|
.prepare(
|
|
|
|
|
"INSERT INTO bills (user_id, name, due_day, expected_amount, active) VALUES (?, 'Rent', 1, 120000, 1)",
|
|
|
|
|
)
|
|
|
|
|
.run(userId).lastInsertRowid;
|
|
|
|
|
const pay = db.prepare(
|
|
|
|
|
"INSERT INTO payments (bill_id, amount, paid_date, payment_source) VALUES (?, ?, ?, 'manual')",
|
|
|
|
|
);
|
2026-07-03 15:15:36 -05:00
|
|
|
pay.run(billId, 8500, '2025-06-15'); // prior year
|
|
|
|
|
pay.run(billId, 9000, '2026-01-10'); // in range
|
|
|
|
|
pay.run(billId, 9500, '2026-07-20'); // out of range, same year
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test.after(() => {
|
|
|
|
|
closeDb();
|
2026-07-06 14:23:53 -05:00
|
|
|
for (const s of ['', '-wal', '-shm']) {
|
|
|
|
|
try {
|
|
|
|
|
fs.unlinkSync(dbPath + s);
|
|
|
|
|
} catch {}
|
|
|
|
|
}
|
2026-07-03 15:15:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('getUserExportData assembles user data with money in dollars', () => {
|
|
|
|
|
const data = getUserExportData(userId);
|
|
|
|
|
assert.equal(data.bills.length, 1);
|
|
|
|
|
assert.equal(data.bills[0].expected_amount, 1200, 'expected_amount is dollars (fromCents)');
|
|
|
|
|
assert.equal(data.payments.length, 3);
|
2026-07-06 14:23:53 -05:00
|
|
|
const amounts = data.payments.map((p) => p.amount).sort((a, b) => a - b);
|
2026-07-03 15:15:36 -05:00
|
|
|
assert.deepEqual(amounts, [85, 90, 95], 'payment amounts in dollars');
|
|
|
|
|
assert.equal(data.metadata.counts.payments, 3);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('payments export by year returns that year only', async () => {
|
|
|
|
|
const { status, json } = await callExport({ year: '2026', format: 'json' });
|
|
|
|
|
assert.equal(status, 200);
|
|
|
|
|
assert.equal(json.count, 2, 'both 2026 payments');
|
|
|
|
|
assert.equal(json.range, '2026');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('payments export by date range filters to the range', async () => {
|
2026-07-06 14:23:53 -05:00
|
|
|
const { status, json } = await callExport({
|
|
|
|
|
from: '2026-01-01',
|
|
|
|
|
to: '2026-06-30',
|
|
|
|
|
format: 'json',
|
|
|
|
|
});
|
2026-07-03 15:15:36 -05:00
|
|
|
assert.equal(status, 200);
|
|
|
|
|
assert.equal(json.count, 1, 'only the Jan 10 payment');
|
|
|
|
|
assert.equal(json.payments[0].paid_amount, 90);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('CSV export sets a filename derived from the range', async () => {
|
2026-07-06 14:23:53 -05:00
|
|
|
const { status, headers, body } = await callExport({
|
|
|
|
|
from: '2026-01-01',
|
|
|
|
|
to: '2026-12-31',
|
|
|
|
|
format: 'csv',
|
|
|
|
|
});
|
2026-07-03 15:15:36 -05:00
|
|
|
assert.equal(status, 200);
|
|
|
|
|
assert.match(headers['Content-Disposition'], /bills-2026-01-01_to_2026-12-31\.csv/);
|
|
|
|
|
assert.match(body, /^Date,Bill,Category/);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('invalid date range is rejected', async () => {
|
|
|
|
|
const bad = await callExport({ from: '2026-12-31', to: '2026-01-01' });
|
|
|
|
|
assert.equal(bad.status, 400);
|
|
|
|
|
const badFmt = await callExport({ from: 'nope', to: '2026-01-01' });
|
|
|
|
|
assert.equal(badFmt.status, 400);
|
|
|
|
|
});
|