@@ -629,4 +629,41 @@ describe('EventRepository', () => {
629629 )
630630 } )
631631 } )
632+
633+ describe ( 'upsertMany' , ( ) => {
634+ it ( 'returns 0 when no events are provided' , async ( ) => {
635+ const result = await repository . upsertMany ( [ ] )
636+
637+ expect ( result ) . to . equal ( 0 )
638+ } )
639+
640+ it ( 'applies NIP-01 tie-breaker in batch conflict condition' , async ( ) => {
641+ const thenStub = sandbox . stub ( ) . callsFake ( ( onfulfilled ) => Promise . resolve ( onfulfilled ( { rowCount : 1 } ) ) )
642+ const whereRawStub = sandbox . stub ( ) . returns ( { then : thenStub } )
643+ const mergeStub = sandbox . stub ( ) . returns ( { whereRaw : whereRawStub } )
644+ const onConflictStub = sandbox . stub ( ) . returns ( { merge : mergeStub } )
645+ const insertStub = sandbox . stub ( ) . returns ( { onConflict : onConflictStub } )
646+ const masterDbClientStub = sandbox . stub ( ) . returns ( { insert : insertStub } ) as unknown as DatabaseClient
647+
648+ ; ( masterDbClientStub as any ) . raw = sandbox . stub ( ) . returns ( 'conflict-target' )
649+
650+ repository = new EventRepository ( masterDbClientStub , rrDbClient )
651+
652+ const event : Event = {
653+ id : 'e527fe8b0f64a38c6877f943a9e8841074056ba72aceb31a4c85e6d10b27095a' ,
654+ pubkey : '55b702c167c85eb1c2d5ab35d68bedd1a35b94c01147364d2395c2f66f35a503' ,
655+ created_at : 1564498626 ,
656+ kind : 0 ,
657+ tags : [ ] ,
658+ content : '{"name":"ottman@minds.io"}' ,
659+ sig : 'd1de98733de2b412549aa64454722d9b66ab3c68e9e0d0f9c5d42e7bd54c30a06174364b683d2c8dbb386ff47f31e6cb7e2f3c3498d8819ee80421216c8309a9' ,
660+ [ ContextMetadataKey ] : { remoteAddress : { address : '::1' } as any } ,
661+ }
662+
663+ const result = await repository . upsertMany ( [ event ] )
664+
665+ expect ( whereRawStub ) . to . have . been . calledOnceWithExactly ( '("events"."event_created_at" < "excluded"."event_created_at" or ("events"."event_created_at" = "excluded"."event_created_at" and "events"."event_id" > "excluded"."event_id"))' )
666+ expect ( result ) . to . equal ( 1 )
667+ } )
668+ } )
632669} )
0 commit comments