feat: Settings → Diagnostics, a consented log level and the tab's own log
A user can raise their diagnostics level to Részletes (DEBUG) or Teljes (TRACE) for 24 hours from a new Settings tab, after a consent box that says what is recorded, that the server keeps it and for how long (the instance's own retention), and that it lapses. Alap is the base and needs no consent. - users.diagnostics_level/_until (migration 0087), written only by PUT /api/me/diagnostics, which computes the expiry itself; the demo account cannot raise it. /api/me reports the level in force. - The server logs an elevated user's requests at that level and stores every line of them (logs.LevelGate on the stdout handler and the sink; the root is lowered only while an elevation is active). Teljes adds the redacted query string to the request line. - POST /api/diagnostics/events stores the browser's log as source=web rows: account from the session, body capped while streaming, events/attrs capped, re-redacted, events under the consented level dropped, timestamps clamped, 30 batches a minute, 409 once the level lapsed. - lib/diagnostics.ts applies the level to the tab and streams only what happens after consent, in batches, never its own request lines, with a keepalive flush on pagehide; stops on lapse (its own timer), on withdrawal and on 409. - Detail hooks at DEBUG: which control was clicked (test id, icon, app copy in English — never user text, input values or a link's query), visibility/bfcache/resize, filter changes (lib/filterLog.ts; typed search only at TRACE), player events (lib/mediaLog.ts; media errors are warnings at every level). Offline/online at the base level. - "Saját napló" viewer: live, newest first, level groups with counts, search, row details, copy the filtered events as JSON lines, clear. - Privacy policy and Terms exist in Hungarian and English (a contract test holds both languages to the same sections); the policy now describes the server logs and diagnostic logging, self-service deletion and password sign-in. Fixed on the way: - A sync auth dependency ran logs.set_user on a copied context, so the account never reached a handler's own log lines (measured); a per-request mutable scope carries it now. - The Settings tab rail left a phone no room (clipped labels, buttons over text): below `sm` it is a scrolling row above the content. - PUT /api/me/preferences took a bare dict body; it is a Pydantic model now.
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// E22 S5: "Saját napló" shows this tab's own log live, newest first, filters it by level group and
|
||||
// text, and copies exactly what is filtered (oldest first, one JSON event per line).
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { act, cleanup, fireEvent, render, screen, within } from "@testing-library/react";
|
||||
|
||||
import { clear, log, setLevel } from "../lib/log";
|
||||
|
||||
vi.mock("react-i18next", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("react-i18next")>();
|
||||
return {
|
||||
...actual,
|
||||
useTranslation: () => ({
|
||||
t: (key: string, opts?: { count?: number }) =>
|
||||
opts?.count != null ? `${key}:${opts.count}` : key,
|
||||
}),
|
||||
};
|
||||
});
|
||||
const copied: string[] = [];
|
||||
vi.mock("../lib/clipboard", () => ({
|
||||
copyText: (text: string) => {
|
||||
copied.push(text);
|
||||
return Promise.resolve(true);
|
||||
},
|
||||
}));
|
||||
|
||||
import MyLogViewer from "./MyLogViewer";
|
||||
|
||||
beforeEach(() => {
|
||||
setLevel("DEBUG");
|
||||
log.info("nav.view", "push feed", { page: "feed" });
|
||||
log.warn("http.request", "GET /api/feed 503", { status: 503 });
|
||||
log.debug("ui.click", "click feed-refresh", { control: "feed-refresh" });
|
||||
});
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
clear();
|
||||
setLevel("INFO");
|
||||
copied.length = 0;
|
||||
});
|
||||
|
||||
const rows = () =>
|
||||
within(screen.getByTestId("my-log-lines"))
|
||||
.queryAllByRole("button")
|
||||
.map((b) => b.textContent);
|
||||
|
||||
describe("MyLogViewer", () => {
|
||||
it("lists the tab's events newest first and follows new ones", async () => {
|
||||
render(<MyLogViewer />);
|
||||
expect(rows().map((r) => r!.includes("ui.click"))).toEqual([true, false, false]);
|
||||
await act(async () => {
|
||||
log.error("app.uncaught", "TypeError: x is undefined");
|
||||
await new Promise((r) => requestAnimationFrame(() => r(null)));
|
||||
});
|
||||
expect(rows()[0]).toContain("app.uncaught");
|
||||
});
|
||||
|
||||
it("filters by level group and by text, and copies only what is shown", async () => {
|
||||
render(<MyLogViewer />);
|
||||
fireEvent.click(screen.getByRole("button", { name: /settings\.myLog\.filters\.warning/ }));
|
||||
expect(rows()).toHaveLength(1);
|
||||
expect(rows()[0]).toContain("GET /api/feed 503");
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: /settings\.myLog\.filters\.all/ }));
|
||||
fireEvent.change(screen.getByRole("searchbox"), { target: { value: "FEED-REFRESH" } });
|
||||
expect(rows()).toHaveLength(1);
|
||||
|
||||
const copy = async () => {
|
||||
await act(async () => {
|
||||
fireEvent.click(screen.getByRole("button", { name: "settings.myLog.copy" }));
|
||||
});
|
||||
return copied[copied.length - 1]!.split("\n").map((l) => JSON.parse(l).event);
|
||||
};
|
||||
expect(await copy()).toEqual(["ui.click"]); // only what the search left
|
||||
|
||||
fireEvent.change(screen.getByRole("searchbox"), { target: { value: "" } });
|
||||
expect(await copy()).toEqual(["nav.view", "http.request", "ui.click"]); // oldest first
|
||||
});
|
||||
|
||||
it("opens a row to its details and clears the log", async () => {
|
||||
render(<MyLogViewer />);
|
||||
const first = within(screen.getByTestId("my-log-lines")).getAllByRole("button")[0]!;
|
||||
fireEvent.click(first);
|
||||
expect(first.getAttribute("aria-expanded")).toBe("true");
|
||||
expect(screen.getByTestId("my-log-lines").textContent).toContain('"control": "feed-refresh"');
|
||||
|
||||
await act(async () => {
|
||||
fireEvent.click(screen.getByRole("button", { name: "settings.myLog.clear" }));
|
||||
await new Promise((r) => requestAnimationFrame(() => r(null)));
|
||||
});
|
||||
expect(rows()).toEqual([]);
|
||||
expect(screen.getByText("settings.myLog.emptyAll")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user