|
| 1 | +// Copyright 2026 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +@Timeout(Duration(seconds: 10)) |
| 16 | +library; |
| 17 | + |
| 18 | +import 'dart:async'; |
| 19 | + |
| 20 | +import 'package:flutter_test/flutter_test.dart'; |
| 21 | +import 'package:http/http.dart' as http; |
| 22 | +import 'package:http/testing.dart'; |
| 23 | + |
| 24 | +import 'package:livekit_client/livekit_client.dart'; |
| 25 | +import 'package:livekit_client/src/proto/livekit_models.pb.dart' as lk_models; |
| 26 | +import 'package:livekit_client/src/proto/livekit_rtc.pb.dart' as lk_rtc; |
| 27 | +import 'package:livekit_client/src/support/http_client.dart'; |
| 28 | +import 'package:livekit_client/src/support/region_url_provider.dart'; |
| 29 | +import 'package:livekit_client/src/support/websocket.dart'; |
| 30 | +import '../mock/e2e_container.dart'; |
| 31 | + |
| 32 | +const cloudUri = 'wss://test.livekit.cloud'; |
| 33 | +const token = 'token'; |
| 34 | + |
| 35 | +/// A provider that already knows its regions, so a failover never has to |
| 36 | +/// fetch them. With no [regions] there is nowhere left to fail over to. |
| 37 | +RegionUrlProvider providerWith(List<String> regions) => |
| 38 | + RegionUrlProvider(url: cloudUri, token: token)..setServerReportedRegions( |
| 39 | + lk_rtc.RegionSettings() |
| 40 | + ..regions.addAll([ |
| 41 | + for (final region in regions) |
| 42 | + lk_rtc.RegionInfo() |
| 43 | + ..region = region |
| 44 | + ..url = 'wss://$region.livekit.cloud', |
| 45 | + ]), |
| 46 | + ); |
| 47 | + |
| 48 | +void main() { |
| 49 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 50 | + |
| 51 | + late E2EContainer container; |
| 52 | + |
| 53 | + // What the validate request answers after a failed socket connect. A 403 is |
| 54 | + // one of the answers that makes connect() fail over to another region. |
| 55 | + late int validateStatus; |
| 56 | + |
| 57 | + setUp(() { |
| 58 | + container = E2EContainer(); |
| 59 | + validateStatus = 403; |
| 60 | + sdkHttpClientFactoryForTesting = (_) => MockClient( |
| 61 | + (request) async => validateStatus == 200 |
| 62 | + ? http.Response('success', 200) |
| 63 | + : http.Response('project not allowed in this region.', validateStatus), |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + tearDown(() async { |
| 68 | + sdkHttpClientFactoryForTesting = null; |
| 69 | + await container.dispose(); |
| 70 | + }); |
| 71 | + |
| 72 | + test('pre-connect audio buffer survives a region failover on the first attempt', () async { |
| 73 | + final room = container.room; |
| 74 | + |
| 75 | + // Known regions, so a failed first attempt retries another region instead |
| 76 | + // of failing the connect. |
| 77 | + room.regionUrlProviderForTesting = providerWith(['other']); |
| 78 | + room.preConnectAudioBuffer.markRecordingForTesting(); |
| 79 | + |
| 80 | + // The first socket connect fails, the retry gets through. |
| 81 | + container.wsConnector.connectError = WebSocketException('first region down'); |
| 82 | + container.wsConnector.connectErrorOnce = true; |
| 83 | + final connecting = room.connect(cloudUri, token); |
| 84 | + await container.answerJoin(); |
| 85 | + await connecting; |
| 86 | + |
| 87 | + expect(room.connectionState, ConnectionState.connected); |
| 88 | + expect(container.wsConnector.uri.toString(), startsWith('wss://other.livekit.cloud')); |
| 89 | + // The buffered microphone track is published from the join response, so |
| 90 | + // the cleanup that runs for the failed attempt must leave the buffer alone. |
| 91 | + expect(room.preConnectAudioBuffer.isRecording, isTrue); |
| 92 | + }); |
| 93 | + |
| 94 | + test('pre-connect audio buffer survives a failover when only the socket is rejected', () async { |
| 95 | + final room = container.room; |
| 96 | + room.regionUrlProviderForTesting = providerWith(['other']); |
| 97 | + room.preConnectAudioBuffer.markRecordingForTesting(); |
| 98 | + |
| 99 | + // The socket is refused while the validate request succeeds, so connect() |
| 100 | + // fails over on the socket error itself. |
| 101 | + validateStatus = 200; |
| 102 | + container.wsConnector.connectError = WebSocketException('rejected'); |
| 103 | + container.wsConnector.connectErrorOnce = true; |
| 104 | + final connecting = room.connect(cloudUri, token); |
| 105 | + await container.answerJoin(); |
| 106 | + await connecting; |
| 107 | + |
| 108 | + expect(room.connectionState, ConnectionState.connected); |
| 109 | + expect(container.wsConnector.uri.toString(), startsWith('wss://other.livekit.cloud')); |
| 110 | + expect(room.preConnectAudioBuffer.isRecording, isTrue); |
| 111 | + }); |
| 112 | + |
| 113 | + test('pre-connect audio buffer is reset when connect fails with no failover', () async { |
| 114 | + final room = container.room; |
| 115 | + room.preConnectAudioBuffer.markRecordingForTesting(); |
| 116 | + |
| 117 | + // Not a LiveKit Cloud url, so there is no other region to try. |
| 118 | + validateStatus = 401; |
| 119 | + container.wsConnector.connectError = WebSocketException('down'); |
| 120 | + await expectLater(room.connect('wss://self.hosted.example', token), throwsA(isA<ConnectException>())); |
| 121 | + await Future<void>.delayed(const Duration(milliseconds: 200)); |
| 122 | + |
| 123 | + expect(room.preConnectAudioBuffer.isRecording, isFalse); |
| 124 | + }); |
| 125 | + |
| 126 | + test('pre-connect audio buffer is reset when the failover has no region left', () async { |
| 127 | + final room = container.room; |
| 128 | + room.regionUrlProviderForTesting = providerWith([]); |
| 129 | + room.preConnectAudioBuffer.markRecordingForTesting(); |
| 130 | + // Stopped with the connect error, which is what also stops the native |
| 131 | + // recorder, rather than with a plain reset. |
| 132 | + final agentReady = expectLater(room.preConnectAudioBuffer.agentReadyFuture, throwsA(isA<ConnectException>())); |
| 133 | + |
| 134 | + container.wsConnector.connectError = WebSocketException('down'); |
| 135 | + await expectLater(room.connect(cloudUri, token), throwsA(isA<ConnectException>())); |
| 136 | + await agentReady; |
| 137 | + |
| 138 | + expect(room.preConnectAudioBuffer.isRecording, isFalse); |
| 139 | + }); |
| 140 | + |
| 141 | + test('pre-connect audio buffer is reset when the failover attempt fails too', () async { |
| 142 | + final room = container.room; |
| 143 | + room.regionUrlProviderForTesting = providerWith(['other']); |
| 144 | + room.preConnectAudioBuffer.markRecordingForTesting(); |
| 145 | + final agentReady = expectLater(room.preConnectAudioBuffer.agentReadyFuture, throwsA(isA<ConnectException>())); |
| 146 | + |
| 147 | + container.wsConnector.connectError = WebSocketException('down'); |
| 148 | + await expectLater(room.connect(cloudUri, token), throwsA(isA<ConnectException>())); |
| 149 | + await agentReady; |
| 150 | + |
| 151 | + expect(room.preConnectAudioBuffer.isRecording, isFalse); |
| 152 | + }); |
| 153 | + |
| 154 | + test('pre-connect audio buffer is reset when disconnect() is called during connect', () async { |
| 155 | + final room = container.room; |
| 156 | + room.preConnectAudioBuffer.markRecordingForTesting(); |
| 157 | + |
| 158 | + // The socket handshake is still in flight when the app gives up. A buffer |
| 159 | + // left recording here would publish the microphone if that connect went |
| 160 | + // on to complete. |
| 161 | + final handshake = Completer<void>(); |
| 162 | + container.wsConnector.connectGate = handshake.future; |
| 163 | + unawaited(room.connect(cloudUri, token).then((_) {}, onError: (_) {})); |
| 164 | + await Future<void>.delayed(const Duration(milliseconds: 10)); |
| 165 | + unawaited(room.disconnect().then((_) {}, onError: (_) {})); |
| 166 | + await Future<void>.delayed(const Duration(milliseconds: 200)); |
| 167 | + |
| 168 | + expect(room.preConnectAudioBuffer.isRecording, isFalse); |
| 169 | + handshake.complete(); |
| 170 | + }); |
| 171 | + |
| 172 | + test('pre-connect audio buffer is still reset when the connection drops', () async { |
| 173 | + final room = container.room; |
| 174 | + await container.connectRoom(); |
| 175 | + room.preConnectAudioBuffer.markRecordingForTesting(); |
| 176 | + |
| 177 | + // The server asks the participant to leave, outside of any connect attempt. |
| 178 | + container.wsConnector.onData( |
| 179 | + lk_rtc.SignalResponse( |
| 180 | + leave: lk_rtc.LeaveRequest( |
| 181 | + action: lk_rtc.LeaveRequest_Action.DISCONNECT, |
| 182 | + reason: lk_models.DisconnectReason.ROOM_DELETED, |
| 183 | + ), |
| 184 | + ).writeToBuffer(), |
| 185 | + ); |
| 186 | + await Future<void>.delayed(const Duration(milliseconds: 200)); |
| 187 | + |
| 188 | + expect(room.preConnectAudioBuffer.isRecording, isFalse); |
| 189 | + }); |
| 190 | +} |
0 commit comments