test: wait for fire-and-forget inserts instead of a fixed 200 ms

Under a full parallel run the event-log and webhook inserts took longer than the sleep, so
2-3 tests failed on most runs (also before M11-T8). They poll for the rows now.

Task: M11-T8
This commit is contained in:
iamdoubz committed 2026-10-20 13:12:59 -05:00
1 parent 5c46bd0e85
commit e4e50f1c0d
2 files changed
+20 -8

No files matched your search

+9 -2
View File
@@ -38,6 +38,13 @@ it('isPrivateAddress', () => {
});
describe.skipIf(!hasTestDb)('API tokens and webhooks', () => {
/** Waits for n webhook_deliveries rows (a fixed sleep was too short under load). */
const deliveries = async (n: number) => {
for (let i = 0; i < 100; i++) {
if ((await t.db.select().from(webhookDeliveries)).length >= n) return;
await new Promise((r) => setTimeout(r, 50));
}
};
let t: Awaited<ReturnType<typeof createTestApp>>;
let admin: string;
let req: (
@@ -186,7 +193,7 @@ describe.skipIf(!hasTestDb)('API tokens and webhooks', () => {
data: { ...call, state: 'talking', answeredAt: call.startedAt },
});
t.live.emit('message', { type: 'call.removed', data: { linkedid: 'L1' } });
await new Promise((r) => setTimeout(r, 200)); // the inserts are fire-and-forget
await deliveries(2); // the inserts are fire-and-forget
await deliverDue(t.db);
// call.answered isn't subscribed.
expect(got.map((g) => JSON.parse(g.body).event)).toEqual(['call.started', 'call.ended']);
@@ -198,7 +205,7 @@ describe.skipIf(!hasTestDb)('API tokens and webhooks', () => {
answer = 500;
got.length = 0;
t.live.emit('message', { type: 'call.updated', data: { ...call, linkedid: 'L2' } });
await new Promise((r) => setTimeout(r, 200));
await deliveries(3);
let now = Date.now();
const gaps: number[] = [];
for (let i = 0; i < 6; i++) {
+11 -6
View File
@@ -10,10 +10,18 @@ import { setSetting } from '../src/services/settings.ts';
import { hasTestDb } from './db.ts';
import { ADMIN, H, createTestApp } from './app.ts';
const settle = () => new Promise((r) => setTimeout(r, 200)); // inserts are fire-and-forget
/** The inserts are fire-and-forget: wait for them (a fixed sleep was too short under load). */
const rowsWhen = async (n: number) => {
for (let i = 0; i < 100; i++) {
const rows = await t.db.select().from(systemEvents);
if (rows.length >= n) return rows;
await new Promise((r) => setTimeout(r, 50));
}
return t.db.select().from(systemEvents);
};
let t: Awaited<ReturnType<typeof createTestApp>>;
describe.skipIf(!hasTestDb)('system status, event log, about', () => {
let t: Awaited<ReturnType<typeof createTestApp>>;
let get: (url: string, cookie?: string) => Promise<LightMyRequestResponse>;
let admin: string;
@@ -82,11 +90,8 @@ describe.skipIf(!hasTestDb)('system status, event log, about', () => {
type: 'system.configApplied',
data: { ok: false, error: 'disk full', restartRequired: true }, // already said: once
});
await settle();
// Sorted: trunk events wait for the trunk's name, so they can land after later ones.
const rows = (await t.db.select().from(systemEvents)).sort((x, y) =>
x.code.localeCompare(y.code),
);
const rows = (await rowsWhen(8)).sort((x, y) => x.code.localeCompare(y.code));
expect(rows.map((r) => [r.severity, r.code, r.source, r.message])).toEqual([
[
'error',