mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
Adds messages/nl.json (AI-initial, informal je/jij register, full key parity with en.json) and registers nl everywhere the locale list lives: config, loaders, Intl/date-fns mapping, zod enums, OpenAPI spec, scripts, the managed-profile picker and the notification locale guards. Dutch follows the Korean pattern for the AI surfaces (English body plus a Dutch reply-language footer, no safety-contract matrix) and gets its own banks in the outbound safety screen (dose, risk, causal, continuation), the prose-grounding and citation-coverage tables, the reference-range prose and the no-key fallback copy, each with Dutch test fixtures. PHQ-9 / GAD-7 / WHO-5 are not marked validated for nl yet: the Dutch wording has not been read against the published forms, so Dutch readers get the English items plus the existing note until it has.
409 lines
14 KiB
TypeScript
409 lines
14 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
import {
|
|
mkdtempSync,
|
|
readFileSync,
|
|
rmSync,
|
|
unlinkSync,
|
|
writeFileSync,
|
|
} from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
/**
|
|
* Runtime detection + once-per-state admin notification for the offline
|
|
* GeoLite2 tier.
|
|
*
|
|
* The CI workflow drops an `.empty` marker into `assets/geolite2/`
|
|
* whenever `MAXMIND_LICENSE_KEY` is unset, so the Docker COPY still
|
|
* lands a non-empty directory in `/opt/geolite2/`. The runtime resolver
|
|
* detects the marker on first fallback and fires a localised
|
|
* notification to every admin.
|
|
*
|
|
* Issue #851: the sent-latch used to be a module-level boolean, so every
|
|
* worker boot re-sent the same notice while the configuration never
|
|
* changed. The anchor now persists in the `notification_events` ledger
|
|
* (via `claimNotificationEvent`) and is released when the state exits —
|
|
* databases configured, or online lookups disabled. The tests pin the
|
|
* full state machine:
|
|
*
|
|
* 1. Entering the unconfigured state sends, once, to every admin.
|
|
* 2. Staying in the state — further lookups, and simulated worker
|
|
* restarts — stays silent.
|
|
* 3. Leaving the state releases the anchor; re-entering sends again.
|
|
* 4. `IP_GEO_LOOKUP_DISABLED=1` counts as having left the state: with
|
|
* no egress there is nothing to warn about.
|
|
*
|
|
* `mmdb-lib` is stubbed via the same shape the existing
|
|
* `geo-asn.test.ts` uses; `fs` is left real because `offlineGeoReady`
|
|
* reads real files at the temp-dir path picked per test. `@/lib/db` is
|
|
* mocked with an in-memory `notification_events` store that outlives
|
|
* `vi.resetModules()`, which is what lets a test simulate a restart
|
|
* while the "database" keeps its rows. `safeFetch` is stubbed so the
|
|
* online-fallback path never leaves the process.
|
|
*/
|
|
|
|
vi.mock("mmdb-lib", () => ({
|
|
Reader: class {
|
|
constructor(_buf: Buffer) {
|
|
void _buf;
|
|
}
|
|
get(ip: string): unknown {
|
|
void ip;
|
|
return null;
|
|
}
|
|
},
|
|
}));
|
|
|
|
vi.mock("@/lib/safe-fetch", () => ({
|
|
safeFetch: vi.fn(async () => ({ ok: false, status: 503 })),
|
|
}));
|
|
|
|
const dispatchSpy = vi.fn(async () => undefined);
|
|
vi.mock("@/lib/notifications/dispatch-localised", () => ({
|
|
dispatchLocalisedNotification: dispatchSpy,
|
|
}));
|
|
|
|
interface EventRow {
|
|
recordUserId: string;
|
|
eventType: string;
|
|
dedupKey: string;
|
|
createdAt: Date;
|
|
}
|
|
|
|
/** In-memory `notification_events` — survives `vi.resetModules()`. */
|
|
const eventStore: EventRow[] = [];
|
|
|
|
const findManySpy = vi.fn(async () => [{ id: "admin-1" }]);
|
|
|
|
const txStub = {
|
|
$queryRaw: async () => [{ locked: 1 }],
|
|
notificationEvent: {
|
|
findFirst: async (args: {
|
|
where: {
|
|
recordUserId: string;
|
|
eventType: string;
|
|
dedupKey: string;
|
|
createdAt: { gte: Date };
|
|
};
|
|
}) =>
|
|
eventStore.find(
|
|
(row) =>
|
|
row.recordUserId === args.where.recordUserId &&
|
|
row.eventType === args.where.eventType &&
|
|
row.dedupKey === args.where.dedupKey &&
|
|
row.createdAt >= args.where.createdAt.gte,
|
|
) ?? null,
|
|
create: async (args: {
|
|
data: { recordUserId: string; eventType: string; dedupKey: string };
|
|
}) => {
|
|
const row: EventRow = { ...args.data, createdAt: new Date() };
|
|
eventStore.push(row);
|
|
return row;
|
|
},
|
|
},
|
|
};
|
|
|
|
vi.mock("@/lib/db", () => ({
|
|
prisma: {
|
|
user: { findMany: findManySpy },
|
|
$transaction: async (fn: (tx: typeof txStub) => Promise<unknown>) =>
|
|
fn(txStub),
|
|
notificationEvent: {
|
|
deleteMany: async (args: {
|
|
where: { eventType: string; dedupKey: string };
|
|
}) => {
|
|
const before = eventStore.length;
|
|
for (let i = eventStore.length - 1; i >= 0; i--) {
|
|
if (
|
|
eventStore[i].eventType === args.where.eventType &&
|
|
eventStore[i].dedupKey === args.where.dedupKey
|
|
) {
|
|
eventStore.splice(i, 1);
|
|
}
|
|
}
|
|
return { count: before - eventStore.length };
|
|
},
|
|
},
|
|
},
|
|
}));
|
|
|
|
const ORIGINAL_ENV = { ...process.env };
|
|
let tmpRoot: string;
|
|
|
|
async function flushMicrotasks(): Promise<void> {
|
|
// The notification path is fire-and-forget — `void evaluate…()` —
|
|
// and pulls in `@/lib/db` + `@/lib/notifications/dispatch-localised`
|
|
// through dynamic `import()` calls, so each dispatch needs several
|
|
// microtask + macrotask flushes before the spy registers the call.
|
|
for (let i = 0; i < 20; i++) {
|
|
await Promise.resolve();
|
|
await new Promise((r) => setTimeout(r, 0));
|
|
}
|
|
}
|
|
|
|
beforeEach(() => {
|
|
process.env = { ...ORIGINAL_ENV };
|
|
delete process.env.IP_GEO_LOOKUP_DISABLED;
|
|
tmpRoot = mkdtempSync(join(tmpdir(), "healthlog-geo-r5-"));
|
|
process.env.GEOLITE2_DIR = tmpRoot;
|
|
dispatchSpy.mockClear();
|
|
findManySpy.mockClear();
|
|
findManySpy.mockResolvedValue([{ id: "admin-1" }]);
|
|
eventStore.length = 0;
|
|
vi.resetModules();
|
|
});
|
|
|
|
afterEach(() => {
|
|
rmSync(tmpRoot, { recursive: true, force: true });
|
|
process.env = ORIGINAL_ENV;
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe("offline-geo runtime detection", () => {
|
|
it("offlineGeoReady() is false when the .empty marker is present", async () => {
|
|
writeFileSync(join(tmpRoot, ".empty"), "");
|
|
const { offlineGeoReady } = await import("../geo");
|
|
expect(offlineGeoReady()).toBe(false);
|
|
});
|
|
|
|
it("offlineGeoReady() is false when the City MMDB is absent", async () => {
|
|
const { offlineGeoReady } = await import("../geo");
|
|
expect(offlineGeoReady()).toBe(false);
|
|
});
|
|
|
|
it("treats a blank GEOLITE2_DIR as unset rather than as the working directory", async () => {
|
|
// A compose `${GEOLITE2_DIR:-}` substitutes to the EMPTY STRING, which
|
|
// is not nullish. Without the trim, `path.join("", "GeoLite2-City.mmdb")`
|
|
// is a RELATIVE path and the readiness check answers from whatever the
|
|
// process working directory happens to hold. Proven by putting a stub
|
|
// MMDB in the working directory and asserting the blank value still
|
|
// resolves to the documented default (which has none).
|
|
const originalCwd = process.cwd();
|
|
writeFileSync(join(tmpRoot, "GeoLite2-City.mmdb"), Buffer.from("stub"));
|
|
process.chdir(tmpRoot);
|
|
try {
|
|
process.env.GEOLITE2_DIR = "";
|
|
const { offlineGeoReady } = await import("../geo");
|
|
expect(offlineGeoReady()).toBe(false);
|
|
} finally {
|
|
process.chdir(originalCwd);
|
|
}
|
|
});
|
|
|
|
it("offlineGeoReady() is true when the City MMDB is present and no marker", async () => {
|
|
writeFileSync(
|
|
join(tmpRoot, "GeoLite2-City.mmdb"),
|
|
Buffer.from("city-stub"),
|
|
);
|
|
const { offlineGeoReady } = await import("../geo");
|
|
expect(offlineGeoReady()).toBe(true);
|
|
});
|
|
|
|
it("fires the admin notification when the marker is present and lookupIpLocation falls back", async () => {
|
|
writeFileSync(join(tmpRoot, ".empty"), "");
|
|
const { lookupIpLocation } = await import("../geo");
|
|
|
|
await lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
|
|
expect(findManySpy).toHaveBeenCalledTimes(1);
|
|
expect(dispatchSpy).toHaveBeenCalledTimes(1);
|
|
expect(dispatchSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
userId: "admin-1",
|
|
titleKey: "notifications.admin.offlineGeoUnavailableTitle",
|
|
messageKey: "notifications.admin.offlineGeoUnavailableBody",
|
|
params: expect.objectContaining({
|
|
host: "ipwho.is",
|
|
docsUrl: expect.stringContaining("docs/self-hosting/geolite2.md"),
|
|
}),
|
|
metadata: expect.objectContaining({ source: "geo-offline-detection" }),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("names the real online provider in the notification params", async () => {
|
|
writeFileSync(join(tmpRoot, ".empty"), "");
|
|
process.env.IP_GEO_LOOKUP_URL = "https://ip-api.example/json";
|
|
const { lookupIpLocation } = await import("../geo");
|
|
|
|
await lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
|
|
expect(dispatchSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
params: expect.objectContaining({ host: "ip-api.example" }),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("points the admin at a reachable action, never at a repository secret", async () => {
|
|
// issue #659 — the message used to tell a self-hoster to set
|
|
// MAXMIND_LICENSE_KEY in this repository's Actions secrets, which is
|
|
// not something they can do. Every locale must name the mount instead.
|
|
const locales = ["de", "en", "es", "fr", "it", "pl", "ko", "nl"] as const;
|
|
for (const locale of locales) {
|
|
const bundle = JSON.parse(
|
|
readFileSync(
|
|
join(__dirname, `../../../messages/${locale}.json`),
|
|
"utf8",
|
|
),
|
|
) as {
|
|
notifications: { admin: { offlineGeoUnavailableBody: string } };
|
|
};
|
|
const body = bundle.notifications.admin.offlineGeoUnavailableBody;
|
|
expect(body, `${locale} still names a build-time secret`).not.toMatch(
|
|
/MAXMIND_LICENSE_KEY|GitHub|Actions|secret/i,
|
|
);
|
|
expect(body, `${locale} does not name the reachable override`).toContain(
|
|
"GEOLITE2_DIR",
|
|
);
|
|
expect(body, `${locale} drops the {host} placeholder`).toContain(
|
|
"{host}",
|
|
);
|
|
expect(body, `${locale} drops the {docsUrl} placeholder`).toContain(
|
|
"{docsUrl}",
|
|
);
|
|
}
|
|
});
|
|
|
|
it("does NOT fire the notification when the City MMDB is present", async () => {
|
|
writeFileSync(
|
|
join(tmpRoot, "GeoLite2-City.mmdb"),
|
|
Buffer.from("city-stub"),
|
|
);
|
|
// The stubbed reader returns null for every IP, which is fine — the
|
|
// lookup will still go through the online path, but the offline
|
|
// check sees a healthy directory so the notification stays silent.
|
|
const { lookupIpLocation } = await import("../geo");
|
|
|
|
await lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
|
|
expect(dispatchSpy).not.toHaveBeenCalled();
|
|
expect(findManySpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does NOT fire the notification when online lookups are disabled", async () => {
|
|
// With IP_GEO_LOOKUP_DISABLED=1 no address leaves the host, so there
|
|
// is no egress for the notice to warn about — the disabled host has
|
|
// left the state exactly like one that mounted the databases.
|
|
writeFileSync(join(tmpRoot, ".empty"), "");
|
|
process.env.IP_GEO_LOOKUP_DISABLED = "1";
|
|
const { lookupIpLocation } = await import("../geo");
|
|
|
|
await lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
|
|
expect(dispatchSpy).not.toHaveBeenCalled();
|
|
expect(findManySpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("fires the notification exactly once across repeated fallbacks in one process", async () => {
|
|
writeFileSync(join(tmpRoot, ".empty"), "");
|
|
const { lookupIpLocation, lookupIpAsn } = await import("../geo");
|
|
|
|
await lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
await lookupIpLocation("1.1.1.1");
|
|
await flushMicrotasks();
|
|
lookupIpAsn("9.9.9.9");
|
|
await flushMicrotasks();
|
|
|
|
// Three fallbacks, exactly one dispatch.
|
|
expect(dispatchSpy).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("falls back silently when no admin user is configured", async () => {
|
|
writeFileSync(join(tmpRoot, ".empty"), "");
|
|
findManySpy.mockResolvedValueOnce([]);
|
|
const { lookupIpLocation } = await import("../geo");
|
|
|
|
await lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
|
|
expect(findManySpy).toHaveBeenCalledTimes(1);
|
|
expect(dispatchSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("lookupIpAsn fires the notification when the ASN reader is missing and the marker is set", async () => {
|
|
writeFileSync(join(tmpRoot, ".empty"), "");
|
|
const { lookupIpAsn } = await import("../geo");
|
|
|
|
const result = lookupIpAsn("8.8.8.8");
|
|
await flushMicrotasks();
|
|
|
|
expect(result).toBeNull();
|
|
expect(dispatchSpy).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("is once per configuration state: silent across restarts, resent only after exit + re-entry (issue #851)", async () => {
|
|
const cityDb = join(tmpRoot, "GeoLite2-City.mmdb");
|
|
|
|
// ── Boot 1: unconfigured → the notice goes out and anchors durably.
|
|
writeFileSync(join(tmpRoot, ".empty"), "");
|
|
let geo = await import("../geo");
|
|
await geo.lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
expect(dispatchSpy).toHaveBeenCalledTimes(1);
|
|
expect(eventStore).toHaveLength(1);
|
|
|
|
// ── Boot 2: worker restart, state unchanged → the persisted anchor
|
|
// keeps it silent. This is the #851 repeat, pinned closed.
|
|
vi.resetModules();
|
|
geo = await import("../geo");
|
|
await geo.lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
expect(dispatchSpy).toHaveBeenCalledTimes(1);
|
|
expect(eventStore).toHaveLength(1);
|
|
|
|
// ── Boot 3: the operator mounts the databases → state exits, the
|
|
// anchor is released, still no new send.
|
|
unlinkSync(join(tmpRoot, ".empty"));
|
|
writeFileSync(cityDb, Buffer.from("city-stub"));
|
|
vi.resetModules();
|
|
geo = await import("../geo");
|
|
await geo.lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
expect(dispatchSpy).toHaveBeenCalledTimes(1);
|
|
expect(eventStore).toHaveLength(0);
|
|
|
|
// ── Boot 4: the databases are gone again → genuine re-entry into the
|
|
// unconfigured state, and the notice fires afresh.
|
|
unlinkSync(cityDb);
|
|
writeFileSync(join(tmpRoot, ".empty"), "");
|
|
vi.resetModules();
|
|
geo = await import("../geo");
|
|
await geo.lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
expect(dispatchSpy).toHaveBeenCalledTimes(2);
|
|
expect(eventStore).toHaveLength(1);
|
|
});
|
|
|
|
it("re-evaluates after a runtime database fetch resets the reader cache", async () => {
|
|
// The runtime GeoLite2 fetch calls `resetGeoLite2ReaderCache()` after
|
|
// placing fresh databases. The next lookup must observe the healthy
|
|
// state and release the anchor without waiting for a restart.
|
|
writeFileSync(join(tmpRoot, ".empty"), "");
|
|
const geo = await import("../geo");
|
|
|
|
await geo.lookupIpLocation("8.8.8.8");
|
|
await flushMicrotasks();
|
|
expect(dispatchSpy).toHaveBeenCalledTimes(1);
|
|
expect(eventStore).toHaveLength(1);
|
|
|
|
unlinkSync(join(tmpRoot, ".empty"));
|
|
writeFileSync(
|
|
join(tmpRoot, "GeoLite2-City.mmdb"),
|
|
Buffer.from("city-stub"),
|
|
);
|
|
geo.resetGeoLite2ReaderCache();
|
|
|
|
await geo.lookupIpLocation("1.1.1.1");
|
|
await flushMicrotasks();
|
|
expect(dispatchSpy).toHaveBeenCalledTimes(1);
|
|
expect(eventStore).toHaveLength(0);
|
|
});
|
|
});
|