|
| 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 { firebaseConfig } from 'lib/firebase'; |
| 20 | +import { getMessaging } from 'firebase/messaging'; |
| 21 | +import { OK, OK_SKIPPED, FAILED } from 'lib/util'; |
| 22 | + |
| 23 | +export type TestResults = { |
| 24 | + initializeAppResult: string, |
| 25 | + initializeMessagingResult: string, |
| 26 | + deleteAppResult: string |
| 27 | +}; |
| 28 | + |
| 29 | +export function initializeTestResults(): TestResults { |
| 30 | + return { |
| 31 | + initializeAppResult: FAILED, |
| 32 | + initializeMessagingResult: FAILED, |
| 33 | + deleteAppResult: FAILED |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +export async function testMessaging(isServer: boolean = false): Promise<TestResults> { |
| 38 | + const result: TestResults = initializeTestResults(); |
| 39 | + if (isServer) { |
| 40 | + result.initializeAppResult = OK_SKIPPED; |
| 41 | + result.initializeMessagingResult = OK_SKIPPED; |
| 42 | + result.deleteAppResult = OK_SKIPPED; |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + const firebaseApp = initializeApp(firebaseConfig); |
| 48 | + if (firebaseApp === null) { |
| 49 | + return result; |
| 50 | + } |
| 51 | + result.initializeAppResult = OK; |
| 52 | + |
| 53 | + const messaging = getMessaging(firebaseApp); |
| 54 | + if (!isServer && messaging || isServer && !messaging) { |
| 55 | + result.initializeMessagingResult = OK; |
| 56 | + } |
| 57 | + |
| 58 | + deleteApp(firebaseApp); |
| 59 | + result.deleteAppResult = OK; |
| 60 | + } catch (e) { |
| 61 | + console.error("Caught error: ", e); |
| 62 | + } |
| 63 | + return result; |
| 64 | +} |
0 commit comments