77 lines
3.7 KiB
TypeScript
77 lines
3.7 KiB
TypeScript
import { DOMImplementation } from "@xmldom/xmldom";
|
|
import { act, createElement } from "react";
|
|
import { createRoot } from "react-dom/client";
|
|
import { expect, it, vi } from "vitest";
|
|
import { RemoteControl } from "../src/client/components/living/RemoteControl";
|
|
|
|
const button = vi.hoisted(() => vi.fn((_props: { title: string; onClick(): void }) => null));
|
|
vi.mock("../src/client/components/living/primitives", () => ({ Btn: button, Pill: () => null }));
|
|
|
|
it("keeps a suspended grant's gamepad attached through focus recovery and removes it on unplug", async () => {
|
|
let frame: FrameRequestCallback | undefined;
|
|
const pad = { index: 0, connected: true, mapping: "standard", buttons: [], axes: [1, 0, 0, 0] };
|
|
const owner = Object.assign(new EventTarget(), {
|
|
HTMLIFrameElement: class {},
|
|
navigator: { getGamepads: () => pad.connected ? [pad] : [] },
|
|
requestAnimationFrame: (next: FrameRequestCallback) => { frame = next; return 1; },
|
|
cancelAnimationFrame: () => { frame = undefined; },
|
|
});
|
|
const page = new DOMImplementation().createDocument("http://www.w3.org/1999/xhtml", "div");
|
|
const events = new EventTarget();
|
|
Object.assign(page, { defaultView: owner, hidden: false, hasFocus: () => true,
|
|
addEventListener: events.addEventListener.bind(events), removeEventListener: events.removeEventListener.bind(events) });
|
|
const container = page.documentElement!;
|
|
Object.assign(container, { addEventListener: vi.fn() });
|
|
Object.assign(owner, { document: page });
|
|
vi.stubGlobal("window", owner);
|
|
vi.stubGlobal("document", page);
|
|
vi.stubGlobal("navigator", owner.navigator);
|
|
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
|
|
|
|
const video = Object.assign(new EventTarget(), { ownerDocument: page, paused: false, ended: false });
|
|
const sent: { type: string; intent?: string; grant?: string }[] = [];
|
|
const channel = Object.assign(new EventTarget(), { readyState: "open", bufferedAmount: 0,
|
|
send: (message: string) => sent.push(JSON.parse(message)) });
|
|
const grant = "1".repeat(64);
|
|
const hostState = async (state: "ready" | "active" | "suspended", intent?: string) => {
|
|
await act(async () => {
|
|
channel.dispatchEvent(new MessageEvent("message", { data: JSON.stringify({
|
|
state, platform: "windows", mode: "ask", coPlay: true, capabilities: ["coPlay", "gamepad"],
|
|
...(intent && { intent, grant }),
|
|
}) }));
|
|
});
|
|
};
|
|
const root = createRoot(container as unknown as HTMLElement);
|
|
try {
|
|
await act(async () => root.render(createElement(RemoteControl, { available: true,
|
|
channel: channel as unknown as RTCDataChannel, videoRef: { current: video as unknown as HTMLVideoElement } })));
|
|
await hostState("ready");
|
|
const request = button.mock.calls.find(([props]) => props.title === "remote.request")!;
|
|
await act(async () => request[0].onClick());
|
|
const intent = sent.find(message => message.type === "request")!.intent!;
|
|
|
|
await hostState("active", intent);
|
|
await act(async () => frame!(100));
|
|
expect(sent.at(-1)).toMatchObject({ type: "gamepad", grant });
|
|
|
|
sent.length = 0;
|
|
await hostState("suspended", intent);
|
|
expect(sent.map(message => message.type)).not.toContain("gamepad-stop");
|
|
expect(frame).toBeUndefined();
|
|
await hostState("active", intent);
|
|
await act(async () => frame!(200));
|
|
expect(sent.at(-1)).toMatchObject({ type: "gamepad", grant });
|
|
|
|
await hostState("suspended", intent);
|
|
sent.length = 0;
|
|
pad.connected = false;
|
|
await act(async () => {
|
|
owner.dispatchEvent(Object.assign(new Event("gamepaddisconnected"), { gamepad: pad }));
|
|
});
|
|
expect(sent.filter(message => message.type === "gamepad-stop")).toEqual([{ type: "gamepad-stop", grant }]);
|
|
} finally {
|
|
await act(async () => root.unmount());
|
|
vi.unstubAllGlobals();
|
|
}
|
|
});
|