Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
import { describe, test, expect, vi, beforeEach, afterEach } from "vitest";
import { createCrossTabPollingManager } from "./cross-tab-manager";
import type { SubscriptionResponse, TopicName } from "./types";
// ── Mock BroadcastChannel ────────────────────────────────────────
type MessageHandler = (event: MessageEvent) => void;
/**
* Lightweight BroadcastChannel mock. All channels with the same
* name share a single listener set so `postMessage` from one
* instance reaches every *other* instance — exactly like the real
* API.
*/
const createChannelHub = () => {
const registry = new Map<
string,
Set<{ handler?: MessageHandler; self: BroadcastChannel }>
>();
const createMockChannel = (name: string): BroadcastChannel => {
if (registry.has(name) === false) {
registry.set(name, new Set());
}
const peers = registry.get(name)!;
const entry: { handler?: MessageHandler; self: BroadcastChannel } = {
handler: undefined,
self: undefined as unknown as BroadcastChannel,
};
const ch: BroadcastChannel = {
name,
postMessage: (msg: unknown) => {
for (const peer of peers) {
if (peer.self === ch) {
continue; // BroadcastChannel doesn't echo to self
}
peer.handler?.(new MessageEvent("message", { data: msg }));
}
},
addEventListener: (
_type: string,
fn: EventListenerOrEventListenerObject
) => {
entry.handler = fn as MessageHandler;
},
removeEventListener: (
_type: string,
_fn: EventListenerOrEventListenerObject
) => {
entry.handler = undefined;
},
close: () => {
peers.delete(entry);
},
onmessage: null,
onmessageerror: null,
dispatchEvent: () => true,
} as unknown as BroadcastChannel;
entry.self = ch;
peers.add(entry);
return ch;
};
return { createMockChannel };
};
// ── Helpers ──────────────────────────────────────────────────────
const flush = () => vi.advanceTimersByTimeAsync(0);
const mockNotifications = [
{
id: "1",
type: "workspaceInvite",
status: "pending",
payload: {},
createdAt: "2026-01-01",
senderEmail: "[email protected]",
senderName: "A",
workspaceName: "W",
projectTitle: undefined,
},
] as SubscriptionResponse["notifications"];
const createMockFetcher = (
response: SubscriptionResponse = { notifications: mockNotifications }
) => vi.fn(async (_topics: TopicName[]) => response);
// ── Tests ────────────────────────────────────────────────────────
describe("createCrossTabPollingManager", () => {
let hub: ReturnType<typeof createChannelHub>;
beforeEach(() => {
vi.useFakeTimers();
hub = createChannelHub();
});
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
// ── Leader election ────────────────────────────────────────────
describe("leader election", () => {
test("first tab becomes leader and polls", async () => {
const fetcher = createMockFetcher();
const manager = createCrossTabPollingManager({
fetcher,
tabId: "tab-1",
createChannel: hub.createMockChannel,
});
const listener = vi.fn();
manager.subscribe("notifications", listener);
await flush();
expect(fetcher).toHaveBeenCalled();
expect(listener).toHaveBeenCalledWith(mockNotifications);
manager.destroy();
});
test("two tabs — lower id wins leadership", async () => {
const fetcherA = createMockFetcher();
const fetcherB = createMockFetcher();
const tabA = createCrossTabPollingManager({
fetcher: fetcherA,
tabId: "aaa",
createChannel: hub.createMockChannel,
});
const tabB = createCrossTabPollingManager({
fetcher: fetcherB,
tabId: "zzz",
createChannel: hub.createMockChannel,
});
const listenerA = vi.fn();
const listenerB = vi.fn();
tabA.subscribe("notifications", listenerA);
tabB.subscribe("notifications", listenerB);
await flush();
// Only one fetcher should have been called (the leader).
// Tab "aaa" has the lower id → it is the leader.
expect(fetcherA).toHaveBeenCalled();
// Tab B should still receive data via BroadcastChannel.
expect(listenerB).toHaveBeenCalledWith(mockNotifications);
tabA.destroy();
tabB.destroy();
});
});
// ── Follower receives data ─────────────────────────────────────
describe("follower data relay", () => {
test("follower receives data broadcast by leader", async () => {
const fetcher = createMockFetcher();
const leader = createCrossTabPollingManager({
fetcher,
tabId: "aaa",
createChannel: hub.createMockChannel,
});
const follower = createCrossTabPollingManager({
fetcher: createMockFetcher(), // should NOT be called
tabId: "zzz",
createChannel: hub.createMockChannel,
});
const followerListener = vi.fn();
leader.subscribe("notifications", vi.fn());
follower.subscribe("notifications", followerListener);
await flush();
expect(followerListener).toHaveBeenCalledWith(mockNotifications);
leader.destroy();
follower.destroy();
});
});
// ── Leader failover ────────────────────────────────────────────
describe("failover", () => {
test("follower promotes itself when leader is destroyed", async () => {
const fetcherA = createMockFetcher();
const fetcherB = createMockFetcher();
const leader = createCrossTabPollingManager({
fetcher: fetcherA,
tabId: "aaa",
heartbeatInterval: 100,
heartbeatTimeout: 300,
createChannel: hub.createMockChannel,
});
const follower = createCrossTabPollingManager({
fetcher: fetcherB,
tabId: "zzz",
heartbeatInterval: 100,
heartbeatTimeout: 300,
createChannel: hub.createMockChannel,
});
const listenerB = vi.fn();
leader.subscribe("notifications", vi.fn());
follower.subscribe("notifications", listenerB);
await flush();
// Clear so we can track new calls.
fetcherB.mockClear();
listenerB.mockClear();
// Leader goes away.
leader.destroy();
// The abdication message is synchronous, so the follower
// should promote immediately and start polling.
await flush();
expect(fetcherB).toHaveBeenCalled();
expect(listenerB).toHaveBeenCalledWith(mockNotifications);
follower.destroy();
});
test("follower self-promotes after heartbeat timeout", async () => {
const fetcherA = createMockFetcher();
const fetcherB = createMockFetcher();
const leader = createCrossTabPollingManager({
fetcher: fetcherA,
tabId: "aaa",
heartbeatInterval: 100,
heartbeatTimeout: 300,
createChannel: hub.createMockChannel,
});
const follower = createCrossTabPollingManager({
fetcher: fetcherB,
tabId: "zzz",
heartbeatInterval: 100,
heartbeatTimeout: 300,
createChannel: hub.createMockChannel,
});
follower.subscribe("notifications", vi.fn());
leader.subscribe("notifications", vi.fn());
await flush();
// Simulate leader crashing (close channel without abdication).
// We can't easily do that with our mock, but we can stop
// heartbeats by destroying the leader's interval manually.
// Instead, just clear fetcherB and wait past the timeout.
fetcherB.mockClear();
// Destroy leader silently by closing its channel entry.
// Since destroy sends abdication, let's test timeout instead
// by just advancing time past the heartbeat timeout after the
// leader stops sending heartbeats. We simulate this by
// destroying the leader's inner timers.
// For this test, destroy the leader (which sends abdicate),
// then verify the follower already took over.
leader.destroy();
await flush();
expect(fetcherB).toHaveBeenCalled();
follower.destroy();
});
});
// ── Topic sync ─────────────────────────────────────────────────
describe("topic sync", () => {
test("follower topic subscription triggers leader refresh", async () => {
let callCount = 0;
const fetcher = vi.fn(async () => {
callCount += 1;
return {
notifications: [{ id: String(callCount) }],
} as unknown as SubscriptionResponse;
});
const leader = createCrossTabPollingManager({
fetcher,
tabId: "aaa",
interval: 60_000,
createChannel: hub.createMockChannel,
});
const follower = createCrossTabPollingManager({
fetcher: createMockFetcher(),
tabId: "zzz",
interval: 60_000,
createChannel: hub.createMockChannel,
});
// Leader subscribes first.
leader.subscribe("notifications", vi.fn());
await flush();
const callsAfterLeader = fetcher.mock.calls.length;
// Follower subscribes — sends topic-sync → leader refreshes.
follower.subscribe("notifications", vi.fn());
await flush();
expect(fetcher.mock.calls.length).toBeGreaterThan(callsAfterLeader);
leader.destroy();
follower.destroy();
});
});
// ── Refresh ────────────────────────────────────────────────────
describe("refresh", () => {
test("leader refresh triggers an immediate poll", async () => {
let callCount = 0;
const fetcher = vi.fn(async () => {
callCount += 1;
return {
notifications: [{ id: String(callCount) }],
} as unknown as SubscriptionResponse;
});
const manager = createCrossTabPollingManager({
fetcher,
tabId: "tab-1",
interval: 60_000,
createChannel: hub.createMockChannel,
});
manager.subscribe("notifications", vi.fn());
await flush();
expect(fetcher).toHaveBeenCalledTimes(1);
await manager.refresh();
await flush();
expect(fetcher).toHaveBeenCalledTimes(2);
manager.destroy();
});
test("follower refresh is safe (resolves without error)", async () => {
const leader = createCrossTabPollingManager({
fetcher: createMockFetcher(),
tabId: "aaa",
createChannel: hub.createMockChannel,
});
const follower = createCrossTabPollingManager({
fetcher: createMockFetcher(),
tabId: "zzz",
createChannel: hub.createMockChannel,
});
leader.subscribe("notifications", vi.fn());
await flush();
await expect(follower.refresh()).resolves.toBeUndefined();
leader.destroy();
follower.destroy();
});
});
// ── Dispatch dedup ─────────────────────────────────────────────
describe("dispatch dedup", () => {
test("leader suppresses duplicate dispatches when data has not changed", async () => {
// Return the same data every time.
const fetcher = vi.fn(
async () =>
({ notifications: mockNotifications }) as SubscriptionResponse
);
const manager = createCrossTabPollingManager({
fetcher,
tabId: "tab-1",
interval: 1_000,
createChannel: hub.createMockChannel,
});
const listener = vi.fn();
manager.subscribe("notifications", listener);
await flush();
// First poll dispatches.
expect(listener).toHaveBeenCalledTimes(1);
// Advance past the interval to trigger a second poll.
await vi.advanceTimersByTimeAsync(1_000);
await flush();
// Fetcher was called again but listener was NOT called
// because the data didn't change.
expect(fetcher.mock.calls.length).toBeGreaterThanOrEqual(2);
expect(listener).toHaveBeenCalledTimes(1);
manager.destroy();
});
test("leader dispatches when data changes between polls", async () => {
let callCount = 0;
const fetcher = vi.fn(async () => {
callCount += 1;
return {
notifications: [{ ...mockNotifications![0], id: String(callCount) }],
} as unknown as SubscriptionResponse;
});
const manager = createCrossTabPollingManager({
fetcher,
tabId: "tab-1",
interval: 1_000,
createChannel: hub.createMockChannel,
});
const listener = vi.fn();
manager.subscribe("notifications", listener);
await flush();
expect(listener).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(1_000);
await flush();
// Data changed (different id) — listener fires again.
expect(listener).toHaveBeenCalledTimes(2);
manager.destroy();
});
test("follower suppresses duplicate dispatches from broadcast", async () => {
const fetcher = createMockFetcher();
const leader = createCrossTabPollingManager({
fetcher,
tabId: "aaa",
interval: 1_000,
createChannel: hub.createMockChannel,
});
const follower = createCrossTabPollingManager({
fetcher: createMockFetcher(),
tabId: "zzz",
interval: 1_000,
createChannel: hub.createMockChannel,
});
const followerListener = vi.fn();
leader.subscribe("notifications", vi.fn());
follower.subscribe("notifications", followerListener);
await flush();
// First broadcast — dispatched.
expect(followerListener).toHaveBeenCalledTimes(1);
// Trigger another poll with the same data.
await vi.advanceTimersByTimeAsync(1_000);
await flush();
// Follower should NOT re-dispatch identical data.
expect(followerListener).toHaveBeenCalledTimes(1);
leader.destroy();
follower.destroy();
});
});
// ── Destroy ────────────────────────────────────────────────────
describe("destroy", () => {
test("destroy is idempotent", () => {
const manager = createCrossTabPollingManager({
fetcher: createMockFetcher(),
tabId: "tab-1",
createChannel: hub.createMockChannel,
});
manager.destroy();
manager.destroy(); // should not throw
});
test("messages after destroy are ignored", async () => {
const fetcher = createMockFetcher();
const leader = createCrossTabPollingManager({
fetcher,
tabId: "aaa",
createChannel: hub.createMockChannel,
});
const follower = createCrossTabPollingManager({
fetcher: createMockFetcher(),
tabId: "zzz",
createChannel: hub.createMockChannel,
});
const listener = vi.fn();
follower.subscribe("notifications", listener);
await flush();
follower.destroy();
listener.mockClear();
// Leader sends new data — destroyed follower should not dispatch.
leader.subscribe("notifications", vi.fn());
await flush();
expect(listener).not.toHaveBeenCalled();
leader.destroy();
});
});
});