Files
sync_replay_project/apps/desktop/test/shell_test.dart
T
foxyzandClaude Opus 5.5 629be0d9ed
ci / core (push) Successful in 28s
ci / server (push) Successful in 1m11s
ci / updater-windows (push) Failing after 6m16s
Restructure step 2: Status as one computed level, the Notifier's real policy, system notifications
- Status: computed from connection, presence, hold, relay, player facts;
  no setters (health writes and HealthStatus gone); pill, strip and
  source line read it; transitions go to the Notifier.
- Notifier: noticeFor(event, context) with self presence, alone, window
  activity, playing and prefs; toasts for joins/leaves/returns/swaps/
  Up next adds (with picture)/problems/chat, the rest logged; while
  sitting out only chat, nudges, starting, Up next (+ joins if chosen);
  alone no social notices; replayed lines logged; owns its toast timer.
- System notifications (flutter_local_notifications: D-Bus on Linux,
  WinRT on Windows) when the window isn't focused; a click focuses it.
- Toasts sit above the control bar and move with it.
- Versions: each member's in the members card and avatar tooltip,
  the server's in the card header; the strip no longer shows it.

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

493 lines
22 KiB
Dart

// restructure: rewrite in step 3 (Your server): the solo strip and Invite
// friends.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:media_kit_video/media_kit_video.dart' show VideoState;
import 'package:sync_relay_core/core.dart';
import 'package:starsync/src/controls/frame.dart' show ControlsFrame, ControlsFrameState;
import 'package:starsync/src/controls/model/controls_prefs.dart';
import 'package:starsync/src/controls/session_controls.dart' show controlsBottomHeight;
import 'package:starsync/src/session/notifier.dart' show Copied;
import 'package:starsync/src/session/identity.dart';
import 'package:starsync/src/shell/app_shell.dart';
import 'package:starsync/src/shell/avatar.dart';
import 'package:starsync/src/shell/motion.dart';
import 'package:starsync/src/shell/party_strip.dart';
import 'package:starsync/src/shell/profile_card.dart';
import 'package:starsync/src/shell/session_player.dart';
import 'package:starsync/src/shell/settings_tab.dart' show SettingsTab;
import 'package:starsync/src/shell/toasts.dart';
import 'package:starsync/src/session/session_coordinator.dart' show ToastItem;
import 'package:starsync/src/state/session_state.dart';
import 'package:starsync/src/state/settings.dart';
import 'package:starsync/src/update/update_controller.dart';
/// Stands in for media_kit's `Video` (which hangs flutter_test) and
/// counts how often one is created.
class FakeVideo extends StatefulWidget {
static int created = 0;
const FakeVideo({super.key});
@override
State<FakeVideo> createState() => _FakeVideoState();
}
class _FakeVideoState extends State<FakeVideo> {
@override
void initState() {
super.initState();
FakeVideo.created++;
}
@override
Widget build(BuildContext context) => const ColoredBox(color: Colors.black);
}
MemberView member(String id, String name, {bool ready = false, bool gone = false, String app = ''}) =>
MemberView(
id: id,
name: name,
ready: ready,
inSet: ready,
buffered: 30,
app: app,
presence: gone ? Presence.gone : Presence.watching);
void main() {
final session = SessionState.instance;
final coord = session.coordinator;
void online(List<MemberView> members, {bool media = true}) {
coord.serverState.value = StateBroadcast(
eventId: 1,
transport: 'held',
position: 0,
serverTime: DateTime.now().toUtc(),
mediaId: media ? 1 : 0,
media: media ? {'name': 'Movie.mkv', 'size': 1, 'duration': 0} : null,
members: members,
);
session.route.value = AppRoute.session;
}
setUp(() {
FakeVideo.created = 0;
session.recents.value = const [];
});
tearDown(() {
session.route.value = AppRoute.home;
coord.serverState.value = null;
coord.playback.alive.value = false;
session.notifier.clear();
});
Future<void> pumpShell(WidgetTester tester, Size size, {UpdateController? updates, VideoViewBuilder? video}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(MaterialApp(
theme: ThemeData.dark(),
home: AppShell(
updates: updates ?? UpdateController(currentVersion: '0.0.0-dev'),
autoUpdateCheck: false,
windowControls: false,
videoBuilder: video ?? (context, controls) => const FakeVideo(),
// A real probe (network, yt-dlp) never answers on the test clock.
linkProbe: (url) async => LinkProbe.file(Uri.parse(url), 1000),
),
));
await tester.pumpAndSettle();
}
final party = [
member('a', 'Alex', ready: true),
member('b', 'Bob'),
member('c', 'Cleo', ready: true),
member('d', 'Dan'),
member('e', 'Eve', gone: true),
member('f', 'Finn'),
];
group('version and update bar', () {
testWidgets('the version at the bottom of Settings, not in the strip; the bar on the tabs, off the full player '
'until the video ends', (tester) async {
final updates = UpdateController(currentVersion: '0.0.0-dev');
await pumpShell(tester, const Size(1280, 800), updates: updates);
expect(find.byKey(const ValueKey('strip-version')), findsNothing);
expect(find.textContaining('dev run'), findsNothing);
await tester.tap(find.byKey(const ValueKey('tab-settings')));
await tester.pumpAndSettle();
expect(
tester.widget<SelectableText>(find.byKey(const ValueKey('settings-version'))).data, contains('dev run'));
await tester.tap(find.byKey(const ValueKey('tab-library')));
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('update-bar')), findsNothing);
updates.status.value = const UpdateStatus(UpdatePhase.available,
manifest: UpdateManifest(version: '9.9.9', protocol: 1, url: 'http://x/a.tgz', notes: ''),
canInstall: true);
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('update-bar')), findsOneWidget);
online(party);
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('update-bar')), findsNothing);
final r = coord.serverState.value!;
coord.serverState.value = StateBroadcast(
eventId: 2,
transport: 'finished',
position: 0,
serverTime: r.serverTime,
mediaId: r.mediaId,
media: r.media,
members: r.members,
);
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('update-bar')), findsOneWidget);
});
});
group('party strip', () {
testWidgets('offline: Join and Host, no party', (tester) async {
await pumpShell(tester, const Size(1280, 800));
expect(find.byKey(const ValueKey('strip-join')), findsOneWidget);
expect(find.byKey(const ValueKey('strip-host')), findsOneWidget);
expect(find.text('Offline'), findsOneWidget);
expect(find.byKey(const ValueKey('party-avatars')), findsNothing);
expect(find.byKey(const ValueKey('strip-leave')), findsNothing);
expect(tester.getSize(find.byType(PartyStrip)).height, partyStripHeight);
});
testWidgets('online: avatars, server and readiness, chat, Leave', (tester) async {
await pumpShell(tester, const Size(1280, 800));
online(party, media: false);
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('strip-join')), findsNothing);
expect(find.byKey(const ValueKey('strip-leave')), findsOneWidget);
expect(find.byKey(const ValueKey('strip-chat')), findsOneWidget);
expect(find.text('ready 2/5'), findsOneWidget, reason: 'Eve is gone: not counted');
final stack = find.byKey(const ValueKey('party-avatars'));
// Five shown, the sixth as "+1"; ready ones first.
expect(find.descendant(of: stack, matching: find.byType(StatusAvatar)), findsNWidgets(5));
expect(find.descendant(of: stack, matching: find.text('+1')), findsOneWidget);
final alex = tester.getTopLeft(find.descendant(of: stack, matching: find.byTooltip('Alex')));
final bob = tester.getTopLeft(find.descendant(of: stack, matching: find.byTooltip('Bob')));
expect(alex.dx, lessThan(bob.dx));
// Leave asks first.
await tester.tap(find.byKey(const ValueKey('strip-leave')));
await tester.pumpAndSettle();
expect(find.text('Leave server?'), findsOneWidget);
await tester.tap(find.text('Cancel'));
await tester.pumpAndSettle();
expect(session.route.value, AppRoute.session);
});
testWidgets('solo: just you, Invite friends opens the host card, Stop asks nothing', (tester) async {
await pumpShell(tester, const Size(1280, 800));
session.solo.value = true;
addTearDown(() => session.solo.value = false);
online([member('me', 'Me')], media: false);
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('solo-chip')), findsOneWidget);
expect(find.byKey(const ValueKey('party-avatars')), findsNothing);
expect(find.byKey(const ValueKey('strip-chat')), findsNothing);
expect(find.byKey(const ValueKey('welcome-band')), findsNothing);
await tester.tap(find.byKey(const ValueKey('strip-invite')));
await tester.pumpAndSettle();
expect(find.text('Invite friends'), findsWidgets);
expect(find.text('Start hosting'), findsOneWidget);
await tester.tapAt(const Offset(5, 790));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const ValueKey('strip-leave')));
await tester.pumpAndSettle();
expect(find.text('Leave server?'), findsNothing);
expect(session.route.value, AppRoute.home);
expect(session.solo.value, isFalse);
});
testWidgets('compact below 1100 px: 3 avatars, no server text', (tester) async {
await pumpShell(tester, const Size(1000, 800));
online(party, media: false);
await tester.pumpAndSettle();
final stack = find.byKey(const ValueKey('party-avatars'));
expect(find.descendant(of: stack, matching: find.byType(StatusAvatar)), findsNWidgets(3));
expect(find.descendant(of: stack, matching: find.text('+3')), findsOneWidget);
expect(find.byKey(const ValueKey('server-label')), findsNothing);
expect(tester.takeException(), isNull);
});
});
group('popovers', () {
testWidgets('members card opens from the avatars and closes outside', (tester) async {
await pumpShell(tester, const Size(1280, 800));
session.serverVersion.value = '1.3.0-dev.1';
addTearDown(() => session.serverVersion.value = '');
online([member('a', 'Alex', ready: true, app: '1.3.0-dev.1'), ...party.skip(1)], media: false);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const ValueKey('avatar-stack')));
await tester.pumpAndSettle();
final card = find.byKey(const ValueKey('members-card'));
expect(card, findsOneWidget);
for (final m in party) {
expect(find.descendant(of: card, matching: find.text(m.name)), findsOneWidget);
}
expect(find.descendant(of: card, matching: find.text('reconnecting…')), findsOneWidget);
// Each member's version in its row, the server's in the header.
expect(find.descendant(of: card, matching: find.byKey(const ValueKey('version-a'))), findsOneWidget);
expect(find.descendant(of: card, matching: find.text('1.3.0-dev.1')), findsOneWidget);
expect(find.descendant(of: card, matching: find.text('Server 1.3.0-dev.1')), findsOneWidget);
// Someone else: nudge or sit them out (no kicking).
await tester.tap(find.descendant(of: find.byKey(const ValueKey('member-b')), matching: find.byTooltip('More')));
await tester.pumpAndSettle();
expect(find.text('Nudge'), findsOneWidget);
expect(find.text('Sit out'), findsOneWidget);
expect(find.text('Kick'), findsNothing);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
await tester.tapAt(const Offset(40, 700));
await tester.pumpAndSettle();
expect(card, findsNothing);
});
testWidgets('profile card opens from your avatar; its links open Settings', (tester) async {
await pumpShell(tester, const Size(1280, 800));
await tester.tap(find.byKey(const ValueKey('strip-profile')));
await tester.pumpAndSettle();
expect(find.byType(ProfileCard), findsOneWidget);
expect(find.text('offline'), findsOneWidget);
await tester.tap(find.text('About'));
await tester.pumpAndSettle();
expect(find.byType(ProfileCard), findsNothing);
expect(find.text('Dev build — update checks are off'), findsOneWidget);
});
testWidgets('showPopover fades and scales in, Esc closes it', (tester) async {
await tester.pumpWidget(MaterialApp(
home: Builder(
builder: (context) => Center(
child: TextButton(
onPressed: () => showPopover<void>(context, at: Alignment.topRight, child: const Text('card')),
child: const Text('open'),
),
),
),
));
await tester.tap(find.text('open'));
await tester.pump();
await tester.pump(easeTime ~/ 2);
final fade = tester.widget<FadeTransition>(
find.ancestor(of: find.text('card'), matching: find.byType(FadeTransition)).first);
expect(fade.opacity.value, inExclusiveRange(0.0, 1.0));
await tester.pumpAndSettle();
expect(find.text('card'), findsOneWidget);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.text('card'), findsNothing);
});
});
testWidgets('Ctrl+V outside a field: a link asks, then fills Library → Link', (tester) async {
await pumpShell(tester, const Size(1280, 800));
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, (call) async {
if (call.method == 'Clipboard.getData') return {'text': 'https://cdn.example/b.mkv'};
return null;
});
addTearDown(() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, null));
// fox_utils' clipboard files: none.
const pasteboard = MethodChannel('pasteboard');
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(pasteboard, (call) async => const <String>[]);
addTearDown(() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(pasteboard, null));
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyV);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('handoff-card')), findsOneWidget);
expect(find.text('Open in Link'), findsOneWidget); // offline
await tester.tap(find.byKey(const ValueKey('handoff-openLink')));
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('handoff-card')), findsNothing);
final field = tester.widget<TextField>(find.byKey(const ValueKey('link-field')));
expect(field.controller!.text, 'https://cdn.example/b.mkv');
expect(tester.takeException(), isNull);
});
testWidgets('profile rename goes to the server through the hook', (tester) async {
final pushed = <String?>[];
final p = Profile.instance;
final before = p.push;
p.push = ({String? name, String? avatar}) => pushed.add(name);
addTearDown(() => p.push = before);
session.lastForm.name = 'Old';
p.name.value = 'Old';
await tester.pumpWidget(MaterialApp(
home: Scaffold(body: Center(child: ProfileCard(onOpenSettings: (_) {}))),
));
await tester.enterText(find.byKey(const ValueKey('profile-name')), 'Neo');
await tester.pump();
expect(p.name.value, 'Neo');
expect(session.lastForm.name, 'Neo');
// Typing settles before it is sent, once.
expect(pushed, isEmpty);
await tester.pump(const Duration(milliseconds: 700));
expect(pushed, ['Neo']);
});
testWidgets('player: mini and full share one video, never two', (tester) async {
coord.playback.alive.value = true;
await pumpShell(tester, const Size(1280, 800));
online(party);
await tester.pumpAndSettle();
final videos = find.byType(FakeVideo);
AppShellState shell() => tester.state<AppShellState>(find.byType(AppShell));
// Joined with something up: it opens full.
expect(shell().mode, PlayerMode.full);
expect(videos, findsOneWidget);
expect(tester.getSize(videos), tester.getSize(find.byKey(const ValueKey('body'))));
Future<void> stepThrough() async {
for (var i = 0; i < 30; i++) {
await tester.pump(const Duration(milliseconds: 16));
expect(videos, findsOneWidget);
}
await tester.pumpAndSettle();
expect(videos, findsOneWidget);
}
// A tab sends it to the mini player.
await tester.tap(find.byKey(const ValueKey('tab-settings')));
await stepThrough();
expect(shell().mode, PlayerMode.mini);
expect(tester.getSize(videos), AppShellState.miniSize);
expect(find.byType(SettingsTab), findsOneWidget);
// Double-click on the mini: full again.
final mini = tester.getCenter(videos);
await tester.tapAt(mini);
await tester.tapAt(mini);
await stepThrough();
expect(shell().mode, PlayerMode.full);
// F: fullscreen hides the strip; Esc leaves it.
await tester.sendKeyEvent(LogicalKeyboardKey.keyF);
await stepThrough();
expect(shell().fullscreen, isTrue);
expect(find.byType(PartyStrip), findsNothing);
// Player keys are debounced on the wall clock.
await tester.runAsync(() => Future<void>.delayed(const Duration(milliseconds: 400)));
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await stepThrough();
expect(shell().fullscreen, isFalse);
expect(find.byType(PartyStrip), findsOneWidget);
// Esc again: big → mini, not leave; from mini it asks to leave.
await tester.runAsync(() => Future<void>.delayed(const Duration(milliseconds: 400)));
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await stepThrough();
expect(shell().mode, PlayerMode.mini);
expect(find.text('Leave server?'), findsNothing);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.text('Leave server?'), findsOneWidget);
await tester.tap(find.text('Cancel'));
await tester.pumpAndSettle();
// Never rebuilt from scratch on the way.
expect(FakeVideo.created, 1);
// Offline: no player at all.
session.route.value = AppRoute.home;
await tester.pumpAndSettle();
expect(videos, findsNothing);
expect(tester.takeException(), isNull);
});
testWidgets('status pill: who is not ready, with the ready count', (tester) async {
coord.playback.alive.value = true;
await pumpShell(tester, const Size(1280, 800));
online([member('a', 'Alex', ready: true), member('b', 'Bob'), member('c', 'Cleo')]);
await tester.pumpAndSettle();
expect(find.text('Waiting for Bob and Cleo'), findsOneWidget);
expect(find.text('1/3 ready'), findsOneWidget);
});
test('a version that is not ours stands out (a dev run matches any)', () {
expect(versionDiffers('1.3.0', own: '1.3.0'), isFalse);
expect(versionDiffers('1.2.4', own: '1.3.0'), isTrue);
expect(versionDiffers('', own: '1.3.0'), isFalse);
expect(versionDiffers('1.2.4', own: '0.0.0-dev'), isFalse);
});
test('toasts: feed toasts carry the actor, the rest are the app\'s', () {
AvatarInfo who(String n) => AvatarInfo(name: n, color: Colors.teal);
final at = DateTime.now();
// The actor is exact: "Bob" is not mistaken for "Bob Jr".
final chat = shellToastFor(ToastItem(id: 1, text: 'Bob Jr: hi there', at: at, actor: 'Bob Jr', chat: true), who);
expect(chat.who!.name, 'Bob Jr');
expect(chat.text, 'hi there');
expect(chat.chat, isTrue);
final joined = shellToastFor(ToastItem(id: 2, text: 'Bob joined', at: at, actor: 'Bob'), who);
expect(joined.who!.name, 'Bob');
expect(joined.text, 'joined');
expect(joined.icon, Icons.login);
// A name that is a prefix of a sentence is not guessed without an actor.
final app = shellToastFor(ToastItem(id: 3, text: 'Bob joined', at: at), who);
expect(app.who, isNull);
expect(app.text, 'Bob joined');
final notice = shellToastFor(ToastItem(id: 4, text: 'Shaders off', at: at), who);
expect(notice.who, isNull);
// An Up next add: who, what, and the item's picture.
final added = shellToastFor(
ToastItem(id: 5, text: 'Bob added Ep 6 to Up next', at: at, actor: 'Bob', thumb: 'h1'), who);
expect((added.who!.name, added.text, added.icon, added.thumb),
('Bob', 'added Ep 6 to Up next', Icons.playlist_add_rounded, 'h1'));
});
testWidgets("toasts slide in and leave when the Notifier's time is up", (tester) async {
await tester.pumpWidget(const MaterialApp(home: Scaffold(body: PartyToasts())));
session.notifier.notify(const Copied('Shaders off'));
await tester.pump();
await tester.pump();
expect(find.text('Shaders off'), findsOneWidget);
await tester.pump(session.notifier.toastLife);
await tester.pumpAndSettle();
expect(find.text('Shaders off'), findsNothing);
});
for (final skin in ControlsSkin.values) {
testWidgets('toasts sit above the control bar and follow it (${skin.name})', (tester) async {
final s = AppSettings.instance;
final hide = s.controlsHideSecs.peek();
s.controlsSkin.value = skin;
s.controlsHideSecs.value = 2;
addTearDown(() {
s.controlsSkin.value = ControlsSkin.standard;
s.controlsHideSecs.value = hide;
});
coord.playback.alive.value = true;
await pumpShell(tester, const Size(1280, 800),
video: (context, controls) => Stack(fit: StackFit.expand, children: [
const ColoredBox(color: Colors.black),
if (controls != null) controls(VideoState()),
]));
online(party);
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
expect(tester.state<AppShellState>(find.byType(AppShell)).mode, PlayerMode.full);
expect(tester.state<ControlsFrameState>(find.byType(ControlsFrame)).visible, isTrue);
session.notifier.notify(const Copied('Link copied'));
await tester.pump();
await tester.pump(const Duration(milliseconds: 400));
final body = tester.getRect(find.byKey(const ValueKey('body')));
final bar = controlsBottomHeight(skin, s.controlsSize.peek());
final up = tester.getRect(find.byType(ToastCard));
expect(up.bottom, lessThanOrEqualTo(body.bottom - bar), reason: 'clear of the control bar');
// The controls hide: the toasts come down to the edge.
await tester.pump(const Duration(seconds: 2));
await tester.pump(const Duration(milliseconds: 600));
expect(tester.state<ControlsFrameState>(find.byType(ControlsFrame)).visible, isFalse);
final down = tester.getRect(find.byType(ToastCard));
expect(down.bottom, closeTo(body.bottom - AppShellState.toastInset, 1));
expect(down.bottom, greaterThan(up.bottom));
session.route.value = AppRoute.home;
await tester.pump(const Duration(seconds: 6));
await tester.pumpAndSettle();
});
}
}