Notify when a game server is turned on or off

A read of a Fragnet account that shows a server on (running or starting) after
it was off, or off (stopped or stopping) after it was on, sends one message
through the configured channels and routing rules, a line for each server.
A server first seen, one whose state is unknown and one being installed or
moved say nothing. Switch: Settings -> Notifications -> What to send (on by
default).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-10 18:56:24 +01:00
1 parent 4d653f5a80
commit 5c6ccc0a4b
10 files changed
+193 -5

No files matched your search

+126
View File
@@ -27,6 +27,7 @@ const fn = await import("../src/services/fragnetSync.js");
const { evaluateHealth, collectSnapshot } = await import("../src/services/healthMonitor.js");
const { subjectOfConditionKey } = await import("../src/services/maintenance.js");
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
const { updateSettings } = await import("../src/services/settingsStore.js");
const { fragnetRouter } = await import("../src/routes/fragnet.js");
const { buildMetrics } = await import("../src/services/metrics.js");
const { setInternalFlag } = await import("../src/services/settingsStore.js");
@@ -440,6 +441,131 @@ describe("what is stored", () => {
});
});
describe("a game server turned on or off", () => {
const prev = (state: string, id = "abcd1234") => ({ id, state });
const cur = (extra: Partial<Srv> = {}) => server(extra);
describe("what counts", () => {
test("running and starting are on; offline and stopping are off; anything else is neither", () => {
assert.deepEqual(["running", "starting", "offline", "stopping", "unknown", "", "suspended"].map(fn.powerOf), [true, true, false, false, null, null, null]);
});
test("on to off and off to on are changes, with the name and the game; the same state is not", () => {
assert.deepEqual(fn.powerChanges([prev("offline")], [cur({ state: "running" })]), [{ id: "abcd1234", name: "survival", game: "Minecraft: Paper", on: true }]);
assert.deepEqual(fn.powerChanges([prev("running")], [cur({ state: "offline" })]).map((c) => c.on), [false]);
assert.deepEqual(fn.powerChanges([prev("running")], [cur({ state: "running" })]), []);
assert.deepEqual(fn.powerChanges([prev("offline")], [cur({ state: "offline" })]), []);
});
test("starting is on and stopping is off, so a start is said once: when it begins, not again when it has finished", () => {
assert.deepEqual(fn.powerChanges([prev("offline")], [cur({ state: "starting" })]).map((c) => c.on), [true]);
assert.deepEqual(fn.powerChanges([prev("starting")], [cur({ state: "running" })]), []);
assert.deepEqual(fn.powerChanges([prev("running")], [cur({ state: "stopping" })]).map((c) => c.on), [false]);
assert.deepEqual(fn.powerChanges([prev("stopping")], [cur({ state: "offline" })]), []);
});
test("a server not seen before, one whose state was or is unknown, and one being installed or moved say nothing", () => {
assert.deepEqual(fn.powerChanges([], [cur()]), []);
assert.deepEqual(fn.powerChanges([prev("unknown")], [cur({ state: "running" })]), []);
assert.deepEqual(fn.powerChanges([prev("running")], [cur({ state: "unknown" })]), []);
assert.deepEqual(fn.powerChanges([prev("offline")], [cur({ state: "running", installing: true })]), []);
assert.deepEqual(fn.powerChanges([prev("offline")], [cur({ state: "running", transferring: true })]), []);
});
test("each server is judged on its own, and they come by name", () => {
const out = fn.powerChanges([prev("running", "a1"), prev("offline", "b2"), prev("running", "c3")], [cur({ id: "c3", name: "rust", state: "offline" }), cur({ id: "a1", name: "ark", state: "running" }), cur({ id: "b2", name: "valheim", state: "running" })]);
assert.deepEqual(out.map((c) => [c.name, c.on]), [["rust", false], ["valheim", true]]);
});
});
describe("when a read is stored", () => {
let id = 0;
before(async () => {
id = (await db.insert(integrations).values({ type: "fragnet", name: "Power account", baseUrl: "https://panel.fragnet.net", config: JSON.stringify({}) }).returning({ id: integrations.id }))[0].id;
});
after(async () => {
await fn.forgetFragnet(id);
await db.delete(integrations).where(eq(integrations.id, id));
});
test("the first read of an account says nothing; the next says what changed since", async () => {
assert.deepEqual(await fn.storeRead(id, read(server({ state: "running" })), new Date()), []);
assert.deepEqual(await fn.storeRead(id, read(server({ state: "running" })), new Date()), []);
const changes = await fn.storeRead(id, read(server({ state: "offline" })), new Date());
assert.deepEqual(changes.map((c) => [c.name, c.on]), [["survival", false]]);
});
});
describe("the message", () => {
let id = 0;
let hook: Server;
const got: string[] = [];
before(async () => {
hook = createHttpServer((req, res) => {
let body = "";
req.on("data", (c) => (body += c));
req.on("end", () => {
got.push((JSON.parse(body) as { content: string }).content);
res.writeHead(200).end("ok");
});
});
await new Promise<void>((resolve) => hook.listen(0, "127.0.0.1", resolve));
await updateSettings({ webhook: { enabled: true, url: `http://127.0.0.1:${(hook.address() as AddressInfo).port}/hook` } });
id = (await db.insert(integrations).values({ type: "fragnet", name: "Fragnet account", baseUrl: "https://panel.fragnet.net", config: JSON.stringify({}) }).returning({ id: integrations.id }))[0].id;
});
after(async () => {
hook.close();
await updateSettings({ webhook: { enabled: false }, notifications: { gameServerPower: true } });
await fn.forgetFragnet(id);
await db.delete(integrations).where(eq(integrations.id, id));
});
const sync = (...servers: Srv[]) => fn.syncFragnet(id, new Date(), async () => read(...servers));
test("a server turned off is said through the configured channels, with its name, game and account", async () => {
await sync(server({ state: "running" }));
assert.equal(got.length, 0, "the first read says nothing");
await sync(server({ state: "offline" }));
assert.equal(got.length, 1);
assert.match(got[0], /^\*\*Homelab Manager — survival turned off\*\*\nsurvival \(Minecraft: Paper\) \[Fragnet account\]: now off$/);
});
test("and turned on again, in the same words", async () => {
await sync(server({ state: "starting" }));
assert.equal(got.length, 2);
assert.match(got[1], /survival turned on\*\*\nsurvival \(Minecraft: Paper\) \[Fragnet account\]: now on$/);
await sync(server({ state: "running" }));
assert.equal(got.length, 2, "the start finishing is not another change");
});
test("several that changed in one read are one message with a line each, and mixed ones say so", async () => {
await sync(server({ id: "aaaa1111", name: "ark", state: "running" }), server({ id: "bbbb2222", name: "rust", state: "offline" }));
got.length = 0;
await sync(server({ id: "aaaa1111", name: "ark", state: "offline" }), server({ id: "bbbb2222", name: "rust", state: "running" }));
assert.equal(got.length, 1);
assert.match(got[0], /^\*\*Homelab Manager — Game servers turned on or off\*\*\nark \(Minecraft: Paper\) \[Fragnet account\]: now off\nrust \(Minecraft: Paper\) \[Fragnet account\]: now on$/);
});
test("a server that is unchanged, or a read that failed, says nothing", async () => {
got.length = 0;
await sync(server({ id: "aaaa1111", name: "ark", state: "offline" }), server({ id: "bbbb2222", name: "rust", state: "running" }));
await fn.syncFragnet(id, new Date(), async () => {
throw new Error("panel down");
});
assert.equal(got.length, 0);
});
test("it is off with its switch in Settings → Notifications → What to send, and a failing channel does not make the read fail", async () => {
await updateSettings({ notifications: { gameServerPower: false } });
got.length = 0;
await sync(server({ id: "aaaa1111", name: "ark", state: "running" }), server({ id: "bbbb2222", name: "rust", state: "offline" }));
assert.equal(got.length, 0, "switched off");
await updateSettings({ notifications: { gameServerPower: true }, webhook: { enabled: true, url: "http://127.0.0.1:1/nothing-listens-here" } });
const status = await sync(server({ id: "aaaa1111", name: "ark", state: "offline" }), server({ id: "bbbb2222", name: "rust", state: "offline" }));
assert.equal(status.ok, true, "the read counts as done whatever happened to the message");
});
});
});
describe("the health pass, the page, the power signals and /metrics", () => {
let fgId = 0;
let app: Server;