package Ġorg . phoenix framework Ċ Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġorg . mockito . kotlin . mock Ċ import Ġorg . mockito . kotlin . wh enever Ċ import Ġorg . junit . jupiter . api . BeforeEach Ċ import Ġorg . junit . jupiter . api . DisplayName Ċ import Ġorg . junit . jupiter . api . Nested Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . mockito . Mock Ċ import Ġorg . mockito . Mockito Annotations Ċ import Ġorg . phoenix framework . utilities . get Bindings Ċ Ċ class ĠPres ence Test Ġ{ ĊĊĠ Ġ@ Mock Ġlateinit Ġvar Ġsocket : ĠSocket ĊĊĠ Ġprivate Ġval Ġfix Join s : ĠPres ence State Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠ Ġ" u 1 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 . 2 ")) )) ĊĠ Ġprivate Ġval Ġfix Le aves : ĠPres ence State Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠ Ġ" u 2 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 2 , Ġ" ph x _ ref " Ġto Ġ" 2 ")) )) ĊĠ Ġprivate Ġval Ġfix State : ĠPres ence State Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠ Ġ" u 1 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 ")) ), ĊĠĠĠĠĠ Ġ" u 2 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 2 , Ġ" ph x _ ref " Ġto Ġ" 2 ")) ), ĊĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "))) ĊĠ Ġ) ĊĊĠ Ġprivate Ġval Ġlist By First : Ġ( Map . Entry < String , ĠPres ence Map >) Ġ-> ĠPres ence Meta Ġ= ĊĠĠĠĠĠ Ġ{ Ġit . value [" metas "] !!. first () Ġ} ĊĊĠ Ġlateinit Ġvar Ġchannel : ĠChannel ĊĠ Ġlateinit Ġvar Ġpresence : ĠPres ence ĊĊĠ Ġ@ BeforeEach ĊĠ Ġinternal Ġfun ĠsetUp () Ġ{ ĊĠĠĠ ĠMockito Annotations . init Mocks ( this ) ĊĊĠĠĠ Ġwhenever ( socket . timeout ). thenReturn ( Defaults . TIMEOUT ) ĊĠĠĠ Ġwhenever ( socket . make Ref ()). thenReturn (" 1 ") ĊĠĠĠ Ġwhenever ( socket . reconnect After Ms ). thenReturn Ġ{ Ġ 1 _ 0 0 0 Ġ} ĊĠĠĠ Ġwhenever ( socket . re join After Ms ). thenReturn ( Defaults . re join Ste pped Back Off ) ĊĠĠĠ Ġwhenever ( socket . dispatch Queue ). thenReturn ( mock ()) ĊĊĠĠĠ Ġchannel Ġ= ĠChannel (" topic ", Ġmap Of (), Ġsocket ) ĊĠĠĠ Ġchannel . join Push . ref Ġ= Ġ" 1 " ĊĊĠĠĠ Ġpresence Ġ= ĠPres ence ( channel ) ĊĠ Ġ} ĊĊĠ Ġ@ Nested ĊĠ Ġ@ DisplayName (" constructor ") ĊĠ Ġinner Ġclass ĠConstructor Ġ{ ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` sets Ġdefaults `() Ġ{ ĊĠĠĠĠĠ ĠassertThat ( presence . state ). isEmpty () ĊĠĠĠĠĠ ĠassertThat ( presence . pending D iffs ). isEmpty () ĊĠĠĠĠĠ ĠassertThat ( presence . channel ). isEqualTo ( channel ) ĊĠĠĠĠĠ ĠassertThat ( presence . join Ref ). isNull () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` bind s Ġto Ġchannel Ġwith Ġdefault Ġarguments `() Ġ{ ĊĠĠĠĠĠ ĠassertThat ( presence . channel . get Bindings (" presence _ state ")). has Size ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( presence . channel . get Bindings (" presence _ diff ")). has Size ( 1 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` bind s Ġto Ġchannel Ġwith Ġcustom Ġoptions `() Ġ{ ĊĠĠĠĠĠ Ġval Ġchannel Ġ= ĠChannel (" topic ", Ġmap Of (), Ġsocket ) ĊĠĠĠĠĠ Ġval Ġcustom Options Ġ= ĠPres ence . Options ( map Of ( ĊĠĠĠĠĠĠĠĠĠ ĠPres ence . Events . STATE Ġto Ġ" custom _ state ", ĊĠĠĠĠĠĠĠĠĠ ĠPres ence . Events . DIFF Ġto Ġ" custom _ diff ")) ĊĊĠĠĠĠĠ Ġval Ġp Ġ= ĠPres ence ( channel , Ġcustom Options ) ĊĠĠĠĠĠ ĠassertThat ( p . channel . get Bindings (" presence _ state ")). isEmpty () ĊĠĠĠĠĠ ĠassertThat ( p . channel . get Bindings (" presence _ diff ")). isEmpty () ĊĠĠĠĠĠ ĠassertThat ( p . channel . get Bindings (" custom _ state ")). has Size ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( p . channel . get Bindings (" custom _ diff ")). has Size ( 1 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` sync s Ġstate Ġand Ġdiffs `() Ġ{ ĊĠĠĠĠĠ Ġval Ġuser 1 : ĠPres ence Map Ġ= Ġmutable Map Of (" metas " Ġto Ġmutable ListOf ( ĊĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 "))) ĊĠĠĠĠĠ Ġval Ġuser 2 : ĠPres ence Map Ġ= Ġmutable Map Of (" metas " Ġto Ġmutable ListOf ( ĊĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 2 , Ġ" ph x _ ref " Ġto Ġ" 2 "))) ĊĠĠĠĠĠ Ġval ĠnewState : ĠPres ence State Ġ= Ġmutable Map Of (" u 1 " Ġto Ġuser 1 , Ġ" u 2 " Ġto Ġuser 2 ) ĊĊĠĠĠĠĠ Ġchannel . trigger (" presence _ state ", ĠnewState , Ġ" 1 ") ĊĠĠĠĠĠ Ġval Ġs Ġ= Ġpresence . list By ( list By First ) ĊĠĠĠĠĠ ĠassertThat ( s ). has Size ( 2 ) ĊĠĠĠĠĠ ĠassertThat ( s [ 0 ][" id "]). isEqualTo ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( s [ 0 ][" ph x _ ref "]). isEqualTo (" 1 ") ĊĊĠĠĠĠĠ ĠassertThat ( s [ 1 ][" id "]). isEqualTo ( 2 ) ĊĠĠĠĠĠ ĠassertThat ( s [ 1 ][" ph x _ ref "]). isEqualTo (" 2 ") ĊĊĠĠĠĠĠ Ġchannel . trigger (" presence _ diff ", ĊĠĠĠĠĠĠĠĠĠ Ġmap Of (" join s " Ġto Ġempty Map (), Ġ" leaves " Ġto Ġmap Of (" u 1 " Ġto Ġuser 1 ))) ĊĠĠĠĠĠ Ġval Ġl Ġ= Ġpresence . list By ( list By First ) ĊĠĠĠĠĠ ĠassertThat ( l ). has Size ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( l [ 0 ][" id "]). isEqualTo ( 2 ) ĊĠĠĠĠĠ ĠassertThat ( l [ 0 ][" ph x _ ref "]). isEqualTo (" 2 ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` app lies Ġpending Ġdiff Ġif Ġstate Ġis Ġnot Ġyet Ġsynced `() Ġ{ ĊĠĠĠĠĠ Ġval Ġon Join s Ġ= Ġmutable ListOf < Triple < String , ĠPres ence Map ?, ĠPres ence Map >>() ĊĠĠĠĠĠ Ġval Ġon Le aves Ġ= Ġmutable ListOf < Triple < String , ĠPres ence Map , ĠPres ence Map >>() ĊĊĠĠĠĠĠ Ġpresence . on Join Ġ{ Ġkey , Ġcurrent , Ġnew Ġ-> Ġon Join s . add ( Triple ( key , Ġcurrent , Ġnew )) Ġ} ĊĠĠĠĠĠ Ġpresence . on Leave Ġ{ Ġkey , Ġcurrent , Ġleft Ġ-> Ġon Le aves . add ( Triple ( key , Ġcurrent , Ġleft )) Ġ} ĊĊĠĠĠĠĠ Ġval Ġuser 1 Ġ= Ġmutable Map Of (" metas " Ġto Ġmutable ListOf ( mutable Map Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 "))) ĊĠĠĠĠĠ Ġval Ġuser 2 Ġ= Ġmutable Map Of (" metas " Ġto Ġmutable ListOf ( mutable Map Of (" id " Ġto Ġ 2 , Ġ" ph x _ ref " Ġto Ġ" 2 "))) ĊĠĠĠĠĠ Ġval Ġuser 3 Ġ= Ġmutable Map Of (" metas " Ġto Ġmutable ListOf ( mutable Map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "))) ĊĊĠĠĠĠĠ Ġval ĠnewState Ġ= Ġmutable Map Of (" u 1 " Ġto Ġuser 1 , Ġ" u 2 " Ġto Ġuser 2 ) ĊĠĠĠĠĠ Ġval Ġleaves Ġ= Ġmap Of (" u 2 " Ġto Ġuser 2 ) ĊĊĠĠĠĠĠ Ġval Ġpayload 1 Ġ= Ġmap Of (" join s " Ġto Ġempty Map (), Ġ" leaves " Ġto Ġleaves ) ĊĠĠĠĠĠ Ġchannel . trigger (" presence _ diff ", Ġpayload 1 , Ġ"") ĊĊĠĠĠĠĠ Ġ// ĠThere Ġis Ġno Ġstate ĊĠĠĠĠĠ ĠassertThat ( presence . list By ( list By First )). isEmpty () ĊĊĠĠĠĠĠ Ġ// Ġpending Ġdiffs Ġ 1 ĊĠĠĠĠĠ ĠassertThat ( presence . pending D iffs ). has Size ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( presence . pending D iffs [ 0 ][" join s "]). isEmpty () ĊĠĠĠĠĠ ĠassertThat ( presence . pending D iffs [ 0 ][" leaves "]). isEqualTo ( leaves ) ĊĊĠĠĠĠĠ Ġchannel . trigger (" presence _ state ", ĠnewState , Ġ"") ĊĠĠĠĠĠ ĠassertThat ( on Le aves ). has Size ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( on Le aves [ 0 ]. first ). isEqualTo (" u 2 ") ĊĠĠĠĠĠ ĠassertThat ( on Le aves [ 0 ]. second [" metas "]). isEmpty () ĊĠĠĠĠĠ ĠassertThat ( on Le aves [ 0 ]. third [" metas "] ! ![ 0 ][" id "]). isEqualTo ( 2 ) ĊĊĠĠĠĠĠ Ġval Ġs Ġ= Ġpresence . list By ( list By First ) ĊĠĠĠĠĠ ĠassertThat ( s ). has Size ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( s [ 0 ][" id "]). isEqualTo ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( s [ 0 ][" ph x _ ref "]). isEqualTo (" 1 ") ĊĠĠĠĠĠ ĠassertThat ( presence . pending D iffs ). isEmpty () ĊĊĠĠĠĠĠ ĠassertThat ( on Join s ). has Size ( 2 ) ĊĠĠĠĠĠ ĠassertThat ( on Join s [ 0 ]. first ). isEqualTo (" u 1 ") ĊĠĠĠĠĠ ĠassertThat ( on Join s [ 0 ]. second ). isNull () ĊĠĠĠĠĠ ĠassertThat ( on Join s [ 0 ]. third [" metas "] ! ![ 0 ][" id "]). isEqualTo ( 1 ) ĊĊĠĠĠĠĠ ĠassertThat ( on Join s [ 1 ]. first ). isEqualTo (" u 2 ") ĊĠĠĠĠĠ ĠassertThat ( on Join s [ 1 ]. second ). isNull () ĊĠĠĠĠĠ ĠassertThat ( on Join s [ 1 ]. third [" metas "] ! ![ 0 ][" id "]). isEqualTo ( 2 ) ĊĊĠĠĠĠĠ Ġ// Ġdisconnect Ġthen Ġreconnect ĊĠĠĠĠĠ ĠassertThat ( presence . is Pending Sync State ). isFalse () ĊĠĠĠĠĠ Ġchannel . join Push . ref Ġ= Ġ" 2 " ĊĠĠĠĠĠ ĠassertThat ( presence . is Pending Sync State ). isTrue () ĊĊĊĠĠĠĠĠ Ġchannel . trigger (" presence _ diff ", ĊĠĠĠĠĠĠĠĠĠ Ġmap Of (" join s " Ġto Ġmap Of (), Ġ" leaves " Ġto Ġmap Of (" u 1 " Ġto Ġuser 1 ))) ĊĠĠĠĠĠ Ġval Ġd Ġ= Ġpresence . list By ( list By First ) ĊĠĠĠĠĠ ĠassertThat ( d ). has Size ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( d [ 0 ][" id "]). isEqualTo ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( d [ 0 ][" ph x _ ref "]). isEqualTo (" 1 ") ĊĊĊĠĠĠĠĠ Ġchannel . trigger (" presence _ state ", ĊĠĠĠĠĠĠĠĠĠ Ġmap Of (" u 1 " Ġto Ġuser 1 , Ġ" u 3 " Ġto Ġuser 3 )) ĊĠĠĠĠĠ Ġval Ġs 2 Ġ= Ġpresence . list By ( list By First ) ĊĠĠĠĠĠ ĠassertThat ( s 2 ). has Size ( 1 ) ĊĠĠĠĠĠ ĠassertThat ( s 2 [ 0 ][" id "]). isEqualTo ( 3 ) ĊĠĠĠĠĠ ĠassertThat ( s 2 [ 0 ][" ph x _ ref "]). isEqualTo (" 3 ") ĊĠĠĠ Ġ} ĊĠĠĠ Ġ/* ĠEnd ĠConstructor Ġ*/ ĊĠ Ġ} ĊĊĠ Ġ@ Nested ĊĠ Ġ@ DisplayName (" sync State ") ĊĠ Ġinner Ġclass ĠSync State Ġ{ ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` sync s Ġempty Ġstate `() Ġ{ ĊĠĠĠĠĠ Ġval ĠnewState : ĠPres ence State Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 1 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 ")) )) ĊĠĠĠĠĠ Ġvar Ġstate : ĠPres ence State Ġ= Ġmutable Map Of () ĊĠĠĠĠĠ Ġval Ġstate Before Ġ= Ġstate ĊĊĠĠĠĠĠ ĠPres ence . sync State ( state , ĠnewState ) ĊĠĠĠĠĠ ĠassertThat ( state ). isEqualTo ( state Before ) ĊĊĠĠĠĠĠ Ġstate Ġ= ĠPres ence . sync State ( state , ĠnewState ) ĊĠĠĠĠĠ ĠassertThat ( state ). isEqualTo ( newState ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` on Join s Ġnew Ġpres ences Ġand Ġon Le aves Ġleft Ġpres ences `() Ġ{ ĊĠĠĠĠĠ Ġval ĠnewState Ġ= Ġfix State ĊĠĠĠĠĠ Ġvar Ġstate Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 4 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 4 , Ġ" ph x _ ref " Ġto Ġ" 4 ")) )) ĊĊĠĠĠĠĠ Ġval Ġjoined : ĠPres ence Diff Ġ= Ġmutable Map Of () ĊĠĠĠĠĠ Ġval Ġleft : ĠPres ence Diff Ġ= Ġmutable Map Of () ĊĊĠĠĠĠĠ Ġval Ġon Join : ĠOn Join Ġ= Ġ{ Ġkey , Ġcurrent , Ġnew Pres Ġ-> ĊĠĠĠĠĠĠĠ Ġval Ġjoin State : ĠPres ence State Ġ= Ġmutable Map Of (" new Pres " Ġto Ġnew Pres ) ĊĠĠĠĠĠĠĠ Ġcurrent ?. let Ġ{ Ġc Ġ-> Ġjoin State [" current "] Ġ= Ġc Ġ} ĊĊĠĠĠĠĠĠĠ Ġjoined [ key ] Ġ= Ġjoin State ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġval Ġon Leave : ĠOn Leave Ġ= Ġ{ Ġkey , Ġcurrent , Ġleft Pres Ġ-> ĊĠĠĠĠĠĠĠ Ġleft [ key ] Ġ= Ġmutable Map Of (" current " Ġto Ġcurrent , Ġ" left Pres " Ġto Ġleft Pres ) ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġval Ġstate Before Ġ= Ġstate ĊĠĠĠĠĠ ĠPres ence . sync State ( state , ĠnewState , Ġon Join , Ġon Leave ) ĊĠĠĠĠĠ ĠassertThat ( state ). isEqualTo ( state Before ) ĊĊĠĠĠĠĠ Ġstate Ġ= ĠPres ence . sync State ( state , ĠnewState , Ġon Join , Ġon Leave ) ĊĠĠĠĠĠ ĠassertThat ( state ). isEqualTo ( newState ) ĊĊĠĠĠĠĠ Ġ// Ġasset Ġequality Ġin Ġjoined ĊĠĠĠĠĠ Ġval Ġjoined Expectation : ĠPres ence Diff Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 1 " Ġto Ġmutable Map Of (" new Pres " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 ")) )), ĊĠĠĠĠĠĠĠĠĠ Ġ" u 2 " Ġto Ġmutable Map Of (" new Pres " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 2 , Ġ" ph x _ ref " Ġto Ġ" 2 ")) )), ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of (" new Pres " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 ")) )) ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠ ĠassertThat ( joined ). isEqualTo ( joined Expectation ) ĊĊĠĠĠĠĠ Ġ// Ġassert Ġequality Ġin Ġleft ĊĠĠĠĠĠ Ġval Ġleft Expectation : ĠPres ence Diff Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 4 " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" current " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" metas " Ġto Ġmutable ListOf ()), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" left Pres " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 4 , Ġ" ph x _ ref " Ġto Ġ" 4 ")) )) ĊĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ ĠassertThat ( left ). isEqualTo ( left Expectation ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` on Join s Ġonly Ġnewly Ġadded Ġmet as `() Ġ{ ĊĠĠĠĠĠ Ġvar Ġstate Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 ")) )) ĊĠĠĠĠĠ Ġval ĠnewState Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 . new ") ĊĠĠĠĠĠĠĠĠĠ Ġ))) ĊĊĠĠĠĠĠ Ġval Ġjoined : ĠPres ence Diff Ġ= Ġmutable Map Of () ĊĠĠĠĠĠ Ġval Ġleft : ĠPres ence Diff Ġ= Ġmutable Map Of () ĊĊĠĠĠĠĠ Ġval Ġon Join : ĠOn Join Ġ= Ġ{ Ġkey , Ġcurrent , Ġnew Pres Ġ-> ĊĠĠĠĠĠĠĠ Ġval Ġjoin State : ĠPres ence State Ġ= Ġmutable Map Of (" new Pres " Ġto Ġnew Pres ) ĊĠĠĠĠĠĠĠ Ġcurrent ?. let Ġ{ Ġc Ġ-> Ġjoin State [" current "] Ġ= Ġc Ġ} ĊĊĠĠĠĠĠĠĠ Ġjoined [ key ] Ġ= Ġjoin State ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġval Ġon Leave : ĠOn Leave Ġ= Ġ{ Ġkey , Ġcurrent , Ġleft Pres Ġ-> ĊĠĠĠĠĠĠĠ Ġleft [ key ] Ġ= Ġmutable Map Of (" current " Ġto Ġcurrent , Ġ" left Pres " Ġto Ġleft Pres ) ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġstate Ġ= ĠPres ence . sync State ( state , ĠnewState , Ġon Join , Ġon Leave ) ĊĠĠĠĠĠ ĠassertThat ( state ). isEqualTo ( newState ) ĊĊĠĠĠĠĠ Ġ// Ġasset Ġequality Ġin Ġjoined ĊĠĠĠĠĠ Ġval Ġjoined Expectation : ĠPres ence Diff Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" new Pres " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 . new ")) ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" current " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "))) ĊĊĠĠĠĠĠĠĠĠĠ Ġ)) ĊĠĠĠĠĠ ĠassertThat ( joined ). isEqualTo ( joined Expectation ) ĊĊĠĠĠĠĠ Ġ// Ġassert Ġequality Ġin Ġleft ĊĠĠĠĠĠ ĠassertThat ( left ). isEmpty () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` on Le aves Ġonly Ġnewly Ġremoved Ġmet as `() Ġ{ ĊĠĠĠĠĠ Ġval ĠnewState Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 ")) )) ĊĠĠĠĠĠ Ġvar Ġstate Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 . left ") ĊĠĠĠĠĠĠĠĠĠ Ġ))) ĊĊĠĠĠĠĠ Ġval Ġjoined : ĠPres ence Diff Ġ= Ġmutable Map Of () ĊĠĠĠĠĠ Ġval Ġleft : ĠPres ence Diff Ġ= Ġmutable Map Of () ĊĊĠĠĠĠĠ Ġval Ġon Join : ĠOn Join Ġ= Ġ{ Ġkey , Ġcurrent , Ġnew Pres Ġ-> ĊĠĠĠĠĠĠĠ Ġval Ġjoin State : ĠPres ence State Ġ= Ġmutable Map Of (" new Pres " Ġto Ġnew Pres ) ĊĠĠĠĠĠĠĠ Ġcurrent ?. let Ġ{ Ġc Ġ-> Ġjoin State [" current "] Ġ= Ġc Ġ} ĊĊĠĠĠĠĠĠĠ Ġjoined [ key ] Ġ= Ġjoin State ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġval Ġon Leave : ĠOn Leave Ġ= Ġ{ Ġkey , Ġcurrent , Ġleft Pres Ġ-> ĊĠĠĠĠĠĠĠ Ġleft [ key ] Ġ= Ġmutable Map Of (" current " Ġto Ġcurrent , Ġ" left Pres " Ġto Ġleft Pres ) ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġstate Ġ= ĠPres ence . sync State ( state , ĠnewState , Ġon Join , Ġon Leave ) ĊĠĠĠĠĠ ĠassertThat ( state ). isEqualTo ( newState ) ĊĊĠĠĠĠĠ Ġ// Ġasset Ġequality Ġin Ġjoined ĊĠĠĠĠĠ Ġval Ġleft Expectation : ĠPres ence Diff Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" left Pres " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 . left ")) ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" current " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "))) ĊĊĠĠĠĠĠĠĠĠĠ Ġ)) ĊĠĠĠĠĠ ĠassertThat ( left ). isEqualTo ( left Expectation ) ĊĊĠĠĠĠĠ Ġ// Ġassert Ġequality Ġin Ġleft ĊĠĠĠĠĠ ĠassertThat ( joined ). isEmpty () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` sync s Ġboth Ġjoined Ġand Ġleft Ġmet as `() Ġ{ ĊĠĠĠĠĠ Ġval ĠnewState Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 . new ") ĊĠĠĠĠĠĠĠĠĠ Ġ))) ĊĊĠĠĠĠĠ Ġvar Ġstate Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 . left ") ĊĠĠĠĠĠĠĠĠĠ Ġ))) ĊĊĠĠĠĠĠ Ġval Ġjoined : ĠPres ence Diff Ġ= Ġmutable Map Of () ĊĠĠĠĠĠ Ġval Ġleft : ĠPres ence Diff Ġ= Ġmutable Map Of () ĊĊĠĠĠĠĠ Ġval Ġon Join : ĠOn Join Ġ= Ġ{ Ġkey , Ġcurrent , Ġnew Pres Ġ-> ĊĠĠĠĠĠĠĠ Ġval Ġjoin State : ĠPres ence State Ġ= Ġmutable Map Of (" new Pres " Ġto Ġnew Pres ) ĊĠĠĠĠĠĠĠ Ġcurrent ?. let Ġ{ Ġc Ġ-> Ġjoin State [" current "] Ġ= Ġc Ġ} ĊĊĠĠĠĠĠĠĠ Ġjoined [ key ] Ġ= Ġjoin State ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġval Ġon Leave : ĠOn Leave Ġ= Ġ{ Ġkey , Ġcurrent , Ġleft Pres Ġ-> ĊĠĠĠĠĠĠĠ Ġleft [ key ] Ġ= Ġmutable Map Of (" current " Ġto Ġcurrent , Ġ" left Pres " Ġto Ġleft Pres ) ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġstate Ġ= ĠPres ence . sync State ( state , ĠnewState , Ġon Join , Ġon Leave ) ĊĠĠĠĠĠ ĠassertThat ( state ). isEqualTo ( newState ) ĊĊĠĠĠĠĠ Ġ// Ġasset Ġequality Ġin Ġjoined ĊĠĠĠĠĠ Ġval Ġjoined Expectation : ĠPres ence Diff Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" new Pres " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 . new ")) ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" current " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 . left "))) ĊĠĠĠĠĠĠĠĠĠ Ġ)) ĊĠĠĠĠĠ ĠassertThat ( joined ). isEqualTo ( joined Expectation ) ĊĊĠĠĠĠĠ Ġ// Ġassert Ġequality Ġin Ġleft ĊĠĠĠĠĠ Ġval Ġleft Expectation : ĠPres ence Diff Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" left Pres " Ġto Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 . left ")) ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" current " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 . new "))) ĊĠĠĠĠĠĠĠĠĠ Ġ)) ĊĠĠĠĠĠ ĠassertThat ( left ). isEqualTo ( left Expectation ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ/* Ġend ĠSync State Ġ*/ ĊĠ Ġ} ĊĊĠ Ġ@ Nested ĊĠ Ġ@ DisplayName (" sync Diff ") ĊĠ Ġinner Ġclass ĠSync Diff Ġ{ ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` sync s Ġempty Ġstate `() Ġ{ ĊĠĠĠĠĠ Ġval Ġjoins : ĠPres ence State Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 1 " Ġto Ġmutable Map Of (" metas " Ġto ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( map Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 ")) )) ĊĠĠĠĠĠ Ġvar Ġstate : ĠPres ence State Ġ= Ġmutable Map Of () ĊĊĠĠĠĠĠ ĠPres ence . sync Diff ( state , Ġmutable Map Of (" join s " Ġto Ġjoins , Ġ" leaves " Ġto Ġmutable Map Of ())) ĊĠĠĠĠĠ ĠassertThat ( state ). isEmpty () ĊĊĠĠĠĠĠ Ġstate Ġ= ĠPres ence . sync Diff ( state , Ġmutable Map Of (" join s " Ġto Ġjoins , Ġ" leaves " Ġto Ġmutable Map Of ())) ĊĠĠĠĠĠ ĠassertThat ( state ). isEqualTo ( join s ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` re moves Ġpresence Ġwhen Ġmeta Ġis Ġempty Ġand Ġadds Ġadditional Ġmeta `() Ġ{ ĊĠĠĠĠĠ Ġvar Ġstate Ġ= Ġfix State ĊĠĠĠĠĠ Ġval Ġdiff : ĠPres ence Diff Ġ= Ġmutable Map Of (" join s " Ġto Ġfix Join s , Ġ" leaves " Ġto Ġfix Le aves ) ĊĠĠĠĠĠ Ġstate Ġ= ĠPres ence . sync Diff ( state , Ġdiff ) ĊĊĠĠĠĠĠ Ġval Ġexpectation : ĠPres ence State Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 1 " Ġto Ġmutable Map Of (" metas " Ġto ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 . 2 ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠĠĠ Ġ" u 3 " Ġto Ġmutable Map Of (" metas " Ġto ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 ")) ĊĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠ ĠassertThat ( state ). isEqualTo ( expect ation ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` re moves Ġmeta Ġwhile Ġleaving Ġkey Ġif Ġother Ġmet as Ġexist `() Ġ{ ĊĠĠĠĠĠ Ġvar Ġstate Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 1 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 . 2 ") ĊĠĠĠĠĠĠĠĠĠ Ġ))) ĊĊĠĠĠĠĠ Ġval Ġleaves Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 1 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 ") ĊĠĠĠĠĠĠĠĠĠ Ġ))) ĊĠĠĠĠĠ Ġval Ġdiff : ĠPres ence Diff Ġ= Ġmutable Map Of (" join s " Ġto Ġmutable Map Of (), Ġ" leaves " Ġto Ġleaves ) ĊĠĠĠĠĠ Ġstate Ġ= ĠPres ence . sync Diff ( state , Ġdiff ) ĊĊĠĠĠĠĠ Ġval Ġexpected State Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 1 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 . 2 ")) )) ĊĠĠĠĠĠ ĠassertThat ( state ). isEqualTo ( expected State ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ/* ĠEnd ĠSync Diff Ġ*/ ĊĠ Ġ} ĊĊĠ Ġ@ Nested ĊĠ Ġ@ DisplayName (" list By ") ĊĠ Ġinner Ġclass ĠList By Ġ{ ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` lists Ġfull Ġpresence Ġby Ġdefault `() Ġ{ ĊĠĠĠĠĠ Ġpresence . state Ġ= Ġfix State ĊĊĠĠĠĠĠ Ġval Ġlist Expectation Ġ= ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠ Ġmap Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 ")) ), ĊĠĠĠĠĠĠĠĠĠ Ġmap Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 2 , Ġ" ph x _ ref " Ġto Ġ" 2 ")) ), ĊĠĠĠĠĠĠĠĠĠ Ġmap Of (" metas " Ġto ĠlistOf ( map Of (" id " Ġto Ġ 3 , Ġ" ph x _ ref " Ġto Ġ" 3 "))) ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠ ĠassertThat ( presence . list ()). isEqualTo ( list Expectation ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġinternal Ġfun Ġ` lists Ġwith Ġcustom Ġfunction `() Ġ{ ĊĠĠĠĠĠ Ġval Ġstate : ĠPres ence State Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" u 1 " Ġto Ġmutable Map Of (" metas " Ġto ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 . first "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 . second ")) ĊĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠ Ġpresence . state Ġ= Ġstate ĊĠĠĠĠĠ Ġval Ġlist By Ġ= Ġpresence . list By Ġ{ Ġit . value [" metas "] !!. first () Ġ} ĊĠĠĠĠĠ ĠassertThat ( list By ). isEqualTo ( listOf ( map Of (" id " Ġto Ġ 1 , Ġ" ph x _ ref " Ġto Ġ" 1 . first "))) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ Ċ }