Files
qinglong/test/back/cron-macros.test.cjs
T
whyour c5458596c8 feat(cron): expose run receipts and modularize execution
Return committed trigger IDs and add CLI wait and trigger-specific logs while
preserving API, CLI, transaction, deduplication and unknown-result semantics.

Move scheduling, lifecycle, settlement, queries and legacy adapters into
back/cron; remove obsolete internal import paths and circular dependencies.

Apply project formatting across the repository, cover CJS/MJS in formatting
hooks, add prettier:check and require formatting checks in AGENTS.md.

Validation: backend build; 1398 backend/frontend tests passed (17 platform
skips); 327 CLI tests passed (6 platform skips); 670 files pass format checks.
2026-11-22 12:24:29 +08:00

100 lines
2.9 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { createCronJob } = require('../../back/cron/scheduling/cronScheduler');
const {
isValidCronSchedule,
} = require('../../back/cron/scheduling/cronSchedule');
// node-cron caches timezone formatters; model each deployment in a fresh process.
if (!process.env.QL_MACRO_TEST_TZ) {
for (const tz of ['UTC', 'Asia/Shanghai']) {
test(`macro execution in ${tz}`, () => {
const env = { ...process.env, TZ: tz, QL_MACRO_TEST_TZ: tz };
delete env.NODE_TEST_CONTEXT;
const result = require('node:child_process').spawnSync(
process.execPath,
['-r', 'ts-node/register/transpile-only', '--test', __filename],
{ env, encoding: 'utf8' },
);
assert.equal(result.status, 0, result.stdout + result.stderr);
assert.match(
result.stdout,
/@annually fires at the expected local times/,
);
});
}
}
for (const tz of process.env.QL_MACRO_TEST_TZ
? [process.env.QL_MACRO_TEST_TZ]
: []) {
for (const [macro, start, next] of [
[
'@annually',
[2026, 11, 31, 23, 59, 59],
[
[2027, 0, 1, 0, 0, 0],
[2028, 0, 1, 0, 0, 0],
],
],
[
'@midnight',
[2026, 8, 27, 23, 59, 59],
[
[2026, 8, 28, 0, 0, 0],
[2026, 8, 29, 0, 0, 0],
],
],
[
'@minutely',
[2026, 8, 27, 12, 34, 59],
[
[2026, 8, 27, 12, 35, 0],
[2026, 8, 27, 12, 36, 0],
],
],
]) {
test(`${macro} fires at the expected local times in ${tz}`, async (t) => {
const previousTZ = process.env.TZ;
process.env.TZ = tz;
t.after(() => {
if (previousTZ === undefined) delete process.env.TZ;
else process.env.TZ = previousTZ;
});
t.mock.timers.enable({
apis: ['Date', 'setTimeout', 'setInterval'],
now: new Date(...start),
});
const expected = next.map((parts) => new Date(...parts).getTime());
const actual = [];
assert.equal(isValidCronSchedule(` ${macro} `), true);
const job = createCronJob(macro, (date) => actual.push(date.getTime()), {
name: macro,
logger: { warn() {}, error() {} },
});
t.after(() => job.cancel());
for (const time of expected) {
t.mock.timers.tick(time - Date.now() - 1);
await new Promise(setImmediate);
assert.equal(actual.includes(time), false, 'must not fire early');
t.mock.timers.tick(1);
await new Promise(setImmediate);
}
assert.deepEqual(actual, expected);
});
}
}
test('new aliases do not enable unknown macros or raw parser extensions', () => {
for (const schedule of [
'@annuallyx',
'@midnightx',
'@minutelyx',
'@secondly',
'@reboot',
'H * * * *',
'0 /5 * * * *',
]) {
assert.equal(isValidCronSchedule(schedule), false, schedule);
}
});