332 lines
10 KiB
JavaScript
332 lines
10 KiB
JavaScript
/**
|
|
* Unit tests for keyboard handlers utilities
|
|
* Tests global keyboard shortcuts and help system integration
|
|
* Requirements: 9.1, 9.3, 9.4, 9.2, 9.5
|
|
*/
|
|
|
|
describe("Keyboard Handlers Utilities", () => {
|
|
test("should have keyboardHandlers module available", () => {
|
|
const keyboardHandlers = require("../../../src/tui/utils/keyboardHandlers.js");
|
|
expect(typeof keyboardHandlers).toBe("object");
|
|
});
|
|
|
|
test("should export required functions", () => {
|
|
const keyboardHandlers = require("../../../src/tui/utils/keyboardHandlers.js");
|
|
|
|
expect(typeof keyboardHandlers.handleGlobalShortcuts).toBe("function");
|
|
expect(typeof keyboardHandlers.createKeyboardHandler).toBe("function");
|
|
expect(typeof keyboardHandlers.navigationKeys).toBe("object");
|
|
expect(typeof keyboardHandlers.helpSystem).toBe("object");
|
|
});
|
|
|
|
test("should define handleGlobalShortcuts function", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain("handleGlobalShortcuts");
|
|
expect(keyboardHandlersContent).toContain("input, key, context");
|
|
expect(keyboardHandlersContent).toContain("toggleHelp");
|
|
expect(keyboardHandlersContent).toContain("navigateBack");
|
|
});
|
|
|
|
test("should handle help toggle shortcut", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain('input === "h"');
|
|
expect(keyboardHandlersContent).toContain('input === "H"');
|
|
expect(keyboardHandlersContent).toContain("toggleHelp()");
|
|
});
|
|
|
|
test("should handle escape key for back navigation", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain("key.escape");
|
|
expect(keyboardHandlersContent).toContain("appState.uiState.helpVisible");
|
|
expect(keyboardHandlersContent).toContain("context.hideHelp()");
|
|
expect(keyboardHandlersContent).toContain("navigateBack()");
|
|
});
|
|
|
|
test("should handle exit shortcuts", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain('key.ctrl && input === "c"');
|
|
expect(keyboardHandlersContent).toContain('input === "q"');
|
|
expect(keyboardHandlersContent).toContain('input === "Q"');
|
|
expect(keyboardHandlersContent).toContain("process.exit(0)");
|
|
});
|
|
|
|
test("should define createKeyboardHandler function", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain("createKeyboardHandler");
|
|
expect(keyboardHandlersContent).toContain("screenHandler, context");
|
|
expect(keyboardHandlersContent).toContain("handleGlobalShortcuts");
|
|
expect(keyboardHandlersContent).toContain("wasHandledGlobally");
|
|
});
|
|
});
|
|
|
|
describe("Navigation Keys Utilities", () => {
|
|
test("should define navigationKeys object", () => {
|
|
const keyboardHandlers = require("../../../src/tui/utils/keyboardHandlers.js");
|
|
|
|
expect(typeof keyboardHandlers.navigationKeys.handleMenuNavigation).toBe(
|
|
"function"
|
|
);
|
|
expect(typeof keyboardHandlers.navigationKeys.handleFormNavigation).toBe(
|
|
"function"
|
|
);
|
|
expect(typeof keyboardHandlers.navigationKeys.handlePagination).toBe(
|
|
"function"
|
|
);
|
|
});
|
|
|
|
test("should define handleMenuNavigation function", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain("handleMenuNavigation:");
|
|
expect(keyboardHandlersContent).toContain("key.upArrow");
|
|
expect(keyboardHandlersContent).toContain("key.downArrow");
|
|
expect(keyboardHandlersContent).toContain("Math.max(0, currentIndex - 1)");
|
|
expect(keyboardHandlersContent).toContain(
|
|
"Math.min(maxIndex, currentIndex + 1)"
|
|
);
|
|
});
|
|
|
|
test("should define handleFormNavigation function", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain("handleFormNavigation:");
|
|
expect(keyboardHandlersContent).toContain("key.tab");
|
|
expect(keyboardHandlersContent).toContain(
|
|
"(currentIndex + 1) % (maxIndex + 1)"
|
|
);
|
|
});
|
|
|
|
test("should define handlePagination function", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain("handlePagination:");
|
|
expect(keyboardHandlersContent).toContain("key.pageUp");
|
|
expect(keyboardHandlersContent).toContain("key.pageDown");
|
|
expect(keyboardHandlersContent).toContain("Math.max(0, currentPage - 1)");
|
|
expect(keyboardHandlersContent).toContain(
|
|
"Math.min(totalPages - 1, currentPage + 1)"
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("Help System Utilities", () => {
|
|
test("should define helpSystem object", () => {
|
|
const keyboardHandlers = require("../../../src/tui/utils/keyboardHandlers.js");
|
|
|
|
expect(typeof keyboardHandlers.helpSystem.getScreenShortcuts).toBe(
|
|
"function"
|
|
);
|
|
expect(typeof keyboardHandlers.helpSystem.getGlobalShortcuts).toBe(
|
|
"function"
|
|
);
|
|
});
|
|
|
|
test("should define screen shortcuts mapping", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain("getScreenShortcuts:");
|
|
expect(keyboardHandlersContent).toContain('"main-menu":');
|
|
expect(keyboardHandlersContent).toContain("configuration:");
|
|
expect(keyboardHandlersContent).toContain("operation:");
|
|
expect(keyboardHandlersContent).toContain("scheduling:");
|
|
expect(keyboardHandlersContent).toContain("logs:");
|
|
expect(keyboardHandlersContent).toContain('"tag-analysis":');
|
|
});
|
|
|
|
test("should define common shortcuts for each screen", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
// Main menu shortcuts
|
|
expect(keyboardHandlersContent).toContain(
|
|
'"↑/↓", description: "Navigate menu"'
|
|
);
|
|
expect(keyboardHandlersContent).toContain(
|
|
'"Enter", description: "Select item"'
|
|
);
|
|
|
|
// Configuration shortcuts
|
|
expect(keyboardHandlersContent).toContain(
|
|
'"Tab", description: "Next field"'
|
|
);
|
|
expect(keyboardHandlersContent).toContain('"Ctrl+S", description: "Save"');
|
|
|
|
// Operation shortcuts
|
|
expect(keyboardHandlersContent).toContain(
|
|
'"Ctrl+C", description: "Cancel"'
|
|
);
|
|
|
|
// Logs shortcuts
|
|
expect(keyboardHandlersContent).toContain('"/", description: "Search"');
|
|
expect(keyboardHandlersContent).toContain(
|
|
'"PgUp/PgDn", description: "Page"'
|
|
);
|
|
});
|
|
|
|
test("should define global shortcuts", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain("getGlobalShortcuts:");
|
|
expect(keyboardHandlersContent).toContain(
|
|
'"h", description: "Toggle help"'
|
|
);
|
|
expect(keyboardHandlersContent).toContain(
|
|
'"Esc", description: "Back/Close"'
|
|
);
|
|
expect(keyboardHandlersContent).toContain('"Ctrl+C", description: "Exit"');
|
|
});
|
|
|
|
test("should have fallback for unknown screens", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const keyboardHandlersPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/utils/keyboardHandlers.js"
|
|
);
|
|
const keyboardHandlersContent = fs.readFileSync(
|
|
keyboardHandlersPath,
|
|
"utf8"
|
|
);
|
|
|
|
expect(keyboardHandlersContent).toContain("shortcuts[screenName] || []");
|
|
});
|
|
});
|
|
|
|
describe("Keyboard Handlers Integration", () => {
|
|
test("should be used by MainMenuScreen", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const mainMenuPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/components/screens/MainMenuScreen.jsx"
|
|
);
|
|
const mainMenuContent = fs.readFileSync(mainMenuPath, "utf8");
|
|
|
|
expect(mainMenuContent).toContain(
|
|
'require("../../utils/keyboardHandlers.js")'
|
|
);
|
|
expect(mainMenuContent).toContain("createKeyboardHandler");
|
|
expect(mainMenuContent).toContain("navigationKeys");
|
|
});
|
|
|
|
test("should provide context to keyboard handlers", () => {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const mainMenuPath = path.join(
|
|
__dirname,
|
|
"../../../src/tui/components/screens/MainMenuScreen.jsx"
|
|
);
|
|
const mainMenuContent = fs.readFileSync(mainMenuPath, "utf8");
|
|
|
|
expect(mainMenuContent).toContain("appState,");
|
|
expect(mainMenuContent).toContain("navigateTo,");
|
|
expect(mainMenuContent).toContain("navigateBack,");
|
|
expect(mainMenuContent).toContain("toggleHelp,");
|
|
expect(mainMenuContent).toContain("showHelp,");
|
|
expect(mainMenuContent).toContain("hideHelp,");
|
|
});
|
|
});
|