openclaw/src/infra/heartbeat-active-hours.test.ts
adhitShet ae4907ce6e
fix(heartbeat): return false for zero-width active-hours window (#21408)
Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 993860bd0393fe9f48022f36c950c069863b4a61
Co-authored-by: adhitShet <131381638+adhitShet@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
2026-02-19 20:03:57 -05:00

87 lines
3.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { isWithinActiveHours } from "./heartbeat-active-hours.js";
function cfgWithUserTimezone(userTimezone = "UTC"): OpenClawConfig {
return {
agents: {
defaults: {
userTimezone,
},
},
};
}
function heartbeatWindow(start: string, end: string, timezone: string) {
return {
activeHours: {
start,
end,
timezone,
},
};
}
describe("isWithinActiveHours", () => {
it("returns true when activeHours is not configured", () => {
expect(
isWithinActiveHours(cfgWithUserTimezone("UTC"), undefined, Date.UTC(2025, 0, 1, 3)),
).toBe(true);
});
it("returns true when activeHours start/end are invalid", () => {
const cfg = cfgWithUserTimezone("UTC");
expect(
isWithinActiveHours(cfg, heartbeatWindow("bad", "10:00", "UTC"), Date.UTC(2025, 0, 1, 9)),
).toBe(true);
expect(
isWithinActiveHours(cfg, heartbeatWindow("08:00", "24:30", "UTC"), Date.UTC(2025, 0, 1, 9)),
).toBe(true);
});
it("returns false when activeHours start equals end", () => {
const cfg = cfgWithUserTimezone("UTC");
expect(
isWithinActiveHours(
cfg,
heartbeatWindow("08:00", "08:00", "UTC"),
Date.UTC(2025, 0, 1, 12, 0, 0),
),
).toBe(false);
});
it("respects user timezone windows for normal ranges", () => {
const cfg = cfgWithUserTimezone("UTC");
const heartbeat = heartbeatWindow("08:00", "24:00", "user");
expect(isWithinActiveHours(cfg, heartbeat, Date.UTC(2025, 0, 1, 7, 0, 0))).toBe(false);
expect(isWithinActiveHours(cfg, heartbeat, Date.UTC(2025, 0, 1, 8, 0, 0))).toBe(true);
expect(isWithinActiveHours(cfg, heartbeat, Date.UTC(2025, 0, 1, 23, 59, 0))).toBe(true);
});
it("supports overnight ranges", () => {
const cfg = cfgWithUserTimezone("UTC");
const heartbeat = heartbeatWindow("22:00", "06:00", "UTC");
expect(isWithinActiveHours(cfg, heartbeat, Date.UTC(2025, 0, 1, 23, 0, 0))).toBe(true);
expect(isWithinActiveHours(cfg, heartbeat, Date.UTC(2025, 0, 1, 5, 30, 0))).toBe(true);
expect(isWithinActiveHours(cfg, heartbeat, Date.UTC(2025, 0, 1, 12, 0, 0))).toBe(false);
});
it("respects explicit non-user timezones", () => {
const cfg = cfgWithUserTimezone("UTC");
const heartbeat = heartbeatWindow("09:00", "17:00", "America/New_York");
expect(isWithinActiveHours(cfg, heartbeat, Date.UTC(2025, 0, 1, 15, 0, 0))).toBe(true);
expect(isWithinActiveHours(cfg, heartbeat, Date.UTC(2025, 0, 1, 23, 30, 0))).toBe(false);
});
it("falls back to user timezone when activeHours timezone is invalid", () => {
const cfg = cfgWithUserTimezone("UTC");
const heartbeat = heartbeatWindow("08:00", "10:00", "Mars/Olympus");
expect(isWithinActiveHours(cfg, heartbeat, Date.UTC(2025, 0, 1, 9, 0, 0))).toBe(true);
expect(isWithinActiveHours(cfg, heartbeat, Date.UTC(2025, 0, 1, 11, 0, 0))).toBe(false);
});
});