@@ -11,6 +11,7 @@ import {
1111 within ,
1212} from "@testing-library/react" ;
1313import userEvent from "@testing-library/user-event" ;
14+ import { useSyncExternalStore } from "react" ;
1415import { ToastProvider } from "../shared/design-system/ui/Toast" ;
1516import { Context } from "@deepseek-ai/cordis" ;
1617import { afterEach , expect , it , vi } from "vitest" ;
@@ -1331,6 +1332,7 @@ it("opens Manage plugins from the Decisions API hint", async () => {
13311332function pluginSectionsFixture (
13321333 cards : SettingsCards ,
13331334 decisions ?: DecisionsService ,
1335+ manager ?: object ,
13341336) {
13351337 const plugin = ( id : string , name : string ) => ( {
13361338 manifest : { id, name, apiVersion : 1 as const } ,
@@ -1367,25 +1369,27 @@ function pluginSectionsFixture(
13671369 change : vi . fn ( ) ,
13681370 reload : vi . fn ( ) ,
13691371 dismissError : vi . fn ( ) ,
1370- // A linked import stays loading, so its dialog can be closed at once.
1371- imports : { git : ( ) => new Promise ( ( ) => { } ) } ,
1372+ ...manager ,
13721373 } as unknown as Parameters < typeof Settings > [ 0 ] [ "plugins" ] ;
13731374 const onSection = vi . fn ( ) ;
13741375 const onPluginDetailsClosed = vi . fn ( ) ;
13751376 const onPluginImportClosed = vi . fn ( ) ;
13761377 /** Per-visit positions, as the navigation service keeps them. */
13771378 const positions = new Map < string , unknown > ( ) ;
1379+ const stored = ( entryId : string ) =>
1380+ positions . get ( JSON . stringify ( [ entryId , "settings.pluginDetails" ] ) ) ;
13781381 /** One request per visit, kept across renders as App's `route.request` is. */
13791382 const requests = new Map < string , unknown > ( ) ;
13801383 const request = ( section : string , visit : Visit = { } ) => {
1381- const { entryId, traversal, ...address } = visit ;
1384+ const { entryId = section , traversal, ...address } = visit ;
13821385 const target = { version : 1 , kind : "settings" , section, ...address } ;
13831386 const key = JSON . stringify ( [ entryId , traversal , target ] ) ;
13841387 if ( ! requests . has ( key ) )
13851388 requests . set ( key , {
1386- ... ( entryId && { entryId } ) ,
1389+ entryId,
13871390 ...( traversal && { traversal } ) ,
13881391 target,
1392+ signal : new AbortController ( ) . signal ,
13891393 complete : ( ) => true ,
13901394 position : ( name : string ) =>
13911395 positions . get ( JSON . stringify ( [ entryId , name ] ) ) ,
@@ -1394,26 +1398,27 @@ function pluginSectionsFixture(
13941398 } ) ;
13951399 return requests . get ( key ) as never ;
13961400 } ;
1397- const view = (
1398- section : string ,
1399- communities = host . communities ,
1400- visit ?: Visit ,
1401- ) => (
1401+ const settings = ( navigation : never , communities = host . communities ) => (
14021402 < div id = "settings-content" tabIndex = { - 1 } >
14031403 < Settings
14041404 { ...host }
14051405 communities = { communities }
14061406 cards = { cards }
14071407 plugins = { plugins }
14081408 decisions = { decisions }
1409- navigation = { request ( section , visit ) }
1409+ navigation = { navigation }
14101410 onSection = { onSection }
14111411 onPluginDetailsClosed = { onPluginDetailsClosed }
14121412 onPluginImportClosed = { onPluginImportClosed }
14131413 navigationPane
14141414 />
14151415 </ div >
14161416 ) ;
1417+ const view = (
1418+ section : string ,
1419+ communities = host . communities ,
1420+ visit ?: Visit ,
1421+ ) => settings ( request ( section , visit ) , communities ) ;
14171422 /** The named link line in a plugin's open details, or null without one. */
14181423 const links = ( name : string ) =>
14191424 within (
@@ -1427,10 +1432,13 @@ function pluginSectionsFixture(
14271432 onSection,
14281433 onPluginDetailsClosed,
14291434 onPluginImportClosed,
1435+ request,
1436+ settings,
14301437 view,
14311438 links,
14321439 linkNames,
14331440 positions,
1441+ stored,
14341442 } ;
14351443}
14361444type Visit = {
@@ -1509,7 +1517,7 @@ it("reopens the details a Manage plugins visit showed on Back or Forward, not on
15091517 pluginId : "buzz.github" ,
15101518 } ,
15111519 ] ) ;
1512- const { view, positions } = pluginSectionsFixture ( cards ) ;
1520+ const { view, positions, stored } = pluginSectionsFixture ( cards ) ;
15131521 const details = ( ) =>
15141522 screen . queryByRole ( "complementary" , { name : "GitHub details" } ) ;
15151523 const row = ( ) => screen . getByRole ( "button" , { name : "View GitHub details" } ) ;
@@ -1559,12 +1567,19 @@ it("reopens the details a Manage plugins visit showed on Back or Forward, not on
15591567 ) ;
15601568 rerender ( view ( "plugins" , undefined , { entryId : "p5" , traversal : true } ) ) ;
15611569 expect ( screen . queryByRole ( "complementary" ) ) . toBeNull ( ) ;
1562- expect ( positions . get ( JSON . stringify ( [ "p5" , "settings.pluginDetails" ] ) ) ) . toBe (
1563- null ,
1564- ) ;
1570+ expect ( stored ( "p5" ) ) . toBeNull ( ) ;
15651571} ) ;
15661572
1567- /** Manage plugins visits from p1, with the open details' plugin name or null. */
1573+ /** The open details' plugin name, or null. */
1574+ const shownDetails = ( ) =>
1575+ screen
1576+ . queryByRole ( "complementary" )
1577+ ?. getAttribute ( "aria-label" )
1578+ ?. replace ( / d e t a i l s $ / , "" ) ?? null ;
1579+ const toggleDetails = ( name : string ) =>
1580+ fireEvent . click ( screen . getByRole ( "button" , { name : `View ${ name } details` } ) ) ;
1581+
1582+ /** Manage plugins visits from p1. */
15681583function pluginVisits ( ) {
15691584 const fixture = pluginSectionsFixture ( registry ( [ ] ) . cards ) ;
15701585 const { rerender } = render (
@@ -1574,28 +1589,65 @@ function pluginVisits() {
15741589 ...fixture ,
15751590 visit : ( section : string , visit : Visit ) =>
15761591 rerender ( fixture . view ( section , undefined , visit ) ) ,
1577- toggle : ( name : string ) =>
1578- fireEvent . click (
1579- screen . getByRole ( "button" , {
1580- name : `View ${ name } details` ,
1581- hidden : true ,
1582- } ) ,
1583- ) ,
1584- shown : ( ) =>
1585- screen
1586- . queryByRole ( "complementary" , { hidden : true } )
1587- ?. getAttribute ( "aria-label" )
1588- ?. replace ( / d e t a i l s $ / , "" ) ?? null ,
1592+ toggle : toggleDetails ,
1593+ shown : shownDetails ,
1594+ } ;
1595+ }
1596+
1597+ /** Settings under a visit store, as App renders `route.request`, so a
1598+ * dismissal can land inside its callback or later, as history does. */
1599+ function pluginHost ( manager : object , first : Visit ) {
1600+ const fixture = pluginSectionsFixture ( registry ( [ ] ) . cards , undefined , manager ) ;
1601+ let current = fixture . request ( "plugins" , first ) ;
1602+ const listeners = new Set < ( ) => void > ( ) ;
1603+ const subscribe = ( listener : ( ) => void ) => {
1604+ listeners . add ( listener ) ;
1605+ return ( ) => listeners . delete ( listener ) ;
1606+ } ;
1607+ const land = ( section : string , visit : Visit ) => {
1608+ current = fixture . request ( section , visit ) ;
1609+ for ( const listener of listeners ) listener ( ) ;
1610+ } ;
1611+ function Host ( ) {
1612+ return fixture . settings ( useSyncExternalStore ( subscribe , ( ) => current ) ) ;
1613+ }
1614+ render ( < Host /> ) ;
1615+ return {
1616+ ...fixture ,
1617+ land,
1618+ visit : ( section : string , visit : Visit ) => act ( ( ) => land ( section , visit ) ) ,
1619+ entry : ( ) => ( current as { entryId : string } ) . entryId ,
1620+ toggle : toggleDetails ,
1621+ shown : shownDetails ,
15891622 } ;
15901623}
15911624const imported = { repository : "https://example.com/p.git" , reference : "main" } ;
1625+ /** A linked import of `id`; Codex is already installed. */
1626+ const linkedImport = ( id : string , install = Promise . resolve ( true ) ) => ( {
1627+ installImport : vi . fn ( ( ) => install ) ,
1628+ imports : {
1629+ git : vi . fn ( async ( ) => ( {
1630+ token : "linked" ,
1631+ source : imported . repository ,
1632+ commit : null ,
1633+ warnings : [ ] ,
1634+ candidates : [
1635+ {
1636+ path : "." ,
1637+ revision : "two" ,
1638+ manifest : { id, name : id , apiVersion : 1 } ,
1639+ } ,
1640+ ] ,
1641+ } ) ) ,
1642+ discard : vi . fn ( async ( ) => { } ) ,
1643+ } ,
1644+ } ) ;
15921645
15931646it ( "shows the list on Back to a visit that closed its details" , ( ) => {
15941647 const { visit, toggle, shown } = pluginVisits ( ) ;
15951648 toggle ( "Codex" ) ;
15961649 toggle ( "Codex" ) ;
1597- visit ( "plugins" , { entryId : "p2" , pluginImport : imported } ) ;
1598- toggle ( "GitHub" ) ;
1650+ visit ( "plugins" , { entryId : "p2" , pluginId : "buzz.github" } ) ;
15991651 expect ( shown ( ) ) . toBe ( "GitHub" ) ;
16001652 visit ( "plugins" , { entryId : "p1" , traversal : true } ) ;
16011653 expect ( shown ( ) ) . toBeNull ( ) ;
@@ -1613,13 +1665,20 @@ it("records the details an adjacent visit inherits", () => {
16131665} ) ;
16141666
16151667it ( "shows the list on Back to a visit that never opened details" , ( ) => {
1616- const { visit, toggle , shown } = pluginVisits ( ) ;
1617- visit ( "plugins" , { entryId : "p2" , pluginImport : imported } ) ;
1618- toggle ( "GitHub" ) ;
1668+ const { visit, shown } = pluginVisits ( ) ;
1669+ visit ( "plugins" , { entryId : "p2" , pluginId : "buzz.github" } ) ;
1670+ expect ( shown ( ) ) . toBe ( "GitHub" ) ;
16191671 visit ( "plugins" , { entryId : "p1" , traversal : true } ) ;
16201672 expect ( shown ( ) ) . toBeNull ( ) ;
16211673} ) ;
16221674
1675+ it ( "records nothing for a section Manage plugins is left for" , ( ) => {
1676+ const { visit, toggle, stored } = pluginVisits ( ) ;
1677+ toggle ( "GitHub" ) ;
1678+ visit ( "appearance" , { entryId : "s2" } ) ;
1679+ expect ( stored ( "s2" ) ) . toBeUndefined ( ) ;
1680+ } ) ;
1681+
16231682it ( "returns from addressed details to the details the earlier visit showed" , ( ) => {
16241683 const { visit, toggle, shown } = pluginVisits ( ) ;
16251684 toggle ( "GitHub" ) ;
@@ -1651,15 +1710,58 @@ it("lands on the list when Close dismisses addressed details", () => {
16511710 expect ( shown ( ) ) . toBeNull ( ) ;
16521711} ) ;
16531712
1654- it ( "keeps the plugin Open in Buzz shows when the import's dismissal lands" , ( ) => {
1655- const { visit, toggle, shown, onPluginImportClosed } = pluginVisits ( ) ;
1713+ it ( "keeps the plugin Open in Buzz shows when the import's dismissal lands later" , async ( ) => {
1714+ const { visit, land, entry, shown, stored, onPluginImportClosed } =
1715+ pluginHost ( linkedImport ( "buzz.codex" ) , { entryId : "p1" } ) ;
16561716 visit ( "plugins" , { entryId : "p2" , pluginImport : imported } ) ;
1657- fireEvent . keyDown ( screen . getByRole ( "dialog" ) , { key : "Escape" } ) ;
1658- expect ( onPluginImportClosed ) . toHaveBeenCalledOnce ( ) ;
1659- // Open in Buzz selects after closing; the browser lands on p1 later.
1660- toggle ( "GitHub" ) ;
1661- visit ( "plugins" , { entryId : "p1" , traversal : true } ) ;
1662- expect ( shown ( ) ) . toBe ( "GitHub" ) ;
1717+ // History returns to p1 after Open in Buzz has shown its plugin.
1718+ onPluginImportClosed . mockImplementation ( ( ) =>
1719+ setTimeout ( ( ) => land ( "plugins" , { entryId : "p1" , traversal : true } ) ) ,
1720+ ) ;
1721+ fireEvent . click ( await screen . findByRole ( "button" , { name : "Open in Buzz" } ) ) ;
1722+ await waitFor ( ( ) => expect ( entry ( ) ) . toBe ( "p1" ) ) ;
1723+ expect ( shown ( ) ) . toBe ( "Codex" ) ;
1724+ expect ( stored ( "p1" ) ) . toBe ( "buzz.codex" ) ;
1725+ } ) ;
1726+
1727+ it ( "records the plugin Open in Buzz shows on the visit replacing its import" , async ( ) => {
1728+ const { visit, land, shown, stored, onPluginImportClosed } = pluginHost (
1729+ linkedImport ( "buzz.codex" ) ,
1730+ { entryId : "i1" , pluginImport : imported } ,
1731+ ) ;
1732+ // Opened from a channel: the import is replaced while it closes.
1733+ onPluginImportClosed . mockImplementation ( ( ) =>
1734+ land ( "plugins" , { entryId : "p2" } ) ,
1735+ ) ;
1736+ fireEvent . click ( await screen . findByRole ( "button" , { name : "Open in Buzz" } ) ) ;
1737+ await waitFor ( ( ) => expect ( shown ( ) ) . toBe ( "Codex" ) ) ;
1738+ expect ( stored ( "p2" ) ) . toBe ( "buzz.codex" ) ;
1739+ visit ( "appearance" , { entryId : "s3" } ) ;
1740+ visit ( "plugins" , { entryId : "p2" , traversal : true } ) ;
1741+ expect ( shown ( ) ) . toBe ( "Codex" ) ;
1742+ } ) ;
1743+
1744+ it ( "keeps an earlier visit's details when an import finishes after Back" , async ( ) => {
1745+ let finish ! : ( installed : boolean ) => void ;
1746+ const installed = new Promise < boolean > ( ( resolve ) => {
1747+ finish = resolve ;
1748+ } ) ;
1749+ const { visit, toggle, shown, onPluginImportClosed } = pluginHost (
1750+ linkedImport ( "example.new" , installed ) ,
1751+ { entryId : "p0" } ,
1752+ ) ;
1753+ toggle ( "Codex" ) ;
1754+ visit ( "plugins" , { entryId : "p1" , pluginId : "buzz.github" } ) ;
1755+ visit ( "plugins" , { entryId : "p2" , pluginImport : imported } ) ;
1756+ fireEvent . click (
1757+ await screen . findByRole ( "button" , { name : "Install plugin" } ) ,
1758+ ) ;
1759+ visit ( "plugins" , { entryId : "p1" , pluginId : "buzz.github" , traversal : true } ) ;
1760+ // The finished install closes a visit that is no longer shown.
1761+ await act ( async ( ) => finish ( true ) ) ;
1762+ await waitFor ( ( ) => expect ( onPluginImportClosed ) . toHaveBeenCalledOnce ( ) ) ;
1763+ visit ( "plugins" , { entryId : "p0" , traversal : true } ) ;
1764+ expect ( shown ( ) ) . toBe ( "Codex" ) ;
16631765} ) ;
16641766
16651767it ( "links a decision provider's details to Decisions API" , async ( ) => {
0 commit comments