mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-29 11:47:01 +00:00
feat: add cron scheduling mode for image update checks (#1460)
* feat: add cron scheduling mode for image update checks Adds a cron scheduling mode alongside the existing fixed-interval dropdown in Settings > Automation > Image update checks. Users can now set a 5-field cron expression (e.g. "0 3 * * 1") for precise time-of-day scheduling of registry polls. - Backend: ImageUpdateService gains mode/cronExpression fields and cron-based nextDelayMs() using the existing cron-parser dependency. PUT /api/image-updates/interval extended with transactional writes and server-authoritative cron validation matching the Scheduled Operations contract. Nicknames like @daily are supported. - Frontend: UpdatesSection gains a SegmentedControl toggle and cron text input with cronstrue-powered live description. The frontend does advisory validation only; backend 400s are surfaced inline. SettingsPrimaryButton used for explicit "Save schedule" action. - No cron jitter (the user chose a specific time). Interval mode keeps existing ±10% jitter. - Tests: 15 new backend tests covering valid cron, invalid cron, 6-field rejection, nickname support, backward compat, runtime fallback, and transactional writes. - Docs: auto-update-policies.mdx, alerts-notifications.mdx, and openapi.yaml updated with new scheduling mode. * fix: add mode and cronExpression to UpdatesSection test fixtures The existing tests failed because the mock status object was missing the new required fields (mode, cronExpression) added with cron scheduling support. Without them, status.mode was undefined, causing uiMode to never match 'interval' and the Select combobox to not render. * fix: prevent SegmentedControl from stretching full-width in SettingsField The flex-col container defaults items to align-self: stretch, making the Interval/Cron toggle bar span the full card width. Add self-start so it sizes to its content.
This commit is contained in:
@@ -122,6 +122,94 @@ describe('PUT /api/image-updates/interval', () => {
|
||||
const statusRes = await request(app).get('/api/image-updates/status').set('Cookie', adminCookie);
|
||||
expect(statusRes.body.intervalMinutes).toBe(30);
|
||||
});
|
||||
|
||||
// ── Cron mode ──────────────────────────────────────────────────────────
|
||||
|
||||
it('persists a valid cron expression and returns the enriched status', async () => {
|
||||
const res = await request(app).put('/api/image-updates/interval')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ minutes: 120, mode: 'cron', cron: '0 3 * * 1' });
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.mode).toBe('cron');
|
||||
expect(res.body.cronExpression).toBe('0 3 * * 1');
|
||||
const settings = DatabaseService.getInstance().getGlobalSettings();
|
||||
expect(settings.image_update_check_mode).toBe('cron');
|
||||
expect(settings.image_update_check_cron).toBe('0 3 * * 1');
|
||||
});
|
||||
|
||||
it('accepts a cron nickname like @daily', async () => {
|
||||
const res = await request(app).put('/api/image-updates/interval')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ minutes: 120, mode: 'cron', cron: '@daily' });
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.mode).toBe('cron');
|
||||
expect(res.body.cronExpression).toBe('@daily');
|
||||
});
|
||||
|
||||
it('rejects a 6-field cron expression', async () => {
|
||||
const res = await request(app).put('/api/image-updates/interval')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ minutes: 120, mode: 'cron', cron: '0 0 3 * * 1' });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/5 fields/);
|
||||
});
|
||||
|
||||
it('rejects a blank cron expression when mode is cron', async () => {
|
||||
const res = await request(app).put('/api/image-updates/interval')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ minutes: 120, mode: 'cron', cron: ' ' });
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it('rejects an invalid cron expression (backend-authoritative)', async () => {
|
||||
const res = await request(app).put('/api/image-updates/interval')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ minutes: 120, mode: 'cron', cron: '0 0 31 2 *' });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/Invalid cron/);
|
||||
});
|
||||
|
||||
it('rejects cron mode without a cron field', async () => {
|
||||
const res = await request(app).put('/api/image-updates/interval')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ minutes: 120, mode: 'cron' });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/Cron expression/);
|
||||
});
|
||||
|
||||
it('clears cron when switching back to interval mode', async () => {
|
||||
// First set cron mode.
|
||||
await request(app).put('/api/image-updates/interval')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ minutes: 120, mode: 'cron', cron: '0 3 * * 1' });
|
||||
// Then switch to interval.
|
||||
const res = await request(app).put('/api/image-updates/interval')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ minutes: 60, mode: 'interval' });
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.mode).toBe('interval');
|
||||
expect(res.body.cronExpression).toBeNull();
|
||||
const settings = DatabaseService.getInstance().getGlobalSettings();
|
||||
expect(settings.image_update_check_mode).toBe('interval');
|
||||
expect(settings.image_update_check_cron).toBe('');
|
||||
});
|
||||
|
||||
it('old-client { minutes } only does not change mode (backward compat)', async () => {
|
||||
// First set cron mode.
|
||||
await request(app).put('/api/image-updates/interval')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ minutes: 120, mode: 'cron', cron: '0 3 * * 1' });
|
||||
// Then send old-client payload.
|
||||
const res = await request(app).put('/api/image-updates/interval')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ minutes: 30 });
|
||||
expect(res.status).toBe(200);
|
||||
// Mode and cron are unchanged.
|
||||
expect(res.body.mode).toBe('cron');
|
||||
expect(res.body.cronExpression).toBe('0 3 * * 1');
|
||||
// Interval was updated (the fallback value).
|
||||
expect(res.body.intervalMinutes).toBe(30);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/image-updates/fleet', () => {
|
||||
|
||||
Reference in New Issue
Block a user