Find LAN servers from browser and review discovered machine names
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/onboarding/discovery/browser_server_discovery.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'browser parses names, default ports, auth and deduplicates endpoints',
|
||||
() async {
|
||||
final scan = BrowserServerDiscovery(
|
||||
fetch: () async => jsonEncode({
|
||||
'discovered_servers': [
|
||||
{
|
||||
'name': 'Linux',
|
||||
'endpoint': 'ws://192.168.1.5:8083/connect',
|
||||
'auth': 'password',
|
||||
'version': 'v1',
|
||||
},
|
||||
{
|
||||
'name': 'Linux',
|
||||
'endpoint': 'ws://192.168.1.5:8083/connect',
|
||||
'auth': 'password',
|
||||
},
|
||||
{
|
||||
'name': 'Mac',
|
||||
'endpoint': 'wss://mac.local/connect',
|
||||
'auth': 'none',
|
||||
},
|
||||
{'name': 'Bad', 'endpoint': 'ws://password@host/connect'},
|
||||
{'name': '', 'endpoint': 'ws://host/connect'},
|
||||
{'name': 'Fragment', 'endpoint': 'ws://host/connect#secret'},
|
||||
],
|
||||
}),
|
||||
);
|
||||
final values = await scan.scan().single;
|
||||
expect(values.map((s) => s.name), ['Linux', 'Mac']);
|
||||
expect(values.first.passwordRequired, isTrue);
|
||||
expect(values.last.passwordRequired, isFalse);
|
||||
expect(values.last.port, 443);
|
||||
},
|
||||
);
|
||||
test('unknown and malformed discovery responses report failure', () async {
|
||||
for (final body in ['not json', '{}', '{"discovered_servers":null}']) {
|
||||
await expectLater(
|
||||
BrowserServerDiscovery(fetch: () async => body).scan(),
|
||||
emitsError(isA<FormatException>()),
|
||||
);
|
||||
}
|
||||
});
|
||||
test(
|
||||
'network failure is surfaced instead of pretending no machines exist',
|
||||
() async {
|
||||
await expectLater(
|
||||
BrowserServerDiscovery(
|
||||
fetch: () async => throw StateError('offline'),
|
||||
).scan(),
|
||||
emitsError(isA<StateError>()),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -61,8 +61,8 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
test('web has no discovery; native platforms do', () {
|
||||
expect(platformServerDiscovery(web: true), isNull);
|
||||
test('web and native platforms provide discovery', () {
|
||||
expect(platformServerDiscovery(web: true), isNotNull);
|
||||
expect(platformServerDiscovery(web: false), isNotNull);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ void main() {
|
||||
expect(find.text('Search again'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('tapping a server fills the address and connects', (
|
||||
testWidgets('tapping a server reviews its name before connecting', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(host(flow(discoverer: discovery)));
|
||||
@@ -101,6 +101,10 @@ void main() {
|
||||
await tester.tap(find.byKey(const Key('discovered-192.168.1.20:8083')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(checked, isEmpty);
|
||||
await tester.ensureVisible(find.byKey(const Key('onboarding-next')));
|
||||
await tester.tap(find.byKey(const Key('onboarding-next')));
|
||||
await tester.pumpAndSettle();
|
||||
final endpoint = Uri.parse('ws://192.168.1.20:8083/connect');
|
||||
expect(checked, [endpoint]);
|
||||
expect(saved, [endpoint.toString()]);
|
||||
@@ -151,6 +155,10 @@ void main() {
|
||||
expect(find.byType(CupertinoListTile), findsOneWidget);
|
||||
await tester.tap(find.byKey(const Key('discovered-192.168.1.30:8080')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(checked, isEmpty);
|
||||
await tester.ensureVisible(find.byKey(const Key('onboarding-next')));
|
||||
await tester.tap(find.byKey(const Key('onboarding-next')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(checked, [Uri.parse('ws://192.168.1.30:8080/connect')]);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/servers/server_catalog.dart';
|
||||
import 'package:pipelines_app/features/settings/saved_server_editor.dart';
|
||||
import 'package:pipelines_app/features/onboarding/discovery/browser_server_discovery.dart';
|
||||
import 'package:pipelines_app/features/onboarding/onboarding_flow.dart';
|
||||
import 'package:pipelines_app/core/session/server_session.dart';
|
||||
|
||||
BrowserServerDiscovery discovery() => BrowserServerDiscovery(
|
||||
fetch: () async => jsonEncode({
|
||||
'discovered_servers': [
|
||||
{
|
||||
'name': 'Linux laptop',
|
||||
'endpoint': 'ws://192.168.1.5:8083/connect',
|
||||
'auth': 'password',
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'choosing a discovered machine prefills editable name and address',
|
||||
(tester) async {
|
||||
SavedServer? saved;
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Builder(
|
||||
builder: (context) => Scaffold(
|
||||
body: TextButton(
|
||||
onPressed: () => editSavedServer(
|
||||
context,
|
||||
discovery: discovery(),
|
||||
onSave: (value) async {
|
||||
saved = value;
|
||||
},
|
||||
),
|
||||
child: const Text('Add server'),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.tap(find.text('Add server'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(const Key('discovery-scan')));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Linux laptop'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester
|
||||
.widget<TextField>(find.byKey(const Key('server-profile-name')))
|
||||
.controller!
|
||||
.text,
|
||||
'Linux laptop',
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<TextField>(
|
||||
find.byKey(const Key('server-profile-addresses')),
|
||||
)
|
||||
.controller!
|
||||
.text,
|
||||
'ws://192.168.1.5:8083/connect',
|
||||
);
|
||||
expect(saved, isNull);
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('server-profile-name')),
|
||||
'My Linux',
|
||||
);
|
||||
await tester.ensureVisible(find.text('Save server'));
|
||||
await tester.tap(find.text('Save server'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(saved!.name, 'My Linux');
|
||||
expect(saved!.endpoints, ['ws://192.168.1.5:8083/connect']);
|
||||
},
|
||||
);
|
||||
testWidgets('onboarding reviews and persists machine name before sign-in', (
|
||||
tester,
|
||||
) async {
|
||||
var checks = 0;
|
||||
String? alias;
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: OnboardingFlow(
|
||||
onDone: (_) {},
|
||||
discovery: discovery(),
|
||||
check: (_) async {
|
||||
checks++;
|
||||
return SessionState.signedOut;
|
||||
},
|
||||
saveServerProfile: (url, name) async {
|
||||
alias = name;
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.ensureVisible(find.byKey(const Key('discovery-scan')));
|
||||
await tester.tap(find.byKey(const Key('discovery-scan')));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.ensureVisible(find.text('Linux laptop'));
|
||||
await tester.tap(find.text('Linux laptop'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(checks, 0);
|
||||
expect(
|
||||
tester
|
||||
.widget<TextField>(find.byKey(const Key('onboarding-server-name')))
|
||||
.controller!
|
||||
.text,
|
||||
'Linux laptop',
|
||||
);
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('onboarding-server-name')),
|
||||
'Workstation',
|
||||
);
|
||||
await tester.ensureVisible(find.byKey(const Key('onboarding-next')));
|
||||
await tester.tap(find.byKey(const Key('onboarding-next')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(checks, 1);
|
||||
expect(alias, 'Workstation');
|
||||
expect(find.byKey(const Key('onboarding-password')), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user