96 lines
3.2 KiB
JavaScript
96 lines
3.2 KiB
JavaScript
// Generated, do not edit. sha256:832fcfe26d54d7a88bf8a52d6e8c9ca907eb53d0298d2cb26f675f06f2a31772
|
|
import { describe, it, before, after } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { spawn } from 'node:child_process';
|
|
import { createServer } from 'node:net';
|
|
import { mkdtemp, rm, mkdir } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const WORTEL = join(fileURLToPath(import.meta.url), '..', '..', '..');
|
|
const SUPERVISOR = join(WORTEL, 'server', 'supervisor.mjs');
|
|
|
|
let dir, proc, poort, basis;
|
|
|
|
const vrijePoort = () => new Promise((klaar, fout) => {
|
|
const s = createServer();
|
|
s.on('error', fout);
|
|
s.listen(0, '127.0.0.1', () => {
|
|
const p = s.address().port;
|
|
s.close(() => klaar(p));
|
|
});
|
|
});
|
|
|
|
const wacht = (ms) => new Promise(klaar => setTimeout(klaar, ms));
|
|
|
|
async function wachtOp(verwacht, timeoutMs = 20_000) {
|
|
const eind = Date.now() + timeoutMs;
|
|
while (Date.now() < eind) {
|
|
let leeft = false;
|
|
try {
|
|
const r = await fetch(`${basis}/api/health`);
|
|
leeft = r.ok;
|
|
} catch { leeft = false; }
|
|
if (leeft === verwacht) return true;
|
|
await wacht(100);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const haal = async (pad) => (await fetch(`${basis}${pad}`)).json();
|
|
const post = async (pad, body) => (await fetch(`${basis}${pad}`, {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
})).json();
|
|
|
|
before(async () => {
|
|
dir = await mkdtemp(join(tmpdir(), 'assetapp-supervisor-'));
|
|
poort = await vrijePoort();
|
|
basis = `http://127.0.0.1:${poort}`;
|
|
proc = spawn(process.execPath, [SUPERVISOR], {
|
|
env: { ...process.env, ASSETAPP_DATA_DIR: dir, PORT: String(poort), ASSETAPP_OPEN: '' },
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
});
|
|
assert.ok(await wachtOp(true), 'de server kwam niet op');
|
|
});
|
|
|
|
after(async () => {
|
|
if (proc && !proc.killed) proc.kill('SIGKILL');
|
|
await wacht(200);
|
|
await rm(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
describe('supervisor: wisselen is een herstart', () => {
|
|
it('komt na een wissel terug met de ándere portefeuille', async () => {
|
|
const voor = await haal('/api/portfolios');
|
|
assert.equal(voor.actief, 'MyAssets');
|
|
|
|
await mkdir(join(dir, 'Demo'), { recursive: true });
|
|
const uit = await post('/api/portfolios/activate', { naam: 'Demo' });
|
|
assert.equal(uit.gewisseld, true);
|
|
assert.equal(uit.supervised, true, 'de server hoort te weten dat hij terugkomt');
|
|
|
|
assert.ok(await wachtOp(false, 10_000), 'de server ging niet neer');
|
|
assert.ok(await wachtOp(true), 'de server kwam niet terug');
|
|
|
|
const na = await haal('/api/portfolios');
|
|
assert.equal(na.actief, 'Demo');
|
|
});
|
|
|
|
it('wisselen naar de portefeuille die al open is, herstart niet', async () => {
|
|
const uit = await post('/api/portfolios/activate', { naam: 'Demo' });
|
|
|
|
assert.equal(uit.gewisseld, false);
|
|
await wacht(500);
|
|
assert.equal((await haal('/api/portfolios')).actief, 'Demo');
|
|
});
|
|
|
|
it('laat de server NIET achter als de supervisor verdwijnt', async () => {
|
|
proc.kill('SIGKILL');
|
|
|
|
assert.ok(await wachtOp(false, 10_000), 'er draait nog een server nadat de supervisor weg is');
|
|
});
|
|
});
|