@@ -56,6 +56,13 @@ class FakeTransport implements Transport {
5656
5757 @override
5858 void sendEnvelope (Envelope env) {}
59+
60+ int forceReconnectCount = 0 ;
61+ @override
62+ void forceReconnect () {
63+ forceReconnectCount++ ;
64+ if (emitConnected) _state.add (WsState .connected);
65+ }
5966}
6067
6168/// In-memory [FlutterSecureStorage] so the controller can persist without the
@@ -229,4 +236,54 @@ void main() {
229236 },
230237 );
231238 });
239+
240+ group ('ConnectionController app-lifecycle reconnect (B10)' , () {
241+ test ('onAppResumed forces an immediate transport reconnect when not connected' , () async {
242+ final storage = _seededStorage ();
243+ final transports = < FakeTransport > [];
244+ final controller = ConnectionController (
245+ storage,
246+ // Never emits connected → controller stays in connecting/reconnecting.
247+ transportFactory: () {
248+ final t = FakeTransport ();
249+ transports.add (t);
250+ return t;
251+ },
252+ browseLan: _fixedBrowse (const []),
253+ rediscoverStall: const Duration (seconds: 30 ),
254+ );
255+ await Future <void >.delayed (Duration .zero);
256+ expect (transports, hasLength (1 ));
257+ expect (transports[0 ].forceReconnectCount, 0 );
258+
259+ controller.onAppResumed ();
260+
261+ expect (transports[0 ].forceReconnectCount, 1 ,
262+ reason: 'foreground should nudge a stalled connection to retry now' );
263+ controller.dispose ();
264+ });
265+
266+ test ('onAppResumed does NOT force a reconnect while already connected' , () async {
267+ final storage = _seededStorage ();
268+ final transports = < FakeTransport > [];
269+ final controller = ConnectionController (
270+ storage,
271+ transportFactory: () {
272+ final t = FakeTransport (emitConnected: true ); // healthy connection
273+ transports.add (t);
274+ return t;
275+ },
276+ browseLan: _fixedBrowse (const []),
277+ rediscoverStall: const Duration (seconds: 30 ),
278+ );
279+ await Future <void >.delayed (Duration .zero);
280+ expect (transports, hasLength (1 ));
281+
282+ controller.onAppResumed ();
283+
284+ expect (transports[0 ].forceReconnectCount, 0 ,
285+ reason: 'a healthy socket must not be dropped on every foreground' );
286+ controller.dispose ();
287+ });
288+ });
232289}
0 commit comments