Files
sync_replay_project/packages/server/test/lifecycle_test.dart
T
foxyzandClaude Opus 5.5 9f2586b7ff
ci / core (push) Successful in 28s
ci / server (push) Successful in 1m5s
ci / updater-windows (push) Successful in 5m48s
Restructure step 1: lasting identity, presence and protocol 16
Server and core: version ranges on both sides with a coded refusal; a
client id maps to the same member and token for the server's life; a
drop is `gone` for a 10-minute grace keeping ready, presence, queue
items, relay and avatar; `bye` leaves at once; ordered takeover (seq,
replaced/stale); presence joining/in/away/gone with one PresencePolicy;
joiners never pause playback and hold a held gate at most ~15 s;
state.hold; feed ids, seq and replay; the relay lives through its
source's grace and continues on re-dial; queue `by` lasting, auto
items and cancel; MediaRef carried; client ids never logged.

App: persisted client id and seq; Connection with unstable/lost/
replaced states, backoff with jitter, no lost/back pair per flap; a drop
keeps the session (state, player, media, relay source); presence in the
strip and members card; sit out = away intent; feed matched by id;
auto sit-out (idle when not playing, minimised for a while; never when
alone) replaces auto-unready; bye on close and Leave, not on update
restart; relay source re-dials through its grace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 21:39:47 +03:00

106 lines
3.5 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:sync_relay_core/core.dart';
import 'package:sync_relay_server/server.dart';
import 'package:test/test.dart';
class _ProbeClient {
final WebSocket ws;
final Completer<void> welcomed = Completer<void>();
final Completer<String> closed = Completer<String>();
_ProbeClient(this.ws) {
ws.listen(
(m) {
if (!welcomed.isCompleted) {
final decoded = jsonDecode('$m') as Map;
expect(decoded['t'], 'welcome');
welcomed.complete();
}
},
onDone: () {
if (!closed.isCompleted) {
closed.complete('done code=${ws.closeCode} reason=${ws.closeReason}');
}
},
onError: (Object e) {
if (!closed.isCompleted) closed.complete('error $e');
},
);
}
static Future<_ProbeClient> hello(int port, String name) async {
final ws = await WebSocket.connect('ws://127.0.0.1:$port/sync');
final c = _ProbeClient(ws);
ws.add(jsonEncode({
't': 'hello',
'proto': RelayConstants.protoVersion,
'secret': 'cycle',
'user': {'name': name},
'media': {'name': '', 'size': 0, 'duration': 0.0},
}));
await c.welcomed.future.timeout(const Duration(seconds: 5));
return c;
}
}
Future<void> _pollBool(
FutureOr<bool> Function() cond,
String what, {
Duration timeout = const Duration(seconds: 8),
}) async {
final deadline = DateTime.now().add(timeout);
while (DateTime.now().isBefore(deadline)) {
if (await cond()) return;
await Future.delayed(const Duration(milliseconds: 100));
}
fail('timed out: $what');
}
void main() {
group('presence lifecycle live', () {
late SyncServer server;
setUp(() async {
server = SyncServer(
const AppConfig(port: 0, secret: 'cycle', name: 'cycle'));
await server.start();
});
tearDown(() async {
await server.stop();
});
test('a clean close is not a leave: the member is gone at once, kept', () async {
final client = await _ProbeClient.hello(server.boundPort, 'alice');
await _pollBool(() => server.hub.engine.members.length == 1, 'join visible');
await client.ws.close();
await _pollBool(() => server.hub.engine.members.values.single.presence == Presence.gone, 'gone');
expect(server.session.tokens, hasLength(1), reason: 'its token works through the grace');
expect(server.hub.sockets, isEmpty);
});
test('bye leaves at once: member, token and socket go', () async {
final client = await _ProbeClient.hello(server.boundPort, 'alice');
await _pollBool(() => server.hub.engine.members.length == 1, 'join visible');
client.ws.add(jsonEncode(const ByeMessage().toJson()));
final outcome = await client.closed.future.timeout(const Duration(seconds: 5));
expect(outcome, contains('code=${CloseCodes.left}'));
expect(server.hub.engine.members, isEmpty);
expect(server.session.tokens, isEmpty);
expect(server.hub.sockets, isEmpty);
});
test('server stop closes client sockets', () async {
final client = await _ProbeClient.hello(server.boundPort, 'bob');
await _pollBool(
() => server.hub.engine.members.length == 1, 'join visible');
await server.stop();
final outcome = await client.closed.future
.timeout(const Duration(seconds: 5), onTimeout: () => 'TIMEOUT');
expect(outcome, isNot('TIMEOUT'), reason: 'client never saw close');
});
});
}