@beechcms/api 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/dashboard/assets/index-BKWnlvnV.css +1 -0
- package/assets/dashboard/assets/index-C1P9BXnU.js +629 -0
- package/assets/dashboard/index.html +2 -2
- package/migrations/0000_v040_base.sql +25 -0
- package/migrations/0029_automations.sql +14 -0
- package/package.json +4 -3
- package/src/factory.ts +12 -4
- package/src/features/automations/__tests__/action-executors.test.ts +268 -0
- package/src/features/automations/__tests__/automation-runner.test.ts +192 -0
- package/src/features/automations/__tests__/automation-runner.utils.test.ts +56 -0
- package/src/features/automations/__tests__/automations.handler.test.ts +260 -0
- package/src/features/automations/__tests__/automations.repository.test.ts +159 -0
- package/src/features/automations/__tests__/automations.schema.test.ts +134 -0
- package/src/features/automations/__tests__/context-resolver.test.ts +122 -0
- package/src/features/automations/__tests__/cron-runner.test.ts +263 -0
- package/src/features/automations/__tests__/cron-runner.utils.test.ts +140 -0
- package/src/features/automations/__tests__/set-variable.executor.test.ts +306 -0
- package/src/features/automations/__tests__/template-grammar.test.ts +304 -0
- package/src/features/automations/__tests__/when-evaluator.test.ts +270 -0
- package/src/features/automations/__tests__/when-pushdown.test.ts +277 -0
- package/src/features/automations/action-executors/create-entry.executor.ts +23 -0
- package/src/features/automations/action-executors/edit-field.executor.ts +22 -0
- package/src/features/automations/action-executors/index.ts +33 -0
- package/src/features/automations/action-executors/send-mail.executor.ts +42 -0
- package/src/features/automations/action-executors/set-variable.executor.ts +146 -0
- package/src/features/automations/action-executors/webhook.executor.ts +25 -0
- package/src/features/automations/automation-runner.ts +81 -0
- package/src/features/automations/automation-runner.utils.ts +43 -0
- package/src/features/automations/automations.handler.ts +193 -0
- package/src/features/automations/automations.schema.ts +160 -0
- package/src/features/automations/context-resolver.ts +148 -0
- package/src/features/automations/cron-runner.ts +136 -0
- package/src/features/automations/cron-runner.utils.ts +40 -0
- package/src/features/automations/filter-translation.ts +42 -0
- package/src/features/automations/index.ts +12 -0
- package/src/features/automations/template-grammar.ts +241 -0
- package/src/features/automations/var-access-resolver.ts +136 -0
- package/src/features/automations/when-evaluator.ts +83 -0
- package/src/features/automations/when-pushdown.ts +53 -0
- package/src/features/content/handlers/create.ts +8 -0
- package/src/features/content/handlers/delete.ts +8 -1
- package/src/features/content/handlers/update.ts +8 -0
- package/src/features/draft/draft.handler.ts +51 -164
- package/src/features/draft/draft.middleware.ts +62 -0
- package/src/features/email/email.service.ts +13 -0
- package/src/features/email/email.types.ts +10 -0
- package/src/features/email/index.ts +2 -1
- package/src/features/email/templates/automation-mail.ts +15 -0
- package/src/features/settings/settings.handler.ts +2 -1
- package/src/index.ts +40 -8
- package/src/middleware/repository.middleware.ts +32 -1
- package/src/public/cache-utils.ts +34 -0
- package/src/public/entry-projection.ts +42 -0
- package/src/public/idempotency.ts +19 -0
- package/src/public/problem-details.ts +5 -0
- package/src/public/public-add.ts +110 -156
- package/src/public/public-read.ts +59 -216
- package/src/public/read-list.ts +50 -0
- package/src/public/read-single.ts +44 -0
- package/src/shared/automations.repository.d1.ts +146 -0
- package/src/shared/d1-content-scan.repository.test.ts +76 -0
- package/src/shared/execution-context-scheduler.ts +9 -0
- package/src/shared/storage-utils.ts +3 -3
- package/src/types.ts +5 -1
- package/src/upload.ts +3 -2
- package/assets/dashboard/assets/index-CH13idU1.js +0 -554
- package/assets/dashboard/assets/index-CewtCjom.css +0 -1
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
2
|
+
import type { Automation, IAutomationRepository, ContentRepository, Seed, IIdGenerator } from '@beechcms/core'
|
|
3
|
+
import { runCronAutomations } from '../cron-runner'
|
|
4
|
+
import type { CronRunnerDeps } from '../cron-runner'
|
|
5
|
+
|
|
6
|
+
vi.mock('../action-executors', () => ({
|
|
7
|
+
executeAction: vi.fn().mockResolvedValue(undefined),
|
|
8
|
+
}))
|
|
9
|
+
|
|
10
|
+
import { executeAction } from '../action-executors'
|
|
11
|
+
const executeActionMock = executeAction as ReturnType<typeof vi.fn>
|
|
12
|
+
|
|
13
|
+
// 2026-05-14T09:00:00Z → minute=0, hour=9, dow=4 (Thursday)
|
|
14
|
+
const TICK = new Date('2026-05-14T09:00:00Z').getTime()
|
|
15
|
+
|
|
16
|
+
const SEED: Seed = {
|
|
17
|
+
slug: 'posts',
|
|
18
|
+
label: 'Post',
|
|
19
|
+
displayNameAlias: 'title',
|
|
20
|
+
branches: [
|
|
21
|
+
{ alias: 'title', type: 'text', label: 'Title' },
|
|
22
|
+
{ alias: 'count', type: 'number', label: 'Count' },
|
|
23
|
+
{ alias: 'published', type: 'boolean', label: 'Published' },
|
|
24
|
+
{ alias: 'publish_date', type: 'date', label: 'Publish Date' },
|
|
25
|
+
{ alias: 'tags_list', type: 'tags', label: 'Tags' },
|
|
26
|
+
{ alias: 'meta_json', type: 'json', label: 'Meta JSON' },
|
|
27
|
+
{ alias: 'body_rich', type: 'richtext', label: 'Body' },
|
|
28
|
+
],
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const STUB_ID_GENERATOR: IIdGenerator = {
|
|
32
|
+
uuid: () => 'test-uuid',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function makeAutomation(overrides: Partial<Automation> = {}): Automation {
|
|
36
|
+
return {
|
|
37
|
+
id: 'auto_01',
|
|
38
|
+
seed_slug: 'posts',
|
|
39
|
+
name: 'every-minute',
|
|
40
|
+
enabled: true,
|
|
41
|
+
triggers: [{ event: 'cron' as const, cron: '* * * * *' }],
|
|
42
|
+
trigger_conditions: null,
|
|
43
|
+
actions: [{ type: 'webhook', url: 'https://example.com/hook' }],
|
|
44
|
+
created_at: 0,
|
|
45
|
+
updated_at: 0,
|
|
46
|
+
...overrides,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function makeDeps(overrides: Partial<CronRunnerDeps> = {}): CronRunnerDeps & {
|
|
51
|
+
listSpy: ReturnType<typeof vi.fn>
|
|
52
|
+
findActiveSpy: ReturnType<typeof vi.fn>
|
|
53
|
+
} {
|
|
54
|
+
const listSpy = vi.fn().mockResolvedValue({ items: [{ id: 'e1', title: 'Hello' }], total: 1 })
|
|
55
|
+
const findActiveSpy = vi.fn().mockResolvedValue([makeAutomation()])
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
automationRepository: {
|
|
59
|
+
findActive: findActiveSpy,
|
|
60
|
+
list: vi.fn(), findById: vi.fn(), create: vi.fn(),
|
|
61
|
+
update: vi.fn(), toggle: vi.fn(), delete: vi.fn(),
|
|
62
|
+
} as unknown as IAutomationRepository,
|
|
63
|
+
contentRepository: { findMany: listSpy } as unknown as ContentRepository,
|
|
64
|
+
getSeed: (slug: string) => slug === 'posts' ? SEED : null,
|
|
65
|
+
env: {},
|
|
66
|
+
idGenerator: STUB_ID_GENERATOR,
|
|
67
|
+
listSpy,
|
|
68
|
+
findActiveSpy,
|
|
69
|
+
...overrides,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
describe('runCronAutomations', () => {
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
executeActionMock.mockReset()
|
|
76
|
+
executeActionMock.mockResolvedValue(undefined)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('calls findActive with ("*", "cron")', async () => {
|
|
80
|
+
const deps = makeDeps()
|
|
81
|
+
await runCronAutomations(deps, TICK)
|
|
82
|
+
expect(deps.findActiveSpy).toHaveBeenCalledWith('*', 'cron')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('batch actions (webhook, send_mail) run once per automation with batch context', async () => {
|
|
86
|
+
const entries = [{ id: 'e1', title: 'A' }, { id: 'e2', title: 'B' }]
|
|
87
|
+
const deps = makeDeps()
|
|
88
|
+
deps.listSpy.mockResolvedValue({ items: entries, total: 2 })
|
|
89
|
+
|
|
90
|
+
await runCronAutomations(deps, TICK)
|
|
91
|
+
|
|
92
|
+
expect(executeActionMock).toHaveBeenCalledTimes(1)
|
|
93
|
+
const [, ctx] = executeActionMock.mock.calls[0]
|
|
94
|
+
// entry is the first entry (for backwards compat); batch count lives in context
|
|
95
|
+
expect(ctx.entry).toMatchObject({ id: 'e1', title: 'A' })
|
|
96
|
+
expect(ctx.context).toBeDefined()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('per-entry actions (edit_field, create_entry) run once per matching entry', async () => {
|
|
100
|
+
const entries = [{ id: 'e1' }, { id: 'e2' }]
|
|
101
|
+
const deps = makeDeps()
|
|
102
|
+
deps.findActiveSpy.mockResolvedValue([makeAutomation({ actions: [{ type: 'edit_field', field: 'title', value: 'updated' }] })])
|
|
103
|
+
deps.listSpy.mockResolvedValue({ items: entries, total: 2 })
|
|
104
|
+
|
|
105
|
+
await runCronAutomations(deps, TICK)
|
|
106
|
+
|
|
107
|
+
expect(executeActionMock).toHaveBeenCalledTimes(2)
|
|
108
|
+
expect(executeActionMock.mock.calls[0][1].entry).toMatchObject({ id: 'e1' })
|
|
109
|
+
expect(executeActionMock.mock.calls[1][1].entry).toMatchObject({ id: 'e2' })
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('skips non-matching cron expression, runs matching one', async () => {
|
|
113
|
+
const matchingAuto = makeAutomation({ id: 'auto_match', triggers: [{ event: 'cron', cron: '* * * * *' }] })
|
|
114
|
+
const nonMatchingAuto = makeAutomation({ id: 'auto_skip', triggers: [{ event: 'cron', cron: '30 9 * * *' }] })
|
|
115
|
+
const entries = [{ id: 'e1' }, { id: 'e2' }]
|
|
116
|
+
|
|
117
|
+
const deps = makeDeps()
|
|
118
|
+
deps.findActiveSpy.mockResolvedValue([matchingAuto, nonMatchingAuto])
|
|
119
|
+
deps.listSpy.mockResolvedValue({ items: entries, total: 2 })
|
|
120
|
+
|
|
121
|
+
await runCronAutomations(deps, TICK)
|
|
122
|
+
|
|
123
|
+
// webhook = batch, runs once for the matching automation only
|
|
124
|
+
expect(executeActionMock).toHaveBeenCalledTimes(1)
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('passes trigger_conditions as translated filters to repository.findMany', async () => {
|
|
128
|
+
const auto = makeAutomation({
|
|
129
|
+
trigger_conditions: {
|
|
130
|
+
kind: 'group',
|
|
131
|
+
op: 'AND',
|
|
132
|
+
children: [
|
|
133
|
+
{ kind: 'predicate', left: { kind: 'ref', key: 'this.title' }, op: 'eq', right: { kind: 'literal', value: 'Hello' } },
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
})
|
|
137
|
+
const deps = makeDeps()
|
|
138
|
+
deps.findActiveSpy.mockResolvedValue([auto])
|
|
139
|
+
|
|
140
|
+
await runCronAutomations(deps, TICK)
|
|
141
|
+
|
|
142
|
+
const call = deps.listSpy.mock.calls[0]
|
|
143
|
+
const options = call[1]
|
|
144
|
+
expect(options.filters).toEqual([
|
|
145
|
+
{ column: 'title', type: 'text', conditions: [{ op: 'eq', value: 'Hello' }] },
|
|
146
|
+
])
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
it('maps all branch types, system columns, unknown columns, and special operators correctly', async () => {
|
|
150
|
+
const auto = makeAutomation({
|
|
151
|
+
trigger_conditions: {
|
|
152
|
+
kind: 'group',
|
|
153
|
+
op: 'AND',
|
|
154
|
+
children: [
|
|
155
|
+
{ kind: 'predicate', left: { kind: 'ref', key: 'this.count' }, op: 'gt', right: { kind: 'literal', value: 5 } },
|
|
156
|
+
{ kind: 'predicate', left: { kind: 'ref', key: 'this.published' }, op: 'eq', right: { kind: 'literal', value: true } },
|
|
157
|
+
{ kind: 'predicate', left: { kind: 'ref', key: 'this.publish_date' }, op: 'isempty' },
|
|
158
|
+
{ kind: 'predicate', left: { kind: 'ref', key: 'this.tags_list' }, op: 'isnotempty' },
|
|
159
|
+
{ kind: 'predicate', left: { kind: 'ref', key: 'this.meta_json' }, op: 'eq', right: { kind: 'literal', value: '{}' } },
|
|
160
|
+
{ kind: 'predicate', left: { kind: 'ref', key: 'this.body_rich' }, op: 'contains', right: { kind: 'literal', value: 'hello' } },
|
|
161
|
+
{ kind: 'predicate', left: { kind: 'ref', key: 'this.status' }, op: 'eq', right: { kind: 'literal', value: 'draft' } },
|
|
162
|
+
{ kind: 'predicate', left: { kind: 'ref', key: 'this.unknown_custom' }, op: 'eq', right: { kind: 'literal', value: 'test' } },
|
|
163
|
+
],
|
|
164
|
+
},
|
|
165
|
+
})
|
|
166
|
+
const deps = makeDeps()
|
|
167
|
+
deps.findActiveSpy.mockResolvedValue([auto])
|
|
168
|
+
|
|
169
|
+
await runCronAutomations(deps, TICK)
|
|
170
|
+
|
|
171
|
+
const call = deps.listSpy.mock.calls[0]
|
|
172
|
+
expect(call[1].filters).toEqual([
|
|
173
|
+
{ column: 'count', type: 'number', conditions: [{ op: 'gt', value: 5 }] },
|
|
174
|
+
{ column: 'published', type: 'boolean', conditions: [{ op: 'eq', value: true }] },
|
|
175
|
+
{ column: 'publish_date', type: 'date', conditions: [{ op: 'is_empty', value: null }] },
|
|
176
|
+
{ column: 'tags_list', type: 'tags', conditions: [{ op: 'is_not_empty', value: null }] },
|
|
177
|
+
{ column: 'meta_json', type: 'json', conditions: [{ op: 'eq', value: '{}' }] },
|
|
178
|
+
{ column: 'body_rich', type: 'text', conditions: [{ op: 'contains', value: 'hello' }] },
|
|
179
|
+
{ column: 'status', type: 'system', conditions: [{ op: 'eq', value: 'draft' }] },
|
|
180
|
+
{ column: 'unknown_custom', type: 'text', conditions: [{ op: 'eq', value: 'test' }] },
|
|
181
|
+
])
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('logs warning and skips when getSeed returns null', async () => {
|
|
185
|
+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
186
|
+
const auto = makeAutomation({ seed_slug: 'unknown' })
|
|
187
|
+
const deps = makeDeps()
|
|
188
|
+
deps.findActiveSpy.mockResolvedValue([auto])
|
|
189
|
+
|
|
190
|
+
await runCronAutomations(deps, TICK)
|
|
191
|
+
|
|
192
|
+
expect(executeActionMock).not.toHaveBeenCalled()
|
|
193
|
+
expect(warnSpy).toHaveBeenCalledWith('[cron] unknown seed', expect.anything())
|
|
194
|
+
warnSpy.mockRestore()
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('continues processing remaining per-entry actions when one entry throws', async () => {
|
|
198
|
+
const entries = [{ id: 'e1' }, { id: 'e2' }, { id: 'e3' }]
|
|
199
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
200
|
+
const deps = makeDeps()
|
|
201
|
+
deps.findActiveSpy.mockResolvedValue([makeAutomation({ actions: [{ type: 'edit_field', field: 'title', value: 'x' }] })])
|
|
202
|
+
deps.listSpy.mockResolvedValue({ items: entries, total: 3 })
|
|
203
|
+
executeActionMock
|
|
204
|
+
.mockResolvedValueOnce(undefined)
|
|
205
|
+
.mockRejectedValueOnce(new Error('boom'))
|
|
206
|
+
.mockResolvedValueOnce(undefined)
|
|
207
|
+
|
|
208
|
+
await runCronAutomations(deps, TICK)
|
|
209
|
+
|
|
210
|
+
expect(executeActionMock).toHaveBeenCalledTimes(3)
|
|
211
|
+
expect(errorSpy).toHaveBeenCalledWith('[cron] entry action failed', expect.anything())
|
|
212
|
+
errorSpy.mockRestore()
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('logs and continues to next automation when repository.findMany throws', async () => {
|
|
216
|
+
const auto1 = makeAutomation({ id: 'auto_01' })
|
|
217
|
+
const auto2 = makeAutomation({ id: 'auto_02' })
|
|
218
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
219
|
+
const deps = makeDeps()
|
|
220
|
+
deps.findActiveSpy.mockResolvedValue([auto1, auto2])
|
|
221
|
+
deps.listSpy
|
|
222
|
+
.mockRejectedValueOnce(new Error('db error'))
|
|
223
|
+
.mockResolvedValueOnce({ items: [{ id: 'e1' }], total: 1 })
|
|
224
|
+
|
|
225
|
+
await runCronAutomations(deps, TICK)
|
|
226
|
+
|
|
227
|
+
expect(errorSpy).toHaveBeenCalledWith('[cron] fetch entries failed', expect.anything())
|
|
228
|
+
// auto2 still processed (webhook = batch, 1 call)
|
|
229
|
+
expect(executeActionMock).toHaveBeenCalledTimes(1)
|
|
230
|
+
errorSpy.mockRestore()
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
it('logs error and returns gracefully when findActive throws', async () => {
|
|
234
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
235
|
+
const deps = makeDeps()
|
|
236
|
+
deps.findActiveSpy.mockRejectedValueOnce(new Error('no such table'))
|
|
237
|
+
|
|
238
|
+
await expect(runCronAutomations(deps, TICK)).resolves.toBeUndefined()
|
|
239
|
+
expect(errorSpy).toHaveBeenCalledWith(
|
|
240
|
+
expect.stringContaining('[cron] Failed to fetch automations'),
|
|
241
|
+
expect.anything(),
|
|
242
|
+
)
|
|
243
|
+
expect(executeActionMock).not.toHaveBeenCalled()
|
|
244
|
+
errorSpy.mockRestore()
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
it('resolves quietly with zero calls when no active automations exist', async () => {
|
|
248
|
+
const deps = makeDeps()
|
|
249
|
+
deps.findActiveSpy.mockResolvedValue([])
|
|
250
|
+
|
|
251
|
+
await expect(runCronAutomations(deps, TICK)).resolves.toBeUndefined()
|
|
252
|
+
expect(executeActionMock).not.toHaveBeenCalled()
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it('skips automation silently when entries list is empty', async () => {
|
|
256
|
+
const deps = makeDeps()
|
|
257
|
+
deps.listSpy.mockResolvedValue({ items: [], total: 0 })
|
|
258
|
+
|
|
259
|
+
await runCronAutomations(deps, TICK)
|
|
260
|
+
|
|
261
|
+
expect(executeActionMock).not.toHaveBeenCalled()
|
|
262
|
+
})
|
|
263
|
+
})
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { cronMatches } from '../cron-runner.utils'
|
|
3
|
+
|
|
4
|
+
const ts = (iso: string) => new Date(iso).getTime()
|
|
5
|
+
|
|
6
|
+
describe('cronMatches', () => {
|
|
7
|
+
it('matches any timestamp with * * * * *', () => {
|
|
8
|
+
expect(cronMatches('* * * * *', ts('2026-05-14T09:37:00Z'))).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('returns false for null', () => {
|
|
12
|
+
expect(cronMatches(null, ts('2026-05-14T09:00:00Z'))).toBe(false)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('returns false for empty string', () => {
|
|
16
|
+
expect(cronMatches('', ts('2026-05-14T09:00:00Z'))).toBe(false)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('returns false for malformed (not 5 fields)', () => {
|
|
20
|
+
expect(cronMatches('* * * *', ts('2026-05-14T09:00:00Z'))).toBe(false)
|
|
21
|
+
expect(cronMatches('* * * * * *', ts('2026-05-14T09:00:00Z'))).toBe(false)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
describe('exact match', () => {
|
|
25
|
+
it('matches 0 9 * * * at 09:00', () => {
|
|
26
|
+
expect(cronMatches('0 9 * * *', ts('2026-05-14T09:00:00Z'))).toBe(true)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('does not match 0 9 * * * at 09:01', () => {
|
|
30
|
+
expect(cronMatches('0 9 * * *', ts('2026-05-14T09:01:00Z'))).toBe(false)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
describe('day-of-week', () => {
|
|
35
|
+
// 2026-05-11 is a Monday (day 1)
|
|
36
|
+
it('matches 0 9 * * 1 on Monday', () => {
|
|
37
|
+
expect(cronMatches('0 9 * * 1', ts('2026-05-11T09:00:00Z'))).toBe(true)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('does not match 0 9 * * 1 on Tuesday', () => {
|
|
41
|
+
expect(cronMatches('0 9 * * 1', ts('2026-05-12T09:00:00Z'))).toBe(false)
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
describe('list', () => {
|
|
46
|
+
it('matches 0 9,17 * * * at 09:00', () => {
|
|
47
|
+
expect(cronMatches('0 9,17 * * *', ts('2026-05-14T09:00:00Z'))).toBe(true)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('matches 0 9,17 * * * at 17:00', () => {
|
|
51
|
+
expect(cronMatches('0 9,17 * * *', ts('2026-05-14T17:00:00Z'))).toBe(true)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('does not match 0 9,17 * * * at 10:00', () => {
|
|
55
|
+
expect(cronMatches('0 9,17 * * *', ts('2026-05-14T10:00:00Z'))).toBe(false)
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
describe('range', () => {
|
|
60
|
+
it('matches 0 9-11 * * * at 09:00', () => {
|
|
61
|
+
expect(cronMatches('0 9-11 * * *', ts('2026-05-14T09:00:00Z'))).toBe(true)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('matches 0 9-11 * * * at 10:00', () => {
|
|
65
|
+
expect(cronMatches('0 9-11 * * *', ts('2026-05-14T10:00:00Z'))).toBe(true)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('matches 0 9-11 * * * at 11:00', () => {
|
|
69
|
+
expect(cronMatches('0 9-11 * * *', ts('2026-05-14T11:00:00Z'))).toBe(true)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('does not match 0 9-11 * * * at 12:00', () => {
|
|
73
|
+
expect(cronMatches('0 9-11 * * *', ts('2026-05-14T12:00:00Z'))).toBe(false)
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
describe('step', () => {
|
|
78
|
+
it('matches */15 * * * * at :00', () => {
|
|
79
|
+
expect(cronMatches('*/15 * * * *', ts('2026-05-14T09:00:00Z'))).toBe(true)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('matches */15 * * * * at :15', () => {
|
|
83
|
+
expect(cronMatches('*/15 * * * *', ts('2026-05-14T09:15:00Z'))).toBe(true)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('matches */15 * * * * at :30', () => {
|
|
87
|
+
expect(cronMatches('*/15 * * * *', ts('2026-05-14T09:30:00Z'))).toBe(true)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('matches */15 * * * * at :45', () => {
|
|
91
|
+
expect(cronMatches('*/15 * * * *', ts('2026-05-14T09:45:00Z'))).toBe(true)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('does not match */15 * * * * at :07', () => {
|
|
95
|
+
expect(cronMatches('*/15 * * * *', ts('2026-05-14T09:07:00Z'))).toBe(false)
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('range + step', () => {
|
|
100
|
+
it('matches 5-30/5 * * * * at :05', () => {
|
|
101
|
+
expect(cronMatches('5-30/5 * * * *', ts('2026-05-14T09:05:00Z'))).toBe(true)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('matches 5-30/5 * * * * at :10', () => {
|
|
105
|
+
expect(cronMatches('5-30/5 * * * *', ts('2026-05-14T09:10:00Z'))).toBe(true)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('matches 5-30/5 * * * * at :30', () => {
|
|
109
|
+
expect(cronMatches('5-30/5 * * * *', ts('2026-05-14T09:30:00Z'))).toBe(true)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('does not match 5-30/5 * * * * at :00', () => {
|
|
113
|
+
expect(cronMatches('5-30/5 * * * *', ts('2026-05-14T09:00:00Z'))).toBe(false)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('does not match 5-30/5 * * * * at :35', () => {
|
|
117
|
+
expect(cronMatches('5-30/5 * * * *', ts('2026-05-14T09:35:00Z'))).toBe(false)
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
describe('edge cases and malformed field expressions', () => {
|
|
122
|
+
it('returns false for non-finite or zero step', () => {
|
|
123
|
+
expect(cronMatches('*/0 * * * *', ts('2026-05-14T09:00:00Z'))).toBe(false)
|
|
124
|
+
expect(cronMatches('*/abc * * * *', ts('2026-05-14T09:00:00Z'))).toBe(false)
|
|
125
|
+
expect(cronMatches('*/-5 * * * *', ts('2026-05-14T09:00:00Z'))).toBe(false)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('returns false when step is applied to a single value instead of range or wildcard', () => {
|
|
129
|
+
expect(cronMatches('5/2 * * * *', ts('2026-05-14T09:05:00Z'))).toBe(false)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('returns false for malformed range bounds', () => {
|
|
133
|
+
expect(cronMatches('abc-def * * * *', ts('2026-05-14T09:00:00Z'))).toBe(false)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('returns false for invalid single numeric field', () => {
|
|
137
|
+
expect(cronMatches('abc * * * *', ts('2026-05-14T09:00:00Z'))).toBe(false)
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
})
|