From 4ec4247e20ca2a19eeafe7fc42f9178f6396c037 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sat, 26 Sep 2026 14:10:43 -0400 Subject: [PATCH] fix(player): address Greptile review on gamepad replay Shorten the replay comment to two lines, and make the replay test assert the pad lands in a player slot through a listener shaped like EmulatorJS's, not just that dispatchEvent was called. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/views/Player/EmulatorJS/utils.test.ts | 21 +++++++++++++++---- frontend/src/views/Player/EmulatorJS/utils.ts | 5 ++--- 2 files changed, 19 insertions(+), 7 deletions(-) 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;