fix(startup): reduce initial loading and unblock app restoration

This commit is contained in:
t8y2 committed 2026-09-25 09:03:15 +08:00
1 parent 55c07f5852
commit 48d66d04d0
23 files changed
+745 -123

No files matched your search

@@ -0,0 +1,34 @@
// @vitest-environment happy-dom
import { afterEach, describe, expect, it, vi } from "vitest";
import { checkStartupAuthentication } from "@/lib/startup/startupAuthentication";
afterEach(() => vi.unstubAllGlobals());
describe("startup authentication", () => {
it("preserves authenticated, disabled-auth and setup-required states", async () => {
for (const state of [
{ required: true, authenticated: true, setup_required: false },
{ required: false, authenticated: false, setup_required: false },
{ required: true, authenticated: false, setup_required: true },
]) {
const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => state });
vi.stubGlobal("fetch", fetchMock);
expect(await checkStartupAuthentication()).toEqual(state);
expect(fetchMock).toHaveBeenCalledWith("/api/auth/check", expect.objectContaining({ credentials: "same-origin" }));
}
});
it.each([null, [], "invalid", {}, { required: "false", authenticated: true }, { required: false, authenticated: "true" }])("fails closed for malformed authentication data: %j", async (state) => {
vi.stubGlobal("fetch", vi.fn().mockResolvedValue({ ok: true, json: async () => state }));
await expect(checkStartupAuthentication()).rejects.toThrow("AUTH_CHECK_FAILED");
});
it("fails closed on unsuccessful responses and forwards cancellation", async () => {
const fetchMock = vi.fn().mockResolvedValue({ ok: false });
vi.stubGlobal("fetch", fetchMock);
const controller = new AbortController();
await expect(checkStartupAuthentication(controller.signal)).rejects.toThrow("AUTH_CHECK_FAILED");
expect(fetchMock.mock.calls[0]?.[1].signal).toBe(controller.signal);
});
});
@@ -0,0 +1,23 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { markStartupPhase } from "@/lib/startup/startupTiming";
afterEach(() => vi.unstubAllGlobals());
describe("startup phase marks", () => {
it("records each phase once without confusing gate mount with app readiness", () => {
const entries = new Set<string>();
const mark = vi.fn((name: string) => entries.add(name));
vi.stubGlobal("performance", { mark, getEntriesByName: (name: string) => (entries.has(name) ? [{}] : []) });
markStartupPhase("gate-mounted");
markStartupPhase("gate-mounted");
expect(mark).toHaveBeenCalledTimes(1);
expect(entries.has("dbx:startup:app-mounted")).toBe(false);
markStartupPhase("app-mounted");
expect(mark).toHaveBeenCalledTimes(2);
});
it("does not block startup when performance marks are unavailable", () => {
vi.stubGlobal("performance", {});
expect(() => markStartupPhase("bootstrap")).not.toThrow();
});
});