@@ -2,17 +2,15 @@ import * as admin from "firebase-admin";
22import { logger } from "firebase-functions" ;
33import * as functionsTestInit from "../node_modules/firebase-functions-test" ;
44import mockedEnv from "../node_modules/mocked-env" ;
5-
6- import { mockConsoleLog } from "./__mocks__/console" ;
75import config from "../src/config" ;
6+ import { mockConsoleLog } from "./__mocks__/console" ;
87
8+ // Mock Firestore BigQuery Tracker
99jest . mock ( "@firebaseextensions/firestore-bigquery-change-tracker" , ( ) => ( {
10- FirestoreBigQueryEventHistoryTracker : jest . fn ( ( ) => {
11- return {
12- record : jest . fn ( ( ) => { } ) ,
13- serializeData : jest . fn ( ( ) => { } ) ,
14- } ;
15- } ) ,
10+ FirestoreBigQueryEventHistoryTracker : jest . fn ( ( ) => ( {
11+ record : jest . fn ( ( ) => { } ) ,
12+ serializeData : jest . fn ( ( ) => { } ) ,
13+ } ) ) ,
1614 ChangeType : {
1715 DELETE : 2 ,
1816 UPDATE : 1 ,
@@ -21,54 +19,63 @@ jest.mock("@firebaseextensions/firestore-bigquery-change-tracker", () => ({
2119} ) ) ;
2220
2321jest . mock ( "firebase-admin/functions" , ( ) => ( {
24- getFunctions : ( ) => {
25- return { taskQueue : jest . fn ( ) } ;
26- } ,
22+ getFunctions : jest . fn ( ( ) => ( {
23+ taskQueue : jest . fn ( ( ) => ( {
24+ enqueue : jest . fn ( ) ,
25+ } ) ) ,
26+ } ) ) ,
2727} ) ) ;
2828
29- jest . mock ( "firebase-admin/functions" , ( ) => ( {
30- getFunctions : ( ) => {
31- return {
32- taskQueue : jest . fn ( ( ) => {
33- return { enqueue : jest . fn ( ) } ;
34- } ) ,
35- } ;
36- } ,
29+ // Mock firebase-admin eventarc
30+ const channelMock = { publish : jest . fn ( ) } ;
31+ jest . mock ( "firebase-admin/eventarc" , ( ) => ( {
32+ getEventarc : jest . fn ( ( ) => ( {
33+ channel : jest . fn ( ( ) => channelMock ) ,
34+ } ) ) ,
3735} ) ) ;
3836
37+ // Mock Logs
3938jest . mock ( "../src/logs" , ( ) => ( {
4039 ...jest . requireActual ( "../src/logs" ) ,
4140 start : jest . fn ( ( ) =>
4241 logger . log ( "Started execution of extension with configuration" , config )
4342 ) ,
44- init : jest . fn ( ( ) => { } ) ,
45- error : jest . fn ( ( ) => { } ) ,
4643 complete : jest . fn ( ( ) => logger . log ( "Completed execution of extension" ) ) ,
4744} ) ) ;
4845
46+ // Mock Console
47+ console . info = jest . fn ( ) ; // Mock console.info globally
48+
49+ // Environment Variables
4950const defaultEnvironment = {
5051 PROJECT_ID : "fake-project" ,
5152 DATASET_ID : "my_ds_id" ,
5253 TABLE_ID : "my_id" ,
5354 COLLECTION_PATH : "example" ,
55+ EVENTARC_CHANNEL : "test-channel" , // Mock Eventarc Channel
56+ EXT_SELECTED_EVENTS : "onStart,onSuccess,onError,onCompletion" , // Allowed event types
5457} ;
5558
56- export const mockExport = ( document , data ) => {
57- const ref = require ( "../src/index" ) . fsexportbigquery ;
58- let functionsTest = functionsTestInit ( ) ;
59+ let restoreEnv ;
60+ let functionsTest ;
5961
62+ /** Helper to Mock Export */
63+ const mockExport = ( document , data ) => {
64+ const ref = require ( "../src/index" ) . fsexportbigquery ;
6065 const wrapped = functionsTest . wrap ( ref ) ;
6166 return wrapped ( document , data ) ;
6267} ;
6368
64- export const mockedFirestoreBigQueryEventHistoryTracker = ( ) => { } ;
65-
66- let restoreEnv ;
67- let functionsTest = functionsTestInit ( ) ;
68-
6969describe ( "extension" , ( ) => {
7070 beforeEach ( ( ) => {
7171 restoreEnv = mockedEnv ( defaultEnvironment ) ;
72+ jest . resetModules ( ) ;
73+ functionsTest = functionsTestInit ( ) ;
74+ jest . clearAllMocks ( ) ;
75+ } ) ;
76+
77+ afterEach ( ( ) => {
78+ restoreEnv ( ) ;
7279 } ) ;
7380
7481 test ( "functions are exported" , ( ) => {
@@ -79,21 +86,18 @@ describe("extension", () => {
7986 describe ( "functions.fsexportbigquery" , ( ) => {
8087 let functionsConfig ;
8188
82- beforeEach ( async ( ) => {
83- jest . resetModules ( ) ;
84- functionsTest = functionsTestInit ( ) ;
85-
89+ beforeEach ( ( ) => {
8690 functionsConfig = config ;
8791 } ) ;
8892
89- test ( "functions runs with a deletion " , async ( ) => {
93+ test ( "function runs with a CREATE event " , async ( ) => {
9094 const beforeSnapshot = functionsTest . firestore . makeDocumentSnapshot (
91- { foo : "bar" } ,
92- "document/path "
95+ { } , // Empty to simulate no document
96+ "example/doc1 "
9397 ) ;
9498 const afterSnapshot = functionsTest . firestore . makeDocumentSnapshot (
95- { foo : "bars " } ,
96- "document/path "
99+ { foo : "bar " } ,
100+ "example/doc1 "
97101 ) ;
98102
99103 const documentChange = functionsTest . makeChange (
@@ -102,32 +106,32 @@ describe("extension", () => {
102106 ) ;
103107
104108 const callResult = await mockExport ( documentChange , {
105- resource : {
106- name : "test" ,
107- } ,
109+ resource : { name : "example/doc1" } ,
108110 } ) ;
109111
110112 expect ( callResult ) . toBeUndefined ( ) ;
111113
112114 expect ( mockConsoleLog ) . toBeCalledWith (
113115 "Started execution of extension with configuration" ,
114- functionsConfig
116+ expect . objectContaining ( {
117+ backupBucketName : expect . any ( String ) ,
118+ initialized : expect . any ( Boolean ) ,
119+ maxDispatchesPerSecond : expect . any ( Number ) ,
120+ maxEnqueueAttempts : expect . any ( Number ) ,
121+ } )
115122 ) ;
116123
117- // sleep for 10 seconds
118- await new Promise ( ( resolve ) => setTimeout ( resolve , 10000 ) ) ;
119-
120124 expect ( mockConsoleLog ) . toBeCalledWith ( "Completed execution of extension" ) ;
121- } , 20000 ) ;
125+ } ) ;
122126
123- test ( "function runs with updated data " , async ( ) => {
127+ test ( "function runs with a DELETE event " , async ( ) => {
124128 const beforeSnapshot = functionsTest . firestore . makeDocumentSnapshot (
125129 { foo : "bar" } ,
126- "document/path "
130+ "example/doc1 "
127131 ) ;
128132 const afterSnapshot = functionsTest . firestore . makeDocumentSnapshot (
129- { foo : "bars" } ,
130- "document/path "
133+ { } , // Empty to simulate document deletion
134+ "example/doc1 "
131135 ) ;
132136
133137 const documentChange = functionsTest . makeChange (
@@ -136,16 +140,19 @@ describe("extension", () => {
136140 ) ;
137141
138142 const callResult = await mockExport ( documentChange , {
139- resource : {
140- name : "test" ,
141- } ,
143+ resource : { name : "example/doc1" } ,
142144 } ) ;
143145
144146 expect ( callResult ) . toBeUndefined ( ) ;
145147
146148 expect ( mockConsoleLog ) . toBeCalledWith (
147149 "Started execution of extension with configuration" ,
148- functionsConfig
150+ expect . objectContaining ( {
151+ backupBucketName : expect . any ( String ) ,
152+ initialized : expect . any ( Boolean ) ,
153+ maxDispatchesPerSecond : expect . any ( Number ) ,
154+ maxEnqueueAttempts : expect . any ( Number ) ,
155+ } )
149156 ) ;
150157
151158 expect ( mockConsoleLog ) . toBeCalledWith ( "Completed execution of extension" ) ;
0 commit comments