|
| 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 | +import { deleteApp, initializeApp } from 'firebase/app'; |
| 18 | +import { getAnalytics, logEvent, isSupported } from 'firebase/analytics'; |
| 19 | +import { firebaseConfig } from 'lib/firebase'; |
| 20 | +import { OK, OK_SKIPPED, FAILED, sleep } from 'lib/util'; |
| 21 | + |
| 22 | +export type TestAnalyticsResult = { |
| 23 | + initializeAppResult: string, |
| 24 | + isSupportedResult: string, |
| 25 | + getAnalyticsResult: string, |
| 26 | + logEventResult: string, |
| 27 | + deleteAppResult: string |
| 28 | +}; |
| 29 | + |
| 30 | +export function createAnalyticsTestResult(): TestAnalyticsResult { |
| 31 | + const testAnalyticsResult: TestAnalyticsResult = { |
| 32 | + initializeAppResult: FAILED, |
| 33 | + isSupportedResult: FAILED, |
| 34 | + getAnalyticsResult: FAILED, |
| 35 | + logEventResult: FAILED, |
| 36 | + deleteAppResult: FAILED |
| 37 | + }; |
| 38 | + return testAnalyticsResult; |
| 39 | +} |
| 40 | + |
| 41 | +export async function testApp(isServerApp: boolean = false): Promise<TestAnalyticsResult> { |
| 42 | + const result: TestAnalyticsResult = createAnalyticsTestResult(); |
| 43 | + if (isServerApp) { |
| 44 | + try { |
| 45 | + // Note: Analytics isn't supported in node environments. |
| 46 | + const firebaseApp = initializeApp(firebaseConfig); |
| 47 | + if (initializeApp !== null) { |
| 48 | + result.initializeAppResult = OK; |
| 49 | + const supported: boolean = await isSupported(); |
| 50 | + if (!supported) { |
| 51 | + result.isSupportedResult = OK; |
| 52 | + } |
| 53 | + deleteApp(firebaseApp); |
| 54 | + result.deleteAppResult = OK; |
| 55 | + } |
| 56 | + result.getAnalyticsResult = OK_SKIPPED; |
| 57 | + result.logEventResult = OK_SKIPPED; |
| 58 | + } catch (e) { |
| 59 | + console.log("Caught error: ", e); |
| 60 | + } |
| 61 | + } else /* !isServer() */ { |
| 62 | + try { |
| 63 | + const firebaseApp = initializeApp(firebaseConfig); |
| 64 | + if (firebaseApp !== null) { |
| 65 | + result.initializeAppResult = OK; |
| 66 | + const supported: boolean = await isSupported(); |
| 67 | + if (supported) { |
| 68 | + result.isSupportedResult = OK; |
| 69 | + } |
| 70 | + const analytics = getAnalytics(firebaseApp); |
| 71 | + if (analytics !== null) { |
| 72 | + result.getAnalyticsResult = OK; |
| 73 | + logEvent(analytics, 'begin_checkout'); |
| 74 | + result.logEventResult = OK; |
| 75 | + } |
| 76 | + // logEvent throws an error if we delete the app here, due to |
| 77 | + // it's asynchronous behavior. |
| 78 | + await sleep(1000); |
| 79 | + deleteApp(firebaseApp); |
| 80 | + result.deleteAppResult = OK; |
| 81 | + } |
| 82 | + } catch (e) { |
| 83 | + console.log("Caught error: ", e); |
| 84 | + } |
| 85 | + } |
| 86 | + return result; |
| 87 | +} |
0 commit comments