From 3b0e824c5b8427eb3725c953f9a41d385424d3be Mon Sep 17 00:00:00 2001 From: Sjoerd de Vries Date: Sat, 5 Sep 2026 15:15:09 +0200 Subject: [PATCH] feat: add daily cron scheduler for price checks Co-Authored-By: Claude Sonnet 5 --- src/scheduler.js | 10 ++++++++++ test/scheduler.test.js | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/scheduler.js create mode 100644 test/scheduler.test.js diff --git a/src/scheduler.js b/src/scheduler.js new file mode 100644 index 0000000..234090f --- /dev/null +++ b/src/scheduler.js @@ -0,0 +1,10 @@ +const cronDefault = require('node-cron'); + +const SCHEDULE = '0 8 * * *'; +const TIMEZONE = 'Europe/Amsterdam'; + +function start(runCheck, { cron = cronDefault } = {}) { + return cron.schedule(SCHEDULE, runCheck, { timezone: TIMEZONE }); +} + +module.exports = { start, SCHEDULE, TIMEZONE }; diff --git a/test/scheduler.test.js b/test/scheduler.test.js new file mode 100644 index 0000000..5d97105 --- /dev/null +++ b/test/scheduler.test.js @@ -0,0 +1,21 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { start, SCHEDULE, TIMEZONE } = require('../src/scheduler'); + +test('start schedules the daily check with the expected cron expression and timezone', () => { + const calls = []; + const fakeCron = { + schedule: (expression, fn, options) => { + calls.push({ expression, fn, options }); + return { stop: () => {} }; + }, + }; + const runCheck = async () => {}; + start(runCheck, { cron: fakeCron }); + assert.equal(calls.length, 1); + assert.equal(calls[0].expression, SCHEDULE); + assert.equal(SCHEDULE, '0 8 * * *'); + assert.equal(calls[0].options.timezone, TIMEZONE); + assert.equal(TIMEZONE, 'Europe/Amsterdam'); + assert.equal(calls[0].fn, runCheck); +});