Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Tests: the password server tests let the server pick its own port, like every other server test since #249 (#118)

Duarte Santos7e80eb3a5289
diff --git a/api/test/server-password.test.js b/api/test/server-password.test.js
index 316990452..0039d8b9a 100644
--- a/api/test/server-password.test.js
+++ b/api/test/server-password.test.js
@@ -5,7 +5,6 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import crypto from 'node:crypto';
-import net from 'node:net';
import http from 'node:http';
import fs from 'node:fs';
import os from 'node:os';
@@ -13,6 +12,7 @@ import path from 'node:path';
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { hashPassword, hashResetCode } from '../password.js';
+import { boundPort } from './helpers.mjs';
const API = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
const SECRET = crypto.randomBytes(32).toString('hex');
@@ -23,9 +23,6 @@ const mintSession = (uid, sv = 0) => {
const payload = `${uid}:${Date.now() + 86400000}:${sv}`;
return payload + '.' + crypto.createHmac('sha256', SECRET).update(payload).digest('base64url');
};
-const freePort = () => new Promise(r => {
- const s = net.createServer(); s.listen(0, '127.0.0.1', () => { const p = s.address().port; s.close(() => r(p)); });
-});
// A passkey in software: a P-256 key whose public half goes into db.json as the COSE key the
// server stores, and whose private half signs assertions the way an authenticator would.
@@ -70,24 +67,20 @@ async function startServer(t, { env = {}, users = [], creds = [], invites = [] }
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gym-pw-'));
fs.writeFileSync(path.join(dataDir, 'secret'), SECRET, { mode: 0o600 });
fs.writeFileSync(path.join(dataDir, 'db.json'), JSON.stringify({ users, creds, subs: [], invites }));
- const port = await freePort();
const child = spawn(process.execPath, ['server.js'], {
cwd: API, stdio: ['ignore', 'pipe', 'pipe'],
env: {
- ...process.env, PORT: String(port), DATA_DIR: dataDir, ORIGIN, RP_ID: 'localhost',
+ ...process.env, PORT: '0', DATA_DIR: dataDir, ORIGIN, RP_ID: 'localhost',
PASSWORD_LOGIN: '1', TRUST_PROXY: '1', INVITE_ONLY: '', ADMIN_UIDS: '', AUDIT_LOG: '1', ...env
}
});
- const h = { api: `http://127.0.0.1:${port}`, log: '', dataDir };
+ const h = { api: '', log: '', dataDir };
child.stdout.on('data', d => h.log += d);
child.stderr.on('data', d => h.log += d);
t.after(() => { child.kill('SIGKILL'); fs.rmSync(dataDir, { recursive: true, force: true }); });
- let up = false;
- for (let i = 0; i < 100 && !up; i++) {
- try { up = (await fetch(`${h.api}/api/health`)).ok; } catch { /* not up yet */ }
- if (!up) await new Promise(r => setTimeout(r, 100));
- }
- assert.ok(up, `server never came up:\n${h.log}`);
+ // The boot line carries the port the listener bound, so it is both the address and the
+ // readiness signal — see boundPort in helpers.mjs for why the test does not pick one.
+ h.api = `http://127.0.0.1:${await boundPort(child, () => h.log)}`;
// Every request looks like the app talking to its own backend unless a test says otherwise.
h.req = async (method, p, { body, cookie, ip = '198.51.100.1', headers = {} } = {}) => {
const r = await fetch(`${h.api}${p}`, {