|
| 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 { getFunctions, httpsCallable, httpsCallableFromURL } from 'firebase/functions'; |
| 21 | +import { getAuth, deleteUser, signInAnonymously } from 'firebase/auth'; |
| 22 | +import { OK, FAILED, waitForUserSignIn } from 'lib/util'; |
| 23 | + |
| 24 | +export type TestResults = { |
| 25 | + initializeAppResult: string, |
| 26 | + initializeAuthResult: string, |
| 27 | + authUserSignedInResult: string, |
| 28 | + initializeFunctionsResult: string, |
| 29 | + createCallTestResult: string, |
| 30 | + callTestInvokedSuccessfullyResult: string, |
| 31 | + callTestResponseResult: string, |
| 32 | + httpCallableTestInvokedSuccessfullyResult: string, |
| 33 | + httpCallableResponseResult: string, |
| 34 | + deleteUserResult: string, |
| 35 | + authSignedOutResult: string, |
| 36 | + deleteAppResult: string |
| 37 | +}; |
| 38 | + |
| 39 | +export function initializeTestResults(): TestResults { |
| 40 | + return { |
| 41 | + initializeAppResult: FAILED, |
| 42 | + initializeAuthResult: FAILED, |
| 43 | + authUserSignedInResult: FAILED, |
| 44 | + initializeFunctionsResult: FAILED, |
| 45 | + createCallTestResult: FAILED, |
| 46 | + callTestInvokedSuccessfullyResult: FAILED, |
| 47 | + callTestResponseResult: FAILED, |
| 48 | + httpCallableTestInvokedSuccessfullyResult: FAILED, |
| 49 | + httpCallableResponseResult: FAILED, |
| 50 | + deleteUserResult: FAILED, |
| 51 | + authSignedOutResult: FAILED, |
| 52 | + deleteAppResult: FAILED |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +export async function testFunctions(): Promise<TestResults> { |
| 57 | + const result: TestResults = initializeTestResults(); |
| 58 | + try { |
| 59 | + const firebaseApp = initializeApp(firebaseConfig); |
| 60 | + if (firebaseApp === null) { |
| 61 | + return result; |
| 62 | + } |
| 63 | + result.initializeAppResult = OK; |
| 64 | + |
| 65 | + const auth = getAuth(firebaseApp); |
| 66 | + await auth.authStateReady(); |
| 67 | + result.initializeAuthResult = OK; |
| 68 | + |
| 69 | + await signInAnonymously(auth); |
| 70 | + await waitForUserSignIn(auth); |
| 71 | + if (auth.currentUser) { |
| 72 | + result.authUserSignedInResult = OK; |
| 73 | + |
| 74 | + const functions = getFunctions(firebaseApp); |
| 75 | + if (functions === null) { |
| 76 | + return result; |
| 77 | + } |
| 78 | + result.initializeFunctionsResult = OK; |
| 79 | + |
| 80 | + const callTest = httpsCallable<{ data: string }, { word: string }>( |
| 81 | + functions, |
| 82 | + 'callTest' |
| 83 | + ); |
| 84 | + if (!callTest) { |
| 85 | + return result; |
| 86 | + } |
| 87 | + result.createCallTestResult = OK; |
| 88 | + const callTestResult = await callTest({ data: 'blah' }); |
| 89 | + result.callTestInvokedSuccessfullyResult = OK; |
| 90 | + if (callTestResult.data.word === 'hellooo') { |
| 91 | + result.callTestResponseResult = OK; |
| 92 | + } |
| 93 | + |
| 94 | + const httpCallableTest = httpsCallableFromURL<{ data: string }, { word: string }>( |
| 95 | + functions, |
| 96 | + `https://us-central1-${firebaseConfig.projectId}.cloudfunctions.net/callTest` |
| 97 | + ); |
| 98 | + const httpsCallableResult = await httpCallableTest({ data: 'blah' }); |
| 99 | + result.httpCallableTestInvokedSuccessfullyResult = OK; |
| 100 | + if (httpsCallableResult.data.word === 'hellooo') { |
| 101 | + result.httpCallableResponseResult = OK; |
| 102 | + } |
| 103 | + |
| 104 | + await deleteUser(auth.currentUser); |
| 105 | + result.deleteUserResult = OK; |
| 106 | + |
| 107 | + await auth.signOut(); |
| 108 | + result.authSignedOutResult = OK; |
| 109 | + } |
| 110 | + |
| 111 | + deleteApp(firebaseApp); |
| 112 | + result.deleteAppResult = OK; |
| 113 | + } catch (e) { |
| 114 | + console.error("Caught error: ", e); |
| 115 | + } |
| 116 | + return result; |
| 117 | +} |
0 commit comments