@@ -60,3 +60,32 @@ async def dropping_listen(
6060 proceed .set ()
6161 with pytest .raises (SubscriptionLost ): # pragma: no branch
6262 await anext (sub )
63+
64+
65+ @requirement ("protocol:request-id:caller-supplied" )
66+ async def test_the_subscription_id_is_the_listen_request_id_the_server_saw (connect : Connect ) -> None :
67+ """The handle's `subscription_id` is the listen request's own JSON-RPC id, known to the caller
68+ while the request is still in flight - the key the server stamps every frame with for demux.
69+
70+ The assertion runs inside the open stream: the ack has arrived but the listen request's
71+ response has not, so the id cannot have come from a response.
72+ """
73+ bus = InMemorySubscriptionBus ()
74+ stock = ListenHandler (bus )
75+ seen : list [types .RequestId ] = []
76+
77+ async def recording_listen (
78+ ctx : ServerRequestContext [Any , Any ], params : types .SubscriptionsListenRequestParams
79+ ) -> types .SubscriptionsListenResult :
80+ assert ctx .request_id is not None
81+ seen .append (ctx .request_id )
82+ return await stock (ctx , params )
83+
84+ server = Server ("subs" , on_subscriptions_listen = recording_listen )
85+ async with connect (server ) as client :
86+ with anyio .fail_after (10 ):
87+ async with client .listen (tools_list_changed = True ) as sub : # pragma: no branch
88+ assert seen == [sub .subscription_id ]
89+ stock .close ()
90+ async for _event in sub :
91+ raise NotImplementedError # unreachable: nothing was published
0 commit comments