mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 20:42:43 +00:00
`/` runs its reads server-side and hands them to TanStack through a HydrationBoundary, so the first HTML already carries real tiles. React threw that away on every cold load. The app shell hydrates first and its mount answers `/api/auth/me`; the dashboard sits below `loading.tsx`, hydrates later, and its first client render therefore read an account the server never had. That disagreed with the streamed HTML, React reported #418 and re-rendered the whole page. The prefetch paid for its queries and delivered none of its paint. The account payload is now withheld from the hydration render wherever it is only rendered. `useAccountOnceMounted()` answers null until the component has mounted, which is what the server rendered, and hands the payload over on the next render. The dashboard page, the caregiver card, the getting-started checklist, the achievements tile and the unit labels read it that way. `useAuth` itself still reads the cache, because the document vault redirects off `user.modules` and a first render claiming "no account" bounces it to `/`. Two tiles disagreed without any account involved. A query reports `isLoading` true on the client's first render and false on the server, so both now gate on whether the cell holds an answer. Nothing in CI ran the shipped configuration: the suite's server turns the prefetch off so route mocks keep governing the dashboard. A second server runs it on, and one spec asserts that no hydration error is reported and that the tile node the server streamed is still the node in the DOM once the page settles. Measured over nine cold loads with the CPU throttled 4x: seven bailouts before and none after, median LCP 869 ms before and 145 ms after. FCP and the time to the first tile in the DOM are unchanged, because the streamed paint was never the problem. Keeping it was.
162 lines
5.2 KiB
TypeScript
162 lines
5.2 KiB
TypeScript
import { existsSync, readFileSync } from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { parse } from "yaml";
|
|
|
|
import integrationConfig from "../../vitest.integration.config.mts";
|
|
import unitConfig from "../../vitest.config.mts";
|
|
|
|
function repositoryRoot(): string {
|
|
const testPath = expect.getState().testPath;
|
|
if (!testPath) throw new Error("Vitest did not expose the current test path");
|
|
return join(dirname(testPath), "../..");
|
|
}
|
|
|
|
function readRepoFile(path: string): string {
|
|
return readFileSync(join(repositoryRoot(), path), "utf8");
|
|
}
|
|
|
|
type WorkflowStep = {
|
|
if?: string;
|
|
uses?: string;
|
|
with?: Record<string, string | number>;
|
|
};
|
|
|
|
type WorkflowJob = {
|
|
env?: Record<string, string>;
|
|
steps?: WorkflowStep[];
|
|
};
|
|
|
|
type Workflow = {
|
|
jobs: Record<string, WorkflowJob>;
|
|
};
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
vi.resetModules();
|
|
});
|
|
|
|
describe("Playwright CI determinism", () => {
|
|
it("fails CI when a retry resolves a flaky test", async () => {
|
|
vi.stubEnv("CI", "true");
|
|
// Re-import after stubbing CI so config conditionals are evaluated as CI.
|
|
const { default: config } = await import("../../playwright.config");
|
|
|
|
expect(config.retries).toBe(2);
|
|
expect(config.failOnFlakyTests).toBe(true);
|
|
expect(config.reporter).toEqual([["github"], ["html", { open: "never" }]]);
|
|
// Two servers, and which is which matters. The suite's own server runs
|
|
// with the dashboard's RSC prefetch OFF so `page.route` fixtures govern
|
|
// what the dashboard paints; the second runs it ON, which is the default
|
|
// and therefore the only configuration self-hosters ever see. A React #418
|
|
// hydration bailout shipped on `/` precisely because nothing in CI ran the
|
|
// shipped one. Pin both so that gap cannot reopen by deletion.
|
|
const servers = config.webServer;
|
|
expect(Array.isArray(servers)).toBe(true);
|
|
const webServers = servers as Array<Record<string, unknown>>;
|
|
expect(webServers).toHaveLength(2);
|
|
for (const server of webServers) {
|
|
expect(server).toMatchObject({
|
|
command: expect.stringContaining(
|
|
`${JSON.stringify(process.execPath)} .next/standalone/server.js`,
|
|
),
|
|
env: expect.objectContaining({
|
|
NATIVE_CANVAS: "off",
|
|
}),
|
|
});
|
|
}
|
|
expect(webServers[0]!.env).toMatchObject({
|
|
DASHBOARD_SSR_PREFETCH: "false",
|
|
});
|
|
expect(webServers[1]!.env).toMatchObject({
|
|
DASHBOARD_SSR_PREFETCH: "true",
|
|
});
|
|
});
|
|
|
|
it("retains the Playwright report after every workflow outcome", () => {
|
|
const workflow = parse(
|
|
readRepoFile(".github/workflows/e2e.yml"),
|
|
) as Workflow;
|
|
const upload = workflow.jobs.e2e?.steps?.find((step) =>
|
|
step.uses?.startsWith("actions/upload-artifact@"),
|
|
);
|
|
|
|
expect(upload).toBeDefined();
|
|
expect(upload?.if).toBe("always()");
|
|
expect(upload?.with).toMatchObject({
|
|
name: "playwright-report",
|
|
path: "playwright-report/",
|
|
"retention-days": 7,
|
|
"if-no-files-found": "ignore",
|
|
});
|
|
});
|
|
|
|
it("uses production-valid secrets and plain-HTTP cookies", () => {
|
|
const workflow = parse(
|
|
readRepoFile(".github/workflows/e2e.yml"),
|
|
) as Workflow;
|
|
const env = workflow.jobs.e2e?.env;
|
|
|
|
expect(env?.API_TOKEN_HMAC_KEY).toMatch(/^[0-9a-f]{64}$/);
|
|
expect(env?.SESSION_COOKIE_SECURE).toBe("false");
|
|
});
|
|
});
|
|
|
|
describe("local worktree isolation", () => {
|
|
it("keeps nested worktree suites out of the root unit run", () => {
|
|
expect(unitConfig.test?.exclude ?? []).toContain(".worktrees/**");
|
|
});
|
|
});
|
|
|
|
describe("integration environment isolation", () => {
|
|
it("registers a worker setup bridge and authoritative test secrets", () => {
|
|
const test = integrationConfig.test;
|
|
|
|
expect(test?.setupFiles ?? []).toContain(
|
|
"./tests/integration/environment-setup.ts",
|
|
);
|
|
expect(test?.env).toMatchObject({
|
|
TZ: "UTC",
|
|
ENCRYPTION_KEY:
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
ENCRYPTION_KEYS: "",
|
|
ENCRYPTION_ACTIVE_KEY_ID: "",
|
|
API_TOKEN_HMAC_KEY:
|
|
"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
|
|
SESSION_SECRET: "integration-test-session-secret-32-bytes",
|
|
});
|
|
expect(
|
|
existsSync(
|
|
join(repositoryRoot(), "tests/integration/environment-setup.ts"),
|
|
),
|
|
).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("container dependency installation", () => {
|
|
it("copies the prepare helper before the Git-free frozen install", () => {
|
|
const dockerfile = readRepoFile("Dockerfile");
|
|
|
|
expect(dockerfile).toContain(
|
|
"COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./\n" +
|
|
"COPY scripts/prepare.mjs scripts/prepare.mjs\n" +
|
|
"RUN pnpm install --frozen-lockfile --prod=false",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("production dependency advisory floors", () => {
|
|
it("pins vulnerable transitive ranges to compatible patched versions", () => {
|
|
const workspace = parse(readRepoFile("pnpm-workspace.yaml")) as {
|
|
overrides?: Record<string, string>;
|
|
};
|
|
|
|
expect(workspace.overrides).toMatchObject({
|
|
"dompurify@<=3.4.10": "^3.4.11",
|
|
"postcss@<8.5.10": "^8.5.10",
|
|
"@hono/node-server@<1.19.13": "^1.19.13",
|
|
"hono@<4.12.25": "^4.12.25",
|
|
});
|
|
});
|
|
});
|