@@ -158,6 +158,43 @@ final class _HeldDeviceInfoConnection extends RecordingConnection
158158 }
159159}
160160
161+ final class _HeldListRequest {
162+ _HeldListRequest (this .path);
163+
164+ final String path;
165+ final Completer <List <RemoteEntry >> result = Completer <List <RemoteEntry >>();
166+ }
167+
168+ /// Returns DEVICE_INFO immediately but holds every initial/manual listing.
169+ final class _HeldListingConnection extends RecordingConnection
170+ implements ConnectionSessionStampSource {
171+ _HeldListingConnection ({required super .deviceInfo})
172+ : super (initial: ConnState .ready);
173+
174+ Object _sessionStamp = Object ();
175+ final List <_HeldListRequest > listRequests = < _HeldListRequest > [];
176+
177+ @override
178+ Object get connectionSessionStamp => _sessionStamp;
179+
180+ @override
181+ Future <List <RemoteEntry >> listDir (String path) {
182+ final _HeldListRequest request = _HeldListRequest (path);
183+ listRequests.add (request);
184+ return request.result.future;
185+ }
186+
187+ void detachSession () {
188+ _sessionStamp = Object ();
189+ emit (ConnState .disconnected);
190+ }
191+
192+ void attachSession () {
193+ _sessionStamp = Object ();
194+ emit (ConnState .ready);
195+ }
196+ }
197+
161198void main () {
162199 Uint8List b (String s) => Uint8List .fromList (utf8.encode (s));
163200
@@ -936,6 +973,49 @@ void main() {
936973 return (connection: connection, c: c);
937974 }
938975
976+ Future <({_HeldListingConnection connection, ProviderContainer c})>
977+ reconnectWithHeldOldList () async {
978+ final _HeldListingConnection connection = _HeldListingConnection (
979+ deviceInfo: oldInfo,
980+ );
981+ addTearDown (connection.dispose);
982+ final ProviderContainer c = await ready (connection);
983+ expect (connection.listRequests, hasLength (1 ));
984+ expect (connection.listRequests.first.path, '/old' );
985+
986+ connection.detachSession ();
987+ await pumpEventQueue ();
988+ connection.scriptedDeviceInfo = currentInfo;
989+ connection.attachSession ();
990+ await pumpEventQueue ();
991+ await pumpEventQueue ();
992+ expect (connection.listRequests, hasLength (2 ));
993+ expect (connection.listRequests[1 ].path, '/flash' );
994+
995+ connection.listRequests[1 ].result.complete (< RemoteEntry > [
996+ const RemoteEntry (name: 'current.py' , isDir: false , size: 10 ),
997+ ]);
998+ await pumpEventQueue ();
999+ expect (state (c).fsRoot, '/flash' );
1000+ expect (state (c).cwd, '/flash' );
1001+ expect (state (c).hasReportedFsRoot, isTrue);
1002+ expect (names (c), < String > {'current.py' });
1003+ expect (state (c).error, isNull);
1004+ return (connection: connection, c: c);
1005+ }
1006+
1007+ Future <({_HeldListingConnection connection, ProviderContainer c})>
1008+ heldInitialList () async {
1009+ final _HeldListingConnection connection = _HeldListingConnection (
1010+ deviceInfo: currentInfo,
1011+ );
1012+ addTearDown (connection.dispose);
1013+ final ProviderContainer c = await ready (connection);
1014+ expect (connection.listRequests, hasLength (1 ));
1015+ expect (connection.listRequests.first.path, '/flash' );
1016+ return (connection: connection, c: c);
1017+ }
1018+
9391019 test (
9401020 'dispose before the queued initial load performs no board I/O' ,
9411021 () async {
@@ -992,6 +1072,103 @@ void main() {
9921072 },
9931073 );
9941074
1075+ test (
1076+ 'a late prior-session initial listing cannot replace current entries' ,
1077+ () async {
1078+ final h = await reconnectWithHeldOldList ();
1079+
1080+ h.connection.listRequests.first.result.complete (< RemoteEntry > [
1081+ const RemoteEntry (name: 'old.py' , isDir: false , size: 3 ),
1082+ ]);
1083+ await pumpEventQueue ();
1084+
1085+ expect (state (h.c).fsRoot, '/flash' );
1086+ expect (state (h.c).cwd, '/flash' );
1087+ expect (names (h.c), < String > {'current.py' });
1088+ expect (state (h.c).error, isNull);
1089+ },
1090+ );
1091+
1092+ test (
1093+ 'a late prior-session initial listing failure cannot replace current state' ,
1094+ () async {
1095+ final h = await reconnectWithHeldOldList ();
1096+
1097+ h.connection.listRequests.first.result.completeError (
1098+ const EIo ('old listing failed late' ),
1099+ );
1100+ await pumpEventQueue ();
1101+
1102+ expect (state (h.c).fsRoot, '/flash' );
1103+ expect (state (h.c).cwd, '/flash' );
1104+ expect (names (h.c), < String > {'current.py' });
1105+ expect (state (h.c).error, isNull);
1106+ },
1107+ );
1108+
1109+ test (
1110+ 'a newer same-session refresh wins over the initial listing' ,
1111+ () async {
1112+ final h = await heldInitialList ();
1113+
1114+ final Future <void > refreshed = ctrl (h.c).refresh ();
1115+ expect (h.connection.listRequests, hasLength (2 ));
1116+ h.connection.listRequests[1 ].result.complete (< RemoteEntry > [
1117+ const RemoteEntry (name: 'fresh.py' , isDir: false , size: 5 ),
1118+ ]);
1119+ await refreshed;
1120+ expect (names (h.c), < String > {'fresh.py' });
1121+
1122+ h.connection.listRequests.first.result.complete (< RemoteEntry > [
1123+ const RemoteEntry (name: 'stale.py' , isDir: false , size: 5 ),
1124+ ]);
1125+ await pumpEventQueue ();
1126+
1127+ expect (names (h.c), < String > {'fresh.py' });
1128+ expect (state (h.c).error, isNull);
1129+ },
1130+ );
1131+
1132+ test (
1133+ 'an older same-session listing failure cannot overwrite a newer refresh' ,
1134+ () async {
1135+ final h = await heldInitialList ();
1136+
1137+ final Future <void > refreshed = ctrl (h.c).refresh ();
1138+ expect (h.connection.listRequests, hasLength (2 ));
1139+ h.connection.listRequests[1 ].result.complete (< RemoteEntry > [
1140+ const RemoteEntry (name: 'fresh.py' , isDir: false , size: 5 ),
1141+ ]);
1142+ await refreshed;
1143+ expect (names (h.c), < String > {'fresh.py' });
1144+
1145+ h.connection.listRequests.first.result.completeError (
1146+ const EIo ('initial listing failed late' ),
1147+ );
1148+ await pumpEventQueue ();
1149+
1150+ expect (names (h.c), < String > {'fresh.py' });
1151+ expect (state (h.c).error, isNull);
1152+ },
1153+ );
1154+
1155+ test ('an initial listing completion after disposal is ignored' , () async {
1156+ final _HeldListingConnection connection = _HeldListingConnection (
1157+ deviceInfo: currentInfo,
1158+ );
1159+ addTearDown (connection.dispose);
1160+ final ProviderContainer c = ProviderContainer (
1161+ overrides: < Override > [connectionProvider.overrideWithValue (connection)],
1162+ );
1163+ c.listen (fileExplorerProvider, (_, _) {});
1164+ await pumpEventQueue ();
1165+ expect (connection.listRequests, hasLength (1 ));
1166+
1167+ c.dispose ();
1168+ connection.listRequests.single.result.complete (const < RemoteEntry > []);
1169+ await pumpEventQueue ();
1170+ });
1171+
9951172 test (
9961173 'listing auto-loads when a connection becomes ready AFTER first build' ,
9971174 () async {
0 commit comments