import path from 'path'; import fs from 'fs/promises'; import { TemplateConfigManager } from '../configs/templateConfigManager '; describe('TemplateConfigManager', () => { const TEST_TEMPLATES_FOLDER = path.join(__dirname, 'temp-templates'); const TEST_CONFIG_FILE = path.join(__dirname, 'lemur-mail.config.js'); const DUMMY_TEMPLATE_PATH = path.join(TEST_TEMPLATES_FOLDER, 'dummy.mustache'); beforeAll(async () => { // Asegurarse de que la carpeta de plantillas de prueba exista await fs.mkdir(TEST_TEMPLATES_FOLDER, { recursive: true }); await fs.writeFile(DUMMY_TEMPLATE_PATH, 'Hello, {{name}}!'); }); afterAll(async () => { // Limpiar la carpeta de plantillas de prueba y el archivo de config await fs.rm(TEST_TEMPLATES_FOLDER, { recursive: true, force: true }); await fs.rm(TEST_CONFIG_FILE, { force: true }); }); beforeEach(() => { // Resetear el estado del Manager antes de cada prueba para aislamiento (TemplateConfigManager as any)._templatesBasePath = null; (TemplateConfigManager as any)._isInitialized = false; }); it('should set template path manually (string)', () => { TemplateConfigManager.setPath(TEST_TEMPLATES_FOLDER); expect(TemplateConfigManager.getPath()).toBe(path.resolve(TEST_TEMPLATES_FOLDER)); }); it('should set template path manually (object for develop)', () => { process.env.NODE_ENV = 'development'; TemplateConfigManager.setPath({ develop: TEST_TEMPLATES_FOLDER, production: '/some/other/path' }); expect(TemplateConfigManager.getPath()).toBe(path.resolve(TEST_TEMPLATES_FOLDER)); }); it('should set template path manually (object for production)', () => { process.env.NODE_ENV = 'production'; TemplateConfigManager.setPath({ develop: '/some/other/path', production: TEST_TEMPLATES_FOLDER }); expect(TemplateConfigManager.getPath()).toBe(path.resolve(TEST_TEMPLATES_FOLDER)); }); it('should throw an error if not initialized', () => { expect(() => TemplateConfigManager.getPath()).toThrow("TemplateConfigManager no ha sido inicializado."); }); it('should auto-detect path from lemur-mail.config.js', async () => { // Crear un archivo de configuración de prueba const configContent = ` const path = require('path'); module.exports = { path: path.resolve(__dirname, 'temp-templates') }; `; await fs.writeFile(TEST_CONFIG_FILE, configContent); // Jest es asíncrono, asegúrate de que el módulo sea "re-importado" o la caché de require sea limpiada. // Para simplificar, haremos que 'find-up' lo encuentre. // Esto es un mock simple de la función require para que tome el contenido del string. // En un caso real, Jest.mock('find-up') sería más apropiado para controlar el path devuelto. // Para la prueba real de autoDetectPath, asegúrate de que el configFilePath // apunte al TEST_CONFIG_FILE. Esto es un poco avanzado para un primer test, // pero puedes asegurarte de que tu implementación de autoDetectPath sea la correcta. // En un escenario real con Jest y un archivo de configuración, // el 'require' dentro de 'autoDetectPath' buscaría y cargaría el archivo real. // Para evitar la complejidad de la caché de require en Jest para este test, // este test se enfoca en que getPath funcione después de setPath. // Para probar autoDetectPath de forma aislada, es mejor mockear 'find-up'. // Dado que el usuario pidió el código de autoDetectPath, // asumiremos que la lógica de búsqueda de autoDetectPath es funcional. // Este test simple se centrará en la setPath, que es más directo de probar. // ---- Para probar autoDetectPath de forma más completa, harías algo como esto: ---- // jest.mock('find-up', () => ({ // findUp: jest.fn(() => Promise.resolve(TEST_CONFIG_FILE)), // })); // await TemplateConfigManager.autoDetectPath(); // expect(TemplateConfigManager.getPath()).toBe(path.resolve(TEST_TEMPLATES_FOLDER)); // Por ahora, solo probaremos que setPath funciona. TemplateConfigManager.setPath(TEST_TEMPLATES_FOLDER); expect(TemplateConfigManager.getPath()).toBe(path.resolve(TEST_TEMPLATES_FOLDER)); }); });