@@ -34,6 +34,7 @@ import * as EnvironmentSupervisor from "../connection/supervisor.ts";
3434import type { WsRpcProtocolClient } from "../rpc/protocol.ts" ;
3535import type { RpcSession } from "../rpc/session.ts" ;
3636import {
37+ createLinkedPullRequestSummaryAtomFamily ,
3738 createPullRequestEnvironmentAtoms ,
3839 createPullRequestStackAtomFamily ,
3940} from "./pullRequests.ts" ;
@@ -808,6 +809,108 @@ it.effect("keeps concurrent diff file reads on different hosts separate", () =>
808809 ) ,
809810) ;
810811
812+ it . effect ( "shares close, reopen, and merge with an untouched client's mounted PR readers" , ( ) =>
813+ Effect . scoped (
814+ Effect . gen ( function * ( ) {
815+ const revision = yield * SubscriptionRef . make ( 0 ) ;
816+ let state : "open" | "closed" | "merged" = "open" ;
817+ const client = {
818+ [ WS_METHODS . pullRequestsSubscribeRefreshes ] : ( ) => SubscriptionRef . changes ( revision ) ,
819+ [ WS_METHODS . pullRequestsSummary ] : ( ) => Effect . sync ( ( ) => ( { state } ) ) ,
820+ [ WS_METHODS . pullRequestsDetail ] : ( ) => Effect . sync ( ( ) => ( { state } ) ) ,
821+ [ WS_METHODS . pullRequestsList ] : ( ) =>
822+ Effect . sync ( ( ) => ( { entries : [ { number : 1 , state } ] } ) ) ,
823+ [ WS_METHODS . pullRequestsRunAction ] : ( input : {
824+ readonly action : "close" | "reopen" | "merge" ;
825+ } ) =>
826+ Effect . gen ( function * ( ) {
827+ state =
828+ input . action === "close" ? "closed" : input . action === "merge" ? "merged" : "open" ;
829+ yield * SubscriptionRef . update ( revision , ( value ) => value + 1 ) ;
830+ } ) ,
831+ } as unknown as WsRpcProtocolClient ;
832+ const writer = yield * makeTestRuntime ( client ) ;
833+ const reader = yield * makeTestRuntime ( client ) ;
834+ const target = {
835+ environmentId : TARGET . environmentId ,
836+ input : {
837+ projectId : ProjectId . make ( "project-1" ) ,
838+ host : "github.example.com" ,
839+ repository : "acme/web" ,
840+ number : 1 ,
841+ } ,
842+ } ;
843+ const detail = reader . atoms . detail ( target ) ;
844+ const summary = createLinkedPullRequestSummaryAtomFamily (
845+ reader . runtime ,
846+ reader . atoms . refreshes ,
847+ ) ( target ) ;
848+ const list = reader . atoms . list ( {
849+ environmentId : TARGET . environmentId ,
850+ input : { state : "all" } ,
851+ } ) ;
852+ const unmountDetail = reader . registry . mount ( detail ) ;
853+ const unmountSummary = reader . registry . mount ( summary ) ;
854+ const unmountList = reader . registry . mount ( list ) ;
855+ yield * Effect . addFinalizer ( ( ) =>
856+ Effect . sync ( ( ) => {
857+ unmountDetail ( ) ;
858+ unmountSummary ( ) ;
859+ unmountList ( ) ;
860+ } ) ,
861+ ) ;
862+ expect ( ( yield * AtomRegistry . getResult ( reader . registry , detail ) ) . state ) . toBe ( "open" ) ;
863+ expect ( ( yield * AtomRegistry . getResult ( reader . registry , summary ) ) . state ) . toBe ( "open" ) ;
864+ expect ( ( yield * AtomRegistry . getResult ( reader . registry , list ) ) . entries [ 0 ] ?. state ) . toBe ( "open" ) ;
865+
866+ for ( const [ action , expected ] of [
867+ [ "close" , "closed" ] ,
868+ [ "reopen" , "open" ] ,
869+ [ "merge" , "merged" ] ,
870+ ] as const ) {
871+ const detailChanged = Latch . makeUnsafe ( ) ;
872+ const summaryChanged = Latch . makeUnsafe ( ) ;
873+ const listChanged = Latch . makeUnsafe ( ) ;
874+ const stops = [
875+ reader . registry . subscribe ( detail , ( result ) => {
876+ if ( AsyncResult . isSuccess ( result ) && result . value . state === expected ) {
877+ detailChanged . openUnsafe ( ) ;
878+ }
879+ } ) ,
880+ reader . registry . subscribe ( summary , ( result ) => {
881+ if ( AsyncResult . isSuccess ( result ) && result . value . state === expected ) {
882+ summaryChanged . openUnsafe ( ) ;
883+ }
884+ } ) ,
885+ reader . registry . subscribe ( list , ( result ) => {
886+ if ( AsyncResult . isSuccess ( result ) && result . value . entries [ 0 ] ?. state === expected ) {
887+ listChanged . openUnsafe ( ) ;
888+ }
889+ } ) ,
890+ ] ;
891+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => stops . forEach ( ( stop ) => stop ( ) ) ) ) ;
892+ const result = yield * Effect . promise ( ( ) =>
893+ writer . atoms . runAction . run ( writer . registry , {
894+ ...target ,
895+ input : { ...target . input , action } ,
896+ } ) ,
897+ ) ;
898+ expect ( AsyncResult . isSuccess ( result ) ) . toBe ( true ) ;
899+ // The second client receives only the server push: no local refresh or timer tick.
900+ yield * detailChanged . await ;
901+ yield * summaryChanged . await ;
902+ yield * listChanged . await ;
903+ expect ( ( yield * AtomRegistry . getResult ( reader . registry , detail ) ) . state ) . toBe ( expected ) ;
904+ expect ( ( yield * AtomRegistry . getResult ( reader . registry , summary ) ) . state ) . toBe ( expected ) ;
905+ expect ( ( yield * AtomRegistry . getResult ( reader . registry , list ) ) . entries [ 0 ] ?. state ) . toBe (
906+ expected ,
907+ ) ;
908+ stops . forEach ( ( stop ) => stop ( ) ) ;
909+ }
910+ } ) ,
911+ ) ,
912+ ) ;
913+
811914it . effect ( "refreshes pull request activity after a comment is updated" , ( ) =>
812915 Effect . scoped (
813916 Effect . gen ( function * ( ) {
0 commit comments