1- import { finalizeEvent } from "nostr-tools" ;
1+ import { compareEvents , finalizeEvent } from "nostr-tools" ;
22import { createDeferred } from "openclaw/plugin-sdk/extension-shared" ;
33import { describe , expect , it , vi } from "vitest" ;
44
@@ -14,6 +14,43 @@ const { PRIVATE_KEY, CHANNEL_ID, startTestBus, signSenderEvent, subscriptionIncl
1414 useBuzzBusLifecycleFixture ( ) ;
1515
1616describe ( "Buzz profile lifecycle" , ( ) => {
17+ it ( "selects the lowest event ID when profiles share a timestamp" , async ( ) => {
18+ relayMocks . auth . mockResolvedValue ( "ok" ) ;
19+ const secretKey = Uint8Array . from ( Buffer . from ( PRIVATE_KEY , "hex" ) ) ;
20+ const profiles = [ "First profile" , "Second profile" ] . map ( ( displayName ) =>
21+ finalizeEvent (
22+ {
23+ kind : 0 ,
24+ created_at : 1_700_000_000 ,
25+ content : JSON . stringify ( { display_name : displayName } ) ,
26+ tags : [ ] ,
27+ } ,
28+ secretKey ,
29+ ) ,
30+ ) ;
31+ const sortedProfiles = profiles . toSorted ( compareEvents ) ;
32+ const lowerIdProfile = sortedProfiles [ 0 ] ;
33+ const higherIdProfile = sortedProfiles [ 1 ] ;
34+ if ( ! lowerIdProfile || ! higherIdProfile ) {
35+ throw new Error ( "Expected two signed profile fixtures" ) ;
36+ }
37+ relayMocks . profileEvents = [ higherIdProfile , lowerIdProfile ] ;
38+
39+ const bus = await startTestBus ( { profileName : "Configured Agent Name" } ) ;
40+
41+ await vi . waitFor ( ( ) =>
42+ expect ( relayMocks . publish . mock . calls . some ( ( [ event ] ) => event . kind === 10_100 ) ) . toBe ( true ) ,
43+ ) ;
44+ const agentProfile = relayMocks . publish . mock . calls
45+ . map ( ( [ event ] ) => event )
46+ . find ( ( event ) => event . kind === 10_100 ) ;
47+ expect ( JSON . parse ( agentProfile ?. content ?? "{}" ) ) . toMatchObject ( {
48+ name : JSON . parse ( lowerIdProfile . content ) . display_name ,
49+ display_name : JSON . parse ( lowerIdProfile . content ) . display_name ,
50+ } ) ;
51+ await bus . close ( ) ;
52+ } ) ;
53+
1754 it ( "isolates message failures from fatal relay failures" , async ( ) => {
1855 relayMocks . auth . mockResolvedValue ( "ok" ) ;
1956 relayMocks . profileEvents = [
0 commit comments