-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp-plugin.jobs.test.ts
More file actions
164 lines (140 loc) · 6.73 KB
/
Copy pathapp-plugin.jobs.test.ts
File metadata and controls
164 lines (140 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import type { PluginContext } from '@objectstack/core';
import { defineJob } from '@objectstack/spec/system';
import { InMemoryMetricsRegistry, OBSERVABILITY_METRICS_SERVICE, SEMCONV } from '@objectstack/observability';
import { CronJobAdapter } from '@objectstack/service-job';
import { AppPlugin } from './app-plugin.js';
/**
* #4567 — declarative cron jobs must actually reach the scheduler.
*
* These run AppPlugin against the REAL `CronJobAdapter` (croner underneath),
* not a recording double: the bug was that the authored schedule was rejected
* *inside* the adapter and the throw was swallowed, so a double that records
* whatever it is handed cannot see it.
*/
describe('AppPlugin — declarative background jobs (#4567)', () => {
let adapter: CronJobAdapter;
let metrics: InMemoryMetricsRegistry;
let ctx: PluginContext;
let readyHooks: Array<() => Promise<void>>;
beforeEach(() => {
adapter = new CronJobAdapter();
metrics = new InMemoryMetricsRegistry();
readyHooks = [];
ctx = {
logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() },
registerService: vi.fn(),
getService: vi.fn((name: string) => {
if (name === 'job') return adapter;
if (name === OBSERVABILITY_METRICS_SERVICE) return metrics;
if (name === 'objectql') return {};
return undefined;
}),
getServices: vi.fn(() => []),
hook: vi.fn((event: string, cb: () => Promise<void>) => {
if (event === 'kernel:ready') readyHooks.push(cb);
}),
trigger: vi.fn(),
} as unknown as PluginContext;
});
afterEach(async () => {
await adapter.destroy();
});
const fireReady = async () => {
for (const cb of readyHooks) await cb();
};
const errorLogs = () => vi.mocked(ctx.logger.error).mock.calls.map(c => String(c[0]));
const warnLogs = () => vi.mocked(ctx.logger.warn).mock.calls.map(c => String(c[0]));
it('schedules a defineJob cron job end-to-end — the adapter holds a live cron task', async () => {
const sweep = vi.fn(async () => { /* handler body */ });
const job = defineJob({
name: 'health_sweep',
schedule: { type: 'cron', expression: '0 1 * * *' },
handler: 'sweep',
});
const plugin = new AppPlugin({
id: 'com.test.jobs',
jobs: [job],
functions: { sweep },
});
await plugin.start!(ctx);
await fireReady();
// Scheduler state, not merely "did not throw": the adapter only records
// a job after `new Cron(...)` succeeded.
expect(await adapter.listJobs()).toContain('health_sweep');
const task = (adapter as unknown as { jobs: Map<string, { task?: { nextRun(): Date | null } }> })
.jobs.get('health_sweep')?.task;
const next = task?.nextRun();
expect(next).toBeInstanceOf(Date);
// '0 1 * * *' with the schema-defaulted UTC timezone.
expect(next!.getUTCHours()).toBe(1);
expect(next!.getUTCMinutes()).toBe(0);
// And the registered task really runs the bundle handler.
await adapter.trigger('health_sweep');
expect(sweep).toHaveBeenCalledTimes(1);
expect(errorLogs()).toEqual([]);
expect(metrics.totalCounter(SEMCONV.jobScheduleFailuresTotal)).toBe(0);
});
it('REVERT-PROOF: the raw authored schedule still breaks the adapter, exactly as #4567 reported', async () => {
const job = defineJob({
name: 'health_sweep',
schedule: { type: 'cron', expression: '0 1 * * *' },
handler: 'sweep',
});
// What AppPlugin used to pass verbatim. The adapter's contract is a bare
// string and it stays strict — remove the downgrade in AppPlugin and the
// end-to-end test above fails with precisely this croner error.
await expect(
adapter.schedule('health_sweep', job.schedule as never, async () => { /* noop */ }),
).rejects.toThrow(/Pattern has to be of type string/i);
expect(await adapter.listJobs()).not.toContain('health_sweep');
});
it('interval jobs keep working (no envelope on that branch)', async () => {
const plugin = new AppPlugin({
id: 'com.test.jobs',
jobs: [defineJob({ name: 'ping', schedule: { type: 'interval', intervalMs: 60_000 }, handler: 'h' })],
functions: { h: vi.fn(async () => { /* noop */ }) },
});
await plugin.start!(ctx);
await fireReady();
expect(await adapter.listJobs()).toContain('ping');
expect(errorLogs()).toEqual([]);
});
it('a job that cannot be scheduled logs ERROR (not a silent warn) and counts', async () => {
const plugin = new AppPlugin({
id: 'com.test.jobs',
jobs: [{
name: 'bad_job',
// A CEL envelope where a cron one belongs — unusable at the boundary.
schedule: { type: 'cron', expression: { dialect: 'cel', source: 'now()' } },
handler: 'h',
enabled: true,
}],
functions: { h: vi.fn(async () => { /* noop */ }) },
});
await plugin.start!(ctx);
await fireReady();
expect(await adapter.listJobs()).not.toContain('bad_job');
// Loud: error level, its own distinct message, and a counter.
const errors = errorLogs();
expect(errors.some(m => m.includes('FAILED TO SCHEDULE'))).toBe(true);
expect(errors.some(m => m.includes('declared but NOT scheduled'))).toBe(true);
expect(vi.mocked(ctx.logger.error).mock.calls[0][2]).toMatchObject({ job: 'bad_job' });
expect(metrics.totalCounter(SEMCONV.jobScheduleFailuresTotal, { job: 'bad_job' })).toBe(1);
// NOT folded into the warn stream that "handler missing"/"disabled" use.
expect(warnLogs().some(m => /schedule/i.test(m))).toBe(false);
});
it('a missing handler stays a warn — the two failures are not one signal', async () => {
const plugin = new AppPlugin({
id: 'com.test.jobs',
jobs: [defineJob({ name: 'orphan', schedule: { type: 'cron', expression: '0 1 * * *' }, handler: 'nope' })],
functions: {},
});
await plugin.start!(ctx);
await fireReady();
expect(warnLogs().some(m => m.includes('job handler not found'))).toBe(true);
expect(errorLogs()).toEqual([]);
expect(metrics.totalCounter(SEMCONV.jobScheduleFailuresTotal)).toBe(0);
});
});