All files / src/firebase firebase.ts

56.25% Statements 27/48
0% Branches 0/5
0% Functions 0/1
56.25% Lines 27/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 681x 1x 1x   1x 1x             1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x   1x   1x 1x   1x 1x 1x 1x 1x                     1x                                 1x      
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getFirestore } from 'firebase/firestore';
 
const isTestEnvironment = typeof process !== 'undefined' && (
	process.env.NODE_ENV === 'test' ||
	process.env.VITEST === 'true' ||
	process.env.TEST === 'true' ||
	(globalThis as any).__vitest__ !== undefined
);
 
// Configuración de Firebase, utilizando variables de entorno
const firebaseConfig = {
	apiKey: Bun.env.FIREBASE_API_KEY,
	authDomain: Bun.env.FIREBASE_AUTH_DOMAIN,
	projectId: Bun.env.FIREBASE_PROJECT_ID,
	storageBucket: Bun.env.FIREBASE_STORAGE_BUCKET,
	messagingSenderId: Bun.env.FIREBASE_MESSAGING_SENDER_ID,
	appId: Bun.env.FIREBASE_APP_ID,
	measurementId: Bun.env.FIREBASE_MEASUREMENT_ID, 
};
 
// Inicialización de Firebase (skip in test environment)
let app: any;
let auth: any;
let db: any;
 
if (isTestEnvironment) {
	// En entorno de prueba, crear objetos simulados que no requieran Firebase real
	app = {};
	auth = {};
	// Crear un mock más completo para el objeto db que funcione con las funciones de Firestore
	db = {
		app: {},
		_delegate: {},
		type: 'firestore'
	};
} else {
	// En entorno de producción, inicializar Firebase normalmente
	app = initializeApp(firebaseConfig);
	auth = getAuth(app);
	db = getFirestore(app);
}
 
export { auth, db };
 
// Función para validar la configuración de Firebase
export function validateFirebaseConfig() {
	// Saltar validación en entorno de prueba
	if (isTestEnvironment) {
		console.log(`[COMPANY_SVC] Firebase config: MOCKED (test environment)`);
		return;
	}
	
	// Valida que todas las variables estén presentes
	for (const [key, value] of Object.entries(firebaseConfig)) {
		if (!value) {
			throw new Error(`Missing Firebase var: ${key}`);
		}
	}
	console.log(`[COMPANY_SVC] Firebase config: OK`);
}
 
// Ejecutar validación al inicializar (solo si no estamos en un entorno de prueba)
if (!isTestEnvironment) {
	validateFirebaseConfig();
}