peppa/packages/testkit/test/net-watch.test.ts

98 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-201: the watchdog watches DNS and the internet from the Peppa host and tells the platform
// channel (no channel — the superadmins) about changes that held for two checks.
import assert from "node:assert/strict";
import { after, describe, test } from "node:test";
import { core, db, runner, teardown } from "./helpers.ts";
import { telegramOutbox } from "@peppa/db";
import { like } from "drizzle-orm";
import { stripAsIs } from "@peppa/shared/i18n";
import type { NetProbes } from "@peppa/runner";
process.env.PEPPA_SECRET = "mock-secret-for-tests";
delete process.env.PEPPA_NET_WATCH;
after(teardown);
/** Mock network: what the system and fallback resolvers give, which addresses answer. */
function mockNet() {
const system = new Map<string, string[]>();
const fallback = new Map<string, string[]>();
const up = new Set<string>();
const probes: NetProbes = {
systemLookup: async (h) => system.get(h) ?? [],
fallbackLookup: async (h) => fallback.get(h) ?? [],
reachable: async (ip) => up.has(ip),
};
return { system, fallback, up, probes };
}
const texts = async (prefix: string) =>
(await db.select().from(telegramOutbox).where(like(telegramOutbox.dedupeKey, `${prefix}%`))).map((r) => ({ chat: r.params.chat_id, text: stripAsIs(String(r.params.text)) }));
describe("net watch", () => {
const hosts = ["mock-relay.example", "mock-ai.example"];
const net = mockNet();
net.system.set("mock-relay.example", ["10.0.0.1"]).set("mock-ai.example", ["10.0.1.1"]);
net.fallback.set("mock-relay.example", ["10.0.0.9"]).set("mock-ai.example", ["10.0.1.1"]);
net.up.add("10.0.0.1").add("10.0.1.1").add("10.0.0.9");
let t = Date.parse("2026-11-17T10:00:00Z");
const tick = () => runner.netWatchTick(db, { probes: net.probes, hosts, now: new Date((t += 5 * 60_000)) });
test("probe: ok, bad addresses from the system DNS, no DNS, down", async () => {
assert.equal((await runner.probeHost("mock-relay.example", net.probes)).state, "ok");
net.up.delete("10.0.0.1");
const p = await runner.probeHost("mock-relay.example", net.probes);
assert.equal(p.state, "blocked");
assert.deepEqual(p.working, ["10.0.0.9"]);
assert.match(stripAsIs(runner.describeProbe(p)), /10\.0\.0\.1.*10\.0\.0\.9.*1\.1\.1\.1/);
net.system.set("mock-relay.example", []);
assert.equal((await runner.probeHost("mock-relay.example", net.probes)).state, "dns");
net.up.delete("10.0.0.9");
assert.equal((await runner.probeHost("mock-relay.example", net.probes)).state, "down");
net.system.set("mock-relay.example", ["10.0.0.1"]);
net.up.add("10.0.0.1").add("10.0.0.9");
});
test("no channel: the superadmins hear a change once it held for two checks, and the recovery", async () => {
const chat = 880_001;
const u = await core.upsertUserFromGitea(db, { id: chat, login: "mock-net-admin" }, { access_token: "x" });
const { users } = await import("@peppa/db");
const { eq } = await import("drizzle-orm");
await db.update(users).set({ telegramChatId: chat, telegramUserId: chat }).where(eq(users.id, u.id));
await core.setPlatformAdmins(db, ["mock-net-admin"]);
await tick();
net.up.delete("10.0.0.1"); // the system DNS points at an unreachable address
await tick();
assert.equal((await texts("mon:net:")).length, 0, "one bad check is not enough");
await tick();
let msgs = await texts("mon:net:");
assert.equal(msgs.length, 1);
assert.match(msgs[0]!.text, /mock-relay\.example.*недоступные адреса 10\.0\.0\.1.*10\.0\.0\.9/);
await tick();
assert.equal((await texts("mon:net:")).length, 1, "the same state is not repeated");
net.up.add("10.0.0.1");
await tick();
await tick();
msgs = await texts("mon:net:");
assert.equal(msgs.length, 2);
assert.match(msgs[1]!.text, /mock-relay\.example снова открывается\. Не открывался 15 мин/);
});
test("the channel is connected: messages go there; no internet is one message, not one per host", async () => {
await core.setSetting(db, "platform.telegram_channel", { chatId: -100777, title: "mock", check: { ok: true, error: null, at: new Date().toISOString() }, events: { releases: true, services: true, digest: true }, digest: { time: "09:00", timezone: "UTC", users: false } });
net.up.clear();
await tick();
await tick();
let msgs = await texts("mon:ch:net:");
assert.equal(msgs.length, 1);
assert.equal(msgs[0]!.chat, -100777);
assert.match(msgs[0]!.text, /нет интернета.*mock-relay\.example, mock-ai\.example/);
net.up.add("10.0.0.1").add("10.0.1.1").add("10.0.0.9");
await tick();
await tick();
msgs = await texts("mon:ch:net:");
assert.equal(msgs.length, 2);
assert.match(msgs[1]!.text, /Интернет .* снова есть — не было 10 мин/);
});
});