Files
crosscode-platform/apps/events/backend/tests/jobs.test.js
T
joshuaandClaude Opus 5.5 bbcafe61b9 Events 3.4.0, Accounts 1.4.0, contracts 1.12.0, agent 1.3.0: jobs from the dashboard, styled Accounts emails, .env from the Hub
- Events and Accounts: Background jobs are switched on or off and rescheduled from the admin;
  the choice is kept in the database (job_overrides), wins over the .env and applies at once.
  Accounts gets an Admin -> Background jobs page and per-job env flags, as Events.
- Accounts: every email styled as Cross Events' (church colour, logo, button, footer), same
  plain text (src/lib/emails.ts).
- Contracts 1.12.0: env-read / env-set agent jobs, envReadResultSchema, isSecretEnvName.
- crosscode script: `crosscode env <church> <product>` reads the .env (secrets hidden) and, with
  --set/--unset/--restart, changes one file, rebuilds the frontend when its file changed, reloads,
  and puts the file back if the site doesn't answer (lib/envedit.cjs, tests/envedit.bats).
- Agent 1.3.0: runs env-read and env-set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 12:14:02 +02:00

90 lines
4.1 KiB
JavaScript

const { JOBS, isEnabled, cronFor, nextRun, isNoop } = require('../src/jobs');
const job = (name) => JOBS.find(j => j.name === name);
describe('job registry', () => {
const saved = { ...process.env };
afterEach(() => { process.env = { ...saved }; });
test('every job has a valid cron schedule and a unique name', () => {
const names = new Set();
for (const j of JOBS) {
expect(names.has(j.name)).toBe(false);
names.add(j.name);
expect(() => nextRun(j.cron)).not.toThrow();
}
});
test('a job is on unless its own flag or JOBS_ENABLED is false', () => {
delete process.env.WAITLIST_JOB_ENABLED;
delete process.env.JOBS_ENABLED;
expect(isEnabled(job('waitlist'))).toBe(true);
process.env.WAITLIST_JOB_ENABLED = 'false';
expect(isEnabled(job('waitlist'))).toBe(false);
process.env.WAITLIST_JOB_ENABLED = 'true';
process.env.JOBS_ENABLED = 'false';
expect(isEnabled(job('waitlist'))).toBe(false);
expect(isEnabled(job('temp-cleanup'))).toBe(false);
});
test('JOB_SCHEDULE_<NAME> overrides the schedule; an invalid one is ignored', () => {
process.env.JOB_SCHEDULE_ANALYTICS_ROLLUP = '30 5 * * *';
expect(cronFor(job('analytics-rollup'))).toBe('30 5 * * *');
process.env.JOB_SCHEDULE_ANALYTICS_ROLLUP = 'every tuesday';
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {});
expect(cronFor(job('analytics-rollup'))).toBe(job('analytics-rollup').cron);
warn.mockRestore();
});
test('3.4.0: an admin choice wins over the env flag and schedule; JOBS_ENABLED=false still wins over everything', () => {
const { setOverridesForTest, envEnabled } = require('../src/jobs');
try {
delete process.env.JOBS_ENABLED;
process.env.WAITLIST_JOB_ENABLED = 'false';
process.env.JOB_SCHEDULE_WAITLIST = '*/5 * * * *';
setOverridesForTest({ waitlist: { enabled: true, cron: '*/20 * * * *' }, backup: { enabled: false } });
expect(envEnabled(job('waitlist'))).toBe(false);
expect(isEnabled(job('waitlist'))).toBe(true);
expect(cronFor(job('waitlist'))).toBe('*/20 * * * *');
expect(isEnabled(job('backup'))).toBe(false);
expect(isEnabled(job('temp-cleanup'))).toBe(true); // no env flag, no choice
process.env.JOBS_ENABLED = 'false';
expect(isEnabled(job('waitlist'))).toBe(false);
// A broken saved schedule falls back to the env's.
delete process.env.JOBS_ENABLED;
setOverridesForTest({ waitlist: { cron: 'nonsense' } });
expect(cronFor(job('waitlist'))).toBe('*/5 * * * *');
expect(isEnabled(job('waitlist'))).toBe(false); // the env flag again
} finally {
setOverridesForTest({});
}
});
test('3.4.0: setOverride checks its input before saving', async () => {
const { setOverride, isCron } = require('../src/jobs');
expect(isCron('*/10 * * * *')).toBe(true);
expect(isCron('0 7 * * *')).toBe(true);
expect(isCron('every day')).toBe(false);
expect(isCron('* * * * * *')).toBe(false); // seconds aren't offered
await expect(setOverride('nope', { enabled: true })).rejects.toMatchObject({ statusCode: 404 });
await expect(setOverride('waitlist', { enabled: 'yes' })).rejects.toMatchObject({ statusCode: 400 });
await expect(setOverride('waitlist', { cron: 'every day' })).rejects.toMatchObject({ statusCode: 400 });
});
test('daily jobs fire at local (Johannesburg) time', () => {
// 07:00 SAST = 05:00 UTC
const next = nextRun('0 7 * * *', new Date('2026-11-04T06:00:00Z'));
expect(next.toISOString()).toBe('2026-11-05T05:00:00.000Z');
expect(nextRun('0 7 * * *', new Date('2026-11-04T04:00:00Z')).toISOString()).toBe('2026-11-04T05:00:00.000Z');
});
test('isNoop: quiet when nothing happened', () => {
expect(isNoop({ expired: 0, offers: 0 })).toBe(true);
expect(isNoop({ reminders: { sessions: 0, sent: 0 }, certificates: { issued: 0, sent: 0 } })).toBe(true);
expect(isNoop({ deleted: 0, errors: 0 })).toBe(true);
expect(isNoop(null)).toBe(true);
expect(isNoop({ sent: 3 })).toBe(false);
expect(isNoop({ file: 'backup.sql.gz' })).toBe(false);
});
});