feat(server): healthz, testable app, local dev script (P0-3)

This commit is contained in:
Benjamin Oezcan
2026-11-19 01:40:31 +01:00
parent 35348324d6
commit fa68ef620d
8 changed files with 1465 additions and 16 deletions

View File

@@ -0,0 +1,39 @@
'use strict';
const { test, before, after } = require('node:test');
const assert = require('node:assert/strict');
const { startServer } = require('./helpers');
let S;
before(async () => { S = await startServer(); });
after(() => S.close());
test('GET /healthz reports ok + version', async () => {
const r = await S.json('GET', '/healthz');
assert.equal(r.status, 200);
assert.equal(r.body.ok, true);
assert.equal(r.body.version, require('../package.json').version);
assert.equal(typeof r.body.uptime, 'number');
});
test('register -> login -> refresh rotates the refresh token', async () => {
const reg = await S.json('POST', '/api/auth/register', { email: 'a@test.dev', pass: 'secret123', name: 'Alice' });
assert.equal(reg.status, 200);
assert.ok(reg.body.idToken && reg.body.refreshToken && reg.body.uid);
const login = await S.json('POST', '/api/auth/login', { email: 'A@test.dev ', pass: 'secret123' });
assert.equal(login.status, 200);
assert.equal(login.body.uid, reg.body.uid);
const me = await S.json('GET', '/api/me', undefined, login.body.idToken);
assert.equal(me.body.uid, reg.body.uid);
const r1 = await S.json('POST', '/api/auth/refresh', { refreshToken: login.body.refreshToken });
assert.equal(r1.status, 200);
const r2 = await S.json('POST', '/api/auth/refresh', { refreshToken: login.body.refreshToken });
assert.equal(r2.status, 401, 'an already used refresh token is rejected');
});
test('wrong password is rejected', async () => {
const r = await S.json('POST', '/api/auth/login', { email: 'a@test.dev', pass: 'nope-nope' });
assert.equal(r.status, 400);
});

23
server/test/helpers.js Normal file
View File

@@ -0,0 +1,23 @@
/* Test helpers: start the real server on a random port with an in-memory database. */
'use strict';
process.env.NODE_ENV = 'test';
process.env.DB_FILE = ':memory:';
delete process.env.JWT_SECRET;
const { listen } = require('../app');
async function startServer() {
const srv = await listen(0);
const base = 'http://127.0.0.1:' + srv.address().port;
const json = async (method, path, body, token) => {
const res = await fetch(base + path, {
method,
headers: Object.assign({ 'Content-Type': 'application/json' }, token ? { Authorization: 'Bearer ' + token } : {}),
body: body === undefined ? undefined : JSON.stringify(body),
});
return { status: res.status, body: await res.json().catch(() => null) };
};
return { base, json, close: () => new Promise(r => { srv.closeAllConnections(); srv.close(r); }) };
}
module.exports = { startServer };