mirror of
https://github.com/gadgethd/ukmesh.git
synced 2026-12-06 03:14:09 +00:00
68 lines
3.0 KiB
JavaScript
68 lines
3.0 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { once } from 'node:events';
|
|
import { createServer } from 'node:http';
|
|
import { test } from 'node:test';
|
|
import { bootstrapAlertReceiver } from './alert-bootstrap.mjs';
|
|
|
|
const freshQueue = { detail: 'no successful forward recorded', pending_count: 0, unresolved_dead_letter_count: 0 };
|
|
|
|
async function runWithReceiver(t, readiness, postStatuses = [202]) {
|
|
const posts = [];
|
|
let checks = 0;
|
|
const server = createServer(async (req, res) => {
|
|
if (req.method === 'GET' && req.url === '/readyz') {
|
|
const state = readiness[Math.min(checks++, readiness.length - 1)];
|
|
res.writeHead(state === 'ready' ? 200 : 503, { 'content-type': 'application/json' });
|
|
return res.end(JSON.stringify(state));
|
|
}
|
|
if (req.method !== 'POST' || req.url !== '/alerts') return res.writeHead(404).end();
|
|
let body = '';
|
|
for await (const chunk of req) body += chunk;
|
|
posts.push({ authorization: req.headers.authorization, body: JSON.parse(body) });
|
|
const status = postStatuses[Math.min(posts.length - 1, postStatuses.length - 1)];
|
|
if (status === 'disconnect') return req.socket.destroy();
|
|
res.writeHead(status).end();
|
|
});
|
|
server.listen(0, '127.0.0.1');
|
|
await once(server, 'listening');
|
|
t.after(() => new Promise(resolve => {
|
|
server.close(resolve);
|
|
server.closeAllConnections();
|
|
}));
|
|
const origin = `http://127.0.0.1:${server.address().port}`;
|
|
return { posts, bootstrap: () => bootstrapAlertReceiver(origin, 'fixture-token', { attempts: 4, intervalMs: 1 }) };
|
|
}
|
|
|
|
test('a fresh queue receives one authenticated probe before becoming ready', async t => {
|
|
const receiver = await runWithReceiver(t, [freshQueue, freshQueue, freshQueue, 'ready']);
|
|
await receiver.bootstrap();
|
|
assert.deepEqual(receiver.posts, [{
|
|
authorization: 'Bearer fixture-token',
|
|
body: { source: 'local-dev', status: 'resolved', check: 'local forwarding bootstrap' },
|
|
}]);
|
|
});
|
|
|
|
test('an unresolved dead letter blocks startup without adding probes', async t => {
|
|
const receiver = await runWithReceiver(t, [{ ...freshQueue, unresolved_dead_letter_count: 1 }]);
|
|
await assert.rejects(receiver.bootstrap, /1 unresolved dead letter\(s\) require redrive/);
|
|
assert.equal(receiver.posts.length, 0);
|
|
});
|
|
|
|
test('existing queued delivery can establish readiness without a new probe', async t => {
|
|
const receiver = await runWithReceiver(t, [{ ...freshQueue, pending_count: 1 }, 'ready']);
|
|
await receiver.bootstrap();
|
|
assert.equal(receiver.posts.length, 0);
|
|
});
|
|
|
|
test('an unconfirmed enqueue is left to durable delivery rather than duplicated', async t => {
|
|
const receiver = await runWithReceiver(t, [freshQueue, freshQueue, 'ready'], ['disconnect']);
|
|
await receiver.bootstrap();
|
|
assert.equal(receiver.posts.length, 1);
|
|
});
|
|
|
|
test('an explicitly rejected enqueue can be retried', async t => {
|
|
const receiver = await runWithReceiver(t, [freshQueue, freshQueue, freshQueue, 'ready'], [503, 202]);
|
|
await receiver.bootstrap();
|
|
assert.equal(receiver.posts.length, 2);
|
|
});
|