fix: clear a stale relay banner once the connection is back

- resuming from the background can fail the first relay request (DNS not
  ready yet) while a later session read and the event stream work; the
  banner stayed because only the exact "Connection lost, reconnecting…"
  text was ever cleared, and a successful session read did not clear it
- transport errors (connect, session list/search/more, archive, active,
  permissions, chat settings, projects, fs home, models) are now marked as
  such and cleared by any successful read or by an event arriving on the
  restarted stream; a pin/archive action error is left for the next action
- the sidebar Retry now reconnects the stored connection when there is no
  client, otherwise re-reads the session list and clears the stale error
- version 0.6.4+604; tests cover the stale-error clearing and rollback
This commit is contained in:
blumia committed 2026-11-27 18:37:19 +08:00
1 parent ba3c820186
commit 9ae1e9b97a
6 files changed
+177 -6

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
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'));
});
}