Files
piik/tests/channel-navigation.test.ts
T
AAA 1379362997 fix(navigation): keep diagnostic reloads in the selected room
Consume the current overlay marker at the room-history owner before reloading. Diagnostic confirmation no longer races its own history.back cleanup, and the selected room and pane survive the reload. Settings-module retry uses the same operation.

Reproduced through the actual More menu. Chrome verifies confirm, cancel, focus and exported timing records. Both TypeScript projects and the affected navigation/debug tests pass within the 317-test acceptance batch.
2026-11-28 00:04:04 -05:00

68 lines
3.0 KiB
TypeScript

import { afterEach, expect, test, vi } from "vitest";
import { useChannel } from "../src/client/channel/use-channel";
import { reloadFromRoomLayer } from "../src/client/channel/use-room-pane";
const { session, cleanups } = vi.hoisted(() => ({
session: { start: vi.fn(), close: vi.fn(), acceptInvitation: vi.fn(), getSnapshot: () => ({}), subscribe: vi.fn(), refreshReadMarkers: vi.fn() },
cleanups: [] as Array<() => void>,
}));
// Run the hook's real event handlers without introducing a DOM renderer dependency.
vi.mock("react", () => ({
useState: () => [session],
useSyncExternalStore: () => session.getSnapshot(),
useEffect: (effect: () => (() => void) | undefined) => {
const cleanup = effect();
if (cleanup) cleanups.push(cleanup);
},
}));
afterEach(() => {
for (const cleanup of cleanups.splice(0)) cleanup();
vi.unstubAllGlobals(); vi.clearAllMocks();
});
test("a diagnostic reload clears the overlay marker and keeps the selected room and other page state", () => {
const page = { piikChannelRoom: "room-1", piikRoomPane: "people", scrollTop: 48 };
const replaceState = vi.fn(), reload = vi.fn();
vi.stubGlobal("history", { state: { ...page, piikRoomLayer: true }, replaceState });
vi.stubGlobal("location", { reload });
reloadFromRoomLayer("/?debug=1");
expect(replaceState).toHaveBeenCalledWith(page, "", "/?debug=1");
expect(replaceState).toHaveBeenCalledBefore(reload);
});
test("a persisted page return restarts the retired session and cleanup removes that listener", () => {
vi.stubGlobal("window", new EventTarget());
vi.stubGlobal("document", new EventTarget());
const shown = (persisted: boolean) => Object.assign(new Event("pageshow"), { persisted });
useChannel();
expect(session.start).toHaveBeenCalledOnce();
window.dispatchEvent(shown(false));
expect(session.start).toHaveBeenCalledOnce();
window.dispatchEvent(new Event("pagehide"));
expect(session.close).toHaveBeenCalledOnce();
window.dispatchEvent(shown(true));
expect(session.start).toHaveBeenCalledTimes(2);
for (const cleanup of cleanups.splice(0)) cleanup();
window.dispatchEvent(shown(true));
expect(session.start).toHaveBeenCalledTimes(2);
});
test("hash invitation admission cannot replay an older room after navigation", async () => {
vi.stubGlobal("window", new EventTarget());
vi.stubGlobal("document", new EventTarget());
vi.stubGlobal("location", new URL("https://channel.test/#room=original"));
let admitFirst!: (room: string) => void;
const first = new Promise<string>(resolve => { admitFirst = resolve; });
session.acceptInvitation.mockReturnValueOnce(first).mockResolvedValueOnce("second");
useChannel();
location.hash = "room=first&invite=first-key";
window.dispatchEvent(new Event("hashchange"));
location.hash = "room=second";
window.dispatchEvent(new Event("hashchange"));
expect(session.acceptInvitation).toHaveBeenCalledTimes(2);
admitFirst("first"); await first;
expect(new URLSearchParams(location.hash.slice(1)).get("room")).toBe("second");
});