import { describe, expect, it } from 'vitest'; import { loadJobs } from './definition.ts'; import { parseDuration } from './duration.ts'; const SYSTEM = ['index', 'status', 'reap-orphans']; describe('parseDuration', () => { it('understands the usual units', () => { expect(parseDuration('500ms')).toBe(500); expect(parseDuration('5s')).toBe(5000); expect(parseDuration('15m')).toBe(900_000); expect(parseDuration('1h')).toBe(3_600_000); expect(parseDuration('2d')).toBe(172_800_000); expect(() => parseDuration('soon')).toThrow(); }); }); describe('loadJobs', () => { it('adds every system job to an empty file and reports them as missing', () => { const loaded = loadJobs(''); expect(loaded.jobs.map((job) => job.id)).toEqual(SYSTEM); expect(loaded.warnings).toEqual([]); expect(loadJobs(loaded.missing).missing).toBe(''); }); it('reads script jobs with their defaults', () => { const { jobs } = loadJobs(` [[job]] id = "services" path = "jobs/services.sh" args = ["--verbose"] every = "15m" on = ["start", "watch"] watch = ["/host/srv"] then = ["index"] env = { MODE = "full" } `); expect(jobs[0]).toEqual({ id: 'services', path: 'jobs/services.sh', args: ['--verbose'], every: 900_000, on: ['start', 'watch'], watch: ['/host/srv'], then: ['index'], timeout: 300_000, env: { MODE: 'full' }, enabled: true, parallel: false, }); }); it('keeps disabled system jobs disabled and warns about them', () => { const loaded = loadJobs(` [[job]] id = "status" builtin = "status" enabled = false `); expect(loaded.jobs.find((job) => job.id === 'status')?.enabled).toBe(false); expect(loaded.warnings).toContain('job "status" is disabled'); expect(loaded.missing).not.toContain('id = "status"'); }); it.each([ ['both kinds', 'id = "a"\nbuiltin = "x"\npath = "y"', 'exactly one of builtin or path'], ['a bad id', 'id = "Bad Id"\npath = "y"', 'id must be'], ['an unknown key', 'id = "a"\npath = "y"\nretries = 3', 'unknown keys retries'], ['content-change on a script', 'id = "a"\npath = "y"\non = ["content-change"]', 'reserved for builtin jobs'], ['watch without paths', 'id = "a"\npath = "y"\non = ["watch"]', 'belong together'], ['a broken cron', 'id = "a"\npath = "y"\ncron = "every day"', 'invalid cron'], ['a tiny interval', 'id = "a"\npath = "y"\nevery = "100ms"', 'at least 1s'], ])('skips a job with %s', (_name, body, message) => { const loaded = loadJobs(`[[job]]\n${body}`); expect(loaded.jobs.map((job) => job.id)).toEqual(SYSTEM); expect(loaded.warnings.join('\n')).toContain(message); }); it('warns about duplicates and unknown then targets', () => { const loaded = loadJobs(` [[job]] id = "a" path = "a.sh" then = ["nowhere"] [[job]] id = "a" path = "b.sh" `); expect(loaded.warnings).toEqual(['job "a" is skipped: duplicate id "a"', 'job "a" triggers unknown job "nowhere"']); }); it('falls back to the system jobs when the file is not valid TOML', () => { const loaded = loadJobs('[[job]\nid = '); expect(loaded.jobs.map((job) => job.id)).toEqual(SYSTEM); expect(loaded.warnings[0]).toContain('cannot be read'); }); });