22import { act , cleanup , render , screen } from "@testing-library/react" ;
33import { afterEach , expect , it , vi } from "vitest" ;
44import { LinkLabel } from "../../bundled/links/InlineLink" ;
5+ import { bindNames } from "../identity-names/service" ;
6+ import { createAgentDirectory } from "../identity-names/testing" ;
57import type { ChannelMessage , Profile } from "../relay/contracts" ;
68import type { RelaySession } from "../relay/session" ;
79import type { ThreadSnapshot , ThreadView } from "../relay/threads" ;
@@ -45,14 +47,19 @@ const ready = (target: ChannelMessage): Partial<ThreadSnapshot> => ({
4547 targetStatus : "ready" ,
4648} ) ;
4749
48- /** Exact readers the test settles by hand; `open` counts undisposed ones. */
50+ type Channel = {
51+ id : string ;
52+ name : string ;
53+ channelType ?: string ;
54+ cached ?: true ;
55+ } ;
56+ /** Exact readers the test settles by hand; `open` counts undisposed ones.
57+ * Names resolve through the app's own names view and agent directory. */
4958function fixture ( {
50- channels = [ { id : "design" , name : "design" } ] as {
51- id : string ;
52- name : string ;
53- channelType ?: string ;
54- } [ ] ,
59+ channels = [ { id : "design" , name : "design" } ] as Channel [ ] ,
5560 profiles = new Map < string , Profile > ( [ [ alice , { name : "Alice" } ] ] ) ,
61+ agents = [ ] as { pubkey : string ; name : string } [ ] ,
62+ shown = [ ] as ChannelMessage [ ] ,
5663 capacity = Number . POSITIVE_INFINITY ,
5764} = { } ) {
5865 const views : {
@@ -90,21 +97,51 @@ function fixture({
9097 } ,
9198 } ;
9299 } ) ;
100+ const changed = ( listeners : Set < ( ) => void > ) => ( ) =>
101+ act ( async ( ) => {
102+ for ( const listener of listeners ) listener ( ) ;
103+ } ) ;
104+ const subscriber = ( listeners : Set < ( ) => void > ) => ( listener : ( ) => void ) => {
105+ listeners . add ( listener ) ;
106+ return ( ) => void listeners . delete ( listener ) ;
107+ } ;
93108 const profileListeners = new Set < ( ) => void > ( ) ;
94- const list = { status : "ready" , channels } ;
109+ const listListeners = new Set < ( ) => void > ( ) ;
110+ const liveListeners = new Set < ( ) => void > ( ) ;
111+ let list = { status : "ready" , channels } ;
112+ let live = { status : "connected" } ;
95113 const ensure = vi . fn ( async ( ) => { } ) ;
114+ const profileQueries = {
115+ snapshot : ( ) => profiles ,
116+ subscribe : subscriber ( profileListeners ) ,
117+ ensure,
118+ } ;
119+ const library = { status : "ready" , definitions : [ ] , identities : agents } ;
120+ const names = bindNames (
121+ {
122+ profiles : profileQueries ,
123+ agentLibrary : {
124+ snapshot : ( ) => library ,
125+ subscribe : ( ) => ( ) => { } ,
126+ refresh : async ( ) => { } ,
127+ retain : ( ) => ( ) => { } ,
128+ } ,
129+ } ,
130+ { snapshot : ( ) => [ createAgentDirectory ( ) ] , subscribe : ( ) => ( ) => { } } ,
131+ ) ;
96132 const session = {
97133 unread : { } ,
98134 thread,
99- profiles : {
100- snapshot : ( ) => profiles ,
101- subscribe ( listener : ( ) => void ) {
102- profileListeners . add ( listener ) ;
103- return ( ) => void profileListeners . delete ( listener ) ;
104- } ,
105- ensure,
135+ names,
136+ profiles : profileQueries ,
137+ channels : {
138+ list : ( ) => list ,
139+ subscribeList : subscriber ( listListeners ) ,
140+ window : ( channelId : string ) => ( {
141+ rows : shown . filter ( ( row ) => row . channelId === channelId ) ,
142+ } ) ,
106143 } ,
107- channels : { list : ( ) => list , subscribeList : ( ) => ( ) => { } } ,
144+ live : { snapshot : ( ) => live , subscribe : subscriber ( liveListeners ) } ,
108145 media : ( ) => undefined ,
109146 } as unknown as RelaySession ;
110147 return {
@@ -115,9 +152,15 @@ function fixture({
115152 open : ( ) => views . filter ( ( view ) => ! view . disposed ) ,
116153 async setProfiles ( next : Map < string , Profile > ) {
117154 profiles = next ;
118- await act ( async ( ) => {
119- for ( const listener of profileListeners ) listener ( ) ;
120- } ) ;
155+ await changed ( profileListeners ) ( ) ;
156+ } ,
157+ async setChannels ( next : Channel [ ] ) {
158+ list = { status : "ready" , channels : next } ;
159+ await changed ( listListeners ) ( ) ;
160+ } ,
161+ async setLive ( status : string ) {
162+ live = { status } ;
163+ await changed ( liveListeners ) ( ) ;
121164 } ,
122165 } ;
123166}
@@ -168,8 +211,9 @@ it("names the linked message's author and channel once its exact reader settles"
168211 expect ( app . ensure ) . toHaveBeenCalledWith ( [ alice ] , "background" ) ;
169212} ) ;
170213
171- it ( "names a reply's author with the thread icon and a workflow by its owner " , async ( ) => {
214+ it ( "names a reply's author with the thread icon and a workflow as its row header does " , async ( ) => {
172215 const app = fixture ( ) ;
216+ const relay = "c" . repeat ( 64 ) ;
173217 render (
174218 < >
175219 { link ( app . session , href ( id ( 1 ) , id ( 9 ) ) ) }
@@ -179,50 +223,137 @@ it("names a reply's author with the thread icon and a workflow by its owner", as
179223 await started ( ) ;
180224 await app . views [ 0 ] ?. publish ( ready ( message ( id ( 1 ) , { threadRootId : id ( 9 ) } ) ) ) ;
181225 await app . views [ 1 ] ?. publish (
182- ready ( message ( id ( 2 ) , { authorId : "c" . repeat ( 64 ) , workflowOwnerId : alice } ) ) ,
226+ ready ( message ( id ( 2 ) , { authorId : relay , workflowOwnerId : alice } ) ) ,
183227 ) ;
184228 const reply = screen . getByRole ( "link" , { name : "Alice in #design" } ) ;
185229 expect ( reply . querySelector ( '[data-link-kind="thread"] svg' ) ) . not . toBeNull ( ) ;
186- expect (
187- screen . getByRole ( "link" , { name : "Workflow · owned by Alice in #design" } ) ,
188- ) . toBeTruthy ( ) ;
230+ // The owner did not send it, so only the workflow is named.
231+ const workflow = screen . getByRole ( "link" , { name : "Workflow in #design" } ) ;
232+ expect ( workflow . textContent ) . toBe ( "Workflow in #design" ) ;
233+ expect ( app . ensure ) . not . toHaveBeenCalledWith ( [ relay ] , "background" ) ;
189234} ) ;
190235
191- it ( "keeps the channel label while the author has no name, and names them once loaded" , async ( ) => {
236+ it ( "never names an author by a key fragment, and names them once their profile does" , async ( ) => {
237+ const agent = "d" . repeat ( 64 ) ;
238+ const unnamed = ( key : string ) : Profile => ( {
239+ name : key . slice ( 0 , 10 ) ,
240+ nameIsFallback : true ,
241+ } ) ;
192242 const app = fixture ( {
193243 profiles : new Map ( [
194- [ alice , { name : alice . slice ( 0 , 10 ) , nameIsFallback : true } ] ,
244+ [ alice , unnamed ( alice ) ] ,
245+ [ agent , unnamed ( agent ) ] ,
195246 ] ) ,
247+ agents : [ { pubkey : agent , name : "Builder" } ] ,
196248 } ) ;
197- render ( link ( app . session , href ( id ( 1 ) ) ) ) ;
249+ // The app's names view hands back the fragment for a profile with no name.
250+ expect ( app . session . names . resolve ( alice ) ) . toBe ( alice . slice ( 0 , 10 ) ) ;
251+ render (
252+ < >
253+ { link ( app . session , href ( id ( 1 ) ) ) }
254+ { link ( app . session , href ( id ( 2 ) ) ) }
255+ </ > ,
256+ ) ;
198257 await started ( ) ;
199258 await app . views [ 0 ] ?. publish ( ready ( message ( id ( 1 ) ) ) ) ;
259+ await app . views [ 1 ] ?. publish ( ready ( message ( id ( 2 ) , { authorId : agent } ) ) ) ;
200260 expect ( screen . getByRole ( "link" , { name : "design" } ) . textContent ) . toBe (
201261 "design" ,
202262 ) ;
203- await app . setProfiles ( new Map ( [ [ alice , { name : "Alice" } ] ] ) ) ;
263+ // An agent's library name is a name even while its profile has none.
264+ expect ( screen . getByRole ( "link" , { name : "Builder in #design" } ) ) . toBeTruthy ( ) ;
265+ await app . setProfiles (
266+ new Map ( [
267+ [ alice , { name : "Alice" } ] ,
268+ [ agent , unnamed ( agent ) ] ,
269+ ] ) ,
270+ ) ;
204271 expect ( screen . getByRole ( "link" , { name : "Alice in #design" } ) ) . toBeTruthy ( ) ;
205272} ) ;
206273
207- it ( "keeps the channel label when the message is unavailable or no reader is free" , async ( ) => {
208- const unavailable = fixture ( ) ;
209- const view = render ( link ( unavailable . session , href ( id ( 1 ) ) ) ) ;
274+ it ( "takes the author from the channel's loaded timeline without reading" , async ( ) => {
275+ const app = fixture ( { shown : [ message ( id ( 1 ) ) ] } ) ;
276+ render (
277+ < >
278+ { link ( app . session , href ( id ( 1 ) ) ) }
279+ { link ( app . session , href ( id ( 2 ) ) ) }
280+ </ > ,
281+ ) ;
210282 await started ( ) ;
211- await unavailable . views [ 0 ] ?. publish ( {
212- status : "idle" ,
283+ expect ( screen . getByRole ( "link" , { name : "Alice in #design" } ) ) . toBeTruthy ( ) ;
284+ // Only the message no timeline holds is read.
285+ expect ( app . thread ) . toHaveBeenCalledOnce ( ) ;
286+ expect ( app . thread ) . toHaveBeenCalledWith ( "design" , id ( 2 ) , { exact : true } ) ;
287+ } ) ;
288+
289+ it ( "waits for the relay to confirm a cached channel before reading" , async ( ) => {
290+ const app = fixture ( {
291+ channels : [ { id : "design" , name : "design" , cached : true } ] ,
292+ } ) ;
293+ render ( link ( app . session , href ( id ( 1 ) ) ) ) ;
294+ await started ( ) ;
295+ expect ( app . thread ) . not . toHaveBeenCalled ( ) ;
296+ expect ( screen . getByRole ( "link" , { name : "design" } ) ) . toBeTruthy ( ) ;
297+ await app . setChannels ( [ { id : "design" , name : "design" } ] ) ;
298+ await started ( ) ;
299+ expect ( app . thread ) . toHaveBeenCalledOnce ( ) ;
300+ await app . views [ 0 ] ?. publish ( ready ( message ( id ( 1 ) ) ) ) ;
301+ expect ( screen . getByRole ( "link" , { name : "Alice in #design" } ) ) . toBeTruthy ( ) ;
302+ } ) ;
303+
304+ it ( "keeps the channel label for an unavailable message and never reads it again" , async ( ) => {
305+ const app = fixture ( ) ;
306+ const view = render ( link ( app . session , href ( id ( 1 ) ) ) ) ;
307+ await started ( ) ;
308+ await app . views [ 0 ] ?. publish ( {
309+ status : "ready" ,
310+ canLoadMore : false ,
213311 targetStatus : "unavailable" ,
214- error : "Message unavailable" ,
215312 } ) ;
216313 expect ( screen . getByRole ( "link" , { name : "design" } ) ) . toBeTruthy ( ) ;
217- expect ( unavailable . open ( ) ) . toHaveLength ( 0 ) ;
314+ expect ( app . open ( ) ) . toHaveLength ( 0 ) ;
315+ // A virtualized remount, or the stream reconnecting, does not re-read it.
218316 view . unmount ( ) ;
317+ render ( link ( app . session , href ( id ( 1 ) ) ) ) ;
318+ await started ( ) ;
319+ await app . setLive ( "retrying" ) ;
320+ await app . setLive ( "connected" ) ;
321+ expect ( app . thread ) . toHaveBeenCalledOnce ( ) ;
322+ expect ( screen . getByRole ( "link" , { name : "design" } ) ) . toBeTruthy ( ) ;
323+ } ) ;
324+
325+ it ( "retries a failed lookup once the live stream reconnects, never in a loop" , async ( ) => {
326+ const app = fixture ( ) ;
327+ render ( link ( app . session , href ( id ( 1 ) ) ) ) ;
328+ await started ( ) ;
329+ await app . views [ 0 ] ?. publish ( {
330+ status : "error" ,
331+ targetStatus : "error" ,
332+ error : "Relay unavailable" ,
333+ } ) ;
334+ await started ( ) ;
335+ expect ( app . thread ) . toHaveBeenCalledOnce ( ) ;
336+ expect ( app . open ( ) ) . toHaveLength ( 0 ) ;
337+ // The stream staying up is no news; connecting again is.
338+ await app . setLive ( "connected" ) ;
339+ expect ( app . thread ) . toHaveBeenCalledOnce ( ) ;
340+ await app . setLive ( "retrying" ) ;
341+ await app . setLive ( "connected" ) ;
342+ expect ( app . thread ) . toHaveBeenCalledTimes ( 2 ) ;
343+ await app . views [ 1 ] ?. publish ( ready ( message ( id ( 1 ) ) ) ) ;
344+ expect ( screen . getByRole ( "link" , { name : "Alice in #design" } ) ) . toBeTruthy ( ) ;
345+ cleanup ( ) ;
346+ // No free reader fails the same way, without retrying until then.
219347 const full = fixture ( { capacity : 0 } ) ;
220348 render ( link ( full . session , href ( id ( 1 ) ) ) ) ;
221349 await started ( ) ;
222350 await started ( ) ;
223- // A failed lookup waits for a later mount instead of retrying in a loop.
224351 expect ( full . thread ) . toHaveBeenCalledOnce ( ) ;
225352 expect ( screen . getByRole ( "link" , { name : "design" } ) ) . toBeTruthy ( ) ;
353+ await full . setLive ( "retrying" ) ;
354+ await full . setLive ( "connected" ) ;
355+ await started ( ) ;
356+ expect ( full . thread ) . toHaveBeenCalledTimes ( 2 ) ;
226357} ) ;
227358
228359it ( "reads nothing for authored labels, drafts, unlisted channels, DMs or Solo chats" , async ( ) => {
0 commit comments