Files
pipelines/app/test/features/settings/saved_server_sign_in_test.dart
T
alan 079924d19a Recover from zero servers, sign each machine in on its own, offer every route
- Removing the primary machine hands pipelines/runs to the next saved machine
  on native hosts; removing the last machine returns to the connect screen
  instead of stranding the app on "Server endpoint not configured".
- Add server probes the new machine and asks for its own password in the same
  page; Settings rows show Connected / Sign in required / Unreachable with the
  reason and offer Sign in; no other machine's credential is ever reused.
- Unreachable addresses explain why: a TLS failure against an IP points at the
  Tailscale name, a non-Pipelines answer points at the port.
- Discovered machines list every advertised route (LAN, Tailscale IP, Tailscale
  name) as a checklist; chosen routes are saved as one machine.
- tool/connection_probe.dart replays the native login and websocket against
  real addresses for diagnosis.
2026-11-08 23:59:20 +00:00

281 lines
10 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/servers/machine_access.dart';
import 'package:pipelines_app/core/servers/server_catalog.dart';
import 'package:pipelines_app/core/servers/server_catalog_store.dart';
import 'package:pipelines_app/features/settings/saved_server_editor.dart';
import 'package:pipelines_app/features/settings/saved_server_row.dart';
import 'package:pipelines_app/features/settings/saved_servers_section.dart';
import 'package:pipelines_app/features/settings/server_connections.dart';
/// A machine authenticator the test scripts: what each probe answers and
/// which password each machine accepts.
class FakeAuthenticator extends ChangeNotifier implements MachineAuthenticator {
final verdicts = <String, MachineAccessResult>{};
final passwords = <String, String>{};
final probes = <String>[];
final signIns = <(String, String)>[];
@override
MachineAccessResult? status(String id) => verdicts[id];
@override
Future<MachineAccessResult> probe(SavedServer server) async {
probes.add(server.id);
return verdicts[server.id] ??
const MachineAccessResult(MachineAccess.connected);
}
@override
Future<String?> authenticate(SavedServer server, String password) async {
signIns.add((server.id, password));
if (passwords[server.id] != password) return 'Wrong password.';
verdicts[server.id] = const MachineAccessResult(MachineAccess.connected);
notifyListeners();
return null;
}
}
Future<void> revealSave(WidgetTester tester) async {
await tester.scrollUntilVisible(
find.text('Save server'),
120,
scrollable: find.byType(Scrollable).last,
);
await tester.pumpAndSettle();
}
Future<void> addServer(
WidgetTester tester, {
required String name,
required String address,
}) async {
await tester.tap(find.text('Add server'));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('server-profile-name')), name);
await tester.enterText(
find.byKey(const Key('server-profile-addresses')),
address,
);
await revealSave(tester);
await tester.tap(find.text('Save server'));
await tester.pumpAndSettle();
}
void main() {
for (final cupertino in [false, true]) {
testWidgets('a new machine asks for its own password ($cupertino)', (
tester,
) async {
final connections = ServerConnections(
store: MemoryServerCatalogStore(
catalog: ServerCatalog(
servers: [
SavedServer.validated(
id: 'mac',
name: 'Mac',
endpoints: ['ws://10.0.0.75:8083/connect'],
),
],
selectedId: 'mac',
),
),
onSelected: (_) async {},
);
addTearDown(connections.dispose);
await connections.load();
final auth = FakeAuthenticator()
..verdicts['mac'] = const MachineAccessResult(MachineAccess.connected);
connections.authenticator = auth;
final body = SingleChildScrollView(
child: SavedServersSection(connections: connections),
);
await tester.pumpWidget(
cupertino
? CupertinoApp(
home: CupertinoPageScaffold(child: SafeArea(child: body)),
)
: MaterialApp(home: Scaffold(body: body)),
);
expect(find.text('Connected'), findsOneWidget);
// The new machine is unknown to the authenticator until saved; a
// password verdict arrives for whatever id the editor assigned.
auth.verdicts.addAll({
for (final id in ['pending']) id: const MachineAccessResult(
MachineAccess.needsPassword,
),
});
await tester.tap(find.text('Add server'));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('server-profile-name')), 'tank');
await tester.enterText(
find.byKey(const Key('server-profile-addresses')),
'ws://10.0.0.100:8082/connect',
);
// Whatever id the editor chose, that machine wants a password.
connections.addListener(() {
for (final server in connections.catalog.servers) {
auth.verdicts.putIfAbsent(
server.id,
() => const MachineAccessResult(MachineAccess.needsPassword),
);
auth.passwords.putIfAbsent(server.id, () => 'tank-secret');
}
});
await revealSave(tester);
await tester.tap(find.text('Save server'));
await tester.pumpAndSettle();
final tank = connections.catalog.servers.firstWhere((s) => s.name == 'tank');
expect(auth.probes, [tank.id]);
// Saved, and the editor now asks this machine's password; the first
// machine's sign-in is not reused.
expect(find.byKey(const Key('server-profile-password')), findsOneWidget);
expect(find.textContaining('asks for its own password'), findsOneWidget);
await tester.enterText(
find.byKey(const Key('server-profile-password')),
'wrong',
);
await tester.tap(find.byKey(const Key('server-profile-sign-in')));
await tester.pumpAndSettle();
expect(find.text('Wrong password.'), findsOneWidget);
expect(find.byKey(const Key('server-profile-password')), findsOneWidget);
await tester.enterText(
find.byKey(const Key('server-profile-password')),
'tank-secret',
);
await tester.tap(find.byKey(const Key('server-profile-sign-in')));
await tester.pumpAndSettle();
expect(auth.signIns, [(tank.id, 'wrong'), (tank.id, 'tank-secret')]);
// Back on the list: both machines connected, each on its own sign-in.
expect(find.byKey(const Key('server-profile-password')), findsNothing);
expect(find.text('Connected'), findsNWidgets(2));
expect(find.text('Address: ws://10.0.0.100:8082/connect'), findsOneWidget);
expect(connections.catalog.active?.id, 'mac');
});
}
testWidgets('an unreachable machine stays saved with the reason shown', (
tester,
) async {
final connections = ServerConnections(
store: MemoryServerCatalogStore(),
onSelected: (_) async {},
);
addTearDown(connections.dispose);
await connections.load();
final auth = FakeAuthenticator();
connections.authenticator = auth;
connections.addListener(() {
for (final server in connections.catalog.servers) {
auth.verdicts.putIfAbsent(
server.id,
() => const MachineAccessResult(
MachineAccess.unreachable,
detail: 'The HTTPS certificate of 100.85.25.33:8443 does not '
'cover an IP address.',
),
);
}
});
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: SavedServersSection(connections: connections),
),
),
),
);
await addServer(
tester,
name: 'Mac by IP',
address: 'wss://100.85.25.33:8443/connect',
);
expect(find.textContaining('could not be reached'), findsOneWidget);
expect(find.textContaining('does not cover an IP address'), findsOneWidget);
expect(find.byKey(const Key('server-profile-password')), findsNothing);
await tester.tap(find.byKey(const Key('server-sign-in-later')));
await tester.pumpAndSettle();
expect(connections.catalog.servers.single.name, 'Mac by IP');
expect(find.textContaining('Unreachable: The HTTPS certificate'), findsOneWidget);
});
testWidgets('a saved machine that wants a password offers Sign in', (
tester,
) async {
final tank = SavedServer.validated(
id: 'tank',
name: 'tank',
endpoints: ['ws://10.0.0.100:8082/connect'],
);
final connections = ServerConnections(
store: MemoryServerCatalogStore(
catalog: ServerCatalog(servers: [tank], selectedId: 'tank'),
),
onSelected: (_) async {},
);
addTearDown(connections.dispose);
await connections.load();
final auth = FakeAuthenticator()
..verdicts['tank'] = const MachineAccessResult(MachineAccess.needsPassword)
..passwords['tank'] = 'tank-secret';
connections.authenticator = auth;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(body: SavedServersSection(connections: connections)),
),
);
expect(find.text('Sign in required'), findsOneWidget);
await tester.tap(find.byKey(savedServerMenuKey('tank')));
await tester.pumpAndSettle();
await tester.tap(find.text('Sign in to tank'));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('machine-password')), 'tank-secret');
await tester.tap(find.byKey(const Key('machine-sign-in')));
await tester.pumpAndSettle();
expect(auth.signIns, [('tank', 'tank-secret')]);
expect(find.text('Connected'), findsOneWidget);
expect(find.text('Sign in required'), findsNothing);
});
testWidgets('editing a name alone does not re-ask the password', (
tester,
) async {
final mac = SavedServer.validated(
id: 'mac',
name: 'Mac',
endpoints: ['ws://10.0.0.75:8083/connect'],
);
final auth = FakeAuthenticator()
..verdicts['mac'] = const MachineAccessResult(MachineAccess.needsPassword);
SavedServer? saved;
await tester.pumpWidget(
MaterialApp(
home: Builder(
builder: (context) => Scaffold(
body: TextButton(
onPressed: () => editSavedServer(
context,
server: mac,
authenticator: auth,
onSave: (value) async => saved = value,
),
child: const Text('Edit'),
),
),
),
),
);
await tester.tap(find.text('Edit'));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('server-profile-name')), 'Studio');
await revealSave(tester);
await tester.tap(find.text('Save server'));
await tester.pumpAndSettle();
expect(saved?.name, 'Studio');
expect(auth.probes, isEmpty);
expect(find.text('Edit'), findsOneWidget);
});
}