1- import { beforeEach , describe , expect , it , vi } from "vitest" ;
1+ import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
22import { EventGenerator } from "../../test" ;
3- import type { NDKEvent } from "../events/index.js" ;
4- import { NDK } from "../ndk/index.js" ;
5- import { NDKSubscription } from "../subscription/index.js" ;
3+ import type { NDKEvent } from "../events" ;
4+ import { NDK } from "../ndk" ;
65import { NDKUser , type NDKUserParams , type ProfilePointer } from "./index.js" ;
76import * as Nip05 from "./nip05.js" ;
87
98describe ( "NDKUser" , ( ) => {
109 let ndk : NDK ;
1110
11+ const FROZEN_TIME = new Date ( "2020-01-01T00:00:00.000Z" ) ;
12+ const NOW_SEC = Math . floor ( FROZEN_TIME . getTime ( ) / 1000 ) ;
13+
1214 beforeEach ( ( ) => {
1315 vi . clearAllMocks ( ) ;
16+
17+ vi . useFakeTimers ( ) ;
18+ vi . setSystemTime ( FROZEN_TIME ) ;
19+
1420 ndk = new NDK ( ) ;
1521 EventGenerator . setNDK ( ndk ) ;
1622 } ) ;
1723
24+ afterEach ( ( ) => {
25+ vi . useRealTimers ( ) ;
26+ } ) ;
27+
1828 describe ( "constructor" , ( ) => {
1929 it ( "sets npub from provided npub" , ( ) => {
2030 const opts : NDKUserParams = {
@@ -84,14 +94,129 @@ describe("NDKUser", () => {
8494 pubkey = user . pubkey ;
8595 } ) ;
8696
97+ it ( "profile returns metadata event" , async ( ) => {
98+ const event = EventGenerator . createEvent (
99+ 0 ,
100+ JSON . stringify ( {
101+ name : "Jeff" ,
102+ picture : "https://image.url" ,
103+ } ) ,
104+ pubkey ,
105+ ) ;
106+ event . created_at = NOW_SEC - 7200 ;
107+
108+ ndk . fetchEvent = vi . fn ( ) . mockResolvedValueOnce ( event ) ;
109+
110+ const profile = await user . fetchProfile ( ) ;
111+ const metadataevent = JSON . parse ( profile . profileEvent ) ;
112+
113+ expect ( metadataevent . id ) . toEqual ( event . id ) ;
114+ } ) ;
115+
116+ it ( "duplicate fetching of profile" , async ( ) => {
117+ const event = EventGenerator . createEvent (
118+ 0 ,
119+ JSON . stringify ( {
120+ name : "Jeff" ,
121+ picture : "https://image.url" ,
122+ } ) ,
123+ pubkey ,
124+ ) ;
125+ event . created_at = NOW_SEC - 7200 ;
126+
127+ ndk . fetchEvent = vi . fn ( ) . mockResolvedValueOnce ( event ) ;
128+
129+ const profile = await user . fetchProfile ( ) ;
130+ expect ( profile ?. name ) . toEqual ( "Jeff" ) ;
131+ expect ( profile ?. picture ) . toEqual ( "https://image.url" ) ;
132+
133+ const profile2 = await user . fetchProfile ( ) ;
134+ expect ( profile2 ?. name ) . toEqual ( "Jeff" ) ;
135+ expect ( profile2 ?. picture ) . toEqual ( "https://image.url" ) ;
136+ } ) ;
137+
138+ it ( "newer profile overwrites older profile (two fetches)" , async ( ) => {
139+ oldEvent = EventGenerator . createEvent (
140+ 0 ,
141+ JSON . stringify ( {
142+ name : "Jeff_OLD" ,
143+ picture : "https://image.url.old" ,
144+ about : "About jeff OLD" ,
145+ } ) ,
146+ pubkey ,
147+ ) ;
148+ oldEvent . created_at = NOW_SEC - 7200 ;
149+
150+ ndk . fetchEvent = vi . fn ( ) . mockResolvedValueOnce ( oldEvent ) ;
151+
152+ await user . fetchProfile ( ) ;
153+ expect ( user . profile ?. name ) . toEqual ( "Jeff_OLD" ) ;
154+ expect ( user . profile ?. picture ) . toEqual ( "https://image.url.old" ) ;
155+ expect ( user . profile ?. about ) . toEqual ( "About jeff OLD" ) ;
156+
157+ newEvent = EventGenerator . createEvent (
158+ 0 ,
159+ JSON . stringify ( {
160+ name : "Jeff" ,
161+ picture : "https://image.url" ,
162+ about : "About jeff" ,
163+ } ) ,
164+ pubkey ,
165+ ) ;
166+ newEvent . created_at = NOW_SEC ;
167+
168+ ndk . fetchEvent = vi . fn ( ) . mockResolvedValueOnce ( newEvent ) ;
169+ await user . fetchProfile ( ) ;
170+ expect ( user . profile ?. name ) . toEqual ( "Jeff" ) ;
171+ expect ( user . profile ?. picture ) . toEqual ( "https://image.url" ) ;
172+ expect ( user . profile ?. about ) . toEqual ( "About jeff" ) ;
173+ } ) ;
174+
175+ it . skip ( "older profile does not overwrite newer profile (two fetches)" , async ( ) => {
176+ newEvent = EventGenerator . createEvent (
177+ 0 ,
178+ JSON . stringify ( {
179+ name : "Jeff" ,
180+ picture : "https://image.url" ,
181+ about : "About jeff" ,
182+ } ) ,
183+ pubkey ,
184+ ) ;
185+ newEvent . created_at = NOW_SEC - 3600 ;
186+
187+ oldEvent = EventGenerator . createEvent (
188+ 0 ,
189+ JSON . stringify ( {
190+ name : "Jeff_OLD" ,
191+ picture : "https://image.url.old" ,
192+ about : "About jeff OLD" ,
193+ } ) ,
194+ pubkey ,
195+ ) ;
196+ oldEvent . created_at = NOW_SEC - 7200 ;
197+
198+ ndk . fetchEvent = vi . fn ( ) . mockResolvedValue ( newEvent ) ;
199+
200+ await user . fetchProfile ( ) ;
201+ expect ( user . profile ?. name ) . toEqual ( "Jeff" ) ;
202+ expect ( user . profile ?. picture ) . toEqual ( "https://image.url" ) ;
203+ expect ( user . profile ?. about ) . toEqual ( "About jeff" ) ;
204+
205+ ndk . fetchEvent = vi . fn ( ) . mockResolvedValue ( oldEvent ) ;
206+ await user . fetchProfile ( ) ;
207+ expect ( user . profile ?. name ) . toEqual ( "Jeff" ) ;
208+ expect ( user . profile ?. picture ) . toEqual ( "https://image.url" ) ;
209+ expect ( user . profile ?. about ) . toEqual ( "About jeff" ) ;
210+ } ) ;
211+
87212 it ( "Returns updated fields" , async ( ) => {
88213 // Use EventGenerator to create profile events
89214 newEvent = EventGenerator . createEvent (
90215 0 ,
91216 JSON . stringify ( {
92217 displayName : "JeffG" ,
93218 name : "Jeff" ,
94- image : "https://image.url" ,
219+ picture : "https://image.url" ,
95220 banner : "https://banner.url" ,
96221 bio : "Some bio info" ,
97222 nip05 : "_@jeffg.fyi" ,
@@ -101,14 +226,14 @@ describe("NDKUser", () => {
101226 } ) ,
102227 pubkey ,
103228 ) ;
104- newEvent . created_at = Math . floor ( Date . now ( ) / 1000 ) - 3600 ;
229+ newEvent . created_at = NOW_SEC - 3600 ;
105230
106231 oldEvent = EventGenerator . createEvent (
107232 0 ,
108233 JSON . stringify ( {
109234 displayName : "JeffG_OLD" ,
110235 name : "Jeff_OLD" ,
111- image : "https://image.url.old" ,
236+ picture : "https://image.url.old" ,
112237 banner : "https://banner.url.old" ,
113238 bio : "Some OLD bio info" ,
114239 nip05 : "OLD@jeffg.fyi" ,
@@ -125,7 +250,7 @@ describe("NDKUser", () => {
125250 await user . fetchProfile ( ) ;
126251 expect ( user . profile ?. displayName ) . toEqual ( "JeffG" ) ;
127252 expect ( user . profile ?. name ) . toEqual ( "Jeff" ) ;
128- expect ( user . profile ?. image ) . toEqual ( "https://image.url" ) ;
253+ expect ( user . profile ?. picture ) . toEqual ( "https://image.url" ) ;
129254 expect ( user . profile ?. banner ) . toEqual ( "https://banner.url" ) ;
130255 expect ( user . profile ?. bio ) . toEqual ( "Some bio info" ) ;
131256 expect ( user . profile ?. nip05 ) . toEqual ( "_@jeffg.fyi" ) ;
@@ -144,7 +269,7 @@ describe("NDKUser", () => {
144269 } ) ,
145270 pubkey ,
146271 ) ;
147- newEvent . created_at = Math . floor ( Date . now ( ) / 1000 ) - 3600 ;
272+ newEvent . created_at = NOW_SEC - 3600 ;
148273
149274 oldEvent = EventGenerator . createEvent (
150275 0 ,
@@ -153,7 +278,7 @@ describe("NDKUser", () => {
153278 } ) ,
154279 pubkey ,
155280 ) ;
156- oldEvent . created_at = Math . floor ( Date . now ( ) / 1000 ) - 7200 ;
281+ oldEvent . created_at = NOW_SEC - 7200 ;
157282
158283 ndk . fetchEvent = vi . fn ( ) . mockResolvedValue ( newEvent ) ;
159284
@@ -170,21 +295,21 @@ describe("NDKUser", () => {
170295 } ) ,
171296 pubkey ,
172297 ) ;
173- newEvent . created_at = Math . floor ( Date . now ( ) / 1000 ) - 3600 ;
298+ newEvent . created_at = NOW_SEC - 3600 ;
174299
175300 oldEvent = EventGenerator . createEvent (
176301 0 ,
177302 JSON . stringify ( {
178- image : "https://set-from-image-field.url" ,
303+ picture : "https://set-from-image-field.url" ,
179304 } ) ,
180305 pubkey ,
181306 ) ;
182- oldEvent . created_at = Math . floor ( Date . now ( ) / 1000 ) - 7200 ;
307+ oldEvent . created_at = NOW_SEC - 7200 ;
183308
184309 ndk . fetchEvent = vi . fn ( ) . mockResolvedValue ( newEvent ) ;
185310
186311 await user . fetchProfile ( ) ;
187- expect ( user . profile ?. image ) . toEqual ( "https://set-from-picture-field.url" ) ;
312+ expect ( user . profile ?. picture ) . toEqual ( "https://set-from-picture-field.url" ) ;
188313 } ) ;
189314
190315 it ( "Allows for arbitrary values to be set on user profiles" , async ( ) => {
@@ -195,7 +320,7 @@ describe("NDKUser", () => {
195320 } ) ,
196321 pubkey ,
197322 ) ;
198- newEvent . created_at = Math . floor ( Date . now ( ) / 1000 ) - 3600 ;
323+ newEvent . created_at = NOW_SEC - 3600 ;
199324
200325 oldEvent = EventGenerator . createEvent (
201326 0 ,
@@ -204,7 +329,7 @@ describe("NDKUser", () => {
204329 } ) ,
205330 pubkey ,
206331 ) ;
207- oldEvent . created_at = Math . floor ( Date . now ( ) / 1000 ) - 7200 ;
332+ oldEvent . created_at = NOW_SEC - 7200 ;
208333
209334 ndk . fetchEvent = vi . fn ( ) . mockResolvedValue ( newEvent ) ;
210335
@@ -215,9 +340,7 @@ describe("NDKUser", () => {
215340
216341 describe ( "validateNip05" , ( ) => {
217342 it ( "validates the NIP-05 for users" , async ( ) => {
218- const user = ndk . getUser ( {
219- pubkey : "1739d937dc8c0c7370aa27585938c119e25c41f6c441a5d34c6d38503e3136ef" ,
220- } ) ;
343+ const user = await ndk . fetchUser ( "1739d937dc8c0c7370aa27585938c119e25c41f6c441a5d34c6d38503e3136ef" ) ;
221344
222345 // Valid NIP-05
223346 const validNip05 = "_@jeffg.fyi" ;
0 commit comments