// @ts-nocheck import { beforeEach, expect, it, vi } from "vitest"; const state = vi.hoisted(() => ({ // Rows the fake information_schema reports, keyed by table name. estimates: {} as Record, failEstimate: false, // Value the exact COUNT(*) returns, and how often it actually ran. exactCounts: 0, exactRuns: 0, })); const tables = vi.hoisted(() => ({ User: { name: "users" }, Rooms: { name: "rooms" }, CameraWeb: { name: "camera_web" }, })); vi.mock("@/lib/db", () => { const chain: any = new Proxy(() => {}, { get: (_t, prop) => { if (prop === Symbol.toStringTag) return "Query"; if (prop === "where") return () => chain; if (prop === "then") return (resolve: any, reject: any) => { // Only the estimate lookup is meant to fail here; the exact // fallback has to keep working, so it is not gated. state.exactRuns += 1; return Promise.resolve([{ total: state.exactCounts }]).then( resolve, reject, ); }; return () => chain; }, }); return { db: { select: () => chain, // The information_schema lookup, the only one that matters here. execute: async () => { if (state.failEstimate) throw Error("information_schema unavailable"); return Object.entries(state.estimates) .filter(([, v]) => v !== null) .map(([TABLE_NAME, TABLE_ROWS]) => ({ TABLE_NAME, TABLE_ROWS })); }, }, ...(tables as any), }; }); import { countOnline, countPhotos, countRooms, countUsers, estimatedRowCounts, } from "./public-counters"; beforeEach(() => { state.estimates = {}; state.failEstimate = false; state.exactCounts = 0; state.exactRuns = 0; }); it("reads the engine estimate instead of scanning the table", async () => { // An exact count is recorded, so a non-zero value here means one really ran. state.estimates = { users: 123_456, rooms: 42, camera_web: 7 }; expect(await countUsers()).toBe(123_456); expect(await countRooms()).toBe(42); expect(await countPhotos()).toBe(7); expect(state.exactRuns).toBe(0); }); it("falls back to the exact count when the engine has no estimate", async () => { // A freshly created InnoDB table can report 0, which would show "0 members". state.estimates = { users: 0, rooms: null, camera_web: null }; state.exactCounts = 999; expect(await countUsers()).toBe(999); expect(await countRooms()).toBe(999); expect(await countPhotos()).toBe(999); expect(state.exactRuns).toBe(3); }); it("falls back to the exact count when the estimate lookup fails", async () => { state.failEstimate = true; state.exactCounts = 55; expect(await countUsers()).toBe(55); }); it("never turns a missing estimate into a zero", async () => { // Showing "0" is worse than being slow: it is visibly wrong. state.estimates = { users: null }; state.exactCounts = 1; expect(await countUsers()).toBe(1); expect(await countUsers()).not.toBe(0); }); it("handles the bigint string the driver may hand back", async () => { state.estimates = { users: "98765" }; expect(await countUsers()).toBe(98_765); }); it("returns a null for every requested table it has no estimate for", async () => { expect(await estimatedRowCounts(["users", "rooms"])).toEqual({ users: null, rooms: null, }); }); it("keeps the online count exact", async () => { // Being a few seconds behind looks broken, and it is an indexed count of a // small subset, so there is nothing to gain by approximating it. state.estimates = { users: 5_000_000 }; state.exactCounts = 12; expect(await countOnline()).toBe(12); });