From 269a4d97fab166d8ca4fd480f1297ed7802db074 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Sun, 1 Nov 2026 21:10:18 +0700 Subject: [PATCH] perf(link): reuse desktop player window state Map runtime queue entries lazily with a 400-entry LRU and retain the last catalog-resolved player window. Overlay enriched current metadata through a lazy singleton view keyed by base, item and index. Compare retained scalar and display-tag inputs directly so mutable extras invalidate without allocating metadata snapshots on cache hits. A 20000-song fixture maps only its 200 requested entries, including a rich current item. Sixty unchanged ticks add no mappings, metadata copies or catalog lookups; full traversal retains at most 400 base memo entries. All 109 scoped tests pass, including the existing playback-details regressions, and eager-overlay and missing-invalidation negative controls fail as expected. --- .../link/desktop/link_desktop_ops.dart | 2 + lib/services/link/desktop/link_player.dart | 189 ++++- test/link_desktop_ops_test.dart | 47 ++ test/link_player_state_cache_test.dart | 687 ++++++++++++++++++ 4 files changed, 905 insertions(+), 20 deletions(-) create mode 100644 test/link_player_state_cache_test.dart diff --git a/lib/services/link/desktop/link_desktop_ops.dart b/lib/services/link/desktop/link_desktop_ops.dart index 94ffe12ac..132ff735e 100644 --- a/lib/services/link/desktop/link_desktop_ops.dart +++ b/lib/services/link/desktop/link_desktop_ops.dart @@ -205,6 +205,7 @@ class LinkDesktopOps { final int Function() _nowMs; final LinkPlaybackDetails _playbackDetails; final _random = math.Random(); + final _playerStateCache = LinkPlayerStateCache(); /// The queue the last `handoff/play` started, which `handoff/append` /// extends. @@ -261,6 +262,7 @@ class LinkDesktopOps { index: _index(), autoplay: _autoplay?.call(), autoMix: _autoMix?.call(), + cache: _playerStateCache, ); /// Notes the desktop's own queue, so `getPlayQueue` knows when it last diff --git a/lib/services/link/desktop/link_player.dart b/lib/services/link/desktop/link_player.dart index bee077762..e93fd51c3 100644 --- a/lib/services/link/desktop/link_player.dart +++ b/lib/services/link/desktop/link_player.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:collection'; import 'package:audio_service/audio_service.dart'; import 'package:flutter/foundation.dart'; @@ -52,6 +53,101 @@ class LinkPlayerItem { final Map audioMetadata; } +class _MappedPlayerItem { + _MappedPlayerItem(this.source) + : _extras = [for (final key in _extraKeys) source.extras?[key]], + _displayMetadata = switch (source.extras?['displayMetadata']) { + final Map value => Map.of(value), + _ => const {}, + }, + item = LinkPlayerItem.fromMediaItem(source); + + static const _extraKeys = [ + 'source', + 'resolvedSource', + 'bit_depth', + 'sample_rate', + 'bitrate', + 'format', + 'explicit', + ]; + + final MediaItem source; + final List _extras; + final Map _displayMetadata; + final LinkPlayerItem item; + + bool matches(MediaItem value) { + if (!identical(source, value)) return false; + final extras = value.extras; + for (var i = 0; i < _extraKeys.length; i++) { + if (_extras[i] != extras?[_extraKeys[i]]) return false; + } + final displayMetadata = extras?['displayMetadata']; + return displayMetadata is Map + ? mapEquals(_displayMetadata, displayMetadata) + : _displayMetadata.isEmpty; + } +} + +class _MappedPlayerQueue extends ListBase { + _MappedPlayerQueue(this._source); + + static const _maxItems = LinkPlayerState.maxQueue * 2; + final List _source; + final _items = {}; + + @override + int get length => _source.length; + + @override + set length(int value) => throw UnsupportedError('Immutable player queue'); + + @override + LinkPlayerItem operator [](int index) { + final source = _source[index]; + final cached = _items.remove(index); + if (cached != null && cached.matches(source)) { + _items[index] = cached; + return cached.item; + } + final mapped = _MappedPlayerItem(source); + if (_items.length >= _maxItems) _items.remove(_items.keys.first); + _items[index] = mapped; + return mapped.item; + } + + @override + void operator []=(int index, LinkPlayerItem value) => + throw UnsupportedError('Immutable player queue'); +} + +class _CurrentPlayerQueue extends ListBase { + _CurrentPlayerQueue(this._base, this._item, this._index) + : _current = _MappedPlayerQueue([_item]); + + final List _base; + final MediaItem _item; + final int _index; + final _MappedPlayerQueue _current; + + @override + int get length => _base.length; + + @override + set length(int value) => throw UnsupportedError('Immutable player queue'); + + @override + LinkPlayerItem operator [](int index) { + RangeError.checkValidIndex(index, this); + return index == _index ? _current[0] : _base[index]; + } + + @override + void operator []=(int index, LinkPlayerItem value) => + throw UnsupportedError('Immutable player queue'); +} + /// The desktop player at one moment. @immutable class LinkPlayerSnapshot { @@ -139,9 +235,14 @@ class RuntimeLinkPlayerPort implements LinkPlayerPort { final MusicPlayerController _controller; List? _mappedQueue; List _mapped = const []; - MediaItem? _mappedCurrent; - List? _currentBase; - List _currentItems = const []; + _CurrentPlayerQueue? _currentItems; + MediaItem? _standaloneItem; + List _standalone = const []; + + @visibleForTesting + int get debugMappedQueueItemCount => _mapped is _MappedPlayerQueue + ? (_mapped as _MappedPlayerQueue)._items.length + : 0; @override LinkPlayerSnapshot snapshot() { @@ -152,10 +253,10 @@ class RuntimeLinkPlayerPort implements LinkPlayerPort { return LinkPlayerSnapshot(updatedMs: now, volume: volume); } final queue = handler.queue.value; - // Map the queue once per change; position ticks reuse it. + // Map only entries read by the state window or an explicit queue request. if (!identical(queue, _mappedQueue)) { _mappedQueue = queue; - _mapped = List.unmodifiable(queue.map(LinkPlayerItem.fromMediaItem)); + _mapped = _MappedPlayerQueue(queue); } final current = handler.mediaItem.value; final state = handler.playbackState.value; @@ -168,24 +269,28 @@ class RuntimeLinkPlayerPort implements LinkPlayerPort { queue[index].id != current.id) { index = queue.indexWhere((item) => item.id == current.id); if (index < 0) { - items = [LinkPlayerItem.fromMediaItem(current)]; + if (!identical(current, _standaloneItem)) { + _standaloneItem = current; + _standalone = _MappedPlayerQueue([current]); + } + items = _standalone; index = 0; } } if (current != null && + identical(items, _mapped) && index >= 0 && - index < queue.length && !identical(current, queue[index])) { - if (!identical(current, _mappedCurrent) || - !identical(items, _currentBase)) { - _mappedCurrent = current; - _currentBase = items; - _currentItems = List.unmodifiable([ - for (var i = 0; i < items.length; i++) - i == index ? LinkPlayerItem.fromMediaItem(current) : items[i], - ]); + final overlay = _currentItems; + if (overlay == null || + !identical(current, overlay._item) || + !identical(items, overlay._base) || + index != overlay._index) { + _currentItems = _CurrentPlayerQueue(items, current, index); } - items = _currentItems; + items = _currentItems!; + } else { + _currentItems = null; } return LinkPlayerSnapshot( queue: items, @@ -333,18 +438,62 @@ TrackRef linkTrackRef(LinkPlayerItem item, {DesktopLinkCatalogIndex? index}) { ); } +/// Retains the last queue window while its entries and catalog stay current. +class LinkPlayerStateCache { + List? _source; + DesktopLinkCatalogIndex? _index; + int _start = -1; + int _end = -1; + List _items = const []; + List _refs = const []; + + List _window( + List source, + ({int start, int end}) window, + DesktopLinkCatalogIndex? index, + ) { + var unchanged = + identical(source, _source) && + identical(index, _index) && + window.start == _start && + window.end == _end; + if (unchanged) { + for (var i = window.start; i < window.end; i++) { + if (!identical(source[i], _items[i - window.start])) { + unchanged = false; + break; + } + } + } + if (!unchanged) { + _source = source; + _index = index; + _start = window.start; + _end = window.end; + _items = [for (var i = window.start; i < window.end; i++) source[i]]; + _refs = List.unmodifiable([ + for (final item in _items) linkTrackRef(item, index: index), + ]); + } + return _refs; + } +} + /// The desktop player as `player.state` and `GET state` send it. LinkPlayerState linkPlayerState( LinkPlayerSnapshot snapshot, { DesktopLinkCatalogIndex? index, bool? autoplay, bool? autoMix, + LinkPlayerStateCache? cache, }) { final window = linkQueueWindow(snapshot.queue.length, snapshot.index); - final queue = [ - for (var i = window.start; i < window.end; i++) - linkTrackRef(snapshot.queue[i], index: index), - ]; + final queue = + cache?._window(snapshot.queue, window, index) ?? + [ + for (var i = window.start; i < window.end; i++) + linkTrackRef(snapshot.queue[i], index: index), + ]; final current = snapshot.current; final inWindow = current != null && diff --git a/test/link_desktop_ops_test.dart b/test/link_desktop_ops_test.dart index 5d94b7da8..3e9b40aca 100644 --- a/test/link_desktop_ops_test.dart +++ b/test/link_desktop_ops_test.dart @@ -133,6 +133,53 @@ void main() { expect((queue.last as Map).containsKey('songId'), isFalse); }); + test('state retains its queue refs until entries or catalog change', () { + final queue = [playerItem('a'), playerItem('b')]; + player.current = snapshot(queue: queue); + final first = ops.state(); + player.current = snapshot(queue: queue, positionMs: 48000, playing: false); + autoplay = true; + autoMix = true; + final next = ops.state(); + expect(identical(next.queue, first.queue), isTrue); + expect(identical(next.track, next.queue[1]), isTrue); + expect(next.positionMs, 48000); + expect(next.playing, isFalse); + expect(next.autoplay, isTrue); + expect(next.autoMix, isTrue); + queue[0] = const LinkPlayerItem( + id: 'a', + path: '/edited.flac', + title: 'Edited', + artist: 'Artist', + ); + final edited = ops.state(); + expect(edited.queue.first.title, 'Edited'); + expect(identical(edited.queue, next.queue), isFalse); + index = DesktopLinkCatalogIndex( + serverId: 'new-server', + songs: [ + const LinkCatalogSong( + id: 'l:new', + path: '/edited.flac', + title: 'Edited', + artist: 'Artist', + album: 'Catalog album', + durationMs: 12345, + isrc: 'EXAMPLE123', + ), + ], + ); + final cataloged = ops.state(); + expect(cataloged.queue.first.songId, 'l:new'); + expect(cataloged.queue.first.serverId, 'new-server'); + expect(cataloged.queue.first.album, 'Catalog album'); + expect(cataloged.queue.first.durationMs, 12345); + expect(cataloged.queue.first.isrc, 'EXAMPLE123'); + expect(first.queue.first.title, 'Title a'); + expect(() => cataloged.queue.clear(), throwsUnsupportedError); + }); + test( 'handoff adopts playback modes and reports automation changes', () async { diff --git a/test/link_player_state_cache_test.dart b/test/link_player_state_cache_test.dart new file mode 100644 index 000000000..50be6cc5d --- /dev/null +++ b/test/link_player_state_cache_test.dart @@ -0,0 +1,687 @@ +import 'dart:collection'; +import 'dart:math'; + +import 'package:audio_service/audio_service.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mornye/services/link/desktop/link_catalog_index.dart'; +import 'package:mornye/services/link/desktop/link_desktop_ops.dart'; +import 'package:mornye/services/link/desktop/link_player.dart'; +import 'package:mornye/services/link/link_host_router.dart'; +import 'package:mornye/services/link/link_playback_details.dart'; +import 'package:mornye/services/link/link_protocol.dart'; +import 'package:mornye/services/music_player_service.dart'; + +import 'support/link_desktop_fakes.dart'; + +class _Handler extends BaseAudioHandler implements MusicPlayerHandler { + @override + Future stop({bool keepResume = false}) async {} + + @override + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); + + Future close() async { + await queue.close(); + await mediaItem.close(); + await playbackState.close(); + } +} + +class _Runtime extends MusicPlayerRuntime { + _Runtime(this.handler); + + @override + final MusicPlayerHandler? handler; +} + +class _CountingMediaItem extends MediaItem { + _CountingMediaItem(int index, {Map? extras, Uri? artwork}) + : super( + id: '$index', + title: 'Song $index', + artist: 'Artist', + artUri: artwork, + extras: extras ?? {'source': '/Music/$index.flac'}, + ); + + static int titleReads = 0; + + @override + String get title { + titleReads++; + return super.title; + } +} + +class _CountingIndex extends DesktopLinkCatalogIndex { + _CountingIndex({super.serverId = 'server', super.songs = const []}); + + int lookups = 0; + + @override + String? songIdForMedia({required String id, required String path}) { + lookups++; + return super.songIdForMedia(id: id, path: path); + } +} + +class _CountingMetadata extends MapBase { + final _values = {'genre': 'Rock', 'lyrics': 'Words'}; + int keyReads = 0; + + @override + int get length => _values.length; + + @override + bool containsKey(Object? key) => _values.containsKey(key); + + @override + Iterable get keys { + keyReads++; + return _values.keys; + } + + @override + dynamic operator [](Object? key) => _values[key]; + + @override + void operator []=(String key, dynamic value) => _values[key] = value; + + @override + void clear() => _values.clear(); + + @override + dynamic remove(Object? key) => _values.remove(key); +} + +class _RecordingDetails extends LinkPlaybackDetails { + LinkPlayerItem? item; + + @override + Future> read(LinkPlayerItem item) async { + this.item = item; + return {}; + } +} + +void _expectMappedItem(LinkPlayerItem actual, MediaItem source) { + final expected = LinkPlayerItem.fromMediaItem(source); + expect(actual.id, expected.id); + expect(actual.path, expected.path); + expect(actual.title, expected.title); + expect(actual.artist, expected.artist); + expect(actual.album, expected.album); + expect(actual.durationMs, expected.durationMs); + expect(actual.artwork, expected.artwork); + expect(actual.metadataSource, expected.metadataSource); + expect(actual.audioMetadata, expected.audioMetadata); +} + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + test('a rich current overlay maps only its 20000-song queue window', () { + final handler = _Handler(); + final runtime = _Runtime(handler); + addTearDown(handler.close); + addTearDown(runtime.dispose); + final media = List.unmodifiable([ + for (var i = 0; i < 20000; i++) _CountingMediaItem(i), + ]); + final tags = _CountingMetadata(); + final current = _CountingMediaItem( + 10000, + artwork: Uri.file('/current.jpg'), + extras: { + 'source': '/Music/10000.flac', + 'resolvedSource': '/received/current.flac', + 'sample_rate': 96000, + 'displayMetadata': tags, + }, + ); + handler.queue.add(media); + handler.mediaItem.add(current); + handler.playbackState.add(PlaybackState(queueIndex: 10000)); + final port = RuntimeLinkPlayerPort(runtime); + final cache = LinkPlayerStateCache(); + final index = _CountingIndex(); + _CountingMediaItem.titleReads = 0; + final snapshot = port.snapshot(); + expect(_CountingMediaItem.titleReads, 0); + final state = linkPlayerState(snapshot, index: index, cache: cache); + expect(_CountingMediaItem.titleReads, 200); + expect(port.debugMappedQueueItemCount, 199); + expect(snapshot.current!.artwork, 'file:///current.jpg'); + expect(snapshot.current!.metadataSource, '/received/current.flac'); + expect(snapshot.current!.audioMetadata['sample_rate'], 96000); + final metadata = snapshot.current!.audioMetadata; + final keyReads = tags.keyReads; + expect(keyReads, greaterThan(0)); + for (var tick = 0; tick < 60; tick++) { + final next = port.snapshot(); + expect(next.queue, same(snapshot.queue)); + expect(next.current!.audioMetadata, same(metadata)); + expect( + linkPlayerState(next, index: index, cache: cache).queue, + same(state.queue), + ); + } + expect(tags.keyReads, keyReads); + expect(_CountingMediaItem.titleReads, 200); + expect(index.lookups, 200); + expect(snapshot.queue[9999].metadataSource, isNull); + for (var i = 0; i < snapshot.queue.length; i++) { + expect(snapshot.queue[i].id, '$i'); + expect(port.debugMappedQueueItemCount, lessThanOrEqualTo(400)); + } + expect(port.debugMappedQueueItemCount, 400); + expect(tags.keyReads, keyReads); + expect(() => snapshot.queue[0] = snapshot.current!, throwsUnsupportedError); + }); + + test( + 'current overlays follow duplicate indices and fallback transitions', + () { + final handler = _Handler(); + final runtime = _Runtime(handler); + addTearDown(handler.close); + addTearDown(runtime.dispose); + const first = MediaItem(id: 'same', title: 'First'); + const second = MediaItem(id: 'same', title: 'Second'); + const current = MediaItem(id: 'same', title: 'Live'); + handler.queue.add([first, second]); + handler.mediaItem.add(current); + handler.playbackState.add(PlaybackState(queueIndex: 0)); + final port = RuntimeLinkPlayerPort(runtime); + final cache = LinkPlayerStateCache(); + final atFirst = port.snapshot(); + expect(atFirst.queue.map((v) => v.title), ['Live', 'Second']); + expect(linkPlayerState(atFirst, cache: cache).index, 0); + handler.playbackState.add(PlaybackState(queueIndex: 1)); + final atSecond = port.snapshot(); + expect(atSecond.queue.map((v) => v.title), ['First', 'Live']); + final movedState = linkPlayerState(atSecond, cache: cache); + expect(movedState.index, 1); + expect(movedState.track!.title, 'Live'); + expect(movedState.queue.first.title, 'First'); + expect(atFirst.queue.map((v) => v.title), ['Live', 'Second']); + expect(port.snapshot().queue, same(atSecond.queue)); + handler.mediaItem.add(second); + final canonical = port.snapshot(); + expect(canonical.queue.map((v) => v.title), ['First', 'Second']); + handler.mediaItem.add(null); + final emptyCurrent = port.snapshot(); + expect(emptyCurrent.queue, same(canonical.queue)); + expect(emptyCurrent.current, isNull); + expect(emptyCurrent.index, -1); + const foreign = MediaItem(id: 'foreign', title: 'Foreign'); + handler.mediaItem.add(foreign); + final standalone = port.snapshot(); + expect(standalone.queue.single.title, 'Foreign'); + expect(standalone.index, 0); + handler.queue.add([]); + expect(port.snapshot().queue, same(standalone.queue)); + handler.mediaItem.add(null); + expect(port.snapshot().queue, isEmpty); + handler.queue.add([first, second]); + handler.mediaItem.add(current); + expect(port.snapshot().queue.map((v) => v.title), ['First', 'Live']); + }, + ); + + for (final mode in ['canonical', 'overlay', 'standalone']) { + test('$mode mapping observes metadata edits, removal and replacement', () { + final handler = _Handler(); + final runtime = _Runtime(handler); + addTearDown(handler.close); + addTearDown(runtime.dispose); + final tags = {'genre': 'Rock', 'lyrics': 'Old words'}; + final extras = { + 'source': '/song.flac', + 'resolvedSource': '/received/song.flac', + 'bit_depth': 16, + 'sample_rate': 44100, + 'bitrate': 320, + 'format': 'flac', + 'explicit': false, + 'displayMetadata': tags, + }; + var current = MediaItem( + id: mode == 'standalone' ? 'foreign' : 'song', + title: 'Song', + artUri: Uri.file('/old.jpg'), + extras: extras, + ); + final queue = [ + mode == 'canonical' + ? current + : const MediaItem(id: 'song', title: 'Queued'), + ]; + handler.queue.add(queue); + handler.mediaItem.add(current); + handler.playbackState.add(PlaybackState(queueIndex: 0)); + final port = RuntimeLinkPlayerPort(runtime); + var previous = port.snapshot(); + var mapped = previous.current!; + _expectMappedItem(mapped, current); + void checkMutation() { + final next = port.snapshot(); + expect(next.queue, same(previous.queue)); + expect(next.current, isNot(same(mapped))); + mapped = next.current!; + _expectMappedItem(mapped, current); + previous = next; + } + + for (final change in { + 'source': 'EXISTS:/updated.flac', + 'resolvedSource': '/received/updated.flac', + 'bit_depth': 24, + 'sample_rate': 96000, + 'bitrate': 2400, + 'format': 'alac', + 'explicit': true, + }.entries) { + extras[change.key] = change.value; + checkMutation(); + } + tags['genre'] = 'Jazz'; + checkMutation(); + tags.remove('genre'); + checkMutation(); + extras['displayMetadata'] = {'album_artist': 'Replacement'}; + checkMutation(); + extras.remove('displayMetadata'); + checkMutation(); + for (final key in [ + 'source', + 'resolvedSource', + 'bit_depth', + 'sample_rate', + 'bitrate', + 'format', + 'explicit', + ]) { + extras.remove(key); + checkMutation(); + } + current = current.copyWith( + artUri: Uri.file('/new.jpg'), + extras: { + 'source': '/replacement.flac', + 'resolvedSource': '/received/replacement.flac', + 'sample_rate': 48000, + 'displayMetadata': {'genre': 'Replacement'}, + }, + ); + if (mode == 'canonical') queue[0] = current; + handler.mediaItem.add(current); + final replaced = port.snapshot(); + expect(replaced.current, isNot(same(mapped))); + _expectMappedItem(replaced.current!, current); + expect(port.snapshot().queue, same(replaced.queue)); + }); + } + + test( + 'playback details read the current overlay after metadata updates', + () async { + final handler = _Handler(); + final runtime = _Runtime(handler); + addTearDown(handler.close); + addTearDown(runtime.dispose); + final tags = {'genre': 'Rock'}; + final extras = { + 'source': '/song.flac', + 'resolvedSource': '/received/old.flac', + 'sample_rate': 44100, + 'displayMetadata': tags, + }; + var current = MediaItem( + id: 'song', + title: 'Song', + artUri: Uri.file('/old.jpg'), + extras: extras, + ); + handler.queue.add([const MediaItem(id: 'song', title: 'Queued')]); + handler.mediaItem.add(current); + handler.playbackState.add(PlaybackState(queueIndex: 0)); + final port = RuntimeLinkPlayerPort(runtime); + final details = _RecordingDetails(); + final ops = LinkDesktopOps( + player: port, + downloads: FakeLinkDownloads(), + downloadItems: () => [], + openUrl: (_) async {}, + index: () => null, + desktopName: () => 'Desktop', + playbackDetails: details, + ); + final router = LinkHostRouter(); + ops.register(router); + Future readDetails() async { + await router.dispatch( + LinkRequest( + requestId: 'details', + op: LinkOps.playbackDetails, + deviceId: 'phone', + args: {'track': ops.state().track!.toJson()}, + ), + ); + _expectMappedItem(details.item!, current); + } + + await readDetails(); + extras['resolvedSource'] = '/received/new.flac'; + extras['sample_rate'] = 96000; + tags['genre'] = 'Jazz'; + await readDetails(); + current = current.copyWith(artUri: Uri.file('/new.jpg')); + handler.mediaItem.add(current); + await readDetails(); + }, + ); + + test('runtime mapping retention stays bounded through a 20000-song walk', () { + final handler = _Handler(); + final runtime = _Runtime(handler); + addTearDown(handler.close); + addTearDown(runtime.dispose); + final media = List.unmodifiable([ + for (var i = 0; i < 20000; i++) _CountingMediaItem(i), + ]); + handler.queue.add(media); + handler.mediaItem.add(media[10000]); + handler.playbackState.add(PlaybackState(queueIndex: 10000)); + final port = RuntimeLinkPlayerPort(runtime); + final snapshot = port.snapshot(); + var peak = 0; + for (var i = 0; i < snapshot.queue.length; i++) { + expect(snapshot.queue[i].id, '$i'); + peak = max(peak, port.debugMappedQueueItemCount); + } + expect(peak, 400); + expect(port.debugMappedQueueItemCount, 400); + expect(snapshot.current!.id, '10000'); + final state = linkPlayerState(snapshot, cache: LinkPlayerStateCache()); + expect(state.index, 100); + expect(state.track!.title, 'Song 10000'); + expect(port.debugMappedQueueItemCount, 400); + }); + + test('cached state matches the original mapping through queue changes', () { + final random = Random(7); + final cache = LinkPlayerStateCache(); + var items = [ + for (var i = 0; i < 500; i++) + LinkPlayerItem( + id: '$i', + path: '/$i.flac', + title: 'Song $i', + artist: 'Artist', + ), + ]; + DesktopLinkCatalogIndex? catalog; + for (var round = 0; round < 200; round++) { + final at = random.nextInt(items.length + 2) - 1; + switch (round % 6) { + case 0: + items = [...items]; + case 1: + if (at >= 0 && at < items.length) { + items[at] = LinkPlayerItem( + id: 'edited-$round', + path: '/edited-$round.flac', + title: 'Edited $round', + artist: 'Artist $round', + album: 'Album $round', + durationMs: round * 1000, + ); + } + case 2: + items.add( + LinkPlayerItem( + id: '$round', + path: '/$round.flac', + title: 'Song $round', + artist: 'Artist', + ), + ); + case 3: + if (at >= 0 && at < items.length) items.removeAt(at); + case 4: + items.setAll(0, items.reversed.toList()); + case 5: + catalog = catalog == null + ? DesktopLinkCatalogIndex( + serverId: 'server', + songs: [ + for (final item in items) + LinkCatalogSong( + id: 'l:${item.id}', + path: item.path, + title: item.title, + artist: item.artist, + album: 'Catalog album', + durationMs: 12345, + isrc: 'EXAMPLE123', + ), + ], + ) + : null; + } + final snapshot = LinkPlayerSnapshot( + queue: items, + index: at, + updatedMs: round * 1000, + positionMs: round * 400, + playing: round.isEven, + durationMs: round.isEven ? null : 12345, + shuffle: round % 3 == 0, + volume: random.nextDouble() * 1.5, + ); + expect( + linkPlayerState(snapshot, index: catalog, cache: cache).toJson(), + linkPlayerState(snapshot, index: catalog).toJson(), + reason: 'round $round', + ); + } + }); + + test( + 'a 20000-song runtime queue maps only the window across ticks', + () async { + final handler = _Handler(); + final runtime = _Runtime(handler); + addTearDown(handler.close); + addTearDown(runtime.dispose); + final media = List.unmodifiable([ + for (var i = 0; i < 20000; i++) _CountingMediaItem(i), + ]); + handler.queue.add(media); + handler.mediaItem.add(media[10000]); + handler.playbackState.add(PlaybackState(queueIndex: 10000)); + final port = RuntimeLinkPlayerPort(runtime); + _CountingMediaItem.titleReads = 0; + final snapshot = port.snapshot(); + expect(_CountingMediaItem.titleReads, 0); + final cache = LinkPlayerStateCache(); + final index = _CountingIndex(); + final first = linkPlayerState(snapshot, index: index, cache: cache); + expect(_CountingMediaItem.titleReads, 200); + expect(index.lookups, 200); + expect(first.queue.first.title, 'Song 9900'); + expect(first.queue.last.title, 'Song 10099'); + expect(first.index, 100); + expect(first.track!.title, 'Song 10000'); + for (var tick = 0; tick < 60; tick++) { + final next = port.snapshot(); + expect(identical(next.queue, snapshot.queue), isTrue); + expect( + identical( + linkPlayerState(next, index: index, cache: cache).queue, + first.queue, + ), + isTrue, + ); + } + expect(_CountingMediaItem.titleReads, 200); + expect(index.lookups, 200); + expect(() => snapshot.queue.clear(), throwsUnsupportedError); + expect(() => first.queue.clear(), throwsUnsupportedError); + + final replacement = [...media]; + replacement[10005] = replacement[10005].copyWith(title: 'Edited song'); + handler.queue.add(List.unmodifiable(replacement)); + final edited = linkPlayerState( + port.snapshot(), + index: index, + cache: cache, + ); + expect(edited.queue[105].title, 'Edited song'); + expect(first.queue[105].title, 'Song 10005'); + expect(index.lookups, 400); + expect(_CountingMediaItem.titleReads, lessThan(401)); + + final before = _CountingMediaItem.titleReads; + handler.mediaItem.add(media[10500]); + handler.playbackState.add(PlaybackState(queueIndex: 10500)); + final moved = linkPlayerState( + port.snapshot(), + index: index, + cache: cache, + ); + expect(moved.queue.first.title, 'Song 10400'); + expect(moved.track!.title, 'Song 10500'); + expect(_CountingMediaItem.titleReads - before, 200); + }, + ); + + test('cached windows retain scalar, metadata and catalog invalidation', () { + final items = [ + for (var i = 0; i < 500; i++) + LinkPlayerItem( + id: '$i', + path: '/$i.flac', + title: 'Song $i', + artist: 'Artist', + ), + ]; + final cache = LinkPlayerStateCache(); + final index = _CountingIndex(); + LinkPlayerSnapshot snapshot({ + int at = 499, + bool playing = false, + int position = 0, + }) => LinkPlayerSnapshot( + queue: items, + index: at, + playing: playing, + positionMs: position, + updatedMs: position, + ); + final first = linkPlayerState(snapshot(), index: index, cache: cache); + final tick = linkPlayerState( + snapshot(playing: true, position: 5000), + index: index, + cache: cache, + autoplay: true, + autoMix: false, + ); + expect(identical(tick.queue, first.queue), isTrue); + expect(tick.index, 199); + expect(tick.playing, isTrue); + expect(tick.positionMs, 5000); + expect(tick.autoplay, isTrue); + expect(tick.autoMix, isFalse); + expect(index.lookups, 200); + + items[498] = const LinkPlayerItem( + id: '498', + path: '/edited.flac', + title: 'Edited', + artist: 'Edited artist', + album: 'Edited album', + durationMs: 1234, + ); + final edited = linkPlayerState(snapshot(), index: index, cache: cache); + expect(edited.queue[198].toJson(), { + 'title': 'Edited', + 'artist': 'Edited artist', + 'album': 'Edited album', + 'durationMs': 1234, + 'matchKey': 'edited|edited artist', + }); + final refreshedIndex = _CountingIndex( + songs: [ + const LinkCatalogSong( + id: 'l:edited', + path: '/edited.flac', + title: 'Edited', + artist: 'Edited artist', + album: 'Catalog album', + durationMs: 4321, + isrc: 'EXAMPLE123', + ), + ], + ); + final refreshed = linkPlayerState( + snapshot(), + index: refreshedIndex, + cache: cache, + ); + expect(refreshed.queue[198].songId, 'l:edited'); + expect(refreshed.queue[198].serverId, 'server'); + expect(refreshed.queue[198].isrc, 'EXAMPLE123'); + expect(refreshed.queue[198].album, 'Edited album'); + expect(refreshed.queue[198].durationMs, 1234); + expect(refreshedIndex.lookups, 200); + final uncataloged = linkPlayerState(snapshot(), cache: cache); + expect(uncataloged.queue[198].songId, isNull); + expect(uncataloged.queue[198].isrc, isNull); + expect(linkPlayerState(snapshot(at: -1), cache: cache).track, isNull); + items.clear(); + final empty = linkPlayerState(snapshot(), cache: cache); + expect(empty.queue, isEmpty); + expect(empty.index, -1); + expect(empty.track, isNull); + }); + + test( + 'runtime queue observes source edits and standalone current items', + () async { + final handler = _Handler(); + final runtime = _Runtime(handler); + addTearDown(handler.close); + addTearDown(runtime.dispose); + final item = _CountingMediaItem(1); + handler.queue.add([item]); + handler.mediaItem.add(item); + handler.playbackState.add(PlaybackState(queueIndex: 0)); + final port = RuntimeLinkPlayerPort(runtime); + final cache = LinkPlayerStateCache(); + final first = port.snapshot(); + linkPlayerState(first, cache: cache); + item.extras!['source'] = 'EXISTS:/edited.flac'; + final edited = port.snapshot(); + expect(identical(edited.queue, first.queue), isTrue); + expect(edited.current!.path, '/edited.flac'); + final foreign = const MediaItem( + id: 'foreign', + title: 'Foreign', + duration: Duration(seconds: 9), + ); + handler.mediaItem.add(foreign); + final standalone = port.snapshot(); + expect(standalone.queue.length, 1); + expect(standalone.index, 0); + expect(standalone.current!.title, 'Foreign'); + expect(identical(port.snapshot().queue, standalone.queue), isTrue); + handler.mediaItem.add(foreign.copyWith(title: 'Edited foreign')); + expect(port.snapshot().current!.title, 'Edited foreign'); + handler.mediaItem.add(null); + expect(port.snapshot().index, -1); + expect(RuntimeLinkPlayerPort(_Runtime(null)).snapshot().queue, isEmpty); + }, + ); +}