Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

test(mobile): the token a relay socket dials with - revoked signs out, an outage is retried

dee5289d5c9346c
diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dart
index e7529cf48..e2ac7ccd0 100644
--- a/mobile/lib/state/app_state.dart
+++ b/mobile/lib/state/app_state.dart
@@ -2992,6 +2992,34 @@ class AppNotifier extends ChangeNotifier {
if (viewer == null) _startLinkRetry(machineId);
}
+ /// The token a token-bearing socket dials with: a viewer's relay socket, or
+ /// the local-manual dev fixture's.
+ Future<String> _socketToken(bool force, String? failedToken) async {
+ final directAuth = viewer?.auth;
+ if (directAuth != null) {
+ // Only a session that is gone for good may sign the person out — see
+ // [WsCredentialRevoked]. An outage fails the refresh too, and is retried.
+ return directAuth
+ .accessToken(force: force, failedToken: failedToken)
+ .onError<DirectAuthException>(
+ (error, _) => throw WsCredentialRevoked(error.message),
+ test: (error) => error.signedOut,
+ );
+ }
+ final fixture = localManualFixture;
+ if (fixture != null) return fixture.apiKey;
+ throw StateError(
+ 'unreachable: only a viewer build and the local-manual dev fixture use a token-bearing WS transport',
+ );
+ }
+
+ /// [_socketToken], for a test: the pool that asks for it dials for real.
+ @visibleForTesting
+ Future<String> socketTokenForTest({
+ bool force = false,
+ String? failedToken,
+ }) => _socketToken(force, failedToken);
+
void _ensurePool() {
if (_pool != null) return;
_pool = WsPool(
@@ -3000,24 +3028,7 @@ class AppNotifier extends ChangeNotifier {
// Every real desktop WsConn dials the local CLI's loopback WS (transportKind.localPlaintext, see
// _conn()), which never calls this — only the compile-time-only local-manual dev fixture (see
// LocalManualFixture) still dials a backend directly with a token.
- accessTokenProvider: (force, failedToken) async {
- final directAuth = viewer?.auth;
- if (directAuth != null) {
- // Only a session that is gone for good may sign the person out — see
- // [WsCredentialRevoked]. An outage fails the refresh too, and is retried.
- return directAuth
- .accessToken(force: force, failedToken: failedToken)
- .onError<DirectAuthException>(
- (error, _) => throw WsCredentialRevoked(error.message),
- test: (error) => error.signedOut,
- );
- }
- final fixture = localManualFixture;
- if (fixture != null) return fixture.apiKey;
- throw StateError(
- 'unreachable: only a viewer build and the local-manual dev fixture use a token-bearing WS transport',
- );
- },
+ accessTokenProvider: _socketToken,
relayCodecs: viewer?.relayCodecs,
transportPlugins: viewer?.transportPlugins,
onAuthFailure: _signedOutAtRuntime,
diff --git a/mobile/test/app_state_sign_in_test.dart b/mobile/test/app_state_sign_in_test.dart
index ed77541f0..e7e64c51e 100644
--- a/mobile/test/app_state_sign_in_test.dart
+++ b/mobile/test/app_state_sign_in_test.dart
@@ -1,10 +1,14 @@
import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/auth/auth_session.dart';
+import 'package:harness_mobile/core/config.dart';
import 'package:harness_mobile/core/models.dart';
import 'package:harness_mobile/state/app_state.dart';
+import 'package:harness_mobile/ws/ws_conn.dart';
import 'viewer_app_fixture.dart';
+import 'voice_fakes.dart' show MemoryKeyValueStore;
/// Signing a phone in and out: the viewer build's half of `AppNotifier` —
/// bootstrap with no CLI beside it, the emailed code and the scanned QR, and
@@ -306,4 +310,78 @@ void main() {
},
);
});
+
+ group('the token a relay socket dials with', () {
+ test('is the session\'s own', () async {
+ final rig = viewerApp();
+ addTearDown(rig.app.dispose);
+ await rig.app.session.saveLogin(token: 'hna_live', refreshToken: 'r1');
+
+ expect(await rig.app.socketTokenForTest(), 'hna_live');
+ expect(
+ await rig.app.socketTokenForTest(failedToken: 'hna_older'),
+ 'hna_live',
+ reason: 'someone already refreshed past the one that failed',
+ );
+ });
+
+ test('no session at all signs the phone out', () async {
+ final rig = viewerApp();
+ addTearDown(rig.app.dispose);
+
+ await expectLater(
+ rig.app.socketTokenForTest(),
+ throwsA(isA<WsCredentialRevoked>()),
+ );
+ });
+
+ test('a refresh the backend refuses signs the phone out', () async {
+ final rig = viewerApp();
+ addTearDown(rig.app.dispose);
+ await rig.app.session.saveLogin(token: 'hna_old', refreshToken: 'r1');
+ rig.viewer.backend.replies['/api/auth/refresh'] = (
+ status: 401,
+ body: {'success': false},
+ );
+
+ await expectLater(
+ rig.app.socketTokenForTest(force: true),
+ throwsA(isA<WsCredentialRevoked>()),
+ );
+ });
+
+ test(
+ 'a refresh that cannot reach the backend is retried, not a sign-out',
+ () async {
+ final rig = viewerApp();
+ addTearDown(rig.app.dispose);
+ await rig.app.session.saveLogin(token: 'hna_old', refreshToken: 'r1');
+
+ await expectLater(
+ rig.app.socketTokenForTest(force: true),
+ throwsA(
+ isA<Object>().having(
+ (error) => error is WsCredentialRevoked,
+ 'a sign-out',
+ isFalse,
+ ),
+ ),
+ );
+ expect(await rig.app.session.accessToken(), 'hna_old');
+ },
+ );
+
+ test('only a viewer or the dev fixture dials with a token', () async {
+ final app = AppNotifier(
+ config: AppConfig.dev,
+ authSession: AuthSession(storage: MemoryKeyValueStore()),
+ configStore: null,
+ cliLogin: FakeSignIn(),
+ );
+ addTearDown(app.dispose);
+ if (app.viewer != null) return; // a viewer host: nothing to check
+
+ await expectLater(app.socketTokenForTest(), throwsStateError);
+ });
+ });
}
diff --git a/mobile/test/viewer_app_fixture.dart b/mobile/test/viewer_app_fixture.dart
index c37247d2f..fe3b5033a 100644
--- a/mobile/test/viewer_app_fixture.dart
+++ b/mobile/test/viewer_app_fixture.dart
@@ -266,17 +266,23 @@ class FakeEmailLogin implements EmailCodeLogin {
/// A viewer's services with every wire faked — see the note at the top.
class FakeViewer implements ViewerServices {
- FakeViewer(AuthSession session)
+ FakeViewer(AuthSession session) : this._(session, FakeHttp({}));
+
+ FakeViewer._(AuthSession session, this.backend)
: keys = ViewerKeyStore(storage: MemoryKeyValueStore()),
auth = DirectAuth(
session: session,
- api: DirectAuthApi(config: AppConfig.dev, dio: FakeHttp({}).dio()),
+ api: DirectAuthApi(config: AppConfig.dev, dio: backend.dio()),
emailCodes: EmailCodeApi(
config: AppConfig.dev,
dio: FakeHttp({}).dio(),
),
);
+ /// The backend's auth routes, as the session's refresh reaches them. Empty,
+ /// every request fails the way a dropped network does.
+ final FakeHttp backend;
+
@override
final ViewerKeyStore keys;