Files
minstrel/flutter_client/test/cache/audio_cache_manager_test.dart
T
bvandeusen 0d171490c3 fix(flutter): analyze errors after Plan B push
Cleared 5 errors + 1 warning + 1 info from CI flutter analyze on 5114a81:

- prefetcher.dart: Riverpod listener callbacks used (_, _) for two
  placeholder params — Dart 3 enforces unique names. Changed to (_, __).
- prefetcher.dart: Riverpod 3's AsyncValue exposes `.value` (nullable)
  not `.valueOrNull`. Three call sites updated.
- sync_controller.dart: doc comment had `?since=<cursor>` → analyzer
  warned about unintended HTML. Wrapped in backticks with `{cursor}`.
- sync_controller.dart: delete loop over heterogeneous Table list
  inferred as List<Table>; drift's delete() expects TableInfo. Unrolled
  to explicit per-table deletes.
- audio_cache_manager_test.dart: db.into(...).insertAll([...]) doesn't
  exist on InsertStatement — only insert/insertOnConflictUpdate. Used
  db.batch((b) => b.insertAll(table, [...])) instead, in two test cases.
- audio_handler.dart: LockCachingAudioSource is marked experimental in
  just_audio. Added // ignore: experimental_member_use — operator
  acknowledged the experimental status during brainstorming and we're
  the project; CI's --fatal-infos would otherwise gate.

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

136 lines
4.2 KiB
Dart

import 'dart:io';
import 'package:dio/dio.dart';
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('eviction order: incidental first, manual never', () async {
final db = _testDb();
addTearDown(db.close);
final tmp = Directory.systemTemp.createTempSync();
final mgr = AudioCacheManager(
db: db,
dioFactory: () async => Dio(),
cacheDirFactory: () async => tmp,
);
final f1 = File('${tmp.path}/audio_cache/inc.mp3');
await f1.create(recursive: true);
await f1.writeAsBytes(List.filled(100, 0));
final f2 = File('${tmp.path}/audio_cache/man.mp3');
await f2.create(recursive: true);
await f2.writeAsBytes(List.filled(100, 0));
await db.batch((b) {
b.insertAll(db.audioCacheIndex, [
AudioCacheIndexCompanion.insert(
trackId: 'inc',
path: f1.path,
sizeBytes: 100,
source: CacheSource.incidental),
AudioCacheIndexCompanion.insert(
trackId: 'man',
path: f2.path,
sizeBytes: 100,
source: CacheSource.manual),
]);
});
expect(await mgr.usageBytes(), 200);
await mgr.evict(targetBytes: 100);
expect(await mgr.isCached('inc'), false);
expect(await mgr.isCached('man'), true);
});
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);
});
}