vps-health-api/__tests__/load-history.test.js
le king fu b740f221af
All checks were successful
CI / verification (pull_request) Successful in 59s
feat(health): 24 h load history and a "saturated" state
GET /health only gave a snapshot, so the monitoring missed the 2026-11-03
saturation and the nights before it. A one-minute tick, armed in the
require.main block, now records host load, CPU by /proc/stat difference
and, when TLS_PROBE_HOSTS is set, the TLS handshake time of each listed
host into an in-memory buffer of 1440 samples (load-history.js, pure).

- /health gains `saturation` ({state: ok|saturated|unknown, since,
  coverageMinutes, load1, cores}) and `tls`, appended after the existing
  keys. Saturated = the last five samples of an unbroken series all have
  load1 > cores; unknown under five samples, after a gap over 3 minutes,
  or when the last sample is over 3 minutes old. No sample at start-up,
  so the state stays unknown for the first five minutes after a deploy.
- GET /health/history (read token) serves {since, coverageMinutes, cores,
  samples}. The buffer restarts from zero at every deploy.
- probeTls() verifies the certificate, times out after 3 s and reports an
  error code only. TLS_PROBE_HOSTS is empty by default: no network call in
  the tests, the CI or boot-from-copy.sh.
- Dockerfile COPY, REVIEW.md Haut table (and review-levels.py), CLAUDE.md,
  docs and .env.example updated.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 20:26:05 -05:00

147 lines
5.9 KiB
JavaScript

// Pure module of #73: no server, the clock is injected (`now`, `at`).
const { createHistory, DEFAULT_CAPACITY } = require("../load-history.js");
const T0 = Date.parse("2026-11-04T12:00:00.000Z");
const MIN = 60 * 1000;
const CORES = 4;
// One sample a minute from T0 + offset minutes, with the given load1 values.
function feed(history, loads, { from = 0, step = 1 } = {}) {
loads.forEach((load1, i) => {
history.record({ at: T0 + (from + i * step) * MIN, load1, load5: load1, cpuPercent: 50 });
});
return T0 + (from + (loads.length - 1) * step) * MIN;
}
const iso = (minutes) => new Date(T0 + minutes * MIN).toISOString();
describe("createHistory", () => {
test("defaults to 1440 samples (24 h at one a minute)", () => {
expect(DEFAULT_CAPACITY).toBe(1440);
});
test("rejects a capacity that is not a positive integer", () => {
expect(() => createHistory({ capacity: 0 })).toThrow();
expect(() => createHistory({ capacity: 1.5 })).toThrow();
});
test("drops the oldest samples beyond capacity", () => {
const history = createHistory({ capacity: 3 });
feed(history, [1, 2, 3, 4, 5]);
const { since, coverageMinutes, samples } = history.snapshot();
expect(samples.map((s) => s.load1)).toEqual([3, 4, 5]);
expect(since).toBe(iso(2));
expect(coverageMinutes).toBe(2);
});
test("an empty history has no since and zero coverage", () => {
expect(createHistory().snapshot()).toEqual({ since: null, coverageMinutes: 0, samples: [] });
});
test("rebuilds each sample from its known fields only", () => {
const history = createHistory();
history.record({ at: T0, load1: 1.5, load5: "x", cpuPercent: null, tls: "nope", extra: "leak" });
expect(history.snapshot().samples).toEqual([
{ at: iso(0), load1: 1.5, load5: null, cpuPercent: null, tls: null },
]);
});
test("refuses a sample without timestamp or load1, or older than the last one", () => {
const history = createHistory();
expect(history.record({ load1: 1 })).toBe(false);
expect(history.record({ at: T0, load1: NaN })).toBe(false);
expect(history.record({ at: T0, load1: 1 })).toBe(true);
expect(history.record({ at: T0, load1: 2 })).toBe(false);
expect(history.record({ at: T0 - MIN, load1: 2 })).toBe(false);
expect(history.size()).toBe(1);
});
test("keeps the tls results of a sample", () => {
const history = createHistory();
const tls = [{ host: "git.example.com", handshakeMs: 12, error: null }];
history.record({ at: T0, load1: 1, tls });
expect(history.snapshot().samples[0].tls).toEqual(tls);
});
});
describe("saturation", () => {
test("unknown while there are fewer than five samples", () => {
const history = createHistory();
expect(history.saturation(T0, CORES)).toEqual({
state: "unknown",
since: null,
coverageMinutes: 0,
load1: null,
cores: CORES,
});
const last = feed(history, [9, 9, 9, 9]);
expect(history.saturation(last, CORES).state).toBe("unknown");
expect(history.saturation(last, CORES).load1).toBe(9);
});
test("unknown -> saturated -> ok, each with the minute it began", () => {
const history = createHistory();
// Minutes 0..2 under the core count, 3..9 above, 10.. back under.
let last = feed(history, [1, 1, 1, 5, 5, 5, 5]);
expect(history.saturation(last, CORES).state).toBe("ok");
expect(history.saturation(last, CORES).since).toBe(iso(0));
last = feed(history, [5, 5, 5], { from: 7 });
expect(history.saturation(last, CORES)).toEqual({
state: "saturated",
since: iso(3),
coverageMinutes: 9,
load1: 5,
cores: CORES,
});
last = feed(history, [2], { from: 10 });
expect(history.saturation(last, CORES)).toMatchObject({ state: "ok", since: iso(10), load1: 2 });
});
test("load1 equal to the core count is not saturation", () => {
const history = createHistory();
const last = feed(history, [4, 4, 4, 4, 4]);
expect(history.saturation(last, CORES).state).toBe("ok");
});
test("one dip under the core count is enough to break the window", () => {
const history = createHistory();
const last = feed(history, [5, 5, 3, 5, 5, 5, 5]);
expect(history.saturation(last, CORES)).toMatchObject({ state: "ok", since: iso(0) });
const next = feed(history, [5], { from: 7 });
expect(history.saturation(next, CORES)).toMatchObject({ state: "saturated", since: iso(3) });
});
test("unknown when the latest sample is older than three minutes (the tick stopped)", () => {
const history = createHistory();
const last = feed(history, [5, 5, 5, 5, 5]);
expect(history.saturation(last + 3 * MIN, CORES).state).toBe("saturated");
expect(history.saturation(last + 3 * MIN + 1, CORES).state).toBe("unknown");
});
test("a gap longer than three minutes breaks the series", () => {
const history = createHistory();
feed(history, [5, 5, 5, 5, 5, 5]);
// Ten minutes without a sample, then four more: only four since the gap.
let last = feed(history, [5, 5, 5, 5], { from: 15 });
expect(history.saturation(last, CORES).state).toBe("unknown");
// The fifth after the gap makes it known again, since the gap.
last = feed(history, [5], { from: 19 });
expect(history.saturation(last, CORES)).toMatchObject({ state: "saturated", since: iso(15) });
// Coverage still counts from the first sample kept.
expect(history.saturation(last, CORES).coverageMinutes).toBe(19);
});
test("a gap of exactly three minutes keeps the series whole", () => {
const history = createHistory();
const last = feed(history, [5, 5, 5, 5, 5], { step: 3 });
expect(history.saturation(last, CORES)).toMatchObject({ state: "saturated", since: iso(0) });
});
test("unknown without a usable core count", () => {
const history = createHistory();
const last = feed(history, [5, 5, 5, 5, 5]);
expect(history.saturation(last, 0).state).toBe("unknown");
});
});