15
15
* limitations under the License.
16
16
*/
17
17
18
- import * as firestore from '@firebase/firestore-types' ;
19
-
20
18
/**
21
19
* NOTE: These helpers are used by api/ tests and therefore may not have any
22
20
* dependencies on src/ files.
@@ -25,38 +23,79 @@ import * as firestore from '@firebase/firestore-types';
25
23
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26
24
declare const __karma__ : any ;
27
25
26
+ enum TargetBackend {
27
+ EMULATOR = 'emulator' ,
28
+ QA = 'qa' ,
29
+ NIGHTLY = 'nightly' ,
30
+ PROD = 'prod'
31
+ }
32
+
28
33
// eslint-disable-next-line @typescript-eslint/no-require-imports
29
34
const PROJECT_CONFIG = require ( '../../../../config/project.json' ) ;
30
35
31
- const EMULATOR_PORT = process . env . FIRESTORE_EMULATOR_PORT ;
32
- const EMULATOR_PROJECT_ID = process . env . FIRESTORE_EMULATOR_PROJECT_ID ;
33
- export const USE_EMULATOR = ! ! EMULATOR_PORT ;
36
+ const TARGET_BACKEND : TargetBackend = getTargetBackend ( ) ;
34
37
35
- const EMULATOR_FIRESTORE_SETTING = {
36
- host : `localhost:${ EMULATOR_PORT } ` ,
37
- ssl : false
38
- } ;
38
+ export const USE_EMULATOR : boolean = TARGET_BACKEND === TargetBackend . EMULATOR ;
39
39
40
- const PROD_FIRESTORE_SETTING = {
41
- host : 'firestore.googleapis.com' ,
42
- ssl : true
40
+ export const DEFAULT_SETTINGS = {
41
+ host : getFirestoreHost ( TARGET_BACKEND ) ,
42
+ ssl : getSslEnabled ( TARGET_BACKEND )
43
43
} ;
44
44
45
- export const DEFAULT_SETTINGS = getDefaultSettings ( ) ;
46
-
47
45
// eslint-disable-next-line no-console
48
46
console . log ( `Default Settings: ${ JSON . stringify ( DEFAULT_SETTINGS ) } ` ) ;
49
47
50
- function getDefaultSettings ( ) : firestore . Settings {
48
+ function parseTargetBackend ( targetBackend : string ) : TargetBackend {
49
+ switch ( targetBackend ) {
50
+ case 'emulator' :
51
+ return TargetBackend . EMULATOR ;
52
+ case 'qa' :
53
+ return TargetBackend . QA ;
54
+ case 'nightly' :
55
+ return TargetBackend . NIGHTLY ;
56
+ case 'prod' :
57
+ return TargetBackend . PROD ;
58
+ default :
59
+ throw Error ( 'Unknown backend configuration used for integration tests.' ) ;
60
+ }
61
+ }
62
+
63
+ function getTargetBackend ( ) : TargetBackend {
51
64
const karma = typeof __karma__ !== 'undefined' ? __karma__ : undefined ;
52
- if ( karma && karma . config . firestoreSettings ) {
53
- return karma . config . firestoreSettings ;
54
- } else {
55
- return USE_EMULATOR ? EMULATOR_FIRESTORE_SETTING : PROD_FIRESTORE_SETTING ;
65
+ if ( karma && karma . config . targetBackend ) {
66
+ return parseTargetBackend ( karma . config . targetBackend ) ;
56
67
}
68
+ if ( process . env . FIRESTORE_TARGET_BACKEND ) {
69
+ return parseTargetBackend ( process . env . FIRESTORE_TARGET_BACKEND ) ;
70
+ }
71
+ if ( process . env . FIRESTORE_EMULATOR_PORT ) {
72
+ return TargetBackend . EMULATOR ;
73
+ }
74
+ return TargetBackend . PROD ;
75
+ }
76
+
77
+ function getFirestoreHost ( targetBackend : TargetBackend ) : string {
78
+ switch ( targetBackend ) {
79
+ case TargetBackend . EMULATOR : {
80
+ const emulatorPort : string =
81
+ process . env . FIRESTORE_EMULATOR_PORT || '8080' ;
82
+ return `localhost:${ emulatorPort } ` ;
83
+ }
84
+ case TargetBackend . QA :
85
+ return 'staging-firestore.sandbox.googleapis.com' ;
86
+ case TargetBackend . NIGHTLY :
87
+ return 'test-firestore.sandbox.googleapis.com' ;
88
+ case TargetBackend . PROD :
89
+ default :
90
+ return 'firestore.googleapis.com' ;
91
+ }
92
+ }
93
+
94
+ function getSslEnabled ( targetBackend : TargetBackend ) : boolean {
95
+ return targetBackend !== TargetBackend . EMULATOR ;
57
96
}
58
97
59
98
export const DEFAULT_PROJECT_ID = USE_EMULATOR
60
- ? EMULATOR_PROJECT_ID
99
+ ? process . env . FIRESTORE_EMULATOR_PROJECT_ID || 'test-emulator'
61
100
: PROJECT_CONFIG . projectId ;
62
101
export const ALT_PROJECT_ID = 'test-db2' ;
0 commit comments