Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import {
MAX_PUBLISH_ATTEMPTS, PUBLISH_LEASE_MS, containerStep, fairPublishOrder, MAX_POSTS_PER_ACCOUNT_PER_RUN, failureMessage, failureOutcome, isTransientGraphCode, staleClaimCutoff,
} from '../../lib/scheduledPublishing.ts';
describe('scheduled publishing rules', () => {
test('the lease outlasts the 14 minute limit both schedulers run under', () => {
assert.ok(PUBLISH_LEASE_MS > 14 * 60 * 1000);
assert.equal(staleClaimCutoff(Date.parse('2026-09-01T12:00:00Z')), '2026-09-01T11:40:00.000Z');
});
test('a container that already went out is never published again, and a ready one is published as it is', () => {
assert.equal(containerStep('PUBLISHED'), 'published');
assert.equal(containerStep('FINISHED'), 'publish');
assert.equal(containerStep('IN_PROGRESS'), 'wait');
assert.equal(containerStep('EXPIRED'), 'recreate');
assert.equal(containerStep('ERROR'), 'fail');
});
test('only the Graph API codes Meta calls temporary are retried', () => {
assert.equal(isTransientGraphCode(2), true);
assert.equal(isTransientGraphCode(9007), true);
assert.equal(isTransientGraphCode(190), false);
assert.equal(isTransientGraphCode(undefined), false);
});
test('a passing failure retries until the last attempt, and a definite one fails at once', () => {
assert.equal(failureOutcome(true, 1), 'retry');
assert.equal(failureOutcome(true, MAX_PUBLISH_ATTEMPTS), 'fail');
assert.equal(failureOutcome(false, 1), 'fail');
});
test('the final message says publishing stopped only when retries ran out', () => {
assert.equal(failureMessage('Invalid image', false, 1), 'Invalid image');
assert.equal(failureMessage('Instagram API unreachable (reset)', true, MAX_PUBLISH_ATTEMPTS),
'Instagram API unreachable (reset). Publishing stopped after 3 attempts; schedule the post again.');
});
test('due posts take turns by account, oldest first, and one account gets a capped share of a run', () => {
const post = (id: string, accountId: string, minute: number) => ({ id, accountId, scheduledAt: `2026-11-05T10:${String(minute).padStart(2, '0')}:00.000Z` });
const busy = Array.from({ length: 20 }, (_, i) => post(`a${i}`, 'busy', i));
const ordered = fairPublishOrder([...busy, post('b1', 'quiet', 30), post('c1', 'other', 25)]);
assert.deepEqual(ordered.slice(0, 3).map(item => item.id), ['a0', 'c1', 'b1']);
assert.equal(ordered.filter(item => item.accountId === 'busy').length, MAX_POSTS_PER_ACCOUNT_PER_RUN);
assert.deepEqual(ordered.filter(item => item.accountId === 'busy').map(item => item.id), ['a0', 'a1', 'a2', 'a3', 'a4']);
assert.deepEqual(fairPublishOrder([]), []);
});
});