import { Firestore } from '../src/types' ; import crypto from 'crypto' ; import admin , { ServiceAccount } from 'firebase-admin' ; export const deleteCollection = async ( firestore , collectionPath ) => { const batch = firestore . batch ( ) ; const snapshot = await firestore . collection ( collectionPath ) . get ( ) ; snapshot . forEach ( ( doc ) => { batch . delete ( doc . ref ) ; } ) ; await batch . commit ( ) ; } ; export const createRandomCollectionName = ( prefix = 'firebase_simple_' ) => { const str = crypto . randomBytes ( 10 ) . toString ( 'hex' ) ; return prefix + str ; } ; export class AdminFirestoreTestUtil { adminFirestore : Firestore ; collectionPath : string ; static initFirestore ( real ) { if ( real ) { const serviceAccount = require ( '../firebase_secret.json' ) ; admin . initializeApp ( { credential : admin . credential . cert ( serviceAccount as ServiceAccount ) , } ) ; } else { process . env . GCLOUD_PROJECT = 'firestore-simple-test' ; process . env . FIRESTORE_EMULATOR_HOST = 'localhost:8080' ; admin . initializeApp ( { } ) ; } return admin . firestore ( ) ; } constructor ( { real } = { } ) { real = real || false ; this . collectionPath = crypto . randomBytes ( 10 ) . toString ( 'hex' ) ; this . adminFirestore = AdminFirestoreTestUtil . initFirestore ( real ) ; } async deleteCollection ( ) { const batch = this . adminFirestore . batch ( ) ; const snapshot = await this . adminFirestore . collection ( this . collectionPath ) . get ( ) ; snapshot . forEach ( ( doc ) => { batch . delete ( doc . ref ) ; } ) ; await batch . commit ( ) ; } async deleteApps ( ) { await Promise . all ( admin . apps . map ( ( app ) => app ?. delete ( ) ) ) ; } }