Files
COMERCIAL-GTI/tests/scheduler.test.mjs
T
Gustavo d330bb5ff0 Baseline commit of the existing Maquina Comercial Pessoal project
Tracks the app that was already in place (capture, treatment, outreach,
WhatsApp integration, server, frontend, tests, docs) so future feature
work can branch and use isolated worktrees.
2026-07-31 10:54:07 -03:00

71 lines
2.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { startOutreachScheduler } from '../src/outreach/scheduler.mjs';
test('scheduler nao sobrepoe ciclos', async () => {
let active = 0;
let maxActive = 0;
let release;
const gate = new Promise((resolve) => { release = resolve; });
const timers = [];
const stop = startOutreachScheduler({
db: {}, whatsapp: {},
runBatch: async () => { active += 1; maxActive = Math.max(maxActive, active); await gate; active -= 1; },
setTimeoutFn: (fn) => { timers.push(fn); return { unref() {} }; },
setIntervalFn: (fn) => { timers.push(fn); return { unref() {} }; },
clearTimeoutFn: () => {}, clearIntervalFn: () => {}
});
timers[0]();
timers[1]();
await new Promise((resolve) => setTimeout(resolve, 1));
assert.equal(maxActive, 1);
release();
await stop();
});
test('stop aborta e aguarda o ciclo ativo', async () => {
let release;
let started;
const gate = new Promise((resolve) => { release = resolve; });
const active = new Promise((resolve) => { started = resolve; });
const timers = [];
let receivedSignal;
const stop = startOutreachScheduler({
db: {}, whatsapp: {},
runBatch: async ({ signal }) => { receivedSignal = signal; started(); await gate; },
setTimeoutFn: (fn) => { timers.push(fn); return { unref() {} }; },
setIntervalFn: (fn) => { timers.push(fn); return { unref() {} }; },
clearTimeoutFn: () => {}, clearIntervalFn: () => {}
});
timers[0]();
await active;
let stopped = false;
const stopping = stop().then(() => { stopped = true; });
await new Promise((resolve) => setTimeout(resolve, 1));
assert.equal(receivedSignal.aborted, true);
assert.equal(stopped, false);
release();
await stopping;
assert.equal(stopped, true);
});
test('scheduler consulta a fila a cada 60 segundos', async () => {
const intervals = [];
const stop = startOutreachScheduler({
db: {},
whatsapp: {},
runBatch: async () => {},
setTimeoutFn: () => ({ unref() {} }),
setIntervalFn: (_fn, ms) => {
intervals.push(ms);
return { unref() {} };
},
clearTimeoutFn: () => {},
clearIntervalFn: () => {}
});
assert.deepEqual(intervals, [60_000]);
await stop();
});