Files
COMERCIAL-GTI/tests/jobs-lease.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

27 lines
1.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { openDb } from '../src/db.mjs';
import { createJobLease } from '../src/jobs/lease.mjs';
test('lease marca running e atualiza heartbeat ate cleanup', () => {
const db = openDb(':memory:');
const id = Number(db.prepare("INSERT INTO exports (tipo, status) VALUES ('excel', 'pending')").run().lastInsertRowid);
let tick;
let cleared = false;
const lease = createJobLease({
db, table: 'exports', id, ownerToken: 'owner-1', pid: 123,
setInterval: (fn) => { tick = fn; return 7; },
clearInterval: (timer) => { assert.equal(timer, 7); cleared = true; }
});
const first = db.prepare('SELECT status, pid, owner_token, heartbeat_at FROM exports WHERE id = ?').get(id);
assert.equal(first.status, 'running');
assert.equal(first.pid, 123);
assert.equal(first.owner_token, 'owner-1');
db.prepare("UPDATE exports SET heartbeat_at = '2000-01-01 00:00:00' WHERE id = ?").run(id);
tick();
assert.notEqual(db.prepare('SELECT heartbeat_at FROM exports WHERE id = ?').get(id).heartbeat_at, '2000-01-01 00:00:00');
lease.close();
assert.equal(cleared, true);
db.close();
});