145 lines
6.5 KiB
JavaScript
145 lines
6.5 KiB
JavaScript
"use strict";
|
|
|
|
// Regression tests for the scheduler recurrence floor.
|
|
//
|
|
// Bug: POST /api/scheduler/:server/tasks stored scheduleValue unvalidated and
|
|
// scheduleToCron() turned it straight into a BullMQ repeat interval —
|
|
// parseInt(value, 10) || 30 only caught NaN and 0, so an authenticated user
|
|
// with scheduler_create could POST scheduleType:"seconds", scheduleValue:"1"
|
|
// and register a job firing every second (a console command or node API call
|
|
// per second, up to their maxSchedules quota). The UI only offers intervals
|
|
// of 10 seconds and up; the server side had no twin of that floor.
|
|
//
|
|
// Fix: scheduleToCron clamps the seconds branch to
|
|
// SCHEDULER_MIN_INTERVAL_SECONDS (10 — the UI floor), and clamps the
|
|
// minutes/hours cron steps into their field ranges, because an out-of-range
|
|
// step (e.g. */70 minutes) is either rejected by cron-parser (a 500 on task
|
|
// creation) or silently collapses onto the top of the field. NaN keeps the
|
|
// legacy defaults (30s / 5m / 1h).
|
|
//
|
|
// index.js boots servers on require and cannot be loaded in-process, so the
|
|
// REAL scheduleToCron is sliced from source and evaluated (same technique as
|
|
// fs-op-ack / scheduler-file-actions tests), then exercised behaviorally.
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const ROOT = path.join(__dirname, "..");
|
|
const INDEX_JS = path.join(ROOT, "index.js");
|
|
const indexSource = fs.readFileSync(INDEX_JS, "utf8");
|
|
|
|
function stripFullLineComments(src) {
|
|
return src
|
|
.split("\n")
|
|
.filter((line) => !/^\s*\/\//.test(line))
|
|
.join("\n");
|
|
}
|
|
|
|
const indexCode = stripFullLineComments(indexSource);
|
|
|
|
function sliceFrom(source, startMarker, endMarker, label) {
|
|
const start = source.indexOf(startMarker);
|
|
assert.ok(start >= 0, `${label}: start marker not found: ${startMarker}`);
|
|
const end = source.indexOf(endMarker, start);
|
|
assert.ok(end > start, `${label}: end marker not found after start: ${endMarker}`);
|
|
return source.slice(start, end);
|
|
}
|
|
|
|
// Slice the REAL constant + function together (the const is declared just
|
|
// above scheduleToCron in index.js), so the evaluated code uses the shipped
|
|
// floor value, not a test-local copy.
|
|
const { SCHEDULER_MIN_INTERVAL_SECONDS, scheduleToCron } = new Function(
|
|
sliceFrom(indexSource, "const SCHEDULER_MIN_INTERVAL_SECONDS", "\nasync function shutdownScheduler(", "scheduleToCron") +
|
|
"\nreturn { SCHEDULER_MIN_INTERVAL_SECONDS, scheduleToCron };"
|
|
)();
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The reported hole: sub-10-second intervals
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("seconds interval is floored at the UI minimum of 10s (the reported hole)", () => {
|
|
for (const bad of ["1", "2", "5", "9"]) {
|
|
assert.deepStrictEqual(
|
|
scheduleToCron("seconds", bad),
|
|
{ type: "interval", ms: SCHEDULER_MIN_INTERVAL_SECONDS * 1000 },
|
|
`scheduleValue ${bad} must clamp to ${SCHEDULER_MIN_INTERVAL_SECONDS}s`
|
|
);
|
|
}
|
|
});
|
|
|
|
test("zero and negative seconds clamp to the floor instead of relying on falsy fallbacks", () => {
|
|
assert.deepStrictEqual(scheduleToCron("seconds", "0"), { type: "interval", ms: 10000 });
|
|
assert.deepStrictEqual(scheduleToCron("seconds", "-100"), { type: "interval", ms: 10000 });
|
|
});
|
|
|
|
test("legitimate seconds intervals pass through unchanged", () => {
|
|
assert.deepStrictEqual(scheduleToCron("seconds", "30"), { type: "interval", ms: 30000 });
|
|
assert.deepStrictEqual(scheduleToCron("seconds", "45"), { type: "interval", ms: 45000 });
|
|
});
|
|
|
|
test("non-numeric seconds keep the legacy 30s default", () => {
|
|
assert.deepStrictEqual(scheduleToCron("seconds", "abc"), { type: "interval", ms: 30000 });
|
|
assert.deepStrictEqual(scheduleToCron("seconds", undefined), { type: "interval", ms: 30000 });
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Cron step ranges stay valid
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("minutes step is clamped into 1..59", () => {
|
|
assert.strictEqual(scheduleToCron("minutes", "1"), "*/1 * * * *");
|
|
assert.strictEqual(scheduleToCron("minutes", "5"), "*/5 * * * *");
|
|
assert.strictEqual(scheduleToCron("minutes", "59"), "*/59 * * * *");
|
|
assert.strictEqual(scheduleToCron("minutes", "70"), "*/59 * * * *");
|
|
assert.strictEqual(scheduleToCron("minutes", "-3"), "*/1 * * * *");
|
|
});
|
|
|
|
test("non-numeric minutes keep the legacy 5-minute default", () => {
|
|
assert.strictEqual(scheduleToCron("minutes", "abc"), "*/5 * * * *");
|
|
});
|
|
|
|
test("hours step is clamped into 1..23", () => {
|
|
assert.strictEqual(scheduleToCron("hourly", "1"), "0 */1 * * *");
|
|
assert.strictEqual(scheduleToCron("hourly", "6"), "0 */6 * * *");
|
|
assert.strictEqual(scheduleToCron("hourly", "23"), "0 */23 * * *");
|
|
assert.strictEqual(scheduleToCron("hourly", "30"), "0 */23 * * *");
|
|
});
|
|
|
|
test("non-numeric hours keep the legacy 1-hour default", () => {
|
|
assert.strictEqual(scheduleToCron("hourly", "abc"), "0 */1 * * *");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Untouched branches must not drift
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("daily and weekly shapes are unchanged", () => {
|
|
assert.strictEqual(scheduleToCron("daily", undefined, "09:15"), "15 9 * * *");
|
|
assert.strictEqual(scheduleToCron("daily", undefined), "0 0 * * *");
|
|
assert.strictEqual(scheduleToCron("weekly", "1", "09:15"), "15 9 * * 1");
|
|
assert.strictEqual(scheduleToCron("weekly", undefined), "0 0 * * 0");
|
|
});
|
|
|
|
test("unknown schedule types still return null (create route 400s on them)", () => {
|
|
assert.strictEqual(scheduleToCron("bogus", "1"), null);
|
|
assert.strictEqual(scheduleToCron(undefined, undefined), null);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Source-level: the floor is a named constant actually referenced
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("scheduleToCron references the named floor constant", () => {
|
|
const fn = sliceFrom(indexCode, "function scheduleToCron(", "\nasync function shutdownScheduler(", "scheduleToCron");
|
|
assert.ok(
|
|
fn.includes("SCHEDULER_MIN_INTERVAL_SECONDS"),
|
|
"the seconds branch must clamp through SCHEDULER_MIN_INTERVAL_SECONDS, not a magic number"
|
|
);
|
|
assert.ok(
|
|
indexCode.includes("const SCHEDULER_MIN_INTERVAL_SECONDS = 10;"),
|
|
"the floor constant must stay declared at 10 (the UI floor)"
|
|
);
|
|
});
|