|
| 1 | +import { expect } from 'vitest' |
| 2 | +import * as helper from './testHelper.ts' |
| 3 | +import Timekeeper from '../src/timekeeper.ts' |
| 4 | +import { ctx } from './hooks.ts' |
| 5 | + |
| 6 | +describe('getSchedule', function () { |
| 7 | + it('should return a single schedule by queue name and key', async function () { |
| 8 | + ctx.boss = await helper.start({ ...ctx.bossConfig }) |
| 9 | + |
| 10 | + await ctx.boss.schedule(ctx.schema, '* * * * *') |
| 11 | + await ctx.boss.schedule(ctx.schema, '0 1 * * *', null, { key: 'a' }) |
| 12 | + |
| 13 | + const schedule = await ctx.boss.getSchedule(ctx.schema, 'a') |
| 14 | + |
| 15 | + helper.assertTruthy(schedule) |
| 16 | + expect(schedule.name).toBe(ctx.schema) |
| 17 | + expect(schedule.key).toBe('a') |
| 18 | + expect(schedule.cron).toBe('0 1 * * *') |
| 19 | + }) |
| 20 | + |
| 21 | + it('should return the default-key schedule when no key is given', async function () { |
| 22 | + ctx.boss = await helper.start({ ...ctx.bossConfig }) |
| 23 | + |
| 24 | + await ctx.boss.schedule(ctx.schema, '* * * * *') |
| 25 | + await ctx.boss.schedule(ctx.schema, '0 1 * * *', null, { key: 'a' }) |
| 26 | + |
| 27 | + const schedule = await ctx.boss.getSchedule(ctx.schema) |
| 28 | + |
| 29 | + helper.assertTruthy(schedule) |
| 30 | + expect(schedule.key).toBe('') |
| 31 | + expect(schedule.cron).toBe('* * * * *') |
| 32 | + }) |
| 33 | + |
| 34 | + it('should return null when the schedule does not exist', async function () { |
| 35 | + ctx.boss = await helper.start({ ...ctx.bossConfig }) |
| 36 | + |
| 37 | + await ctx.boss.schedule(ctx.schema, '* * * * *') |
| 38 | + |
| 39 | + expect(await ctx.boss.getSchedule(ctx.schema, 'nope')).toBeNull() |
| 40 | + }) |
| 41 | + |
| 42 | + it('should reject a missing queue name', async function () { |
| 43 | + ctx.boss = await helper.start({ ...ctx.bossConfig }) |
| 44 | + |
| 45 | + await expect(async () => { |
| 46 | + // @ts-expect-error deliberately calling without the required queue name |
| 47 | + await ctx.boss.getSchedule() |
| 48 | + }).rejects.toThrow() |
| 49 | + }) |
| 50 | +}) |
| 51 | + |
| 52 | +// Pure computation: no database, no running instance. |
| 53 | +describe('previewSchedule', function () { |
| 54 | + function makeTk (config: object = {}) { |
| 55 | + const db = { executeSql: async () => ({ rows: [] }) } |
| 56 | + return new Timekeeper(db as any, {} as any, { schema: 'test', ...config } as any) |
| 57 | + } |
| 58 | + |
| 59 | + it('should return the next occurrences after the reference date', function () { |
| 60 | + const tk = makeTk() |
| 61 | + |
| 62 | + const occurrences = tk.previewSchedule('0 3 * * *', { from: new Date('2026-03-01T00:00:00Z'), count: 3 }) |
| 63 | + |
| 64 | + expect(occurrences.map(d => d.toISOString())).toEqual([ |
| 65 | + '2026-03-01T03:00:00.000Z', |
| 66 | + '2026-03-02T03:00:00.000Z', |
| 67 | + '2026-03-03T03:00:00.000Z' |
| 68 | + ]) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should evaluate the expression in the supplied time zone', function () { |
| 72 | + const tk = makeTk() |
| 73 | + |
| 74 | + const [first] = tk.previewSchedule('0 3 * * *', { |
| 75 | + tz: 'America/Chicago', |
| 76 | + from: new Date('2026-03-01T00:00:00Z'), |
| 77 | + count: 1 |
| 78 | + }) |
| 79 | + |
| 80 | + // 3am US central on March 1 is still standard time (UTC-6) |
| 81 | + expect(first.toISOString()).toBe('2026-03-01T09:00:00.000Z') |
| 82 | + }) |
| 83 | + |
| 84 | + it('should default to five occurrences', function () { |
| 85 | + const tk = makeTk() |
| 86 | + |
| 87 | + expect(tk.previewSchedule('* * * * *').length).toBe(5) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should exclude the reference date itself so pages do not overlap', function () { |
| 91 | + const tk = makeTk() |
| 92 | + |
| 93 | + const from = new Date('2026-03-01T03:00:00Z') |
| 94 | + const [first] = tk.previewSchedule('0 3 * * *', { from, count: 1 }) |
| 95 | + |
| 96 | + expect(first.toISOString()).toBe('2026-03-02T03:00:00.000Z') |
| 97 | + |
| 98 | + // handing the last occurrence back in yields the next page |
| 99 | + const [next] = tk.previewSchedule('0 3 * * *', { from: first, count: 1 }) |
| 100 | + expect(next.toISOString()).toBe('2026-03-03T03:00:00.000Z') |
| 101 | + }) |
| 102 | + |
| 103 | + it('should start from database time when no reference date is given', function () { |
| 104 | + const tk = makeTk() |
| 105 | + |
| 106 | + // an hour of clock skew has to move the first occurrence of an hourly expression |
| 107 | + tk.clockSkew = 60 * 60 * 1000 |
| 108 | + |
| 109 | + const [skewed] = tk.previewSchedule('0 * * * *', { count: 1 }) |
| 110 | + |
| 111 | + tk.clockSkew = 0 |
| 112 | + |
| 113 | + const [local] = tk.previewSchedule('0 * * * *', { count: 1 }) |
| 114 | + |
| 115 | + expect(skewed.getTime()).toBeGreaterThan(local.getTime()) |
| 116 | + }) |
| 117 | + |
| 118 | + it('should reject an expression schedule() would reject', function () { |
| 119 | + const tk = makeTk() |
| 120 | + |
| 121 | + expect(() => tk.previewSchedule('bogus')).toThrow() |
| 122 | + }) |
| 123 | + |
| 124 | + it('should reject an unusable time zone', function () { |
| 125 | + const tk = makeTk() |
| 126 | + |
| 127 | + expect(() => tk.previewSchedule('* * * * *', { tz: 'Mars/Phobos' })).toThrow(/time zone/) |
| 128 | + }) |
| 129 | + |
| 130 | + it('should reject a count outside the supported range', function () { |
| 131 | + const tk = makeTk() |
| 132 | + |
| 133 | + expect(() => tk.previewSchedule('* * * * *', { count: 0 })).toThrow(/count/) |
| 134 | + expect(() => tk.previewSchedule('* * * * *', { count: 1001 })).toThrow(/count/) |
| 135 | + expect(() => tk.previewSchedule('* * * * *', { count: 1.5 })).toThrow(/count/) |
| 136 | + }) |
| 137 | + |
| 138 | + it('should reject an invalid reference date', function () { |
| 139 | + const tk = makeTk() |
| 140 | + |
| 141 | + expect(() => tk.previewSchedule('* * * * *', { from: new Date('nope') })).toThrow(/from/) |
| 142 | + }) |
| 143 | + |
| 144 | + it('should agree with the expression a schedule was stored with', async function () { |
| 145 | + ctx.boss = await helper.start({ ...ctx.bossConfig }) |
| 146 | + |
| 147 | + await ctx.boss.schedule(ctx.schema, '0 3 * * *', null, { key: 'daily', tz: 'America/Chicago' }) |
| 148 | + |
| 149 | + const schedule = await ctx.boss.getSchedule(ctx.schema, 'daily') |
| 150 | + |
| 151 | + helper.assertTruthy(schedule) |
| 152 | + |
| 153 | + const occurrences = ctx.boss.previewSchedule(schedule.cron, { |
| 154 | + tz: schedule.timezone, |
| 155 | + from: new Date('2026-03-01T00:00:00Z'), |
| 156 | + count: 2 |
| 157 | + }) |
| 158 | + |
| 159 | + expect(occurrences.map(d => d.toISOString())).toEqual([ |
| 160 | + '2026-03-01T09:00:00.000Z', |
| 161 | + '2026-03-02T09:00:00.000Z' |
| 162 | + ]) |
| 163 | + }) |
| 164 | +}) |
0 commit comments