@@ -5,18 +5,15 @@ import {
55 GeoPoint as AdminGeoPoint ,
66 Timestamp as AdminTimestamp ,
77} from 'firebase-admin/firestore' ;
8- import {
9- fromFirestoreDocumentData ,
10- toFirestoreDocumentData ,
11- } from './converter.js' ;
8+ import { firestoreDecode , firestoreEncode } from './converter.js' ;
129import { FirestoreSchema , FirestoreField } from 'effect-firebase' ;
1310
1411describe ( 'Firestore Converter' , ( ) => {
15- describe ( 'fromFirestoreDocumentData ' , ( ) => {
12+ describe ( 'firestoreDecode ' , ( ) => {
1613 it ( 'should convert Firestore Timestamp to FirestoreSchema.Timestamp' , ( ) => {
1714 const adminTimestamp = new AdminTimestamp ( 1705315800 , 123000000 ) ;
1815
19- const result = fromFirestoreDocumentData ( {
16+ const result = firestoreDecode ( {
2017 title : 'Test Post' ,
2118 createdAt : adminTimestamp ,
2219 } ) ;
@@ -30,7 +27,7 @@ describe('Firestore Converter', () => {
3027 it ( 'should handle nested timestamps' , ( ) => {
3128 const adminTimestamp = new AdminTimestamp ( 1705315800 , 0 ) ;
3229
33- const result = fromFirestoreDocumentData ( {
30+ const result = firestoreDecode ( {
3431 post : {
3532 createdAt : adminTimestamp ,
3633 title : 'Nested' ,
@@ -43,7 +40,7 @@ describe('Firestore Converter', () => {
4340 it ( 'should handle timestamps in arrays' , ( ) => {
4441 const adminTimestamp = new AdminTimestamp ( 1705315800 , 0 ) ;
4542
46- const result = fromFirestoreDocumentData ( {
43+ const result = firestoreDecode ( {
4744 timestamps : [ adminTimestamp , adminTimestamp ] ,
4845 } ) ;
4946
@@ -52,7 +49,7 @@ describe('Firestore Converter', () => {
5249 } ) ;
5350
5451 it ( 'should preserve null and undefined values' , ( ) => {
55- const result = fromFirestoreDocumentData ( {
52+ const result = firestoreDecode ( {
5653 nullValue : null ,
5754 undefinedValue : undefined ,
5855 stringValue : 'test' ,
@@ -64,18 +61,18 @@ describe('Firestore Converter', () => {
6461 } ) ;
6562
6663 it ( 'should NOT convert null to Timestamp' , ( ) => {
67- const result = fromFirestoreDocumentData ( {
64+ const result = firestoreDecode ( {
6865 createdAt : null ,
6966 } ) ;
7067
7168 expect ( result . createdAt ) . toBeNull ( ) ;
7269 } ) ;
7370 } ) ;
7471
75- describe ( 'toFirestoreDocumentData ' , ( ) => {
72+ describe ( 'firestoreEncode ' , ( ) => {
7673 it ( 'should convert FirestoreSchema.Timestamp to Firestore Timestamp' , ( ) => {
7774 const fakeFirestore = { } as unknown as Firestore ;
78- const result = toFirestoreDocumentData (
75+ const result = firestoreEncode (
7976 fakeFirestore ,
8077 FirestoreSchema . Timestamp . fromMillis ( 1705315800123 )
8178 ) ;
@@ -87,7 +84,7 @@ describe('Firestore Converter', () => {
8784
8885 it ( 'should convert FirestoreSchema.GeoPoint to Firestore GeoPoint' , ( ) => {
8986 const fakeFirestore = { } as unknown as Firestore ;
90- const result = toFirestoreDocumentData (
87+ const result = firestoreEncode (
9188 fakeFirestore ,
9289 new FirestoreSchema . GeoPoint ( {
9390 latitude : 55.6761 ,
@@ -110,7 +107,7 @@ describe('Firestore Converter', () => {
110107 } ,
111108 } as unknown as Firestore ;
112109
113- const result = toFirestoreDocumentData (
110+ const result = firestoreEncode (
114111 fakeFirestore ,
115112 FirestoreSchema . Reference . makeFromPath ( 'posts/post-1' )
116113 ) ;
@@ -121,7 +118,7 @@ describe('Firestore Converter', () => {
121118
122119 it ( 'should convert ServerTimestamp to Firestore field value' , ( ) => {
123120 const fakeFirestore = { } as unknown as Firestore ;
124- const result = toFirestoreDocumentData (
121+ const result = firestoreEncode (
125122 fakeFirestore ,
126123 FirestoreSchema . ServerTimestamp . make ( )
127124 ) ;
@@ -131,17 +128,14 @@ describe('Firestore Converter', () => {
131128
132129 it ( 'should convert Delete to Firestore field value' , ( ) => {
133130 const fakeFirestore = { } as unknown as Firestore ;
134- const result = toFirestoreDocumentData (
135- fakeFirestore ,
136- FirestoreField . delete ( )
137- ) ;
131+ const result = firestoreEncode ( fakeFirestore , FirestoreField . delete ( ) ) ;
138132
139133 expect ( result ) . toStrictEqual ( FieldValue . delete ( ) ) ;
140134 } ) ;
141135
142136 it ( 'should convert ArrayUnion to arrayUnion FieldValue' , ( ) => {
143137 const fakeFirestore = { } as unknown as Firestore ;
144- const result = toFirestoreDocumentData (
138+ const result = firestoreEncode (
145139 fakeFirestore ,
146140 FirestoreField . arrayUnion ( [ 'a' , 'b' ] )
147141 ) ;
@@ -150,19 +144,45 @@ describe('Firestore Converter', () => {
150144
151145 it ( 'should convert ArrayRemove to arrayRemove FieldValue' , ( ) => {
152146 const fakeFirestore = { } as unknown as Firestore ;
153- const result = toFirestoreDocumentData (
147+ const result = firestoreEncode (
154148 fakeFirestore ,
155149 FirestoreField . arrayRemove ( [ 'a' ] )
156150 ) ;
157151 expect ( result ) . toStrictEqual ( FieldValue . arrayRemove ( 'a' ) ) ;
158152 } ) ;
159153
154+ it ( 'should recursively encode values inside ArrayUnion' , ( ) => {
155+ const fakeFirestore = { } as unknown as Firestore ;
156+ const ts = FirestoreSchema . Timestamp . fromMillis ( 1705315800000 ) ;
157+ const result = firestoreEncode (
158+ fakeFirestore ,
159+ FirestoreField . arrayUnion ( [ ts ] )
160+ ) ;
161+ const expected = FieldValue . arrayUnion (
162+ AdminTimestamp . fromMillis ( 1705315800000 )
163+ ) ;
164+ expect ( result ) . toStrictEqual ( expected ) ;
165+ } ) ;
166+
167+ it ( 'should recursively encode values inside ArrayRemove' , ( ) => {
168+ const fakeFirestore = { } as unknown as Firestore ;
169+ const ts = FirestoreSchema . Timestamp . fromMillis ( 1705315800000 ) ;
170+ const result = firestoreEncode (
171+ fakeFirestore ,
172+ FirestoreField . arrayRemove ( [ ts ] )
173+ ) ;
174+ const expected = FieldValue . arrayRemove (
175+ AdminTimestamp . fromMillis ( 1705315800000 )
176+ ) ;
177+ expect ( result ) . toStrictEqual ( expected ) ;
178+ } ) ;
179+
160180 it ( 'should recursively convert nested objects and arrays' , ( ) => {
161181 const fakeFirestore = {
162182 doc : ( path : string ) => ( { path, __tag : 'fake-doc-ref' } ) ,
163183 } as unknown as Firestore ;
164184
165- const result = toFirestoreDocumentData ( fakeFirestore , {
185+ const result = firestoreEncode ( fakeFirestore , {
166186 createdAt : FirestoreSchema . Timestamp . fromMillis ( 1705315800000 ) ,
167187 metadata : {
168188 location : new FirestoreSchema . GeoPoint ( { latitude : 1 , longitude : 2 } ) ,
@@ -212,7 +232,7 @@ describe('Firestore Converter', () => {
212232 const adminGeoPoint = new AdminGeoPoint ( 10 , 20 ) ;
213233 const adminDelete = FieldValue . delete ( ) ;
214234
215- const result = toFirestoreDocumentData ( fakeFirestore , {
235+ const result = firestoreEncode ( fakeFirestore , {
216236 timestamp : adminTimestamp ,
217237 geoPoint : adminGeoPoint ,
218238 delete : adminDelete ,
0 commit comments