feat(server): plan 3i groundwork — AuditLogRepository's reserved methods, the demo connection-swap spike, repository-fitness confirmations

This commit is contained in:
jubnl
2026-09-24 00:56:52 +02:00
parent a5665b5d2d
commit 14721e9658
3 changed files with 293 additions and 4 deletions
@@ -75,4 +75,63 @@ describe('AuditLogRepository', () => {
const auditRow = testDb.prepare('SELECT action FROM audit_log').get() as { action: string };
expect(auditRow.action).toBe('trip.create');
});
// ── Plan 3i Task 0 — listPage/count (AD22/AD23) ────────────────────────────
/** Byte-for-byte AD22 (`admin.service.ts#getAuditLog`). */
const LEGACY_PAGE_SQL = `
SELECT a.id, a.created_at, a.user_id, u.username, u.email as user_email, a.action, a.resource, a.details, a.ip
FROM audit_log a
LEFT JOIN users u ON u.id = a.user_id
ORDER BY a.id DESC
LIMIT ? OFFSET ?
`;
it('AUDITREPO-007: listPage matches the legacy joined SELECT exactly for a known user, a deleted (FK SET NULL) user, and a null user_id row', async () => {
const { user: alice } = createUser(testDb, { username: 'alice', email: 'alice@test.example.com' });
const { user: bob } = createUser(testDb, { username: 'bob', email: 'bob@test.example.com' });
await auditLog.insertEntry({ user_id: alice.id, action: 'trip.create', resource: 'trip', details: '{"title":"Rome"}', ip: '1.1.1.1' });
await auditLog.insertEntry({ user_id: bob.id, action: 'trip.update', resource: 'trip', details: null, ip: '2.2.2.2' });
await auditLog.insertEntry({ user_id: null, action: 'user.login', resource: null, details: null, ip: null });
// bob is deleted after the fact; `audit_log.user_id REFERENCES users(id)
// ON DELETE SET NULL` fires — this row's own user_id becomes NULL, the
// same shape a NULL-user_id row has, reached a different way.
testDb.prepare('DELETE FROM users WHERE id = ?').run(bob.id);
const legacy = testDb.prepare(LEGACY_PAGE_SQL).all(10, 0);
const result = await auditLog.listPage(10, 0);
expect(result).toEqual(legacy);
// The deleted user's row and the originally-null row both keep their
// place — a LEFT JOIN never drops the row.
expect(result).toHaveLength(3);
expect(result.filter((r) => r.username === null && r.user_email === null)).toHaveLength(2);
// The known user's row still carries its username/email.
const aliceRow = result.find((r) => r.user_id === alice.id);
expect(aliceRow?.username).toBe('alice');
expect(aliceRow?.user_email).toBe('alice@test.example.com');
});
it('AUDITREPO-008: listPage honors LIMIT/OFFSET and ORDER BY a.id DESC, matching the legacy statement', async () => {
const { user } = createUser(testDb);
for (let i = 0; i < 5; i++) {
await auditLog.insertEntry({ user_id: user.id, action: `action.${i}`, resource: null, details: null, ip: null });
}
const legacyPage = testDb.prepare(LEGACY_PAGE_SQL).all(2, 1);
const result = await auditLog.listPage(2, 1);
expect(result).toEqual(legacyPage);
expect(result).toHaveLength(2);
});
it('AUDITREPO-009: count matches the legacy COUNT(*) over the whole table', async () => {
const { user } = createUser(testDb);
await auditLog.insertEntry({ user_id: user.id, action: 'a', resource: null, details: null, ip: null });
await auditLog.insertEntry({ user_id: null, action: 'b', resource: null, details: null, ip: null });
const legacyCount = (testDb.prepare('SELECT COUNT(*) as c FROM audit_log').get() as { c: number }).c;
const result = await auditLog.count();
expect(result).toBe(legacyCount);
expect(result).toBe(2);
});
});