v4: settings are stored on the server, starting with the style, the color scheme and the digest switches — every setting lived in the browser's localStorage, so it didn't follow an account to another browser and the server couldn't read it, which the digests need (whether to send each, and in which style). user_settings (migration 0019) holds one row per user who changed anything, the defaults otherwise, and GET/PATCH /api/users/me/settings read and change it (settings-service, model/settings, validators/settings, shared/settings). AuthProvider reads the settings into the session alongside the user, on mount and on focus, so a change made elsewhere arrives; useSettings changes them at once and puts them back if the server refuses. ThemeProvider now sits inside AuthProvider: a signed-in user's choice comes from their settings and is mirrored to localStorage for index.html's first-paint script, a visitor's stays in the browser, and an account with no choice yet takes the one this browser had, once. Until anyone picks a style, the instance's GERBEUR_DEFAULT_STYLE applies, written into index.html's default-style tag along with email-enabled (src/config/instance.ts), and a browser no longer stores the default as if it had been chosen, so changing the instance's style reaches everyone who never picked one. The settings tab gets an Email section with the weekly digest and monthly recap switches, disabled with a note when the server has no SMTP. The style and scheme names moved to shared/theme.ts in the previous commit; the default feed tab and sounds stay in the browser for now. 223 backend tests (new: settings defaults, patches, isolation, refusals and cascade; index.html's new tags) and 658 frontend tests (new: useSettings, the account's theme winning and a browser's carried over, settings read into the session, the digest switches) pass, and tsc, eslint, doc:lint, deno check, deno lint and deno fmt are clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { act, renderHook } from "@testing-library/react";
|
||||
import { act, renderHook, waitFor } from "@testing-library/react";
|
||||
import type { ReactNode } from "react";
|
||||
import { ThemeProvider } from "../../../contexts/theme/ThemeProvider.tsx";
|
||||
import { AuthProvider } from "../../../contexts/auth/AuthProvider.tsx";
|
||||
import { useTheme } from "../../../hooks/theme/useTheme.ts";
|
||||
import { DEFAULT_SETTINGS } from "../../../../shared/settings.ts";
|
||||
import { calls, route } from "../../fetch.ts";
|
||||
import { makeSession, storeSession, wire } from "../../fixtures.ts";
|
||||
|
||||
describe("ThemeProvider", () => {
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
@@ -43,4 +47,57 @@ describe("ThemeProvider", () => {
|
||||
"smooth",
|
||||
);
|
||||
});
|
||||
|
||||
describe("signed in", () => {
|
||||
const authWrapper = ({ children }: { children: ReactNode }) => (
|
||||
<AuthProvider>
|
||||
<ThemeProvider>{children}</ThemeProvider>
|
||||
</AuthProvider>
|
||||
);
|
||||
const signIn = (settings: typeof DEFAULT_SETTINGS) => {
|
||||
const session = makeSession();
|
||||
storeSession(session);
|
||||
route("GET /api/users/me", () => wire(session.user));
|
||||
route("GET /api/users/me/settings", () => settings);
|
||||
route(
|
||||
"PATCH /api/users/me/settings",
|
||||
(req) => ({ ...settings, ...(req.body as object) }),
|
||||
);
|
||||
};
|
||||
|
||||
test("the account's choice wins over this browser's, and is kept here", async () => {
|
||||
localStorage.setItem("style", "nyt");
|
||||
signIn({ ...DEFAULT_SETTINGS, style: "kanagawa", colorScheme: "dark" });
|
||||
const { result } = renderHook(useTheme, { wrapper: authWrapper });
|
||||
await waitFor(() => expect(result.current.style).toBe("kanagawa"));
|
||||
expect(result.current.colorScheme).toBe("dark");
|
||||
expect(root.getAttribute("data-style")).toBe("kanagawa");
|
||||
expect(localStorage.getItem("style")).toBe("kanagawa");
|
||||
expect(calls("PATCH /api/users/me/settings")).toHaveLength(0);
|
||||
|
||||
act(() => result.current.setStyle("melange"));
|
||||
await waitFor(() =>
|
||||
expect(calls("PATCH /api/users/me/settings")[0]?.body).toEqual({
|
||||
style: "melange",
|
||||
})
|
||||
);
|
||||
expect(result.current.style).toBe("melange");
|
||||
act(() => result.current.setStyle("smooth"));
|
||||
act(() => result.current.setColorScheme("auto"));
|
||||
});
|
||||
|
||||
test("an account without a choice takes this browser's, once", async () => {
|
||||
localStorage.setItem("style", "brutalist");
|
||||
signIn({ ...DEFAULT_SETTINGS });
|
||||
const { result } = renderHook(useTheme, { wrapper: authWrapper });
|
||||
await waitFor(() =>
|
||||
expect(calls("PATCH /api/users/me/settings")).toHaveLength(1)
|
||||
);
|
||||
expect(calls("PATCH /api/users/me/settings")[0].body).toEqual({
|
||||
style: "brutalist",
|
||||
});
|
||||
expect(result.current.style).toBe("brutalist");
|
||||
act(() => result.current.setStyle("smooth"));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user