70 lines
2.1 KiB
Dart
70 lines
2.1 KiB
Dart
// Author: fruit.com.cn
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:fruitshorts/core/data/services/playback_position_cache.dart';
|
|
|
|
void main() {
|
|
test('should_store_and_read_recent_position_when_drama_matches', () {
|
|
final now = DateTime(2026, 10, 22, 12);
|
|
final cache = PlaybackPositionCache(now: () => now);
|
|
|
|
cache.save(
|
|
episodeId: 'episode-3',
|
|
dramaId: 'drama-a',
|
|
position: const Duration(minutes: 1, seconds: 20),
|
|
duration: const Duration(minutes: 3),
|
|
);
|
|
|
|
final entry = cache.read('episode-3', dramaId: 'drama-a');
|
|
expect(entry, isNotNull);
|
|
expect(entry!.position, const Duration(minutes: 1, seconds: 20));
|
|
expect(entry.duration, const Duration(minutes: 3));
|
|
expect(entry.updatedAt, now);
|
|
});
|
|
|
|
test('should_reject_cached_position_when_drama_does_not_match', () {
|
|
final cache = PlaybackPositionCache();
|
|
cache.save(
|
|
episodeId: 'episode-3',
|
|
dramaId: 'drama-a',
|
|
position: const Duration(seconds: 80),
|
|
duration: const Duration(minutes: 3),
|
|
);
|
|
|
|
expect(cache.read('episode-3', dramaId: 'drama-b'), isNull);
|
|
});
|
|
|
|
test('should_use_latest_saved_episode_when_timestamps_match', () {
|
|
final now = DateTime(2026, 10, 22, 12);
|
|
final cache = PlaybackPositionCache(now: () => now);
|
|
cache.save(
|
|
episodeId: 'episode-1',
|
|
dramaId: 'drama-a',
|
|
position: const Duration(seconds: 30),
|
|
duration: const Duration(minutes: 3),
|
|
);
|
|
cache.save(
|
|
episodeId: 'episode-2',
|
|
dramaId: 'drama-a',
|
|
position: const Duration(seconds: 45),
|
|
duration: const Duration(minutes: 3),
|
|
);
|
|
|
|
expect(cache.latestForDrama('drama-a')?.episodeId, 'episode-2');
|
|
});
|
|
|
|
test('should_reject_expired_cached_position', () {
|
|
var now = DateTime(2026, 10, 22, 12);
|
|
final cache = PlaybackPositionCache(now: () => now);
|
|
cache.save(
|
|
episodeId: 'episode-3',
|
|
dramaId: 'drama-a',
|
|
position: const Duration(seconds: 80),
|
|
duration: const Duration(minutes: 3),
|
|
);
|
|
now = now.add(const Duration(days: 1));
|
|
|
|
expect(cache.read('episode-3', dramaId: 'drama-a'), isNull);
|
|
});
|
|
}
|