All files / src/firebase firebase.ts

58% Statements 29/50
0% Branches 0/12
0% Functions 0/1
58% Lines 29/50

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 68 69 70 71 72 731x 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                     1x                                 1x      
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getFirestore } from 'firebase/firestore';
 
// Check if we're in a test environment
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: isTestEnvironment ? 'test-api-key' : Bun.env.FIREBASE_API_KEY,
	authDomain: isTestEnvironment ? 'test-project.firebaseapp.com' : Bun.env.FIREBASE_AUTH_DOMAIN,
	projectId: isTestEnvironment ? 'test-project' : Bun.env.FIREBASE_PROJECT_ID,
	storageBucket: isTestEnvironment ? 'test-project.appspot.com' : Bun.env.FIREBASE_STORAGE_BUCKET,
	messagingSenderId: isTestEnvironment ? '123456789' : Bun.env.FIREBASE_MESSAGING_SENDER_ID,
	appId: isTestEnvironment ? '1:123456789:web:abcdef123456' : Bun.env.FIREBASE_APP_ID,
	measurementId: isTestEnvironment ? 'G-ABCDEF123456' : Bun.env.FIREBASE_MEASUREMENT_ID, 
};
 
// Inicialización de Firebase (skip in test environment)
let app: any;
let auth: any;
let db: any;
 
if (isTestEnvironment) {
	// In test environment, create mock objects that don't require real Firebase
	app = { name: '[DEFAULT]', options: firebaseConfig };
	auth = { app, currentUser: null };
	// Create a more complete mock for the db object that will work with Firestore functions
	// This mock object provides just enough structure for the mocks to work
	db = {
		app,
		_delegate: { app },
		type: 'firestore',
		// Add these properties to make it compatible with Firestore functions
		_databaseId: { projectId: 'test-project', database: '(default)' },
		_settings: {}
	};
} else {
	// In production environment, initialize Firebase normally
	app = initializeApp(firebaseConfig);
	auth = getAuth(app);
	db = getFirestore(app);
}
 
export { auth, db };
 
// Función para validar la configuración de Firebase
export function validateFirebaseConfig() {
	// Skip validation in test environment
	if (isTestEnvironment) {
		console.log(`[FORMS_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(`[FORMS_SVC] Firebase config: OK`);
}
 
// Ejecutar validación al inicializar (only if not in test environment)
if (!isTestEnvironment) {
	validateFirebaseConfig();
}