Files
minstrel/flutter_client/test/cache/audio_cache_manager_test.dart
T
bvandeusen 85183c455a test(flutter): re-enable drift tests after ci-flutter:1.26 ships libsqlite3-0
Drops the libsqlite3-missing skip cohort now that the ci-flutter
runner image installs libsqlite3-0 (CI-runner commit on its main).

Per-file removals (no behavior change in tests themselves — they
just stop being skipped):
- `@Tags(['drift'])` + `library;` directive from 5 files.
- `const _skipDrift = ...;` declaration + its rationale comment
  from 6 files (the 5 above + like_button_test.dart, which had its
  own _skipDrift for the rollback-via-drift case).
- `skip: _skipDrift` annotations from 17 test invocations across
  those 6 files (16 single-line + 1 multi-line in like_button).
- Stale `@Tags(['drift']) tier covers it` reference in
  home_screen_test.dart's drift-coverage comment.

Net -79 +18 lines across 7 files; 17 previously-silent tests are
now part of the CI signal. Fable #399.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 18:02:17 -04:00

154 lines
5.0 KiB
Dart

import 'dart:io';
import 'package:dio/dio.dart';
import 'package:drift/drift.dart' show Value;
import 'package:drift/native.dart' show NativeDatabase;
import 'package:flutter_test/flutter_test.dart';
import 'package:minstrel/cache/audio_cache_manager.dart';
import 'package:minstrel/cache/db.dart';
AppDb _testDb() => AppDb(NativeDatabase.memory());
void main() {
test('isCached returns false when no row exists', () async {
final db = _testDb();
addTearDown(db.close);
final mgr = AudioCacheManager(
db: db,
dioFactory: () async => Dio(),
cacheDirFactory: () async => Directory.systemTemp.createTempSync(),
);
expect(await mgr.isCached('nonexistent'), false);
expect(await mgr.pathFor('nonexistent'), null);
});
test('usageBytes sums sizeBytes across rows', () async {
final db = _testDb();
addTearDown(db.close);
final tmp = Directory.systemTemp.createTempSync();
final mgr = AudioCacheManager(
db: db,
dioFactory: () async => Dio(),
cacheDirFactory: () async => tmp,
);
await db.batch((b) {
b.insertAll(db.audioCacheIndex, [
AudioCacheIndexCompanion.insert(
trackId: 'a',
path: 'p',
sizeBytes: 100,
source: CacheSource.manual),
AudioCacheIndexCompanion.insert(
trackId: 'b',
path: 'p',
sizeBytes: 250,
source: CacheSource.incidental),
]);
});
expect(await mgr.usageBytes(), 350);
});
test('rolling cap evicts non-liked LRU; liked protected', () async {
final db = _testDb();
addTearDown(db.close);
final tmp = Directory.systemTemp.createTempSync();
final mgr = AudioCacheManager(
db: db,
dioFactory: () async => Dio(),
cacheDirFactory: () async => tmp,
);
Future<void> mk(String id) async {
final f = File('${tmp.path}/audio_cache/$id.mp3');
await f.create(recursive: true);
await f.writeAsBytes(List.filled(100, 0));
}
await mk('old');
await mk('new');
await mk('lik');
await db.batch((b) {
b.insertAll(db.audioCacheIndex, [
// 'old' has the older lastPlayedAt → evicted first.
AudioCacheIndexCompanion.insert(
trackId: 'old',
path: '${tmp.path}/audio_cache/old.mp3',
sizeBytes: 100,
source: CacheSource.incidental,
lastPlayedAt: Value(DateTime(2020))),
AudioCacheIndexCompanion.insert(
trackId: 'new',
path: '${tmp.path}/audio_cache/new.mp3',
sizeBytes: 100,
source: CacheSource.incidental,
lastPlayedAt: Value(DateTime(2024))),
AudioCacheIndexCompanion.insert(
trackId: 'lik',
path: '${tmp.path}/audio_cache/lik.mp3',
sizeBytes: 100,
source: CacheSource.incidental,
lastPlayedAt: Value(DateTime(2019))),
]);
});
final liked = {'lik'};
final u = await mgr.bucketUsage(liked);
expect(u.liked, 100);
expect(u.rolling, 200);
// Rolling cap fits one 100-byte file; Liked cap huge.
await mgr.evictBuckets(
likedCap: 1 << 30, rollingCap: 100, liked: liked);
expect(await mgr.isCached('old'), false); // oldest rolling, evicted
expect(await mgr.isCached('new'), true); // newer rolling, kept
expect(await mgr.isCached('lik'), true); // liked, protected
});
test('clearAll removes everything including manual', () async {
final db = _testDb();
addTearDown(db.close);
final tmp = Directory.systemTemp.createTempSync();
final mgr = AudioCacheManager(
db: db,
dioFactory: () async => Dio(),
cacheDirFactory: () async => tmp,
);
final f = File('${tmp.path}/audio_cache/man.mp3');
await f.create(recursive: true);
await f.writeAsBytes(List.filled(50, 0));
await db.into(db.audioCacheIndex).insertOnConflictUpdate(
AudioCacheIndexCompanion.insert(
trackId: 'man',
path: f.path,
sizeBytes: 50,
source: CacheSource.manual),
);
expect(await mgr.usageBytes(), 50);
await mgr.clearAll();
expect(await mgr.usageBytes(), 0);
expect(await mgr.isCached('man'), false);
});
test('unpin removes index row + deletes file', () async {
final db = _testDb();
addTearDown(db.close);
final tmp = Directory.systemTemp.createTempSync();
final mgr = AudioCacheManager(
db: db,
dioFactory: () async => Dio(),
cacheDirFactory: () async => tmp,
);
final f = File('${tmp.path}/audio_cache/x.mp3');
await f.create(recursive: true);
await f.writeAsBytes(List.filled(10, 0));
await db.into(db.audioCacheIndex).insertOnConflictUpdate(
AudioCacheIndexCompanion.insert(
trackId: 'x',
path: f.path,
sizeBytes: 10,
source: CacheSource.autoPrefetch),
);
expect(await mgr.isCached('x'), true);
await mgr.unpin('x');
expect(await mgr.isCached('x'), false);
expect(f.existsSync(), false);
});
}