first commit

This commit is contained in:
Cauê Faleiros
2026-07-02 12:54:31 -03:00
commit cd6b4c93f0
17 changed files with 1409 additions and 0 deletions

91
test/config.test.js Normal file
View File

@@ -0,0 +1,91 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { buildConfig, resolveSyncDateRange, validateConfig } = require('../src/config');
test('uses safe Tiny request pacing and DD/MM/YYYY request date format by default', () => {
const config = buildConfig({
TINY_API_TOKEN: 'test-token',
DRY_RUN: 'true',
SYNC_START_DATE: '2026-07-02'
});
validateConfig(config);
assert.equal(config.tinyRequestDelayMs, 5000);
assert.equal(config.tinyDateFormat, 'DD/MM/YYYY');
});
test('validates env date format before syncing', () => {
assert.throws(
() => buildConfig({
TINY_API_TOKEN: 'test-token',
DRY_RUN: 'true',
SYNC_START_DATE: '02/07/2026'
}),
/SYNC_START_DATE must use YYYY-MM-DD format/
);
});
test('accepts ISO Tiny request date format override', () => {
const config = buildConfig({
TINY_API_TOKEN: 'test-token',
DRY_RUN: 'true',
SYNC_START_DATE: '2026-07-02',
TINY_DATE_FORMAT: 'YYYY-MM-DD'
});
validateConfig(config);
assert.equal(config.tinyDateFormat, 'YYYY-MM-DD');
});
test('requires DATABASE_URL only for real DB writes', () => {
assert.doesNotThrow(() => validateConfig(buildConfig({
TINY_API_TOKEN: 'test-token',
DRY_RUN: 'true',
SYNC_START_DATE: '2026-07-02'
})));
assert.throws(
() => validateConfig(buildConfig({
TINY_API_TOKEN: 'test-token',
DRY_RUN: 'false',
SYNC_START_DATE: '2026-07-02'
})),
/DATABASE_URL is required unless DRY_RUN=true/
);
});
test('backfill mode syncs through today when SYNC_END_DATE is empty', () => {
const config = buildConfig({
TINY_API_TOKEN: 'test-token',
DRY_RUN: 'true',
SYNC_MODE: 'backfill',
SYNC_START_DATE: '2026-06-01',
SYNC_TIME_ZONE: 'UTC'
});
validateConfig(config);
assert.deepEqual(resolveSyncDateRange(config, new Date('2026-07-02T12:00:00Z')), {
mode: 'backfill',
startDate: '2026-06-01',
endDate: '2026-07-02',
source: 'backfill through today'
});
});
test('recent mode defaults to today plus yesterday', () => {
const config = buildConfig({
TINY_API_TOKEN: 'test-token',
DRY_RUN: 'true',
SYNC_MODE: 'recent',
RECENT_SYNC_DAYS: '2',
SYNC_TIME_ZONE: 'UTC'
});
validateConfig(config);
assert.deepEqual(resolveSyncDateRange(config, new Date('2026-07-02T12:00:00Z')), {
mode: 'recent',
startDate: '2026-07-01',
endDate: '2026-07-02',
source: 'recent 2 day window'
});
});