@@ -21,11 +21,52 @@ import type {
2121 RegisteredAgentType ,
2222} from "../../features/agents/service" ;
2323import type { Profile } from "../../features/relay/contracts" ;
24- import { AgentBrowse } from "./AgentBrowse" ;
24+ import { useState } from "react" ;
25+ import {
26+ AgentBrowse as Catalog ,
27+ SharedAgentPanel ,
28+ type SharedAgentSelection ,
29+ } from "./AgentBrowse" ;
30+ import { PanelWorkspace } from "../../features/panels/PanelWorkspace" ;
31+ function AgentBrowse (
32+ props : ReturnType < typeof importing > & { relay : RelayData } ,
33+ ) {
34+ const [ selection , setSelection ] = useState < SharedAgentSelection > ( ) ;
35+ return (
36+ < >
37+ < Catalog
38+ relay = { props . relay }
39+ selectedId = { selection ?. agent . id }
40+ onSelect = { setSelection }
41+ />
42+ { selection && (
43+ < section aria-label = "Shared agent profile" >
44+ < PanelWorkspace
45+ value = { selection . agent . id }
46+ select = { ( ) => { } }
47+ items = { [
48+ {
49+ id : selection . agent . id ,
50+ label : selection . agent . name ,
51+ close : ( ) => setSelection ( undefined ) ,
52+ content : < SharedAgentPanel selection = { selection } { ...props } /> ,
53+ } ,
54+ ] }
55+ />
56+ </ section >
57+ ) }
58+ </ >
59+ ) ;
60+ }
2561
2662afterEach ( cleanup ) ;
2763
28- const echo = { key : "test/echo" , title : "Echo" } as RegisteredAgentType ;
64+ const echo = {
65+ key : "test/echo" ,
66+ title : "Echo" ,
67+ defaults : ( ) => ( { config : { model : "default" } } ) ,
68+ importShared : ( source : { model : string } ) => ( { model : source . model } ) ,
69+ } as RegisteredAgentType ;
2970const notes = { key : "test/notes" , title : "Notes" } as RegisteredAgentType ;
3071/** Import's props: one type, and an `agents` whose `port` resolves. */
3172const importing = ( types : readonly RegisteredAgentType [ ] = [ echo ] ) => ( {
@@ -84,13 +125,13 @@ it("browses shared agents and opens read-only details", async () => {
84125 expect ( read ) . toHaveBeenCalledWith ( [ { kinds : [ 30175 ] , limit : 200 } ] , {
85126 signal : expect . any ( AbortSignal ) ,
86127 } ) ;
87- fireEvent . click ( screen . getByRole ( "button" , { name : "View details " } ) ) ;
128+ fireEvent . click ( screen . getByRole ( "button" , { name : "Shared helper " } ) ) ;
88129 expect (
89- await screen . findByRole ( "dialog " , { name : "Shared helper " } ) ,
130+ await screen . findByRole ( "region " , { name : "Shared agent profile " } ) ,
90131 ) . toBeVisible ( ) ;
91132 expect ( screen . getByText ( "Help with tasks." ) ) . toBeVisible ( ) ;
92133 // Unknown to the profile directory, the sharer shows as their short key.
93- expect ( screen . getByText ( "By " ) ) . toBeVisible ( ) ;
134+ expect ( screen . getByText ( "Shared by " ) ) . toBeVisible ( ) ;
94135 expect ( screen . getByText ( / ^ n p u b … / ) ) . toBeVisible ( ) ;
95136} ) ;
96137
@@ -173,9 +214,7 @@ it("shrinks over-budget pages and preserves Load more and explicit retry", async
173214 fireEvent . click ( screen . getByRole ( "button" , { name : "Load more" } ) ) ;
174215 fireEvent . click ( await screen . findByRole ( "button" , { name : "Try again" } ) ) ;
175216 expect ( await screen . findByText ( "Helper 100" ) ) . toBeVisible ( ) ;
176- expect (
177- screen . getAllByRole ( "button" , { name : "View details" } ) ,
178- ) . toHaveLength ( 2 ) ;
217+ expect ( screen . getAllByRole ( "button" , { name : / ^ H e l p e r / } ) ) . toHaveLength ( 2 ) ;
179218 expect (
180219 screen . queryByRole ( "button" , { name : "Load more" } ) ,
181220 ) . not . toBeInTheDocument ( ) ;
@@ -242,12 +281,14 @@ it("names the sharer by their profile, with their key on hover", async () => {
242281 const read = vi . fn ( ) . mockResolvedValue ( [ shared ] ) ;
243282 const profiles = directory ( { [ shared . pubkey ] : { name : "Cora" } } ) ;
244283 render ( < AgentBrowse relay = { community ( read , profiles ) } { ...importing ( ) } /> ) ;
245- fireEvent . click ( await screen . findByRole ( "button" , { name : "View details" } ) ) ;
246- const dialog = await screen . findByRole ( "dialog" , { name : "Shared helper" } ) ;
284+ fireEvent . click ( await screen . findByRole ( "button" , { name : "Shared helper" } ) ) ;
285+ const dialog = await screen . findByRole ( "region" , {
286+ name : "Shared agent profile" ,
287+ } ) ;
247288 expect ( within ( dialog ) . getByText ( "Cora" ) ) . toBeVisible ( ) ;
248289 expect ( within ( dialog ) . queryByText ( / ^ n p u b … / ) ) . not . toBeInTheDocument ( ) ;
249290 expect ( profiles . ensure ) . toHaveBeenCalledWith ( [ shared . pubkey ] ) ;
250- expect ( within ( dialog ) . getByText ( "Model: test-model" ) ) . toBeVisible ( ) ;
291+ expect ( within ( dialog ) . getByText ( "test-model" ) ) . toBeVisible ( ) ;
251292} ) ;
252293
253294it ( "imports a shared agent through the import path, then hands back" , async ( ) => {
@@ -264,38 +305,95 @@ it("imports a shared agent through the import path, then hands back", async () =
264305 ] ) ;
265306 const props = importing ( ) ;
266307 render ( < AgentBrowse relay = { community ( read ) } { ...props } /> ) ;
267- fireEvent . click ( await screen . findByRole ( "button" , { name : "View details" } ) ) ;
268- const dialog = await screen . findByRole ( "dialog" , { name : "Shared helper" } ) ;
308+ fireEvent . click ( await screen . findByRole ( "button" , { name : "Shared helper" } ) ) ;
309+ const dialog = await screen . findByRole ( "region" , {
310+ name : "Shared agent profile" ,
311+ } ) ;
269312 expect ( within ( dialog ) . queryByLabelText ( "Import as" ) ) . not . toBeInTheDocument ( ) ;
270313 fireEvent . click ( within ( dialog ) . getByRole ( "button" , { name : "Import" } ) ) ;
314+ const confirmation = screen . getByRole ( "dialog" , {
315+ name : "Import Shared helper" ,
316+ } ) ;
317+ expect ( within ( confirmation ) . queryByRole ( "combobox" ) ) . not . toBeInTheDocument ( ) ;
318+ expect ( props . agents . port ) . not . toHaveBeenCalled ( ) ;
319+ fireEvent . click ( within ( confirmation ) . getByRole ( "button" , { name : "Import" } ) ) ;
271320 await vi . waitFor ( ( ) => expect ( props . onImported ) . toHaveBeenCalledOnce ( ) ) ;
272321 expect ( props . agents . port ) . toHaveBeenCalledExactlyOnceWith ( {
273322 source : "shared" ,
274323 type : "test/echo" ,
275324 name : "Shared helper" ,
276325 picture : "https://example.test/helper.png" ,
277326 instructions : "Help with tasks." ,
327+ config : { model : "test-model" } ,
278328 } ) ;
279329} ) ;
280330
281- it ( "imports as the chosen type, and keeps the dialog open with the error when it fails" , async ( ) => {
331+ it ( "imports as the chosen type, and keeps the panel open with the error when it fails" , async ( ) => {
282332 const read = vi . fn ( ) . mockResolvedValue ( [ shared ] ) ;
283333 const props = importing ( [ echo , notes ] ) ;
284334 props . agents . port . mockRejectedValueOnce (
285335 new Error ( "Connect to a community first" ) ,
286336 ) ;
287337 render ( < AgentBrowse relay = { community ( read ) } { ...props } /> ) ;
288- fireEvent . click ( await screen . findByRole ( "button" , { name : "View details" } ) ) ;
289- const dialog = await screen . findByRole ( "dialog" , { name : "Shared helper" } ) ;
290- expect (
291- within ( dialog ) . getByRole ( "combobox" , { name : "Import as" } ) ,
292- ) . toBeVisible ( ) ;
338+ fireEvent . click ( await screen . findByRole ( "button" , { name : "Shared helper" } ) ) ;
339+ const dialog = await screen . findByRole ( "region" , {
340+ name : "Shared agent profile" ,
341+ } ) ;
293342 fireEvent . click ( within ( dialog ) . getByRole ( "button" , { name : "Import" } ) ) ;
294- expect ( await within ( dialog ) . findByRole ( "alert" ) ) . toHaveTextContent (
343+ const confirmation = screen . getByRole ( "dialog" , {
344+ name : "Import Shared helper" ,
345+ } ) ;
346+ fireEvent . click (
347+ within ( confirmation ) . getByRole ( "button" , { name : "Change settings" } ) ,
348+ ) ;
349+ expect (
350+ within ( confirmation ) . getByRole ( "combobox" , { name : "Import as" } ) ,
351+ ) . toHaveFocus ( ) ;
352+ fireEvent . click ( within ( confirmation ) . getByRole ( "button" , { name : "Import" } ) ) ;
353+ expect ( await within ( confirmation ) . findByRole ( "alert" ) ) . toHaveTextContent (
295354 "Connect to a community first" ,
296355 ) ;
297356 expect ( props . onImported ) . not . toHaveBeenCalled ( ) ;
298357 expect ( props . agents . port ) . toHaveBeenCalledWith (
299358 expect . objectContaining ( { source : "shared" , type : "test/echo" } ) ,
300359 ) ;
301360} ) ;
361+
362+ it ( "does not silently replace an unavailable shared runtime" , async ( ) => {
363+ const read = vi . fn ( ) . mockResolvedValue ( [ shared ] ) ;
364+ const props = importing ( [ { ...echo , importShared : ( ) => undefined } ] ) ;
365+ render ( < AgentBrowse relay = { community ( read ) } { ...props } /> ) ;
366+ fireEvent . click ( await screen . findByRole ( "button" , { name : "Shared helper" } ) ) ;
367+ fireEvent . click ( screen . getByRole ( "button" , { name : "Import" } ) ) ;
368+ const dialog = screen . getByRole ( "dialog" , { name : "Import Shared helper" } ) ;
369+ expect ( within ( dialog ) . getByRole ( "button" , { name : "Import" } ) ) . toBeDisabled ( ) ;
370+ expect (
371+ within ( dialog ) . getByRole ( "combobox" , { name : "Import as" } ) ,
372+ ) . toBeVisible ( ) ;
373+ expect ( props . agents . port ) . not . toHaveBeenCalled ( ) ;
374+ } ) ;
375+
376+ it ( "collapses long instructions and expands them on request" , async ( ) => {
377+ const read = vi . fn ( ) . mockResolvedValue ( [
378+ {
379+ ...shared ,
380+ content : JSON . stringify ( {
381+ display_name : "Shared helper" ,
382+ system_prompt : "Long instructions. " . repeat ( 100 ) ,
383+ } ) ,
384+ } ,
385+ ] ) ;
386+ render ( < AgentBrowse relay = { community ( read ) } { ...importing ( ) } /> ) ;
387+ fireEvent . click ( await screen . findByRole ( "button" , { name : "Shared helper" } ) ) ;
388+ expect ( screen . getByRole ( "region" , { name : "Instructions" } ) ) . toHaveAttribute (
389+ "data-collapsed" ,
390+ ) ;
391+ fireEvent . click ( screen . getByRole ( "button" , { name : "See more" } ) ) ;
392+ expect (
393+ screen . getByRole ( "region" , { name : "Instructions" } ) ,
394+ ) . not . toHaveAttribute ( "data-collapsed" ) ;
395+ expect ( screen . getByRole ( "button" , { name : "See less" } ) ) . toHaveAttribute (
396+ "aria-expanded" ,
397+ "true" ,
398+ ) ;
399+ } ) ;
0 commit comments