1+ /**
2+ * @license
3+ * Copyright 2024 Google LLC
4+ *
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+
18+ import { deleteApp , initializeApp } from 'firebase/app' ;
19+ import { getDatabase , update , off , onValue , ref , remove , set } from 'firebase/database' ;
20+ import { firebaseConfig } from 'lib/firebase' ;
21+ import { OK , OK_SKIPPED , FAILED } from 'lib/util' ;
22+
23+ export type TestResults = {
24+ initializeAppResult : string ,
25+ initializeDatabaseResult : string ,
26+ createRefResult : string ,
27+ deleteSetValueListenerResult : string ,
28+ deleteUpdateValueListenerResult : string ,
29+ deleteRemoveValueListenerResult : string ,
30+ setValueResult : string ,
31+ updateValueResult : string ,
32+ deleteValueResult : string ,
33+ deleteAppResult : string
34+ } ;
35+
36+ export function initializeTestResults ( ) : TestResults {
37+ return {
38+ initializeAppResult : FAILED ,
39+ initializeDatabaseResult : FAILED ,
40+ createRefResult : FAILED ,
41+ deleteSetValueListenerResult : FAILED ,
42+ deleteUpdateValueListenerResult : FAILED ,
43+ deleteRemoveValueListenerResult : FAILED ,
44+ setValueResult : FAILED ,
45+ updateValueResult : FAILED ,
46+ deleteValueResult : FAILED ,
47+ deleteAppResult : FAILED
48+ } ;
49+ }
50+ export async function testDatabase ( isServer : boolean = false ) : Promise < TestResults > {
51+ const result : TestResults = initializeTestResults ( ) ;
52+ try {
53+ const firebaseApp = initializeApp ( firebaseConfig , "authTest" ) ;
54+ if ( firebaseApp === null ) {
55+ return result ;
56+ }
57+ result . initializeAppResult = OK ;
58+
59+ const db = getDatabase ( firebaseApp ) ;
60+ if ( db === null ) {
61+ return result ;
62+ }
63+ result . initializeDatabaseResult = OK ;
64+
65+ const dbRef = ref ( db , 'abc/def' ) ;
66+ if ( dbRef === null ) {
67+ return result ;
68+ }
69+ result . createRefResult = OK ;
70+
71+ // Set test.
72+ onValue ( dbRef , snap => {
73+ if ( snap . exists ( ) ) {
74+ const value = snap . val ( ) ;
75+ if ( value . text !== undefined && value . text === 'string 123 xyz' ) {
76+ result . setValueResult = OK ;
77+ }
78+ }
79+ } ) ;
80+ await set ( dbRef , { text : 'string 123 xyz' } ) ;
81+ off ( dbRef ) ;
82+ result . deleteSetValueListenerResult = OK ;
83+
84+ // Update test.
85+ onValue ( dbRef , snap => {
86+ if ( snap . exists ( ) ) {
87+ const value = snap . val ( ) ;
88+ if ( value . number !== undefined && value . number === 987 ) {
89+ result . updateValueResult = OK ;
90+ }
91+ }
92+ } ) ;
93+ await update ( dbRef , { number : 987 } ) ;
94+ off ( dbRef ) ;
95+ result . deleteUpdateValueListenerResult = OK ;
96+
97+ // Remove test.
98+ onValue ( dbRef , snap => {
99+ if ( ! snap . exists ( ) ) {
100+ result . deleteValueResult = OK ;
101+ }
102+ } ) ;
103+ await remove ( dbRef ) ;
104+ console . log ( "remove returned" ) ;
105+ off ( dbRef ) ;
106+
107+ result . deleteRemoveValueListenerResult = OK ;
108+
109+ // Note: Deleting the app hangs the SSR pass on playwright on Firefox
110+ // and Chromium, but the hang does not occur when manually testing with
111+ // those browsers.
112+ if ( isServer ) {
113+ result . deleteAppResult = OK_SKIPPED ;
114+ } else {
115+ deleteApp ( firebaseApp ) ;
116+ result . deleteAppResult = OK ;
117+ }
118+ } catch ( e ) {
119+ console . log ( "Caught error: " , e ) ;
120+ }
121+ return result ;
122+ }
0 commit comments