Files
pipelines/app/test/features/settings/settings_controller_test.dart
T

493 lines
16 KiB
Dart

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart'
as transport;
import 'package:pipelines_app/features/settings/settings_controller.dart';
import 'package:pipelines_app/features/settings/settings_copy.dart';
import 'package:pipelines_app/features/settings/settings_store.dart';
import 'fake_channel.dart';
void main() {
test('initializes without opening a connection', () async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
addTearDown(client.dispose);
final controller = SettingsController(
client: client,
store: MemorySettingsStore(
serverUrl: 'ws://saved.test/connect',
workspace: '/srv/pipelines',
),
);
addTearDown(controller.dispose);
await controller.initialize();
await settle();
expect(controller.serverUrl, 'ws://saved.test/connect');
expect(controller.workspaceDraft, '/srv/pipelines');
expect(controller.savedWorkspace, '/srv/pipelines');
expect(factory.channels, isEmpty);
expect(controller.connectionLines, <String>['Not connected']);
});
test(
'an initial endpoint seeds the draft but is not persisted by itself',
() async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
addTearDown(client.dispose);
final store = MemorySettingsStore(serverUrl: 'ws://saved.test/connect');
final controller = SettingsController(
client: client,
store: store,
initialEndpoint: Uri.parse('ws://initial.test:9000'),
);
addTearDown(controller.dispose);
await controller.initialize();
expect(controller.serverUrl, 'ws://initial.test:9000');
expect(store.serverUrl, 'ws://saved.test/connect');
},
);
test('a native/default endpoint seeds an empty store', () async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
addTearDown(client.dispose);
final controller = SettingsController(
client: client,
store: MemorySettingsStore(),
defaultEndpoint: 'ws://page.test/connect',
);
addTearDown(controller.dispose);
await controller.initialize();
expect(controller.serverUrl, 'ws://page.test/connect');
});
test('an invalid URL names the field error and opens no socket', () async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
addTearDown(client.dispose);
final store = MemorySettingsStore();
final controller = SettingsController(client: client, store: store);
addTearDown(controller.dispose);
await controller.initialize();
controller.updateServerUrl('http://example.test');
await controller.connect();
await settle();
expect(controller.urlError, contains('Unsupported scheme'));
expect(factory.channels, isEmpty);
expect(store.serverUrl, '');
});
test('a URL with credentials is rejected before it is persisted', () async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
addTearDown(client.dispose);
final store = MemorySettingsStore();
final controller = SettingsController(client: client, store: store);
addTearDown(controller.dispose);
await controller.initialize();
controller.updateServerUrl('ws://operator:secret@example.test/connect');
await controller.connect();
await settle();
expect(controller.urlError, contains('Credentials are not supported'));
expect(factory.channels, isEmpty);
expect(store.serverUrl, '');
});
test(
'connect validates, normalizes, persists and pings exactly once',
() async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final store = MemorySettingsStore();
final controller = SettingsController(client: client, store: store);
await controller.initialize();
controller.updateServerUrl(' ws://example.test:8123 ');
final connecting = controller.connect();
await settle();
expect(factory.uris.single.toString(), 'ws://example.test:8123/connect');
expect(controller.serverUrl, 'ws://example.test:8123/connect');
expect(store.serverUrl, 'ws://example.test:8123/connect');
factory.last.markReady();
await connecting;
await settle();
final outgoing = factory.last.decodedOutgoing;
expect(outgoing, hasLength(1));
expect(outgoing.single['type'], 'ping');
expect(controller.connection.kind, transport.ConnectionKind.connected);
factory.last.reply(<String, dynamic>{
'id': outgoing.single['id'],
'type': 'pong',
'build': <String, dynamic>{'version': 'v9', 'commit': 'deadbee'},
'time': '2026-10-21T12:00:00Z',
});
await settle();
expect(controller.buildVersion, 'v9');
expect(controller.buildCommit, 'deadbee');
expect(controller.serverTime, '2026-10-21T12:00:00Z');
expect(controller.connectionLines, <String>[
'Connected',
'Server build v9 (deadbee)',
'Server time 2026-10-21T12:00:00Z',
]);
// Rebuilding or notifying must not send another ping for this connection.
await settle();
expect(factory.last.decodedOutgoing, hasLength(1));
controller.dispose();
await client.dispose();
},
);
test(
'the card is pending until the ping answers, then names missing values',
() async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final reply = Completer<Map<String, dynamic>>();
final controller = SettingsController(
client: client,
store: MemorySettingsStore(),
ping: () => reply.future,
);
await controller.initialize();
controller.updateServerUrl('ws://example.test:8123');
final connecting = controller.connect();
await settle();
factory.last.markReady();
await connecting;
await settle();
expect(controller.pingInFlight, isTrue);
expect(controller.connectionLines, <String>[
'Connected',
serverDetailsPending,
]);
reply.complete(<String, dynamic>{'type': 'pong'});
await settle();
expect(controller.pingInFlight, isFalse);
expect(controller.connectionLines, <String>[
'Connected',
'Server build unavailable',
'Server time unavailable',
]);
controller.dispose();
await client.dispose();
},
);
test(
'a ping failure is one explicit line, not two unavailable values',
() async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final controller = SettingsController(
client: client,
store: MemorySettingsStore(),
ping: () async => throw StateError('probe failed'),
);
await controller.initialize();
controller.updateServerUrl('ws://example.test:8123');
final connecting = controller.connect();
await settle();
factory.last.markReady();
await connecting;
await settle();
expect(controller.pingError, startsWith('Server details unavailable:'));
expect(controller.pingError, contains('probe failed'));
expect(controller.pingInFlight, isFalse);
expect(controller.connectionLines, <Object>[
'Connected',
startsWith('Server details unavailable:'),
]);
expect(controller.connection.kind, transport.ConnectionKind.connected);
controller.dispose();
await client.dispose();
},
);
test(
'disconnect clears server details and leaves the shared client alive',
() async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final controller = SettingsController(
client: client,
store: MemorySettingsStore(),
);
await controller.initialize();
controller.updateServerUrl('ws://example.test:8123');
final connecting = controller.connect();
await settle();
factory.last.markReady();
await connecting;
await settle();
factory.last.reply(<String, dynamic>{
'id': factory.last.decodedOutgoing.single['id'],
'type': 'pong',
'build': <String, dynamic>{'version': 'v9'},
'time': '2026-10-21T12:00:00Z',
});
await settle();
expect(controller.buildVersion, 'v9');
await controller.disconnect();
await settle();
expect(controller.connection.kind, transport.ConnectionKind.idle);
expect(controller.buildVersion, '');
expect(controller.serverTime, '');
expect(controller.connectionLines, <String>['Not connected']);
controller.dispose();
await client.dispose();
},
);
test('reconnect opens one new connection after a disconnect', () async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final controller = SettingsController(
client: client,
store: MemorySettingsStore(),
);
await controller.initialize();
controller.updateServerUrl('ws://example.test:8123');
final connecting = controller.connect();
await settle();
factory.last.markReady();
await connecting;
await controller.disconnect();
await settle();
final reconnecting = controller.reconnect();
await settle();
expect(factory.channels, hasLength(2));
factory.last.markReady();
await reconnecting;
await settle();
expect(controller.connection.kind, transport.ConnectionKind.connected);
controller.dispose();
await client.dispose();
});
test(
'disposing the screen does not dispose or disconnect the shared client',
() async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final controller = SettingsController(
client: client,
store: MemorySettingsStore(),
);
await controller.initialize();
controller.updateServerUrl('ws://example.test:8123');
final connecting = controller.connect();
await settle();
factory.last.markReady();
await connecting;
await settle();
controller.dispose();
await settle();
expect(
client.state.kind,
transport.ConnectionKind.connected,
reason: 'the feature must not disconnect a client it does not own',
);
await client.dispose();
},
);
test('opening while already connected pings that connection once', () async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final connecting = client.connect(Uri.parse('ws://example.test:8123'));
await settle();
factory.last.markReady();
await connecting;
await settle();
expect(factory.last.outgoing, isEmpty);
final controller = SettingsController(
client: client,
store: MemorySettingsStore(),
);
await controller.initialize();
await settle();
expect(factory.last.decodedOutgoing, hasLength(1));
expect(factory.last.decodedOutgoing.single['type'], 'ping');
controller.dispose();
await client.dispose();
});
test(
'a failed endpoint write still connects but says it is session-only',
() async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final store = MemorySettingsStore()..failSaves = true;
final controller = SettingsController(client: client, store: store);
await controller.initialize();
controller.updateServerUrl('ws://example.test:8123');
final connecting = controller.connect();
await settle();
expect(factory.channels, hasLength(1));
expect(store.serverUrl, '');
expect(controller.endpointSaveError, isNotEmpty);
factory.last.markReady();
await connecting;
await settle();
expect(controller.connection.kind, transport.ConnectionKind.connected);
controller.dispose();
await client.dispose();
},
);
test('a dropped connection exposes the reason and retry backoff', () async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final controller = SettingsController(
client: client,
store: MemorySettingsStore(),
);
await controller.initialize();
controller.updateServerUrl('ws://example.test:8123');
final connecting = controller.connect();
await settle();
factory.last.markReady();
await connecting;
await settle();
factory.last.fail('socket gone');
await settle();
expect(controller.connection.kind, transport.ConnectionKind.disconnected);
expect(controller.connection.reason, contains('socket gone'));
expect(controller.connection.retrying, isTrue);
expect(
controller.connectionLines,
containsAll(<String>['Disconnected', 'Retrying with backoff\u2026']),
);
// Cancel the client's scheduled retry before the test ends.
await client.disconnect();
controller.dispose();
await client.dispose();
});
test('a mismatched protocol warns and the warning survives a drop', () async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final controller = SettingsController(
client: client,
store: MemorySettingsStore(),
);
await controller.initialize();
controller.updateServerUrl('ws://example.test:8123');
final connecting = controller.connect();
await settle();
factory.last.markReady();
await connecting;
await settle();
expect(controller.protocolWarning, isNull);
factory.last.reply(<String, dynamic>{
'id': factory.last.decodedOutgoing.single['id'],
'type': 'pong',
// The server sends an integer, but the comparison is numeric, like the
// web page's Number() rule.
'protocol': '2',
});
await settle();
expect(
controller.protocolWarning,
'Protocol mismatch: this page speaks protocol 1, '
'the server reports 2.',
);
factory.last.fail('socket gone');
await settle();
expect(controller.connection.kind, transport.ConnectionKind.disconnected);
expect(
controller.protocolWarning,
isNotNull,
reason: 'a known mismatch stays visible while the socket reconnects',
);
// Cancel the client's scheduled retry before the test ends.
await client.disconnect();
controller.dispose();
await client.dispose();
});
test('protocolMismatchWarning ignores matching and absent protocols', () {
expect(protocolMismatchWarning(null), isNull);
expect(protocolMismatchWarning(1), isNull);
expect(protocolMismatchWarning(1.0), isNull);
expect(protocolMismatchWarning(2), contains('the server reports 2'));
});
test(
'workspace save persists only an absolute path and reports failures',
() async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
final store = MemorySettingsStore();
final controller = SettingsController(client: client, store: store);
await controller.initialize();
controller.updateWorkspace('relative/path');
expect(controller.workspaceError, workspacePathError);
await controller.saveWorkspace();
expect(store.workspace, '');
expect(controller.savedWorkspace, '');
controller.updateWorkspace(' /srv/pipelines ');
await controller.saveWorkspace();
expect(store.workspace, '/srv/pipelines');
expect(controller.savedWorkspace, '/srv/pipelines');
expect(controller.workspaceDraft, '/srv/pipelines');
store.failSaves = true;
controller.updateWorkspace('/other/path');
await controller.saveWorkspace();
expect(controller.savedWorkspace, '/srv/pipelines');
expect(controller.workspaceSaveError, isNotEmpty);
controller.dispose();
await client.dispose();
},
);
}