Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ function App() {
const [isVrActive, setIsVrActive] = useState(false);
const [screenPosition, setScreenPosition] = useState({ x: 0, y: 5, z: -5.5 });
const [commandBuffer, setCommandBuffer] = useState('');
const [isKeymapActive, setIsKeymapActive] = useState(false);
const commandBufferRef = useRef('');
const [keymapEnabled, setKeymapEnabled] = useState(false);
const pressedHomeRowKeysRef = useRef(new Map());
Expand Down Expand Up @@ -132,7 +131,7 @@ function App() {
}, [SPECIAL_COMMANDS]);

const handleTerminalKeyDown = useCallback((event) => {
if (isKeymapActive && event.code === 'CapsLock') {
if (keymapEnabled && event.code === 'CapsLock') {
event.preventDefault();
event.stopPropagation();
emitTerminalInput('\x1b');
Expand Down Expand Up @@ -212,7 +211,7 @@ function App() {
emitTerminalInput(encoded);

return true;
}, [emitTerminalInput, SPECIAL_COMMANDS, isPrefixOfCommand, isKeymapActive]);
}, [emitTerminalInput, SPECIAL_COMMANDS, isPrefixOfCommand, keymapEnabled]);

const displaySnapshot = useMemo(() => {
if (!terminalSnapshot) {
Expand Down
61 changes: 61 additions & 0 deletions frontend/src/CapsLock.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { fireEvent, render, screen } from '@testing-library/react';
import App from './App';

describe('CapsLock remapping', () => {
let socket;
let socketHandlers = {};

beforeEach(() => {
socketHandlers = {};
socket = {
on: jest.fn((eventName, handler) => {
socketHandlers[eventName] = handler;
}),
emit: jest.fn(),
close: jest.fn(),
connected: true,
};
window.io = jest.fn(() => socket);
jest.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({
setTransform: jest.fn(),
clearRect: jest.fn(),
fillRect: jest.fn(),
fillText: jest.fn(),
strokeRect: jest.fn(),
beginPath: jest.fn(),
rect: jest.fn(),
clip: jest.fn(),
save: jest.fn(),
restore: jest.fn(),
moveTo: jest.fn(),
lineTo: jest.fn(),
stroke: jest.fn(),
});
});

afterEach(() => {
jest.restoreAllMocks();
});
Comment on lines +36 to +38

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Clean up window.io explicitly in teardown.

jest.restoreAllMocks() won’t undo direct assignment to window.io, so this global can leak into later tests and cause cross-test coupling.

Suggested fix
  afterEach(() => {
    jest.restoreAllMocks();
+   delete window.io;
  });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
afterEach(() => {
jest.restoreAllMocks();
});
afterEach(() => {
jest.restoreAllMocks();
delete window.io;
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/src/CapsLock.test.js` around lines 36 - 38, The
`jest.restoreAllMocks()` call in the afterEach hook does not clean up direct
assignments to the global window.io object, which can leak state between tests
and cause cross-test coupling. Add explicit cleanup in the afterEach hook to
delete or reset the window.io property after jest.restoreAllMocks() is called,
ensuring each test starts with a clean global state.


const activateKeymap = () => {
fireEvent.keyDown(document, { key: '/' });
fireEvent.keyDown(document, { key: 'k' });
fireEvent.keyDown(document, { key: 'e' });
fireEvent.keyDown(document, { key: 'y' });
fireEvent.keyDown(document, { key: 'm' });
fireEvent.keyDown(document, { key: 'a' });
fireEvent.keyDown(document, { key: 'p' });
fireEvent.keyDown(document, { key: 'Enter' });
};

test('CapsLock sends Escape when keymap is enabled', () => {
render(<App />);
const shell = screen.getByTestId('vr-shell');
fireEvent.focus(shell);
activateKeymap();
socket.emit.mockClear();

fireEvent.keyDown(document, { code: 'CapsLock' });
expect(socket.emit).toHaveBeenCalledWith('terminal-input', '\x1b');
});
});