fix(boot): review round 1 — reconcile the tab pin, one loading clock, honest probe failures
Deep-review round 1 over main..improvement/watch-background-audio. BootGate - The tab's account pin is corrected to the account the server answered as. The server honours the pin only for an account still signed in to this browser and otherwise answers as the default, so a tab pinned to an account signed out in another tab ran as the default on the server while reading and writing the gone account's local state. - A mounted tab whose identity changes (its session ended, or the case above) reloads instead of rebuilding the tree in place, like every account switch; that also drops the previous account's query cache. The account key on the tree goes: the readyFor gate already unmounted it on every change. - An unanswered setup probe shows the error with Retry, like an unanswered account probe, instead of the signed-out landing to a signed-in user. - Comments: /api/me never 401s; the ended-session path is identity → nobody. Loading states - LoadingState's reveal delay is one clock across loading states that hand over to each other (boot screen → page-chunk fallback → the page's own fetching state), instead of a CSS delay restarted by each, which blanked a visible spinner for another 300 ms at every hand-off. BootScreen is now the shared loading visual, full screen, counting from navigation start. Plex shared link - Persists only the dimensions the link carries again. Writing the default filters for an omitted `pf` created the stored-filters key, which is what tells SavedViewsProvider the account picked filters, so the account's default Plex saved view was never applied again. Watch page - The Repeat toggle uses the shared pressed-pill paint (a solid fill when on) instead of Button variants that differ only in colour, and its flag goes through storage.ts readJSON/writeJSON. - The actions wrap under the title when the row is too narrow, instead of squeezing the title into a column a word wide on a phone; the title breaks long words. Comments that still justified code by the removed adopt effects (usePref, PrefsProvider, FeedViewProvider) are rewritten against the seed-once model. Tests: BootGate error/retry for both probes, setup mode, a failed background refetch keeping the app, the pin correction and the reload on an identity change; the shared reveal clock; the Plex link persist; the audio signature actually moved off version 4; a video download (av and v) still remuxed to an mp4. Each mutation-checked RED for its own reason.
This commit is contained in:
@@ -9,20 +9,26 @@
|
||||
// Real NavigationProvider, real storage and i18n; only the two boot probes are stubbed. The probe
|
||||
// records EVERY render, so "never showed the wrong page, not even for one render" is what is asserted.
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { render, screen, cleanup, act } from "@testing-library/react";
|
||||
import { render, screen, cleanup, act, fireEvent, waitFor } from "@testing-library/react";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import BootGate from "./BootGate";
|
||||
import { NavigationProvider, useNavigation } from "./NavigationProvider";
|
||||
import { api, type Me } from "../lib/api";
|
||||
import { ACTIVE_ACCOUNT_KEY } from "../lib/activeAccount";
|
||||
import { qk } from "../lib/queryKeys";
|
||||
import i18n from "../i18n";
|
||||
|
||||
const renders: { page: string; lang: string }[] = [];
|
||||
let mounts = 0;
|
||||
function Probe() {
|
||||
const { page } = useNavigation();
|
||||
const { i18n: inst } = useTranslation();
|
||||
renders.push({ page, lang: inst.language });
|
||||
useEffect(() => {
|
||||
mounts++;
|
||||
}, []);
|
||||
return <div data-testid="app">{page}</div>;
|
||||
}
|
||||
|
||||
@@ -37,20 +43,24 @@ function mount() {
|
||||
</BootGate>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
return qc;
|
||||
}
|
||||
const account = (over: Partial<Me> = {}) => ({ id: 5, preferences: {}, ...over }) as Me;
|
||||
const retryButton = () => screen.findByRole("button", { name: i18n.t("common.retry") });
|
||||
|
||||
beforeEach(async () => {
|
||||
localStorage.clear();
|
||||
sessionStorage.clear(); // a NEW tab: nothing pinned yet
|
||||
window.history.replaceState(null, "", "/");
|
||||
renders.length = 0;
|
||||
mounts = 0;
|
||||
await i18n.changeLanguage("en");
|
||||
vi.spyOn(api, "setupStatus").mockResolvedValue({ configured: true });
|
||||
});
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("BootGate", () => {
|
||||
@@ -90,4 +100,77 @@ describe("BootGate", () => {
|
||||
await screen.findByTestId("app");
|
||||
expect(sessionStorage.getItem(ACTIVE_ACCOUNT_KEY)).toBeNull();
|
||||
});
|
||||
|
||||
it("setup mode: mounts the app (App shows the wizard) without asking for the account", async () => {
|
||||
vi.mocked(api.setupStatus).mockResolvedValue({ configured: false });
|
||||
const me = vi.spyOn(api, "me").mockResolvedValue(account());
|
||||
mount();
|
||||
|
||||
await screen.findByTestId("app");
|
||||
expect(me).not.toHaveBeenCalled();
|
||||
expect(sessionStorage.getItem(ACTIVE_ACCOUNT_KEY)).toBeNull();
|
||||
});
|
||||
|
||||
it("an unanswered setup probe is an error with a retry — never the signed-out landing", async () => {
|
||||
vi.mocked(api.setupStatus)
|
||||
.mockRejectedValueOnce(new Error("502"))
|
||||
.mockResolvedValue({ configured: true });
|
||||
vi.spyOn(api, "me").mockResolvedValue(account());
|
||||
mount();
|
||||
|
||||
fireEvent.click(await retryButton());
|
||||
await screen.findByTestId("app");
|
||||
expect(renders.length).toBeGreaterThan(0);
|
||||
expect(sessionStorage.getItem(ACTIVE_ACCOUNT_KEY)).toBe("5"); // it mounted for the account
|
||||
});
|
||||
|
||||
it("an unanswered first account probe is an error with a retry", async () => {
|
||||
vi.spyOn(api, "me").mockRejectedValueOnce(new Error("502")).mockResolvedValue(account());
|
||||
mount();
|
||||
|
||||
fireEvent.click(await retryButton());
|
||||
await screen.findByTestId("app");
|
||||
expect(sessionStorage.getItem(ACTIVE_ACCOUNT_KEY)).toBe("5");
|
||||
});
|
||||
|
||||
it("a background refetch that fails keeps the app mounted, as it was", async () => {
|
||||
const me = vi.spyOn(api, "me").mockResolvedValue(account());
|
||||
const qc = mount();
|
||||
await screen.findByTestId("app");
|
||||
|
||||
me.mockRejectedValue(new Error("502"));
|
||||
await act(() => qc.refetchQueries({ queryKey: qk.me() }));
|
||||
// react-query hands the error to its observers on a timer: let that render land before asserting.
|
||||
await act(() => new Promise((r) => setTimeout(r, 20)));
|
||||
|
||||
expect(qc.getQueryState(qk.me())?.status).toBe("error");
|
||||
expect(screen.getByTestId("app")).toBeTruthy();
|
||||
expect(mounts).toBe(1);
|
||||
});
|
||||
|
||||
it("a pin naming another account than the server answered as is corrected before the app reads it", async () => {
|
||||
// Pinned to 9, but 9 was signed out in another tab: the server answers as the browser's default, 5.
|
||||
sessionStorage.setItem(ACTIVE_ACCOUNT_KEY, "9");
|
||||
localStorage.setItem("siftlode.startOnHome.5", "0");
|
||||
localStorage.setItem("siftlode.page.5", "downloads");
|
||||
vi.spyOn(api, "me").mockResolvedValue(account());
|
||||
mount();
|
||||
|
||||
expect((await screen.findByTestId("app")).textContent).toBe("downloads");
|
||||
expect(renders.map((r) => r.page)).toEqual(renders.map(() => "downloads"));
|
||||
expect(sessionStorage.getItem(ACTIVE_ACCOUNT_KEY)).toBe("5");
|
||||
});
|
||||
|
||||
it("a mounted tab whose identity changes reloads rather than keep the previous account's tree", async () => {
|
||||
const me = vi.spyOn(api, "me").mockResolvedValue(account());
|
||||
const qc = mount();
|
||||
await screen.findByTestId("app");
|
||||
const reload = vi.fn();
|
||||
vi.stubGlobal("location", { ...window.location, reload });
|
||||
|
||||
me.mockResolvedValue(null); // the session ended
|
||||
await act(() => qc.refetchQueries({ queryKey: qk.me() }));
|
||||
|
||||
await waitFor(() => expect(reload).toHaveBeenCalled());
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user