252 lines
9.2 KiB
Dart
252 lines
9.2 KiB
Dart
import 'package:pipelines_app/application/app.dart';
|
|
import 'package:pipelines_app/application/app_router.dart';
|
|
import 'package:pipelines_app/application/app_route.dart';
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:pipelines_app/application/pipelines_app.dart';
|
|
import 'package:pipelines_app/application/pipelines_root.dart';
|
|
import 'package:pipelines_app/application/server_endpoint.dart';
|
|
import 'package:pipelines_app/core/servers/server_catalog.dart';
|
|
import 'package:pipelines_app/core/servers/server_catalog_store.dart';
|
|
import 'package:pipelines_app/core/session/server_session.dart';
|
|
import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
|
import 'package:pipelines_app/features/settings/settings_store.dart';
|
|
import 'package:pipelines_app/features/settings/saved_servers_section.dart';
|
|
|
|
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
|
|
|
|
SavedServer saved(String id, List<String> endpoints) =>
|
|
SavedServer.validated(id: id, name: id, endpoints: endpoints);
|
|
|
|
Future<void> frames(WidgetTester tester) async {
|
|
for (var frame = 0; frame < 12; frame++) {
|
|
await tester.pump(const Duration(milliseconds: 20));
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
setUp(() async {
|
|
SharedPreferences.setMockInitialValues({
|
|
'pipelines.session.v2.home.cookie': 'pipelines_session=home',
|
|
'pipelines.session.v2.work.cookie': 'pipelines_session=work',
|
|
});
|
|
await ServerSession.load();
|
|
});
|
|
|
|
testWidgets(
|
|
'catalog reset to another saved URL cannot reuse original scoped login',
|
|
(tester) async {
|
|
final store = const SharedPreferencesServerCatalogStore();
|
|
final original = await store.load(
|
|
legacyEndpoint: 'wss://original.test/connect',
|
|
);
|
|
expect(await store.save(original), isTrue);
|
|
final preferences = await SharedPreferences.getInstance();
|
|
await preferences.setString(
|
|
'pipelines.session.cookie',
|
|
'pipelines_session=original',
|
|
);
|
|
await preferences.setString(
|
|
'pipelines.session.v2.legacy-server.cookie',
|
|
'pipelines_session=original',
|
|
);
|
|
await preferences.remove(serverCatalogKey);
|
|
await ServerSession.load();
|
|
await tester.pumpWidget(
|
|
PipelinesRoot(
|
|
nativeUpdatesSupported: false,
|
|
server: ServerEndpoint.resolve(
|
|
base: Uri.parse('file:///'),
|
|
web: false,
|
|
configured: 'wss://different.test/connect',
|
|
),
|
|
signedIn: true,
|
|
catalogStore: store,
|
|
settingsStore: MemorySettingsStore(),
|
|
check: (_) async => SessionState.signedOut,
|
|
clientFactory: () => throw StateError(
|
|
'Must not connect before signing in to the new server',
|
|
),
|
|
),
|
|
);
|
|
await frames(tester);
|
|
expect(find.byType(PipelinesApp), findsNothing);
|
|
expect(ServerSession.cookie, isNull);
|
|
expect(
|
|
ServerSession.headersFor(Uri.parse('wss://different.test/connect')),
|
|
isEmpty,
|
|
);
|
|
final recovered = await store.load();
|
|
expect(recovered.active?.id, isNot('legacy-server'));
|
|
expect(recovered.active?.legacyEndpoint, isNull);
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
},
|
|
);
|
|
|
|
testWidgets(
|
|
'switch closes old socket, resets app and persists selected profile',
|
|
(tester) async {
|
|
final home = saved('home', [
|
|
'wss://home.test/connect',
|
|
'ws://192.168.1.2:8083/connect',
|
|
]);
|
|
final work = saved('work', ['wss://work.test/connect']);
|
|
final store = MemoryServerCatalogStore(
|
|
catalog: ServerCatalog(servers: [home, work], selectedId: home.id),
|
|
);
|
|
final clients = <PipelinesClient>[];
|
|
final channels = <FakeChannel>[];
|
|
final attempted = <Uri>[];
|
|
PipelinesClient factory() {
|
|
final channel = FakeChannel()..readyCompleter.complete();
|
|
channels.add(channel);
|
|
final client = PipelinesClient(
|
|
channelFactory: (uri) {
|
|
attempted.add(uri);
|
|
return channel;
|
|
},
|
|
);
|
|
clients.add(client);
|
|
return client;
|
|
}
|
|
|
|
final server = ServerEndpoint.resolve(
|
|
base: Uri.parse('file:///'),
|
|
web: false,
|
|
configured: home.endpoints.first,
|
|
);
|
|
await tester.pumpWidget(
|
|
PipelinesRoot(
|
|
nativeUpdatesSupported: false,
|
|
server: server,
|
|
catalogStore: store,
|
|
clientFactory: factory,
|
|
settingsStore: MemorySettingsStore(),
|
|
check: (_) async => SessionState.signedIn,
|
|
),
|
|
);
|
|
await frames(tester);
|
|
final app = tester.widget<PipelinesApp>(find.byType(PipelinesApp));
|
|
expect(app.serverId, 'home');
|
|
expect(app.serverAddresses?.map((uri) => uri.toString()), home.endpoints);
|
|
final oldKey = app.key;
|
|
final oldClient = clients.single;
|
|
final request = expectLater(
|
|
oldClient.call('chat_prompt', const {
|
|
'chat_prompt': {'session_id': 'same-id', 'text': 'do not replay'},
|
|
}),
|
|
throwsA(isA<StateError>()),
|
|
);
|
|
final switched = app.servers!.select('work');
|
|
await frames(tester);
|
|
expect(await switched, isTrue);
|
|
await request;
|
|
final selected = tester.widget<PipelinesApp>(find.byType(PipelinesApp));
|
|
expect(selected.serverId, 'work');
|
|
expect(selected.key, isNot(oldKey));
|
|
expect(selected.resetRoute, isTrue);
|
|
expect(store.catalog?.selectedId, 'work');
|
|
expect(attempted.map((uri) => uri.host), ['home.test', 'work.test']);
|
|
expect(
|
|
ServerSession.headersFor(Uri.parse(home.endpoints.first)),
|
|
isEmpty,
|
|
);
|
|
expect(ServerSession.headersFor(Uri.parse(work.endpoints.first)), {
|
|
'Cookie': 'pipelines_session=work',
|
|
});
|
|
expect(
|
|
channels.last.outgoing
|
|
.map((frame) => jsonDecode(frame as String))
|
|
.where((frame) => frame['type'] == 'chat_prompt'),
|
|
isEmpty,
|
|
);
|
|
await expectLater(
|
|
oldClient.connect(Uri.parse(home.endpoints.first)),
|
|
throwsStateError,
|
|
);
|
|
// A create intent must survive the root replacing the app/socket.
|
|
final createSwitch = selected.servers!.requestNewChat('home');
|
|
await frames(tester);
|
|
expect(await createSwitch, isTrue);
|
|
final creating = tester.widget<PipelinesApp>(find.byType(PipelinesApp));
|
|
expect(creating.serverId, 'home');
|
|
final host = tester.widget<MyApp>(find.byType(MyApp));
|
|
final router = host.routerConfig!.routerDelegate as AppRouterDelegate;
|
|
expect(router.route.kind, AppRouteKind.chat);
|
|
// The new socket is connected; harness discovery belongs only to it.
|
|
expect(find.byKey(const Key('chat-create-confirm')), findsOneWidget);
|
|
expect(creating.servers!.newChatServerId, isNull);
|
|
final newRequests = channels.last.outgoing
|
|
.map((frame) => jsonDecode(frame as String))
|
|
.toList();
|
|
expect(
|
|
newRequests.where((frame) => frame['type'] == 'chat_create'),
|
|
isEmpty,
|
|
);
|
|
expect(
|
|
channels[1].outgoing
|
|
.map((frame) => jsonDecode(frame as String))
|
|
.where((frame) => frame['type'] == 'chat_backends'),
|
|
isEmpty,
|
|
);
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
},
|
|
);
|
|
|
|
testWidgets(
|
|
'removing selected server never selects another or revives legacy login',
|
|
(tester) async {
|
|
SharedPreferences.setMockInitialValues({
|
|
'pipelines.session.cookie': 'pipelines_session=old-home',
|
|
});
|
|
await ServerSession.load();
|
|
final home = saved('legacy-server', ['wss://old-home.test/connect']);
|
|
final store = MemoryServerCatalogStore(
|
|
catalog: ServerCatalog(servers: [home], selectedId: home.id),
|
|
);
|
|
await tester.pumpWidget(
|
|
PipelinesRoot(
|
|
nativeUpdatesSupported: false,
|
|
server: ServerEndpoint.resolve(
|
|
base: Uri.parse('file:///'),
|
|
web: false,
|
|
configured: home.endpoints.first,
|
|
),
|
|
catalogStore: store,
|
|
settingsStore: MemorySettingsStore(),
|
|
check: (_) async => SessionState.signedIn,
|
|
clientFactory: () => PipelinesClient(
|
|
channelFactory: (_) => FakeChannel()..readyCompleter.complete(),
|
|
),
|
|
),
|
|
);
|
|
await frames(tester);
|
|
final connections = tester
|
|
.widget<PipelinesApp>(find.byType(PipelinesApp))
|
|
.servers!;
|
|
final removed = connections.remove(home.id);
|
|
await frames(tester);
|
|
expect(await removed, isTrue);
|
|
expect(connections.catalog.active, isNull);
|
|
expect(find.byType(PipelinesApp), findsNothing);
|
|
expect(find.byType(SavedServersSection), findsOneWidget);
|
|
final added = connections.save(
|
|
saved('new-work', ['wss://new-work.test/connect']),
|
|
);
|
|
await frames(tester);
|
|
expect(await added, isTrue);
|
|
expect(ServerSession.cookie, isNull);
|
|
expect(
|
|
ServerSession.headersFor(Uri.parse('wss://new-work.test/connect')),
|
|
isEmpty,
|
|
);
|
|
expect(find.byType(PipelinesApp), findsNothing);
|
|
expect(find.text('wss://new-work.test/connect'), findsWidgets);
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
},
|
|
);
|
|
}
|