LAN discovery: server advertises _pipelines._tcp over mDNS; app finds servers on the connect screen
Server: internal/discovery publishes a DNS-SD record (hashicorp/mdns) with the port LAN clients dial (the web proxy via -mdns-port, which the supervisor passes, or a non-loopback -addr) and TXT v, name, path, tls, auth. On by default; -mdns=false or PIPELINES_MDNS=off disables it. Failures are logged and never block startup; the advert is withdrawn on shutdown. App: the Connect to Pipelines screen gets 'Find servers on this network' behind a ServerDiscovery interface (Bonsoir: Android NSD with multicast lock, iOS Bonjour with NSLocalNetworkUsageDescription and NSBonjourServices). Tapping a server fills the address and connects; manual entry stays. Web has no scan.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/onboarding/discovery/discovered_server.dart';
|
||||
import 'package:pipelines_app/features/onboarding/discovery/server_discovery.dart';
|
||||
|
||||
void main() {
|
||||
test('a proxied server becomes a ws endpoint that asks for a password', () {
|
||||
final server = DiscoveredServer.fromRecord(
|
||||
instance: 'Pipelines-box',
|
||||
addresses: ['fe80::1', '192.168.1.20'],
|
||||
port: 8083,
|
||||
attributes: {
|
||||
'v': 'v1.4.0',
|
||||
'name': 'box',
|
||||
'path': '/connect',
|
||||
'tls': '0',
|
||||
'auth': 'password',
|
||||
},
|
||||
)!;
|
||||
expect(server.name, 'box');
|
||||
expect(server.endpoint, Uri.parse('ws://192.168.1.20:8083/connect'));
|
||||
expect(server.address, '192.168.1.20:8083');
|
||||
expect(server.passwordRequired, isTrue);
|
||||
expect(server.version, 'v1.4.0');
|
||||
});
|
||||
|
||||
test('TLS, no auth, missing TXT and IPv6-only records', () {
|
||||
final secure = DiscoveredServer.fromRecord(
|
||||
instance: 'Pipelines-box',
|
||||
addresses: ['2001:db8::5'],
|
||||
port: 443,
|
||||
attributes: {'tls': '1', 'auth': 'none'},
|
||||
)!;
|
||||
expect(secure.name, 'Pipelines-box');
|
||||
expect(secure.endpoint, Uri.parse('wss://[2001:db8::5]:443/connect'));
|
||||
expect(secure.passwordRequired, isFalse);
|
||||
});
|
||||
|
||||
test('falls back to the SRV host name, and refuses nothing to dial', () {
|
||||
final named = DiscoveredServer.fromRecord(
|
||||
instance: 'Pipelines-box',
|
||||
addresses: ['fe80::1', '127.0.0.1'],
|
||||
hostname: 'box.local.',
|
||||
port: 8083,
|
||||
)!;
|
||||
expect(named.endpoint, Uri.parse('ws://box.local:8083/connect'));
|
||||
expect(
|
||||
DiscoveredServer.fromRecord(
|
||||
instance: 'x',
|
||||
addresses: const [],
|
||||
port: 8083,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
DiscoveredServer.fromRecord(
|
||||
instance: 'x',
|
||||
addresses: ['10.0.0.2'],
|
||||
port: 0,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('web has no discovery; native platforms do', () {
|
||||
expect(platformServerDiscovery(web: true), isNull);
|
||||
expect(platformServerDiscovery(web: false), isNotNull);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/session/server_session.dart';
|
||||
import 'package:pipelines_app/features/onboarding/discovery/discovered_server.dart';
|
||||
import 'package:pipelines_app/features/onboarding/discovery/server_discovery.dart';
|
||||
import 'package:pipelines_app/features/onboarding/onboarding_flow.dart';
|
||||
|
||||
/// A discoverer the test drives: each scan is a controller the test feeds.
|
||||
class FakeDiscovery implements ServerDiscovery {
|
||||
final scans = <StreamController<List<DiscoveredServer>>>[];
|
||||
int cancelled = 0;
|
||||
|
||||
StreamController<List<DiscoveredServer>> get current => scans.last;
|
||||
|
||||
@override
|
||||
Stream<List<DiscoveredServer>> scan() {
|
||||
final controller = StreamController<List<DiscoveredServer>>(
|
||||
onCancel: () => cancelled++,
|
||||
);
|
||||
scans.add(controller);
|
||||
return controller.stream;
|
||||
}
|
||||
}
|
||||
|
||||
const box = DiscoveredServer(name: 'box', host: '192.168.1.20', port: 8083);
|
||||
const studio = DiscoveredServer(
|
||||
name: 'studio',
|
||||
host: '192.168.1.30',
|
||||
port: 8080,
|
||||
passwordRequired: false,
|
||||
);
|
||||
|
||||
Widget host(Widget child, {bool cupertino = false}) =>
|
||||
cupertino ? CupertinoApp(home: child) : MaterialApp(home: child);
|
||||
|
||||
void main() {
|
||||
late FakeDiscovery discovery;
|
||||
late List<Uri> checked;
|
||||
late List<String> saved;
|
||||
|
||||
setUp(() {
|
||||
discovery = FakeDiscovery();
|
||||
checked = [];
|
||||
saved = [];
|
||||
});
|
||||
|
||||
OnboardingFlow flow({ServerDiscovery? discoverer, ValueChanged<Uri>? done}) =>
|
||||
OnboardingFlow(
|
||||
discovery: discoverer,
|
||||
check: (uri) async {
|
||||
checked.add(uri);
|
||||
return SessionState.signedOut;
|
||||
},
|
||||
saveServer: (url) async => saved.add(url),
|
||||
onDone: done ?? (_) {},
|
||||
);
|
||||
|
||||
testWidgets('no discoverer (web) shows manual entry only', (tester) async {
|
||||
await tester.pumpWidget(host(flow()));
|
||||
expect(find.byKey(const Key('onboarding-server')), findsOneWidget);
|
||||
expect(find.byKey(const Key('discovery-scan')), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('scanning lists servers as they answer, then settles', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(host(flow(discoverer: discovery)));
|
||||
expect(find.text('Find servers on this network'), findsOneWidget);
|
||||
await tester.tap(find.byKey(const Key('discovery-scan')));
|
||||
await tester.pump();
|
||||
expect(find.byKey(const Key('discovery-scanning')), findsOneWidget);
|
||||
|
||||
discovery.current.add([box]);
|
||||
await tester.pump();
|
||||
expect(find.text('box'), findsOneWidget);
|
||||
expect(find.text('192.168.1.20:8083 · password'), findsOneWidget);
|
||||
|
||||
discovery.current.add([box, studio]);
|
||||
await tester.pump();
|
||||
expect(find.text('studio'), findsOneWidget);
|
||||
expect(find.text('192.168.1.30:8080'), findsOneWidget);
|
||||
|
||||
await tester.pump(const Duration(seconds: 8));
|
||||
expect(find.byKey(const Key('discovery-scanning')), findsNothing);
|
||||
expect(discovery.cancelled, 1);
|
||||
expect(find.text('box'), findsOneWidget);
|
||||
expect(find.text('Search again'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('tapping a server fills the address and connects', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(host(flow(discoverer: discovery)));
|
||||
await tester.tap(find.byKey(const Key('discovery-scan')));
|
||||
await tester.pump();
|
||||
discovery.current.add([box]);
|
||||
await tester.pump();
|
||||
await tester.tap(find.byKey(const Key('discovered-192.168.1.20:8083')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final endpoint = Uri.parse('ws://192.168.1.20:8083/connect');
|
||||
expect(checked, [endpoint]);
|
||||
expect(saved, [endpoint.toString()]);
|
||||
// The server wants its password: the flow moved to sign-in.
|
||||
expect(find.byKey(const Key('onboarding-password')), findsOneWidget);
|
||||
expect(find.byKey(const Key('discovery-scan')), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('an empty scan says so and keeps manual entry', (tester) async {
|
||||
await tester.pumpWidget(host(flow(discoverer: discovery)));
|
||||
await tester.tap(find.byKey(const Key('discovery-scan')));
|
||||
await tester.pump();
|
||||
discovery.current.add(const []);
|
||||
await tester.pump(const Duration(seconds: 8));
|
||||
expect(find.byKey(const Key('discovery-empty')), findsOneWidget);
|
||||
expect(find.byKey(const Key('onboarding-server')), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byKey(const Key('discovery-scan')));
|
||||
await tester.pump();
|
||||
expect(discovery.scans, hasLength(2));
|
||||
expect(find.byKey(const Key('discovery-empty')), findsNothing);
|
||||
expect(find.byKey(const Key('discovery-scanning')), findsOneWidget);
|
||||
await tester.pump(const Duration(seconds: 8));
|
||||
});
|
||||
|
||||
testWidgets('a browse failure is shown and the scan can be retried', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(host(flow(discoverer: discovery)));
|
||||
await tester.tap(find.byKey(const Key('discovery-scan')));
|
||||
await tester.pump();
|
||||
discovery.current.addError(StateError('multicast blocked'));
|
||||
await tester.pump();
|
||||
expect(find.byKey(const Key('discovery-error')), findsOneWidget);
|
||||
expect(find.textContaining('multicast blocked'), findsOneWidget);
|
||||
expect(find.byKey(const Key('discovery-scanning')), findsNothing);
|
||||
// The timer was cancelled with the scan; nothing fires later.
|
||||
await tester.pump(const Duration(seconds: 8));
|
||||
expect(find.byKey(const Key('discovery-error')), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Cupertino host lists and connects the same way', (tester) async {
|
||||
await tester.pumpWidget(host(flow(discoverer: discovery), cupertino: true));
|
||||
await tester.tap(find.byKey(const Key('discovery-scan')));
|
||||
await tester.pump();
|
||||
discovery.current.add([studio]);
|
||||
await tester.pump();
|
||||
expect(find.byType(CupertinoListTile), findsOneWidget);
|
||||
await tester.tap(find.byKey(const Key('discovered-192.168.1.30:8080')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(checked, [Uri.parse('ws://192.168.1.30:8080/connect')]);
|
||||
});
|
||||
|
||||
testWidgets('leaving the screen mid-scan stops browsing', (tester) async {
|
||||
await tester.pumpWidget(host(flow(discoverer: discovery)));
|
||||
await tester.tap(find.byKey(const Key('discovery-scan')));
|
||||
await tester.pump();
|
||||
await tester.pumpWidget(host(const SizedBox()));
|
||||
expect(discovery.cancelled, 1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user