// test/unit/sessionManager.test.js
const fs = require('fs'); // For testDebugLog
const path = require('path'); // For LISTENER_LOG_FILE
// Ensure SessionManager is at least importable, even if not used directly in this minimal test
const { SessionManager } = require('../../lib/sessionManager.js');
const LISTENER_LOG_FILE = path.join(__dirname, '../../listener-debug.log');
// Simplified testDebugLog, ensure fs is available
const testDebugLog = (message) => {
try {
const timestamp = new Date().toISOString();
// Use fs directly as it's a core Node module
fs.appendFileSync(LISTENER_LOG_FILE, `[${timestamp}] [MINIMAL_TEST_DEBUG] ${message}\n`);
} catch (err) {
// Fallback to console.error if file logging fails
console.error(`[MINIMAL_TEST_DEBUG] FILE LOG FAILED: ${err.message} | Original message: ${message}`);
}
};
describe('Minimal SessionManager Test Suite with Basic SM', () => {
let sessionManager;
const testSessionDir = path.join(__dirname, '../../.test-sessions-minimal');
beforeAll(() => {
// Initialize log file for this minimal test run
try {
if (fs.existsSync(LISTENER_LOG_FILE)) {
// You can choose to delete the old log or append. For now, appending.
// fs.unlinkSync(LISTENER_LOG_FILE);
}
fs.appendFileSync(LISTENER_LOG_FILE, `[${new Date().toISOString()}] Minimal test log initialized.\n`);
SessionManager.setListenerLogPath(LISTENER_LOG_FILE); // So SM internal logs also go here
// Clean up test session directory before tests if it exists
if (fs.existsSync(testSessionDir)) {
fs.rmSync(testSessionDir, { recursive: true, force: true });
}
fs.mkdirSync(testSessionDir, { recursive: true });
} catch (err) {
console.error(`Failed to initialize minimal listener log: ${err.message}`);
}
});
beforeEach(async () => {
testDebugLog("BEFORE EACH: Creating SessionManager instance");
// Ensure the test session directory exists and is clean for each test
if (fs.existsSync(testSessionDir)) {
fs.rmSync(testSessionDir, { recursive: true, force: true });
}
fs.mkdirSync(testSessionDir, { recursive: true });
sessionManager = new SessionManager({ sessionDir: testSessionDir, anInMemoryFilesystem: null });
// Mock _loadSessionFromFile for this basic test to avoid file system complexities not yet under test
// Or ensure the sessionDir is writable and SessionManager handles it.
// For now, let's assume init will work with a clean directory or we mock parts of it if needed.
// sessionManager.init() is not a public method; constructor handles initialization.
testDebugLog("BEFORE EACH: SessionManager instance created (constructor handles init)");
});
afterEach(async () => {
console.error("MINIMAL AFTER EACH (SM) ENTERED - CONSOLE");
testDebugLog("MINIMAL AFTER EACH (SM) ENTERED - FILE");
if (sessionManager) {
testDebugLog("AFTER EACH (SM): About to call SM._logListenerActivity directly.");
if (typeof sessionManager._logListenerActivity === 'function') {
sessionManager._logListenerActivity('Direct call from test afterEach before destroy');
testDebugLog("AFTER EACH (SM): SM._logListenerActivity was called directly.");
} else {
testDebugLog("AFTER EACH (SM): SM._logListenerActivity is NOT a function on the instance!");
}
testDebugLog("AFTER EACH (SM): Calling sessionManager.destroy()");
await sessionManager.destroy();
testDebugLog("AFTER EACH (SM): sessionManager.destroy() completed");
sessionManager = null; // Help with GC and prevent re-use
}
testDebugLog(`MINIMAL afterEach (SM) END. Current SIGINT: ${process.listenerCount('SIGINT')}, SIGTERM: ${process.listenerCount('SIGTERM')}`);
// Clean up test session directory after each test
// if (fs.existsSync(testSessionDir)) {
// fs.rmSync(testSessionDir, { recursive: true, force: true });
// }
});
it('A single minimal passing test case with SM', () => {
console.error("MINIMAL PASSING TEST CASE (SM) EXECUTING - CONSOLE");
testDebugLog("MINIMAL PASSING TEST CASE (SM) EXECUTING - FILE");
expect(sessionManager).toBeDefined();
// Check initial listener counts if SM constructor adds them
// This depends on when SessionManager adds its listeners. If it's in the constructor:
// testDebugLog(`In-test SIGINT: ${process.listenerCount('SIGINT')}, SIGTERM: ${process.listenerCount('SIGTERM')} (after SM instantiation)`);
expect(true).toBe(true);
});
// Optional: Uncomment this test to see if console.error from afterEach appears for failing tests
// it('A failing minimal test case', () => {
// console.error("MINIMAL FAILING TEST CASE EXECUTING - CONSOLE");
// testDebugLog("MINIMAL FAILING TEST CASE EXECUTING - FILE");
// expect(false).toBe(true); // This will fail
// });
});