Files
sync_replay_project/apps/desktop/test/clock_offset_test.dart
T
foxyzandClaude Opus 5.5 51c1c5ab78
ci / core (push) Successful in 31s
ci / server (push) Successful in 1m3s
ci / updater-windows (push) Successful in 6m6s
Restructure step R: the app is Starsync (dev.foxyz.starsync)
- Linux app id dev.foxyz.starsync, binary starsync; Windows starsync.exe,
  company foxyz; server starsync-server; Dart package starsync; archives
  starsync-<ver>-*.
- 1.2.x compatibility: watch_together and watch-together-server
  launchers (Linux) / copies (Windows) in the archive; the updater
  accepts both names and relaunches the new binary; update.sh/.bat use
  whichever server exists.
- First start copies (never moves) the old support folder and this
  app's Hive boxes out of ~/Documents into <support>/hive; a marker
  makes it run once; AppDirs is a port; settings schema version with
  ordered migrations.
- Launcher entry dev.foxyz.starsync.desktop; the old one removed.
- Repository name and URLs unchanged.

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

53 lines
2.0 KiB
Dart

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:sync_relay_core/core.dart';
import 'package:sync_relay_server/server.dart';
import 'package:starsync/src/sync/state_diff.dart';
import 'package:starsync/src/sync/sync_client.dart';
Future<void> _poll(FutureOr<bool> Function() cond, String what) async {
final end = DateTime.now().add(const Duration(seconds: 10));
while (DateTime.now().isBefore(end)) {
if (await cond()) return;
await Future.delayed(const Duration(milliseconds: 50));
}
fail('timed out waiting for $what');
}
void main() {
test('a client whose clock is 3 s off still extrapolates the server position',
() async {
final server = SyncServer(const AppConfig(port: 0, secret: '', name: 'clk'));
await server.start();
const skew = Duration(seconds: 3);
final client = SyncClient(clock: () => DateTime.now().toUtc().add(skew));
try {
await client.connect(
wsUri: Uri.parse('ws://127.0.0.1:${server.boundPort}/sync'),
secret: '',
name: 'skewed');
client.sendReady(true);
client.sendReport(position: 0, buffered: 10, eventId: 0);
await _poll(() => client.lastState?.transport == 'playing', 'playing');
for (var i = 0; i < 5; i++) {
client.sendPing();
await Future.delayed(const Duration(milliseconds: 50));
}
await _poll(() => client.clockSync.hasSample, 'a pong');
expect(client.clockSync.offsetMs, closeTo(-skew.inMilliseconds, 50));
final st = client.lastState!;
final truth = server.hub.engine.positionAt();
// The bug: extrapolating with the local clock is off by the skew.
final naive = serverPositionAt(st, DateTime.now().toUtc().add(skew));
expect(naive - truth, closeTo(3, 0.1));
// The fix: extrapolate on the server's clock.
final corrected = serverPositionAt(st, client.serverNow());
expect(corrected, closeTo(truth, 0.1));
} finally {
await client.dispose();
await server.stop();
}
});
}