94 lines
4.5 KiB
TypeScript
94 lines
4.5 KiB
TypeScript
import { afterEach, expect, test, vi } from "vitest";
|
|
import { rememberRoom, selectedRoom } from "../src/client/channel/navigation";
|
|
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");
|
|
});
|
|
|
|
test("a room switch writes an address for the selected room and discards the previous room invite", () => {
|
|
const location = new URL("https://channel.test/?debug=1#room=first&invite=old-key&owner=owner-key&launch=launch-key&piik-client=1");
|
|
const page = { piikChannelRoom: "remembered", piikRoomPane: "chat" };
|
|
const entry = { state: page };
|
|
const replaceState = vi.fn((state, _title, url) => { entry.state = state; location.href = String(url); });
|
|
vi.stubGlobal("location", location);
|
|
vi.stubGlobal("history", Object.assign(entry, { replaceState }));
|
|
expect(selectedRoom()).toBe("first");
|
|
rememberRoom("first");
|
|
expect(new URLSearchParams(location.hash.slice(1)).get("invite")).toBe("old-key");
|
|
rememberRoom("second room");
|
|
expect(location.href).toBe("https://channel.test/?debug=1#room=second+room&owner=owner-key&launch=launch-key&piik-client=1");
|
|
expect(history.state).toEqual({ ...page, piikChannelRoom: "second room" });
|
|
expect(selectedRoom()).toBe("second room");
|
|
rememberRoom("");
|
|
expect(location.href).toBe("https://channel.test/?debug=1#owner=owner-key&launch=launch-key&piik-client=1");
|
|
expect(selectedRoom()).toBe("");
|
|
});
|
|
|
|
test("a roomless address can restore an older history entry's room", () => {
|
|
vi.stubGlobal("location", new URL("https://channel.test/"));
|
|
vi.stubGlobal("history", { state: { piikChannelRoom: "remembered" } });
|
|
expect(selectedRoom()).toBe("remembered");
|
|
});
|