Files
SpotiFLAC-Mobile/test/queue_dedup_test.dart
zarzet 9dd2188aa1 feat(queue): add songs without queueing duplicates
A No duplicates in queue switch in a new Queue section of Playback
settings, off by default. While it is on, Add to queue, Play next and
adding many songs move an entry already coming up instead of queueing a
second copy, and a batch adds each song once, in its order. The song
playing and the songs before it are never touched, so they can be
queued again.

Identity is where a song plays from (`queueIdentityKey`), not its id or
tags: one file has different ids in the Library, the download history
and a plain path, while a FLAC and an MP3 copy share their tags and are
both meant to be queued. A `file://` URI counts as its path, a SAF
document is matched by provider and document id so a folder-tree URI of
it matches too, and `network://` sources (NAS, Subsonic, a paired
device) and CUE tracks name one song each. Stream URLs compare as is:
one with a fresh token never matches, which keeps a copy rather than
merging two songs.

Every path that adds to the queue goes through `enqueue` and
`enqueueAll`, so Link follows the playing device's own setting: songs a
phone queues on the computer, and a computer queueing a phone's songs
there. Autoplay already leaves out songs coming up and is unchanged.
The moved entry is removed without marking it dismissed for Autoplay,
and the shuffle order and play history follow the removal.
2026-11-08 16:33:35 +07:00

142 lines
4.4 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:mornye/models/settings.dart';
import 'package:mornye/services/queue_dedup.dart';
import 'package:mornye/services/playable_media.dart';
PlayableMedia _song(String source, {String? id}) => PlayableMedia(
id: id ?? source,
source: source,
title: source,
artist: 'Artist',
);
List<String> _sources(List<PlayableMedia> media) => [
for (final item in media) item.source,
];
void main() {
group('queueIdentityKey', () {
test('matches one file whatever id it was queued with', () {
expect(
queueIdentityKey(_song('/music/a.flac', id: 'library-7')),
queueIdentityKey(_song('/music/a.flac', id: 'history-3')),
);
});
test('a file URI counts as its path', () {
expect(
queueIdentityKey(_song('file:///music/a%20b.flac')),
'/music/a b.flac',
);
});
test('a SAF document matches through a folder tree', () {
const document =
'content://com.android.externalstorage.documents/document/'
'primary%3AMusic%2Fa.flac';
const tree =
'content://com.android.externalstorage.documents/tree/'
'primary%3AMusic/document/primary%3AMusic%2Fa.flac';
expect(queueIdentityKey(_song(tree)), queueIdentityKey(_song(document)));
expect(
queueIdentityKey(_song(document)),
isNot(
queueIdentityKey(
_song(
'content://com.android.externalstorage.documents/document/'
'primary%3AMusic%2Fb.flac',
),
),
),
);
});
test('other providers and sources keep their own identity', () {
expect(
queueIdentityKey(_song('content://media/external/audio/media/42')),
'content://media/external/audio/media/42',
);
expect(
queueIdentityKey(_song('network://nas/Album/a.flac')),
'network://nas/Album/a.flac',
);
expect(
queueIdentityKey(_song('https://example.com/a.flac?token=1')),
isNot(queueIdentityKey(_song('https://example.com/a.flac?token=2'))),
);
expect(
queueIdentityKey(_song('/music/album.cue#track01')),
isNot(queueIdentityKey(_song('/music/album.cue#track02'))),
);
});
test('a different file with the same tags is a different song', () {
expect(
queueIdentityKey(_song('/music/a.flac')),
isNot(queueIdentityKey(_song('/music/a.mp3'))),
);
});
});
group('planQueueWithoutDuplicates', () {
final queue = [
_song('/played.flac'),
_song('/current.flac'),
_song('/next.flac'),
_song('/later.flac'),
];
test('replaces upcoming copies, highest index first', () {
final plan = planQueueWithoutDuplicates(
queue: queue,
current: 1,
incoming: [_song('/next.flac'), _song('/later.flac')],
);
expect(plan.remove, [3, 2]);
expect(_sources(plan.insert), ['/next.flac', '/later.flac']);
});
test('never touches the current or played songs', () {
final plan = planQueueWithoutDuplicates(
queue: queue,
current: 1,
incoming: [_song('/current.flac'), _song('/played.flac')],
);
expect(plan.remove, isEmpty);
expect(_sources(plan.insert), ['/current.flac', '/played.flac']);
});
test('adds each song of a batch once, in order', () {
final plan = planQueueWithoutDuplicates(
queue: queue,
current: 1,
incoming: [
_song('/new.flac'),
_song('/later.flac'),
_song('/new.flac', id: 'again'),
],
);
expect(plan.remove, [3]);
expect(_sources(plan.insert), ['/new.flac', '/later.flac']);
});
test('an empty queue only drops repeats within the batch', () {
final plan = planQueueWithoutDuplicates(
queue: const [],
current: -1,
incoming: [_song('/a.flac'), _song('/a.flac')],
);
expect(plan.remove, isEmpty);
expect(_sources(plan.insert), ['/a.flac']);
});
});
test('the setting defaults to off and round-trips through JSON', () {
expect(const AppSettings().queueNoDuplicates, isFalse);
expect(AppSettings.fromJson(const {}).queueNoDuplicates, isFalse);
final on = const AppSettings().copyWith(queueNoDuplicates: true);
expect(on.toJson()['queueNoDuplicates'], isTrue);
expect(AppSettings.fromJson(on.toJson()).queueNoDuplicates, isTrue);
});
}