MovieMapper/tests/legacy/test-folder-click-handler.js
Jarian Cottingham 1a35eb346e chore: reorganize repo layout, remove dead files
- Move phase/plan docs into docs/
- Move legacy node:test files into tests/legacy/ with README
- Remove .backup file, test audit artifacts, and unused AI prompt/skill files
- Remove broken iOS GitHub workflows (reference missing MovieMapper-iOS/)
2026-08-20 20:11:57 +00:00

337 lines
10 KiB
JavaScript

// Test folder click handler functionality - simplified version
const { test } = require('node:test');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const AppState = require('./utils/renderer/AppState');
test('UIManager handles folder click for regular folders', async () => {
// Create a temporary test directory
const testDir = path.join(__dirname, 'test_folder_click');
if (!fs.existsSync(testDir)) {
fs.mkdirSync(testDir);
}
try {
// Create mock UIManager with just the methods we need to test
const mockUIManager = {
currentDirectory: null,
selectedDirEl: { textContent: '' },
appState: {
navigationStack: [],
currentDepth: 0,
addToNavigationStack: function(dir) {
this.navigationStack.push(dir);
this.currentDepth = this.navigationStack.length - 1;
},
getNavigationStack: function() {
return this.navigationStack;
},
canGoBack: function() {
return this.currentDepth > 0;
},
goBack: function() {
if (this.currentDepth > 0) {
this.navigationStack.pop();
this.currentDepth--;
return this.navigationStack[this.currentDepth];
}
return null;
}
},
scanDirectory: async function(dir) {
return { success: true, files: [] };
},
_logAuditEvent: async function() {},
openDirectory: async function(directory) {
this.appState.addToNavigationStack(directory);
this.currentDirectory = directory;
},
handleFolderClick: async function(folderPath, folderName) {
console.log('Folder clicked:', folderName, 'at path:', folderPath);
// Validate folder path exists
if (!fs.existsSync(folderPath) || !fs.statSync(folderPath).isDirectory()) {
console.error('Invalid folder path:', folderPath);
return; // Don't navigate
}
// Open the folder
await this.openDirectory(folderPath);
}
};
// Test with non-existent folder (should not navigate)
const nonExistentPath = '/non/existent/path';
const consoleError = console.error;
console.error = () => {}; // Suppress error output
try {
await mockUIManager.handleFolderClick(nonExistentPath, 'nonexistent');
// Should not navigate to non-existent folder
assert.strictEqual(mockUIManager.currentDirectory, null, 'Should not navigate to non-existent folder');
} finally {
console.error = consoleError;
}
// Test with valid folder
await mockUIManager.handleFolderClick(testDir, 'test_folder_click');
assert.strictEqual(mockUIManager.currentDirectory, testDir, 'Should navigate to valid folder');
assert.strictEqual(mockUIManager.appState.getNavigationStack().length, 1, 'Should add folder to navigation stack');
} finally {
// Clean up test directory
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true });
}
}
});
test('UIManager validates folder path before navigation', async () => {
const mockUIManager = {
currentDirectory: '/test/source',
selectedDirEl: { textContent: '' },
appState: {
navigationStack: ['/test/source'],
currentDepth: 0,
addToNavigationStack: function(dir) {
this.navigationStack.push(dir);
this.currentDepth = this.navigationStack.length - 1;
},
canGoBack: function() {
return this.currentDepth > 0;
},
goBack: function() {
if (this.currentDepth > 0) {
this.navigationStack.pop();
this.currentDepth--;
return this.navigationStack[this.currentDepth];
}
return null;
}
},
openDirectory: async function(directory) {
this.appState.addToNavigationStack(directory);
this.currentDirectory = directory;
},
handleFolderClick: async function(folderPath, folderName) {
console.log('Folder clicked:', folderName, 'at path:', folderPath);
// Validate folder path exists
if (!fs.existsSync(folderPath) || !fs.statSync(folderPath).isDirectory()) {
console.error('Invalid folder path:', folderPath);
return; // Don't navigate
}
// Open the folder
await this.openDirectory(folderPath);
}
};
// Test with non-existent folder
const nonExistentPath = '/non/existent/path';
const consoleError = console.error;
console.error = () => {}; // Suppress error output
try {
await mockUIManager.handleFolderClick(nonExistentPath, 'nonexistent');
// Should not navigate to non-existent folder
assert.strictEqual(mockUIManager.currentDirectory, '/test/source', 'Should not change directory for non-existent folder');
} finally {
console.error = consoleError;
}
});
test('UIManager handles special folder navigation', async () => {
const testDir = path.join(__dirname, 'test_special_folders');
// Create special folder structure
if (!fs.existsSync(testDir)) {
fs.mkdirSync(testDir);
}
const extrasPath = path.join(testDir, 'extras');
const behindScenesPath = path.join(testDir, 'behind the scenes');
if (!fs.existsSync(extrasPath)) {
fs.mkdirSync(extrasPath);
}
if (!fs.existsSync(behindScenesPath)) {
fs.mkdirSync(behindScenesPath);
}
try {
const mockUIManager = {
currentDirectory: null,
selectedDirEl: { textContent: '' },
appState: {
navigationStack: [],
currentDepth: 0,
addToNavigationStack: function(dir) {
this.navigationStack.push(dir);
this.currentDepth = this.navigationStack.length - 1;
},
getNavigationStack: function() {
return this.navigationStack;
},
canGoBack: function() {
return this.currentDepth > 0;
},
goBack: function() {
if (this.currentDepth > 0) {
this.navigationStack.pop();
this.currentDepth--;
return this.navigationStack[this.currentDepth];
}
return null;
}
},
scanDirectory: async function(dir) {
return { success: true, files: [] };
},
_logAuditEvent: async function() {},
openDirectory: async function(directory) {
this.appState.addToNavigationStack(directory);
this.currentDirectory = directory;
},
handleFolderClick: async function(folderPath, folderName) {
console.log('Folder clicked:', folderName, 'at path:', folderPath);
// Validate folder path exists
if (!fs.existsSync(folderPath) || !fs.statSync(folderPath).isDirectory()) {
console.error('Invalid folder path:', folderPath);
return; // Don't navigate
}
// Open the folder
await this.openDirectory(folderPath);
}
};
// Test navigating to extras folder
await mockUIManager.handleFolderClick(extrasPath, 'extras');
assert.strictEqual(mockUIManager.currentDirectory, extrasPath, 'Should navigate to extras folder');
// Test navigating to behind the scenes folder
await mockUIManager.handleFolderClick(behindScenesPath, 'behind the scenes');
assert.strictEqual(mockUIManager.currentDirectory, behindScenesPath, 'Should navigate to behind the scenes folder');
// Verify navigation stack
const stack = mockUIManager.appState.getNavigationStack();
assert.ok(stack.includes(extrasPath));
assert.ok(stack.includes(behindScenesPath));
} finally {
// Clean up
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true });
}
}
});
test('Folder click handler validates path exists', () => {
const testDir = path.join(__dirname, 'test_path_validation');
if (!fs.existsSync(testDir)) {
fs.mkdirSync(testDir);
}
try {
const mockUIManager = {
currentDirectory: testDir,
selectedDirEl: { textContent: '' },
appState: {
navigationStack: [testDir],
currentDepth: 0,
addToNavigationStack: function(dir) {
this.navigationStack.push(dir);
this.currentDepth = this.navigationStack.length - 1;
},
goBack: function() {
if (this.currentDepth > 0) {
this.navigationStack.pop();
this.currentDepth--;
return this.navigationStack[this.currentDepth];
}
return null;
}
},
openDirectory: async function(directory) {
this.appState.addToNavigationStack(directory);
this.currentDirectory = directory;
},
handleFolderClick: function(folderPath, folderName) {
if (!fs.existsSync(folderPath) || !fs.statSync(folderPath).isDirectory()) {
return; // Don't navigate
}
this.openDirectory(folderPath);
}
};
// Test with file instead of directory
const testFile = path.join(testDir, 'testfile.txt');
fs.writeFileSync(testFile, 'test content');
const consoleError = console.error;
console.error = () => {};
try {
mockUIManager.handleFolderClick(testFile, 'testfile.txt');
// Should not navigate to file
assert.strictEqual(mockUIManager.currentDirectory, testDir, 'Should not navigate to file');
} finally {
console.error = consoleError;
}
// Test with non-existent directory
const nonExistentDir = path.join(testDir, 'nonexistent');
mockUIManager.handleFolderClick(nonExistentDir, 'nonexistent');
assert.strictEqual(mockUIManager.currentDirectory, testDir, 'Should not navigate to non-existent directory');
} finally {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true });
}
}
});
test('UIManager handleFolderClick method exists', () => {
const mockUIManager = {
handleFolderClick: function(folderPath, folderName) {
// Mock implementation
}
};
assert.ok(mockUIManager.handleFolderClick, 'handleFolderClick method should exist');
assert.strictEqual(typeof mockUIManager.handleFolderClick, 'function', 'handleFolderClick should be a function');
});
console.log('All folder click handler tests passed!');