Files
patriothacks/src/shell/ShellLayout.test.tsx
T

112 lines
4.2 KiB
TypeScript

// @vitest-environment happy-dom
import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen, waitFor } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("../admin/loadHollowDemo", async (importOriginal) => {
const actual = await importOriginal<typeof import("../admin/loadHollowDemo")>();
return { ...actual, loadHollowDemoFixture: vi.fn(actual.loadHollowDemoFixture) };
});
vi.mock("../admin/seedEvents", async (importOriginal) => {
const actual = await importOriginal<typeof import("../admin/seedEvents")>();
return { ...actual, hasDemoFixtureBeenLoaded: vi.fn(actual.hasDemoFixtureBeenLoaded) };
});
import { hasDemoFixtureBeenLoaded } from "../admin/seedEvents";
import { loadHollowDemoFixture } from "../admin/loadHollowDemo";
import { AppRoutes } from "../AppRoutes";
import { db } from "../lib/db";
import { readAll, type StoredEventLogEntry } from "../lib/eventlog";
afterEach(cleanup);
beforeEach(async () => {
indexedDB.deleteDatabase(db.name);
db.close();
await db.open();
vi.mocked(loadHollowDemoFixture).mockClear();
vi.mocked(hasDemoFixtureBeenLoaded).mockClear();
});
function renderShellAtHome() {
return render(
<MemoryRouter initialEntries={["/"]}>
<AppRoutes />
</MemoryRouter>,
);
}
/**
* Inline, UN-mocked marker check for test polling: `hasDemoFixtureBeenLoaded`
* itself is a counted spy in this file (used to assert how many times
* *production* code invokes it), so polling through the spy would inflate
* that count with the poll's own repeated calls instead of the real
* ShellLayout/loadHollowDemoFixture call sites.
*/
function fixtureMarkerPresent(entries: readonly StoredEventLogEntry[]): boolean {
return entries.some((e) => (e.payload as { type?: string })?.type === "seed.demo.loaded");
}
async function waitForAutoSeedToSettle() {
await waitFor(async () => {
expect(fixtureMarkerPresent(await readAll())).toBe(true);
});
}
describe("ShellLayout - boot-time Hollow demo auto-seed (plan todo 5)", () => {
it("happy: mounting the shell on a genuinely empty Event Log auto-seeds the Hollow demo fixture without ever visiting /admin", async () => {
expect(await readAll()).toHaveLength(0);
renderShellAtHome();
await waitForAutoSeedToSettle();
expect(loadHollowDemoFixture).toHaveBeenCalledTimes(1);
const balance = await screen.findByTestId("wallet-balance");
await waitFor(() => {
expect(balance.textContent).not.toBe("--");
});
expect(balance.textContent).not.toBe("No balance yet");
const numericBalance = Number(balance.textContent);
expect(Number.isFinite(numericBalance)).toBe(true);
expect(numericBalance).toBeGreaterThan(0);
expect(screen.getByRole("grid", { name: "Miniapps" })).toBeInTheDocument();
});
it("failure (idempotent): a second mount (simulating a reload) never re-invokes the seeding function and appends zero duplicate events", async () => {
const { unmount } = renderShellAtHome();
await waitForAutoSeedToSettle();
expect(loadHollowDemoFixture).toHaveBeenCalledTimes(1);
// 2 total production calls so far: ShellLayout's own check (false, so
// it called the seeder) + the seeder's own internal idempotency check
// (also false, at entry, before it appended anything).
expect(hasDemoFixtureBeenLoaded).toHaveBeenCalledTimes(2);
const countAfterFirstMount = (await readAll()).length;
expect(countAfterFirstMount).toBeGreaterThan(0);
unmount();
renderShellAtHome();
// Wait for the 3rd total production call: ShellLayout's second-mount
// check. A correct idempotent wiring finds the marker and returns
// without ever calling `loadHollowDemoFixture` again, so its own
// internal 4th call never happens.
await waitFor(() => {
expect(hasDemoFixtureBeenLoaded).toHaveBeenCalledTimes(3);
});
expect(loadHollowDemoFixture).toHaveBeenCalledTimes(1);
const entries = await readAll();
expect(entries).toHaveLength(countAfterFirstMount);
const markerEntries = entries.filter(
(e) => (e.payload as { type?: string }).type === "seed.demo.loaded",
);
expect(markerEntries).toHaveLength(1);
});
});