Servers: name a machine after its hostname once it connects

A saved server still named by the app ('My server', 'This server',
'Original legacy server' or its address host/IP) takes the hostname the
machine reports in get_status the first time the app connects; typed
names are kept. New servers (legacy migration, Settings with a blank
name, onboarding) start with the address host. A browser on its page
origin with no saved profile shows the reported hostname too. No server
change: get_status already reports the hostname.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 15:58:28 +00:00
parent 3e817a189b
commit c264f865ff
16 changed files with 654 additions and 24 deletions
@@ -12,11 +12,22 @@ import 'package:pipelines_app/features/settings/server_connections.dart';
class _Gateway implements ApiGateway {
final events = StreamController<JsonMap>.broadcast();
String? hostname;
final calls = <String>[];
@override
Stream<JsonMap> get frames => events.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async =>
type == 'attention_acks' || type == 'ack_attention'
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
calls.add(type);
return _reply(type);
}
JsonMap _reply(String type) => type == 'get_status' && hostname != null
? {
'type': 'status',
'status': {'hostname': hostname},
}
: type == 'attention_acks' || type == 'ack_attention'
? {
'type': 'attention_acks',
'attention_acks': {'chat_seen': {}, 'dismissed': []},
@@ -82,6 +93,52 @@ void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() => SharedPreferences.setMockInitialValues({}));
test('a connected machine named by its address takes its hostname', () async {
final placeholder = SavedServer.validated(
id: 'one',
name: 'one.test',
endpoints: ['ws://one.test:8083/connect'],
);
final typed = SavedServer.validated(
id: 'two',
name: 'Laptop',
endpoints: ['ws://two.test:8083/connect'],
);
final servers = await _servers([placeholder, typed]);
final machines = <_Machine>[];
final pool = ChatServerPool(
servers,
web: false,
connect: (server) async {
final machine = _Machine(server)
..gateway.hostname = server.id == 'one' ? 'tank' : 'mac-mini';
machines.add(machine);
return machine.connection;
},
);
try {
await _flush();
expect(servers.catalog.servers.map((s) => s.name), ['tank', 'Laptop']);
expect(pool.machines.first.adapter!.server.name, 'tank');
// Asked once per machine, not on every reconnect.
machines.first.states.add(
const ConnectionState(ConnectionKind.connected),
);
await _flush();
expect(
machines.first.gateway.calls.where((type) => type == 'get_status'),
hasLength(1),
);
} finally {
pool.dispose();
servers.dispose();
await _flush();
for (final machine in machines) {
await machine.finish();
}
}
});
test('an older async detach cannot close a newer connection', () async {
final profile = _server('one');
final servers = await _servers([profile]);
@@ -178,7 +178,7 @@ void main() {
});
final migrated = await store.load(legacyEndpoint: legacy);
expect(migrated.selectedId, 'legacy-server');
expect(migrated.active!.name, 'My server');
expect(migrated.active!.name, 'legacy.test');
expect(migrated.active!.endpoints, ['ws://legacy.test/connect']);
final preferences = await SharedPreferences.getInstance();
expect(preferences.containsKey(serverCatalogKey), isFalse);
@@ -0,0 +1,83 @@
import 'package:flutter_test/flutter_test.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/servers/server_naming.dart';
SavedServer named(String name, [String url = 'ws://192.168.1.20:8083/ws']) =>
SavedServer.validated(id: 'a', name: name, endpoints: [url]);
void main() {
test('app-chosen names are placeholders; typed names are not', () {
for (final name in [
'My server',
'This server',
'Original legacy server',
'192.168.1.20',
]) {
expect(hasPlaceholderName(named(name)), isTrue, reason: name);
}
expect(
hasPlaceholderName(named('Tank.local', 'wss://tank.local/connect')),
isTrue,
);
// A backup address's host is also a name the app could have chosen.
expect(
hasPlaceholderName(
SavedServer.validated(
id: 'a',
name: '100.64.0.2',
endpoints: ['ws://lan.test/ws', 'ws://100.64.0.2:8083/ws'],
),
),
isTrue,
);
for (final name in ['Home', 'my server', 'tank', '192.168.1.2']) {
expect(hasPlaceholderName(named(name)), isFalse, reason: name);
}
});
test('renames only placeholders, only to a nonempty new hostname', () {
final renamed = renamedToHostname(
SavedServer.validated(
id: 'legacy-server',
name: 'My server',
endpoints: ['ws://192.168.1.20:8083/ws'],
legacyEndpoint: 'ws://192.168.1.20:8083/ws',
),
' tank ',
)!;
expect(renamed.name, 'tank');
expect(renamed.id, 'legacy-server');
expect(renamed.endpoints, ['ws://192.168.1.20:8083/ws']);
expect(renamed.legacyEndpoint, 'ws://192.168.1.20:8083/ws');
expect(renamedToHostname(named('Home'), 'tank'), isNull);
expect(renamedToHostname(named('My server'), ' '), isNull);
expect(renamedToHostname(named('192.168.1.20'), '192.168.1.20'), isNull);
// The hostname is kept as the server reports it, domain included.
expect(
renamedToHostname(named('This server'), 'tank.lan')!.name,
'tank.lan',
);
});
test('reads the hostname from a get_status reply', () async {
expect(
hostnameFromStatus({
'type': 'status',
'status': {'hostname': 'tank\n'},
}),
'tank',
);
expect(hostnameFromStatus({'type': 'error', 'error': 'nope'}), '');
expect(hostnameFromStatus({'type': 'status', 'status': {}}), '');
});
test('a migrated legacy server is named by its address host', () async {
final catalog = await MemoryServerCatalogStore().load(
legacyEndpoint: 'ws://192.168.1.20:8083/ws',
);
expect(catalog.active!.name, '192.168.1.20');
expect(hasPlaceholderName(catalog.active!), isTrue);
expect(temporaryServerName('wss://tank.example/connect'), 'tank.example');
});
}