11// @vitest -environment jsdom
2- import { act , cleanup , render , screen } from "@testing-library/react" ;
3- import { afterEach , expect , it } from "vitest" ;
2+ import { act , cleanup , render , screen , waitFor } from "@testing-library/react" ;
3+ import { afterEach , expect , it , vi } from "vitest" ;
44import { createRelaySession } from "../../features/relay/session" ;
55import type { RelayData } from "../../features/relay/service" ;
66import { bindNames } from "../../features/identity-names/service" ;
77import { profileTarget } from "../../features/profiles/target" ;
88import { ProfilePanel } from "./ProfilePanel" ;
99
10+ import { agentDirectory } from "../agents/directory" ;
11+ import type { LiveCallbacks } from "../../features/relay/live" ;
12+ import { keypair , profile } from "../../features/relay/testing" ;
13+
1014const key = "a" . repeat ( 64 ) ;
1115afterEach ( cleanup ) ;
1216it ( "updates a mounted profile from the shared name view without replacing its identity" , async ( ) => {
@@ -17,7 +21,15 @@ it("updates a mounted profile from the shared name view without replacing its id
1721 register ( ) { } ,
1822 bind ( source ) {
1923 return bindNames ( source , {
20- snapshot : ( ) => [ { id : "test" , activate ( ) { } , resolve : ( ) => name } ] ,
24+ snapshot : ( ) => [
25+ {
26+ id : "test" ,
27+ activate ( ) {
28+ return undefined ;
29+ } ,
30+ resolve : ( ) => name ,
31+ } ,
32+ ] ,
2133 subscribe ( listener ) {
2234 notify = listener ;
2335 return ( ) => { } ;
@@ -54,6 +66,116 @@ it("updates a mounted profile from the shared name view without replacing its id
5466 expect ( screen . getByRole ( "heading" , { name } ) ) . toBeTruthy ( ) ;
5567 expect ( owner . session . profiles . snapshot ( ) . size ) . toBe ( 0 ) ;
5668 } finally {
69+ vi . useRealTimers ( ) ;
70+ cleanup ( ) ;
71+ owner . dispose ( ) ;
72+ }
73+ } ) ;
74+
75+ it ( "recovers provider-owned names in the same live session and releases demand when disabled" , async ( ) => {
76+ const person = keypair ( ) ;
77+ const relayKey = keypair ( ) ;
78+ const publicProfile = profile ( person , { name : "Public Carl" } ) ;
79+ let live ! : LiveCallbacks ;
80+ let entries = [ agentDirectory ] ;
81+ let changed = ( ) => { } ;
82+ const read = vi . fn ( async ( ) => ( {
83+ definitions : [ ] ,
84+ identities : [ { pubkey : person . pubkey , name : "Configured Carl" } ] ,
85+ } ) ) ;
86+ const owner = createRelaySession (
87+ {
88+ viewer : person . pubkey ,
89+ relayAuthor : relayKey . pubkey ,
90+ scope : "wss://relay.example.test" ,
91+ query : async ( ) => [ publicProfile ] ,
92+ media : ( ) => undefined ,
93+ readAgentLibrary : read ,
94+ subscribe ( callbacks ) {
95+ live = callbacks ;
96+ return { update ( ) { } , retry ( ) { } , dispose ( ) { } } ;
97+ } ,
98+ } ,
99+ {
100+ identityNames : {
101+ register ( ) { } ,
102+ bind ( source ) {
103+ return bindNames ( source , {
104+ snapshot : ( ) => entries ,
105+ subscribe ( listener ) {
106+ changed = listener ;
107+ return ( ) => { } ;
108+ } ,
109+ } ) ;
110+ } ,
111+ } ,
112+ } ,
113+ ) ;
114+ const snapshot = {
115+ status : "ready" as const ,
116+ generation : 1 ,
117+ session : owner . session ,
118+ } ;
119+ const relay : RelayData = {
120+ snapshot : ( ) => snapshot ,
121+ subscribe : ( ) => ( ) => { } ,
122+ retry ( ) { } ,
123+ disconnect ( ) { } ,
124+ clearCache : async ( ) => { } ,
125+ } ;
126+ const reconnect = ( ) => {
127+ live . state ( { status : "retrying" , routes : [ ] } ) ;
128+ live . state ( { status : "connected" , routes : [ ] } ) ;
129+ live . established ( ) ;
130+ } ;
131+ try {
132+ render (
133+ < ProfilePanel
134+ relay = { relay }
135+ target = { profileTarget ( person . pubkey ) ?? "" }
136+ close = { ( ) => { } }
137+ /> ,
138+ ) ;
139+ await screen . findByRole ( "heading" , { name : "Configured Carl" } ) ;
140+ await waitFor ( ( ) =>
141+ expect ( owner . session . profiles . snapshot ( ) . get ( person . pubkey ) ?. name ) . toBe (
142+ "Public Carl" ,
143+ ) ,
144+ ) ;
145+ act ( ( ) => live . state ( { status : "connected" , routes : [ ] } ) ) ;
146+ act ( ( ) => live . state ( { status : "retrying" , routes : [ ] } ) ) ;
147+ expect ( screen . getByRole ( "heading" , { name : "Public Carl" } ) ) . toBeTruthy ( ) ;
148+ expect ( owner . session . agentLibrary . snapshot ( ) . status ) . toBe ( "idle" ) ;
149+ expect ( read ) . toHaveBeenCalledTimes ( 1 ) ;
150+ act ( ( ) => {
151+ live . state ( { status : "connected" , routes : [ ] } ) ;
152+ live . established ( ) ;
153+ } ) ;
154+ await screen . findByRole ( "heading" , { name : "Configured Carl" } ) ;
155+ expect ( read ) . toHaveBeenCalledTimes ( 2 ) ;
156+ vi . useFakeTimers ( ) ;
157+ act ( ( ) => {
158+ entries = [ ] ;
159+ changed ( ) ;
160+ reconnect ( ) ;
161+ } ) ;
162+ await act ( async ( ) => {
163+ await vi . advanceTimersByTimeAsync ( 0 ) ;
164+ } ) ;
165+ vi . useRealTimers ( ) ;
166+ expect ( screen . getByRole ( "heading" , { name : "Public Carl" } ) ) . toBeTruthy ( ) ;
167+ expect ( read ) . toHaveBeenCalledTimes ( 2 ) ;
168+ act ( ( ) => {
169+ entries = [ agentDirectory ] ;
170+ changed ( ) ;
171+ } ) ;
172+ await screen . findByRole ( "heading" , { name : "Configured Carl" } ) ;
173+ expect ( read ) . toHaveBeenCalledTimes ( 3 ) ;
174+ owner . dispose ( ) ;
175+ act ( reconnect ) ;
176+ expect ( read ) . toHaveBeenCalledTimes ( 3 ) ;
177+ } finally {
178+ vi . useRealTimers ( ) ;
57179 cleanup ( ) ;
58180 owner . dispose ( ) ;
59181 }
0 commit comments