Files
siftlode/frontend/src/components/ConfigPanel.choice.dom.test.tsx
T
peter a28193a00c test: pin the focus restore, the choice labels and the phone layout to their fix
Three round-1 fixes whose tests passed against the broken version too.

The focus restore keeps watching until the element that closed the box is GONE, rather than spending
itself on the first render after the change — the distinction Chrome exposed and jsdom did not,
because jsdom commits the save in one render where Chrome takes two. Driven now through the general
case instead: the user moved the focus elsewhere while the request was out, renders keep coming
while that element is still there, and the restore happens only once it disappears.

The choice test's i18n mock returned an option's bare value, which is exactly what dropping the
`config.choices.*` lookup renders — a Hungarian admin would have seen `on`/`off` instead of
`GPU`/`Szoftver` with the gate green. Marked labels now, still short enough not to move the
segmented-bar budget the same file measures.

The admin config row's stacking had no test at all, only its sibling fix did — and stacking is the
only fix for a text or number key, whose fixed-width input is the wide control. Class contract, as
SearchBar.dom.test.tsx already does for the narrow-header rules, since jsdom does no layout.

Also: the time-zone cleanup deleted TZ, and Node re-reads it on assignment only, so the process kept
the last zone the file set. Harmless until someone appends a test below it.
2026-09-17 23:45:41 +02:00

141 lines
5.6 KiB
TypeScript

// @vitest-environment jsdom
//
// A closed-set config key is picked, not typed: a short list renders as a segmented bar, a long one as
// a dropdown, and the pick is what Save sends. A cleared draft (reset to the default) shows the default
// as the active option rather than no option at all.
//
// i18n is a key-passthrough, except for an option's label: a full key would distort the
// bar-or-dropdown choice, which weighs the real label length, so a choice id resolves to its value
// under a one-character marker. The marker is what makes a lost lookup visible — rendering the bare
// value instead of the id would be indistinguishable from `label: c`. The admin config API is stubbed.
import { afterEach, describe, expect, it, vi } from "vitest";
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import type { ConfigItem } from "../lib/api";
vi.mock("react-i18next", async (importOriginal) => {
const actual = await importOriginal<typeof import("react-i18next")>();
return {
...actual,
useTranslation: () => ({
// Only an id the fixture really defines resolves: a dropped lookup renders the bare value and
// a malformed id renders the key (long enough to turn every bar into a dropdown), so the
// assertions below see both.
t: (key: string) => CHOICE_LABELS[key] ?? key,
i18n: { language: "en" },
}),
};
});
const choice = (key: string, choices: string[], value: string, is_set = false): ConfigItem => ({
key,
type: "choice",
group: "g",
secret: false,
min: null,
max: null,
is_set,
allow_empty: false,
choices,
value,
default: choices[0],
});
const PRESETS = ["ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow"];
const LEVELS = ["TRACE", "DEBUG", "INFO", "WARNING", "ERROR"];
const CHOICE_LABELS: Record<string, string> = Object.fromEntries(
(
[
["plex_transcode_hw", ["auto", "on", "off"]],
["plex_transcode_preset", PRESETS],
["log_level", LEVELS],
] as const
).flatMap(([key, choices]) =>
choices.map((c) => [`config.choices.${key}.${c}`, `~${c}`] as const),
),
);
const setConfig = vi.fn((_key: string, _value: unknown) => Promise.resolve({}));
const resetConfig = vi.fn((_key: string) => Promise.resolve({}));
vi.mock("../lib/api", async (importOriginal) => {
const actual = await importOriginal<typeof import("../lib/api")>();
return {
...actual,
api: {
...actual.api,
adminConfig: () =>
Promise.resolve({
groups: {
g: [
choice("plex_transcode_hw", ["auto", "on", "off"], "on", true),
choice("plex_transcode_preset", PRESETS, "veryfast"),
choice("log_level", LEVELS, "INFO"),
],
},
secrets_manageable: true,
}),
setConfig: (key: string, value: unknown) => setConfig(key, value),
resetConfig: (key: string) => resetConfig(key),
},
};
});
vi.mock("../lib/useMe", () => ({ useMe: () => ({ email_configured: true }) }));
import ConfigPanel from "./ConfigPanel";
afterEach(() => {
cleanup();
vi.clearAllMocks();
});
const mount = () =>
render(
<QueryClientProvider client={new QueryClient()}>
<ConfigPanel />
</QueryClientProvider>,
);
describe("ConfigPanel choice keys", () => {
it("renders a short list as a segmented bar and saves the pick", async () => {
mount();
const bar = await screen.findByRole("group", { name: "config.fields.plex_transcode_hw.label" });
const pressed = () =>
[...bar.querySelectorAll('[aria-pressed="true"]')].map((b) => b.textContent);
expect(pressed()).toEqual(["~on"]);
expect(screen.queryByRole("textbox")).toBeNull(); // no free-text field for a choice key
fireEvent.click(screen.getByRole("button", { name: "~off" }));
fireEvent.click(await screen.findByRole("button", { name: "config.save" }));
await waitFor(() => expect(setConfig).toHaveBeenCalledWith("plex_transcode_hw", "off"));
});
it("shows the default as active once the override is reset, and Save resets it", async () => {
mount();
await screen.findByRole("group", { name: "config.fields.plex_transcode_hw.label" });
fireEvent.click(screen.getByRole("button", { name: /config\.reset$/ }));
expect(screen.getByRole("button", { name: "~auto" }).getAttribute("aria-pressed")).toBe("true");
fireEvent.click(await screen.findByRole("button", { name: "config.save" }));
await waitFor(() => expect(resetConfig).toHaveBeenCalledWith("plex_transcode_hw"));
});
it("renders a long list as a dropdown showing only the value", async () => {
mount();
const trigger = await screen.findByRole("button", {
name: "config.fields.plex_transcode_preset.label: ~veryfast",
});
expect(trigger.textContent).toBe("~veryfast");
fireEvent.click(trigger);
// The popover rows are queried as FilterSelect.dom.test does (`hidden: true`, picked by text).
const rows = screen.getAllByRole("menuitemradio", { hidden: true });
fireEvent.click(rows.find((r) => r.textContent === "~slow")!);
fireEvent.click(await screen.findByRole("button", { name: "config.save" }));
await waitFor(() => expect(setConfig).toHaveBeenCalledWith("plex_transcode_preset", "slow"));
});
it("a few options with long labels are a dropdown too: a bar that wide does not fit a phone", async () => {
mount();
await screen.findByRole("button", { name: "config.fields.log_level.label: ~INFO" });
expect(screen.queryByRole("group", { name: "config.fields.log_level.label" })).toBeNull();
});
});