|
| 1 | +// Heartbeat active-hours evidence runs the real bounded scheduler and reload path. |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { pathToFileURL } from "node:url"; |
| 5 | +import type { OpenClawConfig } from "../../../../src/config/types.openclaw.js"; |
| 6 | +import { formatErrorMessage } from "../../../../src/infra/errors.js"; |
| 7 | +import { isWithinActiveHours } from "../../../../src/infra/heartbeat-active-hours.js"; |
| 8 | +import { startHeartbeatRunner } from "../../../../src/infra/heartbeat-runner.js"; |
| 9 | +import { createQaScriptEvidenceWriter } from "./script-evidence.js"; |
| 10 | + |
| 11 | +const DEFAULT_TIMEOUT_MS = 5_000; |
| 12 | +const HEARTBEAT_INTERVAL = "100ms"; |
| 13 | + |
| 14 | +type HeartbeatRuntimeOptions = { |
| 15 | + artifactBase: string; |
| 16 | + repoRoot: string; |
| 17 | + timeoutMs: number; |
| 18 | +}; |
| 19 | + |
| 20 | +type SchedulerObservation = { |
| 21 | + at: string; |
| 22 | + outcome: "active-fire" | "quiet-hours-skip"; |
| 23 | +}; |
| 24 | + |
| 25 | +function parseOptions(argv: string[], repoRoot = process.cwd()): HeartbeatRuntimeOptions { |
| 26 | + let artifactBase = path.join(repoRoot, ".artifacts", "qa-e2e", "heartbeat-active-hours"); |
| 27 | + let timeoutMs = DEFAULT_TIMEOUT_MS; |
| 28 | + for (let index = 0; index < argv.length; index += 1) { |
| 29 | + const arg = argv[index]; |
| 30 | + if (arg === "--output-dir") { |
| 31 | + artifactBase = path.resolve(repoRoot, argv[++index] ?? ""); |
| 32 | + continue; |
| 33 | + } |
| 34 | + if (arg === "--timeout-ms") { |
| 35 | + timeoutMs = Number(argv[++index]); |
| 36 | + continue; |
| 37 | + } |
| 38 | + if (arg === "--") { |
| 39 | + continue; |
| 40 | + } |
| 41 | + throw new Error(`Unknown argument: ${arg}`); |
| 42 | + } |
| 43 | + if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) { |
| 44 | + throw new Error("--timeout-ms must be a positive number"); |
| 45 | + } |
| 46 | + return { artifactBase, repoRoot, timeoutMs }; |
| 47 | +} |
| 48 | + |
| 49 | +function heartbeatConfig(quietHours: boolean): OpenClawConfig { |
| 50 | + return { |
| 51 | + agents: { |
| 52 | + defaults: { |
| 53 | + heartbeat: { |
| 54 | + activeHours: quietHours |
| 55 | + ? { start: "00:00", end: "00:00", timezone: "UTC" } |
| 56 | + : { start: "00:00", end: "24:00", timezone: "UTC" }, |
| 57 | + every: HEARTBEAT_INTERVAL, |
| 58 | + target: "none", |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +async function waitForObservation( |
| 66 | + observations: SchedulerObservation[], |
| 67 | + outcome: SchedulerObservation["outcome"], |
| 68 | + afterCount: number, |
| 69 | + timeoutMs: number, |
| 70 | +) { |
| 71 | + const deadline = Date.now() + timeoutMs; |
| 72 | + while (Date.now() < deadline) { |
| 73 | + if (observations.slice(afterCount).some((entry) => entry.outcome === outcome)) { |
| 74 | + return; |
| 75 | + } |
| 76 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 77 | + } |
| 78 | + throw new Error(`heartbeat scheduler did not observe ${outcome} within ${timeoutMs}ms`); |
| 79 | +} |
| 80 | + |
| 81 | +function createWriter(options: HeartbeatRuntimeOptions) { |
| 82 | + return createQaScriptEvidenceWriter({ |
| 83 | + artifactBase: options.artifactBase, |
| 84 | + logFileName: "heartbeat-active-hours.log", |
| 85 | + primaryModel: "heartbeat/scheduler", |
| 86 | + providerMode: "mock-openai", |
| 87 | + repoRoot: options.repoRoot, |
| 88 | + target: { |
| 89 | + id: "heartbeat-active-hours", |
| 90 | + title: "Heartbeat active-hours scheduler", |
| 91 | + sourcePath: "test/e2e/qa-lab/runtime/heartbeat-active-hours-runtime.ts", |
| 92 | + primaryCoverageIds: ["automation.active-hours"], |
| 93 | + docsRefs: ["docs/gateway/heartbeat.md"], |
| 94 | + codeRefs: [ |
| 95 | + "test/e2e/qa-lab/runtime/heartbeat-active-hours-runtime.ts", |
| 96 | + "src/infra/heartbeat-runner.ts", |
| 97 | + "src/infra/heartbeat-active-hours.ts", |
| 98 | + ], |
| 99 | + }, |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +export async function runHeartbeatActiveHoursRuntime(options: HeartbeatRuntimeOptions) { |
| 104 | + await fs.mkdir(options.artifactBase, { recursive: true }); |
| 105 | + const writer = createWriter(options); |
| 106 | + const startedAt = Date.now(); |
| 107 | + const observations: SchedulerObservation[] = []; |
| 108 | + let currentConfig = heartbeatConfig(false); |
| 109 | + const runner = startHeartbeatRunner({ |
| 110 | + cfg: currentConfig, |
| 111 | + readCurrentConfig: () => currentConfig, |
| 112 | + runOnce: async ({ cfg, heartbeat }) => { |
| 113 | + const active = isWithinActiveHours(cfg, heartbeat); |
| 114 | + const outcome = active ? "active-fire" : "quiet-hours-skip"; |
| 115 | + observations.push({ at: new Date().toISOString(), outcome }); |
| 116 | + writer.appendLog(`heartbeat-active-hours: ${outcome}\n`); |
| 117 | + return active |
| 118 | + ? { status: "ran", durationMs: 1 } |
| 119 | + : { status: "skipped", reason: "quiet-hours" }; |
| 120 | + }, |
| 121 | + stableSchedulerSeed: "qa-heartbeat-active-hours", |
| 122 | + }); |
| 123 | + try { |
| 124 | + await waitForObservation(observations, "active-fire", 0, options.timeoutMs); |
| 125 | + const beforeQuiet = observations.length; |
| 126 | + currentConfig = heartbeatConfig(true); |
| 127 | + runner.updateConfig(currentConfig); |
| 128 | + await waitForObservation(observations, "quiet-hours-skip", beforeQuiet, options.timeoutMs); |
| 129 | + const beforeReload = observations.length; |
| 130 | + currentConfig = heartbeatConfig(false); |
| 131 | + runner.updateConfig(currentConfig); |
| 132 | + await waitForObservation(observations, "active-fire", beforeReload, options.timeoutMs); |
| 133 | + |
| 134 | + const summaryPath = path.join(options.artifactBase, "heartbeat-active-hours-summary.json"); |
| 135 | + await fs.writeFile(summaryPath, `${JSON.stringify({ observations }, null, 2)}\n`, "utf8"); |
| 136 | + return await writer.write({ |
| 137 | + artifacts: [{ kind: "summary", filePath: summaryPath }], |
| 138 | + details: "Observed active fire, quiet-hours skip, and active-hours reload fire", |
| 139 | + durationMs: Math.max(1, Date.now() - startedAt), |
| 140 | + status: "pass", |
| 141 | + }); |
| 142 | + } catch (error) { |
| 143 | + const details = formatErrorMessage(error); |
| 144 | + writer.appendLog(`heartbeat-active-hours: ${details}\n`); |
| 145 | + return await writer.write({ |
| 146 | + details, |
| 147 | + durationMs: Math.max(1, Date.now() - startedAt), |
| 148 | + status: "fail", |
| 149 | + }); |
| 150 | + } finally { |
| 151 | + runner.stop(); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +export const testing = { heartbeatConfig, parseOptions, waitForObservation }; |
| 156 | + |
| 157 | +if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) { |
| 158 | + runHeartbeatActiveHoursRuntime(parseOptions(process.argv.slice(2))) |
| 159 | + .then((evidence) => { |
| 160 | + const status = evidence.entries[0]?.result.status; |
| 161 | + process.stdout.write(`heartbeat-active-hours: ${status}\n`); |
| 162 | + process.exitCode = status === "pass" ? 0 : 1; |
| 163 | + }) |
| 164 | + .catch((error: unknown) => { |
| 165 | + process.stderr.write(`heartbeat-active-hours: ${formatErrorMessage(error)}\n`); |
| 166 | + process.exitCode = 1; |
| 167 | + }); |
| 168 | +} |
0 commit comments