diff --git a/frontend/src/views/Player/EmulatorJS/utils.test.ts b/frontend/src/views/Player/EmulatorJS/utils.test.ts index ae253401c..48433c7dd 100644 --- a/frontend/src/views/Player/EmulatorJS/utils.test.ts +++ b/frontend/src/views/Player/EmulatorJS/utils.test.ts @@ -179,19 +179,32 @@ describe("installEJSDefaultOptionsTrap", () => { expect(emulator.preGetSetting).toBe(patched); }); - it("replays connected for pads the gamepad handler saw before its listener", () => { - const connected = vi.fn(); + it("assigns pads the gamepad handler saw before its listener to players", () => { const emulator = makeEmulator({}); + emulator.gamepadSelection = ["", "", "", ""]; + type Listener = (arg: { gamepadIndex: number }) => void; + const listeners: Record = {}; emulator.gamepad = { gamepads: [{ index: 0, id: "pad" }, null, { index: 2, id: "pad" }], + on(name: string, cb: Listener) { + listeners[name] = cb; + }, dispatchEvent(name: string, arg: { gamepadIndex: number }) { - if (name === "connected") connected(arg.gamepadIndex); + listeners[name]?.(arg); }, }; + // Mirrors EmulatorJS 4.2.3's listener: fill the first free player slot. + const onConnected: Listener = ({ gamepadIndex }) => { + const pad = emulator.gamepad.gamepads[gamepadIndex]; + const slot = emulator.gamepadSelection.indexOf(""); + if (slot !== -1) + emulator.gamepadSelection[slot] = `${pad.id}_${pad.index}`; + }; + emulator.gamepad.on("connected", onConnected); window.EJS_emulator = emulator; window.EJS_emulator = emulator; - expect(connected.mock.calls).toEqual([[0], [2]]); + expect(emulator.gamepadSelection).toEqual(["pad_0", "pad_2", "", ""]); }); it("keeps booting when a replayed connected handler throws", () => { diff --git a/frontend/src/views/Player/EmulatorJS/utils.ts b/frontend/src/views/Player/EmulatorJS/utils.ts index fd37c89b4..8d10f24b7 100644 --- a/frontend/src/views/Player/EmulatorJS/utils.ts +++ b/frontend/src/views/Player/EmulatorJS/utils.ts @@ -510,9 +510,8 @@ function installDefaultOptionsFallback(emulator: any) { } } -// GamepadHandler polls once in its constructor, before EmulatorJS registers -// its "connected" listener, so a pad the page already sees (e.g. the one that -// pressed Play) is never assigned to a player until it reconnects. +// GamepadHandler polls before EmulatorJS registers its "connected" listener, +// so pads it already saw (e.g. the one that pressed Play) get no player. // eslint-disable-next-line @typescript-eslint/no-explicit-any function replayConnectedGamepads(emulator: any) { const handler = emulator.gamepad;