@@ -38,10 +38,13 @@ import * as logger from './logger';
3838import * as client from './client' ;
3939import * as storage from './storage' ;
4040import * as internalApi from './internal-api' ;
41+ import * as indexeddb from './indexeddb' ;
42+ import * as debug from './debug' ;
4143import { deleteApp , FirebaseApp } from '@firebase/app' ;
4244import { CustomProvider , ReCaptchaV3Provider } from './providers' ;
4345import { AppCheckService } from './factory' ;
4446import { AppCheckToken } from './public-types' ;
47+ import { getDebugToken } from './debug' ;
4548
4649describe ( 'api' , ( ) => {
4750 let app : FirebaseApp ;
@@ -118,6 +121,49 @@ describe('api', () => {
118121 } )
119122 ) . to . equal ( appCheckInstance ) ;
120123 } ) ;
124+ it ( 'starts debug mode on first call' , async ( ) => {
125+ let token : string = '' ;
126+ const fakeWrite = ( tokenToWrite : string ) : Promise < void > => {
127+ token = tokenToWrite ;
128+ return Promise . resolve ( ) ;
129+ } ;
130+ stub ( indexeddb , 'writeDebugTokenToIndexedDB' ) . callsFake ( fakeWrite ) ;
131+ stub ( indexeddb , 'readDebugTokenFromIndexedDB' ) . resolves ( token ) ;
132+ const consoleStub = stub ( console , 'log' ) ;
133+ self . FIREBASE_APPCHECK_DEBUG_TOKEN = true ;
134+ initializeAppCheck ( app , {
135+ provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
136+ } ) ;
137+ // Ensure getDebugToken() call inside `initializeAppCheck()`
138+ // has time to resolve, and double check its value matches that
139+ // written to indexedDB.
140+ expect ( await getDebugToken ( ) ) . to . equal ( token ) ;
141+ expect ( consoleStub . args [ 0 ] [ 0 ] ) . to . include ( token ) ;
142+ self . FIREBASE_APPCHECK_DEBUG_TOKEN = undefined ;
143+ } ) ;
144+ it ( 'does not call initializeDebugMode on second call' , async ( ) => {
145+ self . FIREBASE_APPCHECK_DEBUG_TOKEN = 'abcdefg' ;
146+ const consoleStub = stub ( console , 'log' ) ;
147+ const initializeDebugModeSpy = spy ( debug , 'initializeDebugMode' ) ;
148+ // First call, should call initializeDebugMode()
149+ initializeAppCheck ( app , {
150+ provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
151+ } ) ;
152+ expect ( initializeDebugModeSpy ) . to . be . called ;
153+ initializeDebugModeSpy . resetHistory ( ) ;
154+ // Second call, should not call initializeDebugMode()
155+ initializeAppCheck ( app , {
156+ provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
157+ } ) ;
158+ const token = await getDebugToken ( ) ;
159+ expect ( token ) . to . equal ( 'abcdefg' ) ;
160+ // Two console logs of the token, for each initializeAppCheck call.
161+ expect ( consoleStub . args [ 0 ] [ 0 ] ) . to . include ( token ) ;
162+ expect ( consoleStub . args [ 1 ] [ 0 ] ) . to . include ( token ) ;
163+ expect ( consoleStub . args [ 1 ] [ 0 ] ) . to . equal ( consoleStub . args [ 0 ] [ 0 ] ) ;
164+ expect ( initializeDebugModeSpy ) . to . not . be . called ;
165+ self . FIREBASE_APPCHECK_DEBUG_TOKEN = undefined ;
166+ } ) ;
121167
122168 it ( 'initialize reCAPTCHA when a ReCaptchaV3Provider is provided' , ( ) => {
123169 const initReCAPTCHAStub = stub ( reCAPTCHA , 'initialize' ) . returns (
0 commit comments