Files
minstrel/flutter_client/test/settings/storage_section_test.dart
T
bvandeusen 3f61079c02 feat(offline): #427 S2(+S3) — two-bucket cache model
Replaces the single 5GB capBytes with independent Liked + Rolling
budgets (5GB each default). Bucket = liked-ness, NOT CacheSource:
a cached track currently in the liked set is charged to / evicted
under Liked; everything else is Rolling. Storage-only dedup — it
never filters playback (S4's offline lists query the whole index).

- db.dart: schema 8→9, AudioCacheIndex.lastPlayedAt (real play
  recency for S4 + rolling LRU; migration backfills to cachedAt).
  drift codegen run.
- cache_settings: likedCapBytes + rollingCapBytes (+ setters); old
  cache_cap_bytes key dropped, defaults reapply (not data loss).
- audio_cache_manager: touch(); bucketUsage() counts orphan
  partials (LockCaching files never indexed) as Rolling so the cap
  truly bounds disk; evictBuckets() drains non-liked LRU then
  sweeps orphans, Liked only by its own (large) cap — normal use
  never evicts the user's liked library.
- prefetcher → evictBuckets with the cached liked set.
- storage_section: two cap selectors + per-bucket usage (folds in
  S3 to avoid a broken intermediate).
- Explicit Download dropped: removed album + playlist Download
  buttons, autoPlaylist pins, now-unused imports.
- Tests updated/compiled (drift-cohort tests are CI-skipped).

High blast radius (eviction deletes files) — liked-protective by
design; needs operator device-check before "done".

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 21:07:45 -04:00

87 lines
3.2 KiB
Dart

@Tags(['drift'])
library;
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:minstrel/auth/auth_provider.dart';
import 'package:minstrel/settings/storage_section.dart';
import 'package:minstrel/theme/theme_data.dart';
class _MockStorage extends Mock implements FlutterSecureStorage {}
// StorageSection.initState calls audioCacheManagerProvider.usageBytes()
// which opens the AppDb via NativeDatabase. CI's flutter-ci runner
// lacks libsqlite3.so so this silently emits a warning today and would
// break under stricter analyze. Skipped with the drift cohort.
//
// testWidgets only accepts a bool for skip (unlike test() which takes
// a String reason). Reason: 'libsqlite3 missing on flutter-ci runner;
// on-device covers'. See Fable #399.
const _skipDrift = true;
void main() {
setUpAll(() {
registerFallbackValue('');
});
testWidgets('renders Storage heading + all controls', skip: _skipDrift, (t) async {
final storage = _MockStorage();
when(() => storage.read(key: any(named: 'key')))
.thenAnswer((_) async => null);
when(() => storage.write(
key: any(named: 'key'), value: any(named: 'value')))
.thenAnswer((_) async {});
await t.pumpWidget(ProviderScope(
overrides: [secureStorageProvider.overrideWithValue(storage)],
child: MaterialApp(
theme: buildDarkTheme(),
home: const Scaffold(body: StorageSection()),
),
));
await t.pumpAndSettle();
expect(find.text('Storage'), findsOneWidget);
expect(find.text('Liked cache limit'), findsOneWidget);
expect(find.text('Recently-played cache limit'), findsOneWidget);
expect(find.text('Pre-fetch ahead'), findsOneWidget);
expect(find.byKey(const Key('clear_cache_button')), findsOneWidget);
expect(find.byKey(const Key('sync_now_button')), findsOneWidget);
expect(find.byKey(const Key('cache_liked_toggle')), findsOneWidget);
expect(find.byKey(const Key('liked_cap_selector')), findsOneWidget);
expect(find.byKey(const Key('rolling_cap_selector')), findsOneWidget);
expect(find.byKey(const Key('prefetch_selector')), findsOneWidget);
});
testWidgets('Clear cache button shows confirm dialog', skip: _skipDrift, (t) async {
final storage = _MockStorage();
when(() => storage.read(key: any(named: 'key')))
.thenAnswer((_) async => null);
when(() => storage.write(
key: any(named: 'key'), value: any(named: 'value')))
.thenAnswer((_) async {});
await t.pumpWidget(ProviderScope(
overrides: [secureStorageProvider.overrideWithValue(storage)],
child: MaterialApp(
theme: buildDarkTheme(),
home: const Scaffold(body: StorageSection()),
),
));
await t.pumpAndSettle();
await t.tap(find.byKey(const Key('clear_cache_button')));
await t.pumpAndSettle();
expect(find.text('Clear cache?'), findsOneWidget);
// Cancel returns to the original state.
await t.tap(find.text('Cancel'));
await t.pumpAndSettle();
expect(find.text('Clear cache?'), findsNothing);
});
}