The browser now keeps its half of the one event shape logs.py defines: lib/log.ts is a levelled, redacted, bounded (2000 events) in-memory ring with the server's level names and severity numbers. Nothing leaves the browser yet; sending is the consented diagnostics level (S5). - lib/http.ts `send` is the one fetch: every request carries an X-Request-Id and becomes one http.request event. The server echoes the matched route template in X-Route (only for a matched route, so a raw path with a value in it is never sent back); without one the client templates the path itself. req, beacon, the upload XHR, the public watch page and the legal pages all go through it. - Hooks: app start snapshot, uncaught errors and rejections, History push/replace/popstate (a scroll-only replace is not an event; parameter values only at DEBUG, typed search only at TRACE), notifications, the error dialog, the crash boundary, failed queries and mutations (key head only), and the messages socket (lifecycle, frame types at DEBUG). - Redaction is one contract: backend/tests/fixtures/log_redaction_cases.json runs against both redact() implementations. - Lint: fetch, XMLHttpRequest and WebSocket each belong to one logging module, console to none; a contract test proves the rules fire. Fixes a socket takeover: the close of a socket released by the last subscriber cleared the newer socket a quick re-subscribe had opened and scheduled a third, leaving two live sockets that delivered every message twice (measured in Chromium: 3 created / 2 live before, 2 / 1 after).
75 lines
2.7 KiB
TypeScript
75 lines
2.7 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("./api", () => ({ getActiveAccount: () => 7 }));
|
|
|
|
// A WebSocket double the test drives: each instance is recorded, and its events are fired by hand.
|
|
class FakeSocket {
|
|
static all: FakeSocket[] = [];
|
|
url: string;
|
|
onopen: (() => void) | null = null;
|
|
onmessage: ((ev: { data: string }) => void) | null = null;
|
|
onclose: ((ev: { code: number; wasClean: boolean }) => void) | null = null;
|
|
onerror: (() => void) | null = null;
|
|
closed = false;
|
|
constructor(url: string) {
|
|
this.url = url;
|
|
FakeSocket.all.push(this);
|
|
}
|
|
close() {
|
|
this.closed = true; // the real close event arrives later, on its own task — fired by the test
|
|
}
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
vi.resetModules();
|
|
FakeSocket.all = [];
|
|
vi.stubGlobal("WebSocket", FakeSocket);
|
|
vi.stubGlobal("location", { protocol: "https:", host: "app.example" });
|
|
});
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
const frame = (sock: FakeSocket) =>
|
|
sock.onmessage?.({ data: JSON.stringify({ type: "message", message: { id: 1 } }) });
|
|
|
|
describe("messagesSocket", () => {
|
|
it("a socket closed on unsubscribe cannot take over from the one a re-subscribe opened", async () => {
|
|
const { onMessage } = await import("./messagesSocket");
|
|
const first = vi.fn();
|
|
onMessage(first)(); // subscribe, then leave (a remount / StrictMode's double effect)
|
|
const got = vi.fn();
|
|
onMessage(got);
|
|
const [old, current] = FakeSocket.all;
|
|
expect(old!.closed).toBe(true);
|
|
old!.onclose?.({ code: 1000, wasClean: true }); // the OLD socket's close lands late
|
|
|
|
vi.advanceTimersByTime(60_000);
|
|
expect(FakeSocket.all).toHaveLength(2); // no third socket
|
|
frame(current!);
|
|
expect(got).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("reconnects when the current socket drops, and logs it without the account query", async () => {
|
|
// Imported after resetModules, like the socket module: the ring it writes to is this instance.
|
|
const { onMessage } = await import("./messagesSocket");
|
|
const { events } = await import("./log");
|
|
onMessage(() => {});
|
|
const [sock] = FakeSocket.all;
|
|
expect(sock!.url).toBe("wss://app.example/api/messages/ws?account=7");
|
|
sock!.onopen?.();
|
|
sock!.onclose?.({ code: 1006, wasClean: false });
|
|
vi.advanceTimersByTime(1000);
|
|
expect(FakeSocket.all).toHaveLength(2);
|
|
|
|
const logged = events().map((e) => [e.event, e.level, e.attrs?.path]);
|
|
expect(logged).toEqual([
|
|
["ws.open", "INFO", "/api/messages/ws"],
|
|
["ws.close", "WARNING", "/api/messages/ws"],
|
|
]);
|
|
expect(JSON.stringify(events())).not.toContain("account");
|
|
});
|
|
});
|