Skip to content

Commit b46731d

Browse files
committed
[red] reject stale filesystem root discovery
Signed-off-by: Viwat Vchirawongkwin <viwat.v@chula.ac.th>
1 parent 51bad6e commit b46731d

1 file changed

Lines changed: 193 additions & 89 deletions

File tree

‎app/test/unit/file_explorer_controller_test.dart‎

Lines changed: 193 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,36 @@ final class _BatchDeleteConnection extends RecordingConnection
128128
}
129129
}
130130

131+
/// Holds each DEVICE_INFO response so reconnect ordering can be exercised.
132+
final class _HeldDeviceInfoConnection extends RecordingConnection
133+
implements ConnectionSessionStampSource {
134+
_HeldDeviceInfoConnection() : super(initial: ConnState.ready);
135+
136+
Object _sessionStamp = Object();
137+
final List<Completer<DeviceInfo>> deviceInfoRequests =
138+
<Completer<DeviceInfo>>[];
139+
140+
@override
141+
Object get connectionSessionStamp => _sessionStamp;
142+
143+
@override
144+
Future<DeviceInfo> deviceInfo() {
145+
final Completer<DeviceInfo> request = Completer<DeviceInfo>();
146+
deviceInfoRequests.add(request);
147+
return request.future;
148+
}
149+
150+
void detachSession() {
151+
_sessionStamp = Object();
152+
emit(ConnState.disconnected);
153+
}
154+
155+
void attachSession() {
156+
_sessionStamp = Object();
157+
emit(ConnState.ready);
158+
}
159+
}
160+
131161
void main() {
132162
Uint8List b(String s) => Uint8List.fromList(utf8.encode(s));
133163

@@ -864,102 +894,176 @@ void main() {
864894
});
865895
});
866896

867-
group(
868-
'FR-FILES-1 auto-load on connection (the facade never re-fires build)',
869-
() {
870-
test(
871-
'listing auto-loads when a connection becomes ready AFTER first build',
872-
() async {
873-
final FakeConnection fake = FakeConnection(
874-
initial: ConnState.disconnected,
875-
);
876-
addTearDown(fake.dispose);
877-
await fake.putFile('/main.py', b('print(1)\n'));
897+
group('FR-FILES-1 auto-load on connection (the facade never re-fires build)', () {
898+
const DeviceInfo oldInfo = DeviceInfo(
899+
chip: 'esp32',
900+
mpyVersion: '1.28.0',
901+
freeMem: 32000,
902+
fsRoot: '/old',
903+
);
904+
const DeviceInfo currentInfo = DeviceInfo(
905+
chip: 'esp32-s3',
906+
mpyVersion: '1.28.0',
907+
freeMem: 48000,
908+
fsRoot: '/flash',
909+
);
878910

879-
final ProviderContainer c = await ready(fake);
880-
expect(
881-
state(c).entries,
882-
isEmpty,
883-
reason: 'nothing to list while disconnected',
884-
);
885-
expect(
886-
state(c).loading,
887-
isFalse,
888-
reason: 'no listing is in flight while disconnected',
889-
);
911+
Future<({_HeldDeviceInfoConnection connection, ProviderContainer c})>
912+
reconnectWithHeldOldInfo() async {
913+
final _HeldDeviceInfoConnection connection = _HeldDeviceInfoConnection();
914+
addTearDown(connection.dispose);
915+
await connection.mkdir('/old');
916+
await connection.mkdir('/flash');
917+
await connection.putFile('/flash/current.py', b('# current\n'));
890918

891-
// The board connects: the explorer must re-root + list by ITSELF —
892-
// no manual refresh (the stable facade never re-fires build; the
893-
// controller listens to ConnState instead).
894-
fake.emit(ConnState.ready);
895-
await pumpEventQueue();
896-
await pumpEventQueue();
919+
final ProviderContainer c = await ready(connection);
920+
expect(connection.deviceInfoRequests, hasLength(1));
897921

898-
expect(
899-
names(c),
900-
contains('main.py'),
901-
reason: 'the listing auto-loads on the new connection',
902-
);
903-
},
904-
);
922+
connection.detachSession();
923+
await pumpEventQueue();
924+
connection.attachSession();
925+
await pumpEventQueue();
926+
expect(connection.deviceInfoRequests, hasLength(2));
905927

906-
test(
907-
'running -> ready (a program finishing) keeps the user\'s cwd',
908-
() async {
909-
final FakeConnection fake = FakeConnection(initial: ConnState.ready);
910-
addTearDown(fake.dispose);
911-
await fake.mkdir('/lib');
912-
await fake.putFile('/lib/util.py', b('# util\n'));
913-
914-
final ProviderContainer c = await ready(fake);
915-
await ctrl(c).into('lib');
916-
expect(state(c).cwd, '/lib');
917-
918-
// A program runs and finishes: ready -> running -> ready must NOT
919-
// re-root the explorer out of the directory being browsed.
920-
fake.emit(ConnState.running);
921-
await pumpEventQueue();
922-
fake.emit(ConnState.ready);
923-
await pumpEventQueue();
924-
await pumpEventQueue();
928+
connection.deviceInfoRequests[1].complete(currentInfo);
929+
await pumpEventQueue();
930+
await pumpEventQueue();
931+
expect(state(c).fsRoot, '/flash');
932+
expect(state(c).cwd, '/flash');
933+
expect(state(c).hasReportedFsRoot, isTrue);
934+
expect(names(c), contains('current.py'));
935+
expect(state(c).error, isNull);
936+
return (connection: connection, c: c);
937+
}
925938

926-
expect(
927-
state(c).cwd,
928-
'/lib',
929-
reason: 'a run finishing must not yank the user back to fsRoot',
930-
);
931-
},
932-
);
939+
test(
940+
'a late prior-session DEVICE_INFO success cannot replace the current root',
941+
() async {
942+
final h = await reconnectWithHeldOldInfo();
933943

934-
test(
935-
'disconnect clears the stale listing; the next connection re-lists',
936-
() async {
937-
final FakeConnection fake = FakeConnection(initial: ConnState.ready);
938-
addTearDown(fake.dispose);
939-
await fake.putFile('/main.py', b('print(1)\n'));
944+
h.connection.deviceInfoRequests.first.complete(oldInfo);
945+
await pumpEventQueue();
946+
await pumpEventQueue();
940947

941-
final ProviderContainer c = await ready(fake);
942-
expect(names(c), contains('main.py'));
948+
expect(state(h.c).fsRoot, '/flash');
949+
expect(state(h.c).cwd, '/flash');
950+
expect(state(h.c).hasReportedFsRoot, isTrue);
951+
expect(names(h.c), contains('current.py'));
952+
expect(state(h.c).error, isNull);
953+
},
954+
);
943955

944-
fake.emit(ConnState.disconnected);
945-
await pumpEventQueue();
946-
expect(
947-
state(c).entries,
948-
isEmpty,
949-
reason: 'a stale listing from the old board is dropped',
950-
);
956+
test(
957+
'a late prior-session DEVICE_INFO failure cannot overwrite current state',
958+
() async {
959+
final h = await reconnectWithHeldOldInfo();
951960

952-
await fake.putFile('/new.py', b('print(2)\n'));
953-
fake.emit(ConnState.ready);
954-
await pumpEventQueue();
955-
await pumpEventQueue();
956-
expect(
957-
names(c),
958-
contains('new.py'),
959-
reason: 'the new session auto-lists',
960-
);
961-
},
962-
);
963-
},
964-
);
961+
h.connection.deviceInfoRequests.first.completeError(
962+
const EIo('old session failed late'),
963+
);
964+
await pumpEventQueue();
965+
await pumpEventQueue();
966+
967+
expect(state(h.c).fsRoot, '/flash');
968+
expect(state(h.c).cwd, '/flash');
969+
expect(state(h.c).hasReportedFsRoot, isTrue);
970+
expect(names(h.c), contains('current.py'));
971+
expect(state(h.c).error, isNull);
972+
},
973+
);
974+
975+
test(
976+
'listing auto-loads when a connection becomes ready AFTER first build',
977+
() async {
978+
final FakeConnection fake = FakeConnection(
979+
initial: ConnState.disconnected,
980+
);
981+
addTearDown(fake.dispose);
982+
await fake.putFile('/main.py', b('print(1)\n'));
983+
984+
final ProviderContainer c = await ready(fake);
985+
expect(
986+
state(c).entries,
987+
isEmpty,
988+
reason: 'nothing to list while disconnected',
989+
);
990+
expect(
991+
state(c).loading,
992+
isFalse,
993+
reason: 'no listing is in flight while disconnected',
994+
);
995+
996+
// The board connects: the explorer must re-root + list by ITSELF —
997+
// no manual refresh (the stable facade never re-fires build; the
998+
// controller listens to ConnState instead).
999+
fake.emit(ConnState.ready);
1000+
await pumpEventQueue();
1001+
await pumpEventQueue();
1002+
1003+
expect(
1004+
names(c),
1005+
contains('main.py'),
1006+
reason: 'the listing auto-loads on the new connection',
1007+
);
1008+
},
1009+
);
1010+
1011+
test(
1012+
'running -> ready (a program finishing) keeps the user\'s cwd',
1013+
() async {
1014+
final FakeConnection fake = FakeConnection(initial: ConnState.ready);
1015+
addTearDown(fake.dispose);
1016+
await fake.mkdir('/lib');
1017+
await fake.putFile('/lib/util.py', b('# util\n'));
1018+
1019+
final ProviderContainer c = await ready(fake);
1020+
await ctrl(c).into('lib');
1021+
expect(state(c).cwd, '/lib');
1022+
1023+
// A program runs and finishes: ready -> running -> ready must NOT
1024+
// re-root the explorer out of the directory being browsed.
1025+
fake.emit(ConnState.running);
1026+
await pumpEventQueue();
1027+
fake.emit(ConnState.ready);
1028+
await pumpEventQueue();
1029+
await pumpEventQueue();
1030+
1031+
expect(
1032+
state(c).cwd,
1033+
'/lib',
1034+
reason: 'a run finishing must not yank the user back to fsRoot',
1035+
);
1036+
},
1037+
);
1038+
1039+
test(
1040+
'disconnect clears the stale listing; the next connection re-lists',
1041+
() async {
1042+
final FakeConnection fake = FakeConnection(initial: ConnState.ready);
1043+
addTearDown(fake.dispose);
1044+
await fake.putFile('/main.py', b('print(1)\n'));
1045+
1046+
final ProviderContainer c = await ready(fake);
1047+
expect(names(c), contains('main.py'));
1048+
1049+
fake.emit(ConnState.disconnected);
1050+
await pumpEventQueue();
1051+
expect(
1052+
state(c).entries,
1053+
isEmpty,
1054+
reason: 'a stale listing from the old board is dropped',
1055+
);
1056+
1057+
await fake.putFile('/new.py', b('print(2)\n'));
1058+
fake.emit(ConnState.ready);
1059+
await pumpEventQueue();
1060+
await pumpEventQueue();
1061+
expect(
1062+
names(c),
1063+
contains('new.py'),
1064+
reason: 'the new session auto-lists',
1065+
);
1066+
},
1067+
);
1068+
});
9651069
}

0 commit comments

Comments
 (0)