@@ -4912,80 +4912,102 @@ describe("gateway healthHandlers.health cache freshness", () => {
49124912 expect ( payload ?. configReload ?. hotReloadStatus ) . toBe ( "disabled" ) ;
49134913 } ) ;
49144914
4915- it ( "refreshes cached health when a runtime account is missing from the cached account summary" , async ( ) => {
4916- const cached = createSingleChannelHealthSnapshot ( {
4917- channelId : "discord" ,
4918- label : "Discord" ,
4919- running : true ,
4920- connected : true ,
4921- } ) ;
4922- const fresh = {
4923- ...cached ,
4924- ts : cached . ts + 1 ,
4925- channels : {
4926- discord : {
4927- ...cached . channels . discord ,
4928- accounts : {
4929- ...cached . channels . discord . accounts ,
4930- work : channelHealthAccount ( { accountId : "work" , running : true , connected : true } ) ,
4931- } ,
4932- } ,
4933- } ,
4934- } ;
4935- const { respond, refreshHealthSnapshot } = await requestHealthSnapshot ( {
4936- cached,
4937- fresh,
4938- runtimeSnapshot : {
4939- channels : { } ,
4940- channelAccounts : {
4941- discord : { work : { accountId : "work" , running : true , connected : true } } ,
4942- } ,
4943- } ,
4944- } ) ;
4945-
4946- expect ( refreshHealthSnapshot ) . toHaveBeenCalledWith ( {
4947- probe : false ,
4948- includeSensitive : false ,
4949- } ) ;
4950- expect ( respond ) . toHaveBeenCalledWith ( true , fresh , undefined ) ;
4951- } ) ;
4915+ it . each ( [
4916+ {
4917+ change : "adds a running account" ,
4918+ previousAccountIds : [ "default" ] ,
4919+ nextAccountIds : [ "default" , "work" ] ,
4920+ } ,
4921+ {
4922+ change : "adds an uninitialized account" ,
4923+ previousAccountIds : [ "default" ] ,
4924+ nextAccountIds : [ "default" , "work" ] ,
4925+ uninitializedAccountId : "work" ,
4926+ } ,
4927+ {
4928+ change : "removes a runtime account" ,
4929+ previousAccountIds : [ "default" , "work" ] ,
4930+ nextAccountIds : [ "default" ] ,
4931+ } ,
4932+ {
4933+ change : "removes an entire channel plugin" ,
4934+ previousAccountIds : [ "default" ] ,
4935+ nextAccountIds : [ ] ,
4936+ } ,
4937+ {
4938+ change : "re-adds an uninitialized channel plugin" ,
4939+ previousAccountIds : [ ] ,
4940+ nextAccountIds : [ "default" ] ,
4941+ uninitializedAccountId : "default" ,
4942+ } ,
4943+ ] ) (
4944+ "refreshes cached health after hot reload $change" ,
4945+ async ( { previousAccountIds, nextAccountIds, uninitializedAccountId } ) => {
4946+ const current = createSingleChannelHealthSnapshot ( {
4947+ channelId : "discord" ,
4948+ label : "Discord" ,
4949+ running : true ,
4950+ connected : true ,
4951+ } ) ;
4952+ const account = ( accountId : string ) =>
4953+ channelHealthAccount ( { accountId, running : true , connected : true } ) ;
4954+ const summary = ( accountIds : string [ ] ) =>
4955+ accountIds . length === 0
4956+ ? createHealthSnapshot ( { } )
4957+ : {
4958+ ...current ,
4959+ channels : {
4960+ discord : {
4961+ ...current . channels . discord ,
4962+ accounts : Object . fromEntries ( accountIds . map ( ( id ) => [ id , account ( id ) ] ) ) ,
4963+ } ,
4964+ } ,
4965+ } ;
4966+ const runtime = ( accountIds : string [ ] , uninitialized ?: string ) => ( {
4967+ channels :
4968+ previousAccountIds . length === 0 && accountIds . length > 0
4969+ ? { discord : { accountId : accountIds [ 0 ] } }
4970+ : { } ,
4971+ channelAccounts :
4972+ accountIds . length === 0
4973+ ? { }
4974+ : {
4975+ discord : Object . fromEntries (
4976+ accountIds . map ( ( id ) => [
4977+ id ,
4978+ id === uninitialized ? { accountId : id } : account ( id ) ,
4979+ ] ) ,
4980+ ) ,
4981+ } ,
4982+ } ) ;
4983+ const cached = summary ( previousAccountIds ) ;
4984+ const fresh = summary ( nextAccountIds ) ;
4985+ const refreshHealthSnapshot = vi
4986+ . fn ( )
4987+ . mockResolvedValueOnce ( cached )
4988+ . mockResolvedValueOnce ( fresh ) ;
4989+
4990+ await requestHealthSnapshot ( {
4991+ cached,
4992+ refreshHealthSnapshot,
4993+ runtimeSnapshot : runtime ( previousAccountIds ) ,
4994+ } ) ;
4995+ expect ( refreshHealthSnapshot ) . toHaveBeenCalledOnce ( ) ;
49524996
4953- it ( "refreshes cached health after hot reload removes a runtime account" , async ( ) => {
4954- const current = createSingleChannelHealthSnapshot ( {
4955- channelId : "discord" ,
4956- label : "Discord" ,
4957- running : true ,
4958- connected : true ,
4959- } ) ;
4960- const cached = {
4961- ...current ,
4962- channels : {
4963- discord : {
4964- ...current . channels . discord ,
4965- accounts : {
4966- ...current . channels . discord . accounts ,
4967- work : channelHealthAccount ( { accountId : "work" , running : true , connected : true } ) ,
4968- } ,
4969- } ,
4970- } ,
4971- } ;
4972- const { respond, refreshHealthSnapshot } = await requestHealthSnapshot ( {
4973- cached,
4974- fresh : current ,
4975- runtimeSnapshot : {
4976- channels : { } ,
4977- channelAccounts : {
4978- discord : { default : { accountId : "default" , running : true , connected : true } } ,
4979- } ,
4980- } ,
4981- } ) ;
4997+ const { respond } = await requestHealthSnapshot ( {
4998+ cached,
4999+ refreshHealthSnapshot,
5000+ runtimeSnapshot : runtime ( nextAccountIds , uninitializedAccountId ) ,
5001+ } ) ;
49825002
4983- expect ( refreshHealthSnapshot ) . toHaveBeenCalledWith ( {
4984- probe : false ,
4985- includeSensitive : false ,
4986- } ) ;
4987- expect ( respond ) . toHaveBeenCalledWith ( true , current , undefined ) ;
4988- } ) ;
5003+ expect ( refreshHealthSnapshot ) . toHaveBeenCalledTimes ( 2 ) ;
5004+ expect ( refreshHealthSnapshot ) . toHaveBeenLastCalledWith ( {
5005+ probe : false ,
5006+ includeSensitive : false ,
5007+ } ) ;
5008+ expect ( respond ) . toHaveBeenCalledWith ( true , fresh , undefined ) ;
5009+ } ,
5010+ ) ;
49895011} ) ;
49905012
49915013describe ( "logs.tail" , ( ) => {
0 commit comments