108 lines
3.3 KiB
Dart
108 lines
3.3 KiB
Dart
import 'dart:convert';
|
|||
|
|
import 'dart:typed_data';
|
||
|
|
|
||
|
|
import 'package:flutter_test/flutter_test.dart';
|
||
|
|
import 'package:openchamber/api/client.dart';
|
||
|
|
import 'package:openchamber/api/models.dart';
|
||
|
|
import 'package:openchamber/api/transport.dart';
|
||
|
|
import 'package:openchamber/state.dart';
|
||
|
|
|
||
|
|
/// A relay connection that drops the first session-list read, then works.
|
||
|
|
class _FakeTransport implements Transport {
|
||
|
|
bool failList;
|
||
|
|
bool failArchive;
|
||
|
|
|
||
|
|
_FakeTransport({this.failList = true, this.failArchive = false});
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<TransportResponse> send(TransportRequest r) async {
|
||
|
|
if (r.path == '/api/session' && failList) {
|
||
|
|
failList = false;
|
||
|
|
return _reply(503, {
|
||
|
|
'error':
|
||
|
|
'Could not connect to the relay: SocketException: Failed host lookup: relay.example'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
if (r.path == '/api/session') {
|
||
|
|
return _reply(200, {
|
||
|
|
'data': [
|
||
|
|
{
|
||
|
|
'id': 'ses_1',
|
||
|
|
'title': 'Session',
|
||
|
|
'time': {'created': 1, 'updated': 2},
|
||
|
|
'location': {'directory': '/home/me/project'},
|
||
|
|
}
|
||
|
|
]
|
||
|
|
});
|
||
|
|
}
|
||
|
|
if (r.path.startsWith('/api/session/') && r.method == 'PATCH') {
|
||
|
|
if (failArchive) {
|
||
|
|
return _reply(500, {'error': 'archive failed'});
|
||
|
|
}
|
||
|
|
return _reply(200, {'data': <String, dynamic>{}});
|
||
|
|
}
|
||
|
|
return _reply(200, <String, dynamic>{});
|
||
|
|
}
|
||
|
|
|
||
|
|
TransportResponse _reply(int status, Object body) => TransportResponse(
|
||
|
|
status,
|
||
|
|
const {'content-type': 'application/json'},
|
||
|
|
Stream.value(Uint8List.fromList(utf8.encode(jsonEncode(body)))),
|
||
|
|
);
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<TransportSocket> webSocket(String path,
|
||
|
|
{String? rawQuery, Map<String, String> headers = const {}}) async =>
|
||
|
|
throw UnimplementedError();
|
||
|
|
|
||
|
|
@override
|
||
|
|
void setBearerToken(String? token) {}
|
||
|
|
|
||
|
|
@override
|
||
|
|
void close() {}
|
||
|
|
}
|
||
|
|
|
||
|
|
void main() {
|
||
|
|
test('a successful session read clears a stale relay error', () async {
|
||
|
|
final app = AppState();
|
||
|
|
addTearDown(app.dispose);
|
||
|
|
final transport = _FakeTransport();
|
||
|
|
app.client = Client(Uri.parse('http://test'), transport: transport);
|
||
|
|
|
||
|
|
await app.refreshSessions();
|
||
|
|
expect(app.error, contains('relay'));
|
||
|
|
|
||
|
|
// The tunnel came back on its own; the next read succeeds.
|
||
|
|
await app.refreshSessions();
|
||
|
|
expect(app.sessions, hasLength(1));
|
||
|
|
expect(app.error, isNull);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('the banner Retry reads the list again and clears the error', () async {
|
||
|
|
final app = AppState();
|
||
|
|
addTearDown(app.dispose);
|
||
|
|
final transport = _FakeTransport();
|
||
|
|
app.client = Client(Uri.parse('http://test'), transport: transport);
|
||
|
|
|
||
|
|
await app.refreshSessions();
|
||
|
|
expect(app.error, isNotNull);
|
||
|
|
|
||
|
|
await app.retryConnection();
|
||
|
|
expect(app.sessions, hasLength(1));
|
||
|
|
expect(app.error, isNull);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('a session action error is not wiped by a routine refresh', () async {
|
||
|
|
final app = AppState();
|
||
|
|
addTearDown(app.dispose);
|
||
|
|
final transport = _FakeTransport(failList: false, failArchive: true);
|
||
|
|
app.client = Client(Uri.parse('http://test'), transport: transport);
|
||
|
|
|
||
|
|
await app.setArchived(Session(id: 'ses_2', title: 'Old'), true);
|
||
|
|
expect(app.error, contains('archive failed'));
|
||
|
|
|
||
|
|
await app.refreshSessions();
|
||
|
|
// The banner Retry dismisses action errors; a background refresh does not.
|
||
|
|
expect(app.error, contains('archive failed'));
|
||
|
|
});
|
||
|
|
}
|