pangolin/server/lib/statusHistory.test.ts

47 lines
1.3 KiB
TypeScript

import { computeBuckets } from "./statusHistory";
import { test } from "node:test";
import assert from "node:assert/strict";
function event(id: number, timestamp: number, status: string) {
return {
entityType: "resource",
entityId: 1,
orgId: "org",
status,
timestamp,
id
};
}
// Regression test for https://github.com/fosrl/pangolin/issues/3994
test("computeBuckets same-second ordering", () => {
// Two days ago the resource went unhealthy and recovered within the same
// second. The database may return the two rows in either order.
const nowSec = Math.floor(Date.now() / 1000);
const flap = nowSec - 2 * 86400;
const unhealthy = event(1, flap, "unhealthy");
const healthy = event(2, flap, "healthy");
for (const events of [
[unhealthy, healthy],
[healthy, unhealthy]
]) {
const { buckets, totalDowntime } = computeBuckets(
events,
5,
"healthy",
0
);
const order = events.map((e) => e.id).join(",");
assert.equal(
totalDowntime,
0,
`No downtime when the later event is healthy (rows ${order})`
);
assert.equal(
buckets.at(-1)!.status,
"good",
`Today should be good (rows ${order})`
);
}
});