11// This test requires a Firestore emulator running. Run via:
22// npm run test:firestore
3- // Or manually:
4- // FIRESTORE_EMULATOR_HOST="localhost:8080" npx jest src/variables.utils.test.ts
53
6- // Set project ID before importing firebase-admin
7- if ( ! process . env . GCLOUD_PROJECT ) {
8- process . env . GCLOUD_PROJECT = 'demo-deliberate-lab' ;
9- }
10-
11- import * as admin from 'firebase-admin' ;
4+ import { app } from './app' ;
125import {
136 initializeTestEnvironment ,
147 RulesTestEnvironment ,
@@ -27,23 +20,15 @@ import {
2720} from '@deliberation-lab/utils' ;
2821import { generateVariablesForScope } from './variables.utils' ;
2922
30- let mockFirestore : FirebaseFirestore . Firestore ;
31-
32- jest . mock ( './app' , ( ) => ( {
33- app : {
34- firestore : ( ) => mockFirestore ,
35- } ,
36- } ) ) ;
23+ const firestore = app . firestore ( ) ;
3724
3825describe ( 'generateVariablesForScope' , ( ) => {
3926 let testEnv : RulesTestEnvironment ;
4027
4128 beforeAll ( async ( ) => {
42- const projectId = process . env . GCLOUD_PROJECT || 'demo-deliberate-lab' ;
43-
4429 // Initialize test environment for cleanup utilities
4530 testEnv = await initializeTestEnvironment ( {
46- projectId,
31+ projectId : 'demo-deliberate-lab' ,
4732 firestore : process . env . FIRESTORE_EMULATOR_HOST
4833 ? undefined
4934 : {
@@ -52,24 +37,14 @@ describe('generateVariablesForScope', () => {
5237 } ,
5338 } ) ;
5439
55- // Initialize Firebase Admin SDK - this connects to the emulator
56- // via FIRESTORE_EMULATOR_HOST environment variable and has full
57- // Firestore API support including .count()
58- if ( ! admin . apps . length ) {
59- admin . initializeApp ( { projectId} ) ;
60- }
61- mockFirestore = admin . firestore ( ) ;
62- mockFirestore . settings ( { ignoreUndefinedProperties : true } ) ;
40+ // Clear any leftover data from previous test runs
41+ await testEnv . clearFirestore ( ) ;
6342 } ) ;
6443
6544 afterAll ( async ( ) => {
6645 await testEnv . cleanup ( ) ;
6746 } ) ;
6847
69- beforeEach ( async ( ) => {
70- await testEnv . clearFirestore ( ) ;
71- } ) ;
72-
7348 describe ( 'static variables' , ( ) => {
7449 it ( 'should generate static variables for matching scope' , async ( ) => {
7550 const config = createStaticVariableConfig ( {
@@ -167,7 +142,7 @@ describe('generateVariablesForScope', () => {
167142 expect ( result1 [ 'condition' ] ) . toBe ( JSON . stringify ( 'control' ) ) ;
168143
169144 // Add a participant to Firestore
170- await mockFirestore
145+ await firestore
171146 . collection ( `experiments/${ experimentId } /participants` )
172147 . doc ( 'p1' )
173148 . set (
@@ -189,7 +164,7 @@ describe('generateVariablesForScope', () => {
189164 expect ( result2 [ 'condition' ] ) . toBe ( JSON . stringify ( 'treatment' ) ) ;
190165
191166 // Add second participant
192- await mockFirestore
167+ await firestore
193168 . collection ( `experiments/${ experimentId } /participants` )
194169 . doc ( 'p2' )
195170 . set (
@@ -225,7 +200,7 @@ describe('generateVariablesForScope', () => {
225200
226201 // Add participants with existing assignments
227202 // 2 in group A, 1 in group B, 0 in group C
228- await mockFirestore
203+ await firestore
229204 . collection ( `experiments/${ experimentId } /participants` )
230205 . doc ( 'existing1' )
231206 . set ( {
@@ -238,7 +213,7 @@ describe('generateVariablesForScope', () => {
238213 variableMap : { group : JSON . stringify ( 'A' ) } ,
239214 } ) ;
240215
241- await mockFirestore
216+ await firestore
242217 . collection ( `experiments/${ experimentId } /participants` )
243218 . doc ( 'existing2' )
244219 . set ( {
@@ -251,7 +226,7 @@ describe('generateVariablesForScope', () => {
251226 variableMap : { group : JSON . stringify ( 'A' ) } ,
252227 } ) ;
253228
254- await mockFirestore
229+ await firestore
255230 . collection ( `experiments/${ experimentId } /participants` )
256231 . doc ( 'existing3' )
257232 . set ( {
@@ -328,7 +303,7 @@ describe('generateVariablesForScope', () => {
328303 const cohortB = 'cohort-B' ;
329304
330305 // Add 1 participant to cohort A
331- await mockFirestore
306+ await firestore
332307 . collection ( `experiments/${ experimentId } /participants` )
333308 . doc ( 'pA1' )
334309 . set (
@@ -410,7 +385,7 @@ describe('generateVariablesForScope', () => {
410385 expect ( result0 [ 'weighted_condition' ] ) . toBe ( JSON . stringify ( 'A' ) ) ;
411386
412387 // Add participant 0
413- await mockFirestore
388+ await firestore
414389 . collection ( `experiments/${ expId } /participants` )
415390 . doc ( 'wp0' )
416391 . set (
@@ -432,7 +407,7 @@ describe('generateVariablesForScope', () => {
432407 expect ( result1 [ 'weighted_condition' ] ) . toBe ( JSON . stringify ( 'A' ) ) ;
433408
434409 // Add participant 1
435- await mockFirestore
410+ await firestore
436411 . collection ( `experiments/${ expId } /participants` )
437412 . doc ( 'wp1' )
438413 . set (
@@ -472,7 +447,7 @@ describe('generateVariablesForScope', () => {
472447
473448 // Add 1 participant to each group (50/50 split)
474449 // Target is 67/33, so A is under-represented
475- await mockFirestore
450+ await firestore
476451 . collection ( `experiments/${ expId } /participants` )
477452 . doc ( 'existing1' )
478453 . set ( {
@@ -485,7 +460,7 @@ describe('generateVariablesForScope', () => {
485460 variableMap : { weighted_group : JSON . stringify ( 'A' ) } ,
486461 } ) ;
487462
488- await mockFirestore
463+ await firestore
489464 . collection ( `experiments/${ expId } /participants` )
490465 . doc ( 'existing2' )
491466 . set ( {
0 commit comments