Event webhooks: game_server.started and game_server.stopped

Sent for every Fragnet game server seen turned on or off, whatever the notification switch says, with the server, game, account and, when a power signal sent from here caused it, which signal and who sent it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-14 22:59:40 +01:00
1 parent 2986734196
commit 25183c0320
10 files changed
+127 -9

No files matched your search

+93 -1
View File
@@ -20,7 +20,7 @@ process.env.HEALTH_STARTUP_GRACE_MS = "0";
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, integrations, fragnetState, fragnetPowerLog, auditLog } = await import("../src/db/schema.js");
const { users, integrations, fragnetState, fragnetPowerLog, auditLog, eventWebhooks } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const adapter = await import("../src/integrations/fragnet/adapter.js");
const fn = await import("../src/services/fragnetSync.js");
@@ -564,6 +564,98 @@ describe("a game server turned on or off", () => {
assert.equal(status.ok, true, "the read counts as done whatever happened to the message");
});
});
describe("the event webhooks", () => {
let id = 0;
let hook: Server;
let hookId = 0;
const got: { event: string; data: { gameServer: Record<string, unknown> } }[] = [];
before(async () => {
hook = createHttpServer((req, res) => {
let body = "";
req.on("data", (c) => (body += c));
req.on("end", () => {
got.push(JSON.parse(body));
res.writeHead(200).end("ok");
});
});
await new Promise<void>((resolve) => hook.listen(0, "127.0.0.1", resolve));
const url = `http://127.0.0.1:${(hook.address() as AddressInfo).port}/hook`;
hookId = (await db.insert(eventWebhooks).values({ label: "Home Assistant", url, events: JSON.stringify(["game_server.started", "game_server.stopped"]) }).returning({ id: eventWebhooks.id }))[0].id;
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 db.delete(eventWebhooks).where(eq(eventWebhooks.id, hookId));
await updateSettings({ 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));
const heard = async (n: number) => {
for (let t = 0; t < 3000 && got.length < n; t += 25) await new Promise((r) => setTimeout(r, 25));
await new Promise((r) => setTimeout(r, 100));
};
test("a server turned off is game_server.stopped and turned on is game_server.started, with the server, the game and the account", async () => {
await sync(server({ state: "running" }));
await new Promise((r) => setTimeout(r, 150));
assert.equal(got.length, 0, "the first read says nothing");
await sync(server({ state: "offline" }));
await heard(1);
assert.equal(got.length, 1);
assert.equal(got[0].event, "game_server.stopped");
assert.deepEqual({ ...got[0].data.gameServer, at: "x" }, { id: "abcd1234", name: "survival", game: "Minecraft: Paper", integrationId: id, integrationName: "Fragnet account", on: false, at: "x", signal: null, by: null });
await sync(server({ state: "starting" }));
await heard(2);
assert.equal(got[1].event, "game_server.started");
assert.equal(got[1].data.gameServer.on, true);
await sync(server({ state: "running" }));
await new Promise((r) => setTimeout(r, 150));
assert.equal(got.length, 2, "the start finishing is not another change");
});
test("each server that changed is its own event", 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" }));
await heard(2);
assert.deepEqual(got.map((g) => [g.event, g.data.gameServer.name]).sort(), [["game_server.started", "rust"], ["game_server.stopped", "ark"]]);
});
test("they hear of it whatever the notification switch says, and a change made by a signal sent from here says who sent it", async () => {
await updateSettings({ notifications: { gameServerPower: false } });
const { recordSignal } = await import("../src/services/fragnetPower.js");
await sync(server({ id: "aaaa1111", name: "ark", state: "offline" }), server({ id: "bbbb2222", name: "rust", state: "running" }));
got.length = 0;
await recordSignal(id, { id: "aaaa1111", name: "ark" }, "start", "Ada");
await sync(server({ id: "aaaa1111", name: "ark", state: "starting" }), server({ id: "bbbb2222", name: "rust", state: "running" }));
await heard(1);
assert.equal(got.length, 1);
assert.equal(got[0].event, "game_server.started");
assert.equal(got[0].data.gameServer.signal, "start");
assert.equal(got[0].data.gameServer.by, "Ada");
});
test("an unchanged server or a failed read sends nothing, and a webhook that did not ask for them hears nothing", async () => {
got.length = 0;
await sync(server({ id: "aaaa1111", name: "ark", state: "starting" }), server({ id: "bbbb2222", name: "rust", state: "running" }));
await fn.syncFragnet(id, new Date(), async () => {
throw new Error("panel down");
});
await db.update(eventWebhooks).set({ events: JSON.stringify(["alert.opened"]) }).where(eq(eventWebhooks.id, hookId));
await sync(server({ id: "aaaa1111", name: "ark", state: "offline" }), server({ id: "bbbb2222", name: "rust", state: "running" }));
await new Promise((r) => setTimeout(r, 300));
assert.equal(got.length, 0);
});
test("they are two of the events a webhook can ask for", async () => {
const { webhookEvents } = await import("../src/db/schema.js");
assert.ok(webhookEvents.includes("game_server.started") && webhookEvents.includes("game_server.stopped"));
const { parseEvents } = await import("../src/services/eventWebhooks.js");
assert.deepEqual(parseEvents(JSON.stringify(["game_server.started", "nonsense"])), ["game_server.started"]);
});
});
});
describe("the health pass, the page, the power signals and /metrics", () => {