2026-05-30 20:04:50 -05:00
|
|
|
const test = require('node:test');
|
|
|
|
|
const assert = require('node:assert/strict');
|
|
|
|
|
const fs = require('node:fs');
|
|
|
|
|
const os = require('node:os');
|
|
|
|
|
const path = require('node:path');
|
|
|
|
|
|
|
|
|
|
const dbPath = path.join(os.tmpdir(), `bill-tracker-category-reorder-test-${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-05-30 20:04:50 -05:00
|
|
|
|
|
|
|
|
function createUser(db, suffix) {
|
2026-07-06 14:23:53 -05:00
|
|
|
return db
|
|
|
|
|
.prepare(
|
|
|
|
|
`
|
2026-05-30 20:04:50 -05:00
|
|
|
INSERT INTO users (username, password_hash, role, active, email, created_at, updated_at)
|
|
|
|
|
VALUES (?, 'x', 'user', 1, ?, datetime('now'), datetime('now'))
|
2026-07-06 14:23:53 -05:00
|
|
|
`,
|
|
|
|
|
)
|
|
|
|
|
.run(`category-reorder-${suffix}`, `category-reorder-${suffix}@local`).lastInsertRowid;
|
2026-05-30 20:04:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createCategory(db, userId, name) {
|
2026-07-06 14:23:53 -05:00
|
|
|
return db
|
|
|
|
|
.prepare(
|
|
|
|
|
`
|
2026-05-30 20:04:50 -05:00
|
|
|
INSERT INTO categories (user_id, name)
|
|
|
|
|
VALUES (?, ?)
|
2026-07-06 14:23:53 -05:00
|
|
|
`,
|
|
|
|
|
)
|
|
|
|
|
.run(userId, name).lastInsertRowid;
|
2026-05-30 20:04:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function callCategoriesRoute(routePath, method, { userId, params = {}, body = {} }) {
|
2026-07-06 11:26:50 -05:00
|
|
|
const categoriesRouter = require('../routes/categories.cts');
|
2026-07-06 14:23:53 -05:00
|
|
|
const layer = categoriesRouter.stack.find(
|
|
|
|
|
(item) => item.route?.path === routePath && item.route.methods[method],
|
|
|
|
|
);
|
2026-05-30 20:04:50 -05:00
|
|
|
assert.ok(layer, `route ${method.toUpperCase()} ${routePath} should exist`);
|
|
|
|
|
const handler = layer.route.stack[0].handle;
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const req = {
|
|
|
|
|
body,
|
|
|
|
|
params,
|
|
|
|
|
user: { id: userId, role: 'user' },
|
|
|
|
|
};
|
|
|
|
|
const res = {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
status(code) {
|
|
|
|
|
this.statusCode = code;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
|
|
|
|
json(data) {
|
|
|
|
|
resolve({ status: this.statusCode, data });
|
|
|
|
|
},
|
|
|
|
|
};
|
2026-07-06 14:46:32 -05:00
|
|
|
// Simulate the terminal error handler: a thrown ApiError becomes a formatted
|
|
|
|
|
// response (mirrors utils/apiError formatError + server.cts).
|
|
|
|
|
const onError = (err) => {
|
|
|
|
|
resolve({
|
|
|
|
|
status: err.status || 500,
|
|
|
|
|
data: {
|
|
|
|
|
error: err.code || 'INTERNAL_ERROR',
|
|
|
|
|
message: err.message,
|
|
|
|
|
code: err.code || 'INTERNAL_ERROR',
|
|
|
|
|
field: err.field ?? null,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
2026-05-30 20:04:50 -05:00
|
|
|
try {
|
2026-07-06 14:46:32 -05:00
|
|
|
const maybe = handler(req, res);
|
|
|
|
|
if (maybe && typeof maybe.then === 'function') maybe.catch(onError);
|
2026-05-30 20:04:50 -05:00
|
|
|
} catch (err) {
|
2026-07-06 14:46:32 -05:00
|
|
|
onError(err);
|
2026-05-30 20:04:50 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test.after(() => {
|
|
|
|
|
closeDb();
|
|
|
|
|
for (const suffix of ['', '-wal', '-shm']) {
|
|
|
|
|
fs.rmSync(`${dbPath}${suffix}`, { force: true });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('category reorder endpoint persists category order for the current user', async () => {
|
|
|
|
|
const db = getDb();
|
|
|
|
|
const userId = createUser(db, 'owner');
|
|
|
|
|
const food = createCategory(db, userId, 'Food');
|
|
|
|
|
const loans = createCategory(db, userId, 'Loans');
|
|
|
|
|
const utilities = createCategory(db, userId, 'Utilities');
|
|
|
|
|
|
|
|
|
|
const response = await callCategoriesRoute('/reorder', 'put', {
|
|
|
|
|
userId,
|
|
|
|
|
body: {
|
|
|
|
|
[utilities]: 0,
|
|
|
|
|
[food]: 1,
|
|
|
|
|
[loans]: 2,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert.equal(response.status, 200);
|
|
|
|
|
assert.equal(response.data.success, true);
|
|
|
|
|
|
|
|
|
|
const ordered = await callCategoriesRoute('/', 'get', { userId });
|
|
|
|
|
assert.deepEqual(
|
2026-07-06 14:23:53 -05:00
|
|
|
ordered.data.filter((cat) => [food, loans, utilities].includes(cat.id)).map((cat) => cat.id),
|
2026-05-30 20:04:50 -05:00
|
|
|
[utilities, food, loans],
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('category reorder rejects categories outside the current user scope', async () => {
|
|
|
|
|
const db = getDb();
|
|
|
|
|
const ownerId = createUser(db, 'scoped-owner');
|
|
|
|
|
const otherId = createUser(db, 'scoped-other');
|
|
|
|
|
const ownerCategory = createCategory(db, ownerId, 'Owner Category');
|
|
|
|
|
const otherCategory = createCategory(db, otherId, 'Other Category');
|
|
|
|
|
|
|
|
|
|
const response = await callCategoriesRoute('/reorder', 'put', {
|
|
|
|
|
userId: ownerId,
|
|
|
|
|
body: {
|
|
|
|
|
[ownerCategory]: 0,
|
|
|
|
|
[otherCategory]: 1,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert.equal(response.status, 404);
|
|
|
|
|
});
|