chore(release): v1.4.27

v1.4.27 — Mobile capability and maintainer-finding cleanup.

ResponsiveSheet primitive (bottom-sheet on narrow viewports, centred dialog on desktop)
applied to BD, Weight, Glucose, Mood, Activity forms. Maintainer-finding cleanups across
Coach, Insights, Reports, Privacy. GeoLite2 offline geocoding with resilience layer
(graceful fallback when EULA not yet propagated). E2E gap-fill coverage. 450-line
release notes attached to the GitHub Release.
This commit is contained in:
Marc-André Bombeck authored and GitHub committed 2026-05-15 22:59:10 +02:00
1 parent d98e85cd03
commit 4f09c3f862
314 files changed
+26801 -4419

No files matched your search

+6 -6
View File
@@ -141,13 +141,13 @@ describe("DEFAULT_DASHBOARD_LAYOUT contract", () => {
expect(ids).toContain("achievements");
});
it("includes the insightsPreview widget (v1.4.16 phase D reconcile C2)", () => {
// CRITICAL C2: dashboard preview of the polished AI recommendations
// surface. Same Zod-enum-drift trap as A5 — if the widget id isn't
// listed here, the API PUT silently 422s and the layout toggle
// never persists. Pin the contract to catch a regression early.
it("does NOT advertise a retired insightsPreview widget (v1.4.27 B1)", () => {
// v1.4.27 B1 retired the standalone dashboard preview because it
// duplicated the much-richer `/insights` advisor surface. The
// widget id must stay out of the default layout so a stale client
// cannot reintroduce it via a layout PUT.
const ids = DEFAULT_DASHBOARD_LAYOUT.widgets.map((w) => w.id);
expect(ids).toContain("insightsPreview");
expect(ids).not.toContain("insightsPreview");
});
it("survives a round-trip through serialize → resolve unchanged", () => {
+267
View File
@@ -0,0 +1,267 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
/**
* v1.4.27 B3 — ASN + carrier lookup tests.
*
* The MaxMind GeoLite2-ASN MMDB is too large for the test fixtures
* directory (~10 MB) and shipping it would bloat git history. These
* tests mock `mmdb-lib` directly so the assertion focuses on the
* resolver contract:
*
* - Private / loopback IPs short-circuit to `null` without touching
* the reader.
* - A missing MMDB file (no offline DB on the host) yields `null`.
* - A row from the DB with both `autonomous_system_number` and
* `autonomous_system_organization` is folded into `{ asn, carrier }`.
* - Reader throws are swallowed (the helper is called from a
* fire-and-forget audit-log path; a thrown error would surface as
* an unhandled rejection).
* - The offline-first `lookupIpLocation` path picks the German city
* name first, English second, with the country ISO code from
* `country` or `registered_country`.
*
* The MMDB Reader is mocked once at the top of the file; per-test
* shape control happens through the `setReaderRow` + `setReaderThrows`
* helpers below. The `fs` stub is also installed module-wide so the
* lazy loader believes the DB exists.
*/
interface AsnRow {
autonomous_system_number?: number;
autonomous_system_organization?: string;
}
interface CityRow {
city?: { names?: Record<string, string> };
country?: { iso_code?: string };
registered_country?: { iso_code?: string };
}
// Test-time spy state — captured by the mocked Reader. The Reader
// constructor inspects the buffer header to decide whether it's
// serving City or ASN rows.
const readerState = {
city: null as ((ip: string) => CityRow | null) | null,
asn: null as ((ip: string) => AsnRow | null) | null,
cityThrows: false,
asnThrows: false,
cityExists: false,
asnExists: false,
};
vi.mock("mmdb-lib", () => ({
Reader: class {
private kind: "city" | "asn";
constructor(buf: Buffer) {
const head = buf.toString("utf-8", 0, 4);
this.kind = head === "city" ? "city" : "asn";
}
get(ip: string): AsnRow | CityRow | null {
if (this.kind === "asn") {
if (readerState.asnThrows) throw new Error("mmdb-asn boom");
return readerState.asn ? readerState.asn(ip) : null;
}
if (readerState.cityThrows) throw new Error("mmdb-city boom");
return readerState.city ? readerState.city(ip) : null;
}
},
}));
vi.mock("node:fs", async () => {
const actual = await vi.importActual<typeof import("node:fs")>("node:fs");
const existsSync = (p: string) => {
if (p.endsWith("GeoLite2-City.mmdb")) return readerState.cityExists;
if (p.endsWith("GeoLite2-ASN.mmdb")) return readerState.asnExists;
return false;
};
const readFileSync = (p: string) => {
if (p.endsWith("GeoLite2-City.mmdb"))
return Buffer.from("city-mmdb-stub");
if (p.endsWith("GeoLite2-ASN.mmdb"))
return Buffer.from("asn-mmdb-stub");
throw new Error(`unexpected read ${p}`);
};
return {
...actual,
existsSync,
readFileSync,
default: { ...actual, existsSync, readFileSync },
};
});
const ORIGINAL_ENV = { ...process.env };
beforeEach(async () => {
process.env = { ...ORIGINAL_ENV };
delete process.env.IP_GEO_LOOKUP_DISABLED;
delete process.env.GEOLITE2_DIR;
readerState.city = null;
readerState.asn = null;
readerState.cityThrows = false;
readerState.asnThrows = false;
readerState.cityExists = false;
readerState.asnExists = false;
vi.resetModules();
// Reset the lazy cache so each test gets a fresh Reader load.
const mod = await import("../geo");
mod.__resetGeoLite2CacheForTests();
});
afterEach(() => {
process.env = ORIGINAL_ENV;
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
describe("lookupIpAsn — offline ASN resolver (v1.4.27 B3)", () => {
it("returns null for private + loopback addresses without touching the reader", async () => {
const get = vi.fn();
readerState.asn = get;
readerState.asnExists = true;
const { lookupIpAsn } = await import("../geo");
expect(lookupIpAsn("10.0.0.5")).toBeNull();
expect(lookupIpAsn("127.0.0.1")).toBeNull();
expect(lookupIpAsn("::1")).toBeNull();
expect(lookupIpAsn(null)).toBeNull();
expect(get).not.toHaveBeenCalled();
});
it("returns null when the GeoLite2-ASN MMDB is missing", async () => {
readerState.asnExists = false;
const { lookupIpAsn } = await import("../geo");
expect(lookupIpAsn("8.8.8.8")).toBeNull();
});
it("returns { asn, carrier } when the reader resolves a public IP", async () => {
readerState.asnExists = true;
readerState.asn = () => ({
autonomous_system_number: 3320,
autonomous_system_organization: "Deutsche Telekom AG",
});
const { lookupIpAsn } = await import("../geo");
expect(lookupIpAsn("84.131.0.1")).toEqual({
asn: 3320,
carrier: "Deutsche Telekom AG",
});
});
it("returns null when the reader has no row for the IP", async () => {
readerState.asnExists = true;
readerState.asn = () => null;
const { lookupIpAsn } = await import("../geo");
expect(lookupIpAsn("8.8.8.8")).toBeNull();
});
it("returns the asn with carrier=null when the org field is missing", async () => {
readerState.asnExists = true;
readerState.asn = () => ({ autonomous_system_number: 64500 });
const { lookupIpAsn } = await import("../geo");
expect(lookupIpAsn("8.8.8.8")).toEqual({ asn: 64500, carrier: null });
});
it("swallows reader throws and returns null (caller is fire-and-forget)", async () => {
readerState.asnExists = true;
readerState.asnThrows = true;
const { lookupIpAsn } = await import("../geo");
expect(lookupIpAsn("8.8.8.8")).toBeNull();
});
});
describe("lookupIpLocation — offline-first city resolver (v1.4.27 B3)", () => {
it("prefers the German exonym from the MMDB Names record", async () => {
readerState.cityExists = true;
readerState.city = () => ({
city: { names: { de: "München", en: "Munich" } },
country: { iso_code: "DE" },
});
const fetchSpy = vi.fn();
vi.stubGlobal("fetch", fetchSpy);
const { lookupIpLocation } = await import("../geo");
expect(await lookupIpLocation("85.214.0.1")).toBe("München, DE");
// Offline hit — no online fallback was needed.
expect(fetchSpy).not.toHaveBeenCalled();
});
it("falls back to the English city name when the German exonym is absent", async () => {
readerState.cityExists = true;
readerState.city = () => ({
city: { names: { en: "Birmingham" } },
country: { iso_code: "GB" },
});
const fetchSpy = vi.fn();
vi.stubGlobal("fetch", fetchSpy);
const { lookupIpLocation } = await import("../geo");
expect(await lookupIpLocation("212.58.224.1")).toBe("Birmingham, GB");
});
it("accepts the registered_country ISO when country is absent", async () => {
readerState.cityExists = true;
readerState.city = () => ({
city: { names: { en: "Geneva" } },
registered_country: { iso_code: "CH" },
});
const { lookupIpLocation } = await import("../geo");
expect(await lookupIpLocation("194.158.0.1")).toBe("Geneva, CH");
});
it("falls back to the online provider when the offline DB has no city/country", async () => {
readerState.cityExists = true;
readerState.city = () => ({ city: { names: {} } });
const fetchSpy = vi.fn().mockResolvedValue(
new Response(
new TextEncoder().encode(
JSON.stringify({ success: true, city: "Hamburg", country_code: "DE" }),
).buffer as ArrayBuffer,
{
status: 200,
headers: { "content-type": "application/json; charset=utf-8" },
},
),
);
vi.stubGlobal("fetch", fetchSpy);
const { lookupIpLocation } = await import("../geo");
expect(await lookupIpLocation("8.8.8.8")).toBe("Hamburg, DE");
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
it("falls back to the online provider when the offline DB is missing entirely", async () => {
readerState.cityExists = false;
const fetchSpy = vi.fn().mockResolvedValue(
new Response(
new TextEncoder().encode(
JSON.stringify({ success: true, city: "Berlin", country_code: "DE" }),
).buffer as ArrayBuffer,
{
status: 200,
headers: { "content-type": "application/json; charset=utf-8" },
},
),
);
vi.stubGlobal("fetch", fetchSpy);
const { lookupIpLocation } = await import("../geo");
expect(await lookupIpLocation("8.8.8.8")).toBe("Berlin, DE");
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
it("skips the online fallback when IP_GEO_LOOKUP_DISABLED=1 and offline misses", async () => {
process.env.IP_GEO_LOOKUP_DISABLED = "1";
readerState.cityExists = false;
const fetchSpy = vi.fn();
vi.stubGlobal("fetch", fetchSpy);
const { lookupIpLocation } = await import("../geo");
expect(await lookupIpLocation("8.8.8.8")).toBeNull();
expect(fetchSpy).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,186 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
/**
* v1.4.27 R5 — runtime detection + one-shot 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, fires a single localised
* notification to every admin, and stays silent for the rest of the
* process lifetime.
*
* The tests cover the three states the design pins:
*
* 1. `.empty` marker present, City MMDB absent → notification fires
* on first public-IP lookup.
* 2. No marker, City MMDB present → no notification fires.
* 3. Marker present, repeat lookups → notification fires once and
* never again.
*
* `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, and the
* `dispatchLocalisedNotification` + `prisma.user.findMany` are mocked
* so the test never reaches a database or a sender.
*/
vi.mock("mmdb-lib", () => ({
Reader: class {
constructor(_buf: Buffer) {
void _buf;
}
get(ip: string): unknown {
void ip;
return null;
}
},
}));
const dispatchSpy = vi.fn(async () => undefined);
vi.mock("@/lib/notifications/dispatch-localised", () => ({
dispatchLocalisedNotification: dispatchSpy,
}));
const findManySpy = vi.fn(async () => [{ id: "admin-1" }]);
vi.mock("@/lib/db", () => ({
prisma: {
user: { findMany: findManySpy },
},
}));
const ORIGINAL_ENV = { ...process.env };
let tmpRoot: string;
async function flushMicrotasks(): Promise<void> {
// The notification path is fire-and-forget — `void notify…()` —
// 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" }]);
vi.resetModules();
});
afterEach(() => {
rmSync(tmpRoot, { recursive: true, force: true });
process.env = ORIGINAL_ENV;
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
describe("offline-geo runtime detection (v1.4.27 R5)", () => {
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("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 one-shot admin notification when the marker is present and lookupIpLocation falls back", async () => {
writeFileSync(join(tmpRoot, ".empty"), "");
// Disable the online fallback so the test does not hit the network.
process.env.IP_GEO_LOOKUP_DISABLED = "1";
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({
secretsUrl: expect.stringContaining(
"github.com/MBombeck/HealthLog/settings/secrets/actions",
),
}),
metadata: expect.objectContaining({ source: "geo-offline-detection" }),
}),
);
});
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.
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 (process-level latch)", async () => {
writeFileSync(join(tmpRoot, ".empty"), "");
process.env.IP_GEO_LOOKUP_DISABLED = "1";
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"), "");
process.env.IP_GEO_LOOKUP_DISABLED = "1";
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);
});
});