feat(flutter): drift-first MyQuarantine
Final slice of the smooth-loading pass. Adds CachedQuarantineMine (schema 5, columnar so flag/unflag can do row-level mutation) and rewires MyQuarantineController to read from drift via watch() + SWR refresh; flag/unflag write drift first and roll back on REST failure. Public API (.flag / .unflag / .isHidden) unchanged so existing call sites (library_screen Hidden tab, TrackActionsSheet) keep working. Tests updated to match: bypassed-build-via-_StubController approach no longer makes sense now that state lives in drift, so the suite is rewritten against NativeDatabase.memory() with the same libsqlite3 skip the sync_controller suite uses on the CI runner. The Hidden tab now paints from disk on cold open, the list is queryable offline, and a flag from another device that arrived in this user's quarantine via SSE-triggered invalidate lands the same way as a local flag.
This commit is contained in:
@@ -1,11 +1,23 @@
|
||||
@Tags(['drift'])
|
||||
library;
|
||||
|
||||
import 'package:drift/native.dart' show NativeDatabase;
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'package:minstrel/api/endpoints/quarantine.dart';
|
||||
import 'package:minstrel/models/quarantine_mine.dart';
|
||||
import 'package:minstrel/cache/audio_cache_manager.dart' show appDbProvider;
|
||||
import 'package:minstrel/cache/db.dart';
|
||||
import 'package:minstrel/models/track.dart';
|
||||
import 'package:minstrel/quarantine/quarantine_provider.dart';
|
||||
|
||||
// SKIP NOTE: drift's NativeDatabase needs the system libsqlite3.so.
|
||||
// The flutter-ci runner image doesn't ship it. Same skip as
|
||||
// sync_controller_test.dart — unblock by adding libsqlite3-dev to the
|
||||
// CI image, or switch to sqlite3/wasm. On-device runs cover real
|
||||
// behaviour today.
|
||||
const _skipDrift = 'libsqlite3 missing on flutter-ci runner; on-device covers';
|
||||
|
||||
class _StubQuarantineApi implements QuarantineApi {
|
||||
_StubQuarantineApi({this.shouldThrow = false});
|
||||
bool shouldThrow;
|
||||
@@ -25,13 +37,6 @@ class _StubQuarantineApi implements QuarantineApi {
|
||||
}
|
||||
}
|
||||
|
||||
class _StubController extends MyQuarantineController {
|
||||
_StubController(this._initial);
|
||||
final List<QuarantineMineRow> _initial;
|
||||
@override
|
||||
Future<List<QuarantineMineRow>> build() async => _initial;
|
||||
}
|
||||
|
||||
const _track = TrackRef(
|
||||
id: 't1',
|
||||
title: 'Roygbiv',
|
||||
@@ -44,44 +49,55 @@ const _track = TrackRef(
|
||||
streamUrl: '',
|
||||
);
|
||||
|
||||
const _existing = QuarantineMineRow(
|
||||
trackId: 't1',
|
||||
reason: 'bad_rip',
|
||||
notes: null,
|
||||
createdAt: '2026-05-01T00:00:00Z',
|
||||
trackTitle: 'Roygbiv',
|
||||
trackDurationMs: 137000,
|
||||
albumId: 'a1',
|
||||
albumTitle: 'Geogaddi',
|
||||
artistId: 'ar1',
|
||||
artistName: 'Boards of Canada',
|
||||
);
|
||||
ProviderContainer _container({required AppDb db, required QuarantineApi api}) {
|
||||
return ProviderContainer(overrides: [
|
||||
appDbProvider.overrideWithValue(db),
|
||||
quarantineApiProvider.overrideWith((ref) async => api),
|
||||
]);
|
||||
}
|
||||
|
||||
Future<void> _seed(AppDb db) async {
|
||||
await db.into(db.cachedQuarantineMine).insert(
|
||||
CachedQuarantineMineCompanion.insert(
|
||||
trackId: 't1',
|
||||
reason: 'bad_rip',
|
||||
createdAt: '2026-05-01T00:00:00Z',
|
||||
trackTitle: 'Roygbiv',
|
||||
albumId: 'a1',
|
||||
albumTitle: 'Geogaddi',
|
||||
artistId: 'ar1',
|
||||
artistName: 'Boards of Canada',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('flag prepends synthetic row and calls server', () async {
|
||||
test('flag inserts a drift row and calls the server', skip: _skipDrift,
|
||||
() async {
|
||||
final db = AppDb(NativeDatabase.memory());
|
||||
addTearDown(db.close);
|
||||
final api = _StubQuarantineApi();
|
||||
final container = ProviderContainer(overrides: [
|
||||
quarantineApiProvider.overrideWith((ref) async => api),
|
||||
myQuarantineProvider.overrideWith(() => _StubController(const [])),
|
||||
]);
|
||||
final container = _container(db: db, api: api);
|
||||
addTearDown(container.dispose);
|
||||
|
||||
await container.read(myQuarantineProvider.future);
|
||||
await container.read(myQuarantineProvider.notifier).flag(_track, 'bad_rip', '');
|
||||
await container
|
||||
.read(myQuarantineProvider.notifier)
|
||||
.flag(_track, 'bad_rip', '');
|
||||
|
||||
expect(api.flagCalls, 1);
|
||||
final list = container.read(myQuarantineProvider).value!;
|
||||
expect(list, hasLength(1));
|
||||
expect(list.first.trackId, 't1');
|
||||
expect(list.first.reason, 'bad_rip');
|
||||
final rows = await db.select(db.cachedQuarantineMine).get();
|
||||
expect(rows, hasLength(1));
|
||||
expect(rows.first.trackId, 't1');
|
||||
expect(rows.first.reason, 'bad_rip');
|
||||
});
|
||||
|
||||
test('flag rolls back on server failure', () async {
|
||||
test('flag rolls back the drift insert on server failure', skip: _skipDrift,
|
||||
() async {
|
||||
final db = AppDb(NativeDatabase.memory());
|
||||
addTearDown(db.close);
|
||||
final api = _StubQuarantineApi(shouldThrow: true);
|
||||
final container = ProviderContainer(overrides: [
|
||||
quarantineApiProvider.overrideWith((ref) async => api),
|
||||
myQuarantineProvider.overrideWith(() => _StubController(const [])),
|
||||
]);
|
||||
final container = _container(db: db, api: api);
|
||||
addTearDown(container.dispose);
|
||||
|
||||
await container.read(myQuarantineProvider.future);
|
||||
@@ -91,28 +107,33 @@ void main() {
|
||||
.flag(_track, 'bad_rip', ''),
|
||||
throwsException,
|
||||
);
|
||||
expect(container.read(myQuarantineProvider).value, isEmpty);
|
||||
final rows = await db.select(db.cachedQuarantineMine).get();
|
||||
expect(rows, isEmpty);
|
||||
});
|
||||
|
||||
test('unflag removes the row and calls server', () async {
|
||||
test('unflag deletes the drift row and calls the server', skip: _skipDrift,
|
||||
() async {
|
||||
final db = AppDb(NativeDatabase.memory());
|
||||
addTearDown(db.close);
|
||||
await _seed(db);
|
||||
final api = _StubQuarantineApi();
|
||||
final container = ProviderContainer(overrides: [
|
||||
quarantineApiProvider.overrideWith((ref) async => api),
|
||||
myQuarantineProvider.overrideWith(() => _StubController(const [_existing])),
|
||||
]);
|
||||
final container = _container(db: db, api: api);
|
||||
addTearDown(container.dispose);
|
||||
|
||||
await container.read(myQuarantineProvider.future);
|
||||
await container.read(myQuarantineProvider.notifier).unflag('t1');
|
||||
|
||||
expect(api.unflagCalls, 1);
|
||||
expect(container.read(myQuarantineProvider).value, isEmpty);
|
||||
final rows = await db.select(db.cachedQuarantineMine).get();
|
||||
expect(rows, isEmpty);
|
||||
});
|
||||
|
||||
test('isHidden reflects current state', () async {
|
||||
final container = ProviderContainer(overrides: [
|
||||
myQuarantineProvider.overrideWith(() => _StubController(const [_existing])),
|
||||
]);
|
||||
test('isHidden reflects drift state', skip: _skipDrift, () async {
|
||||
final db = AppDb(NativeDatabase.memory());
|
||||
addTearDown(db.close);
|
||||
await _seed(db);
|
||||
final api = _StubQuarantineApi();
|
||||
final container = _container(db: db, api: api);
|
||||
addTearDown(container.dispose);
|
||||
|
||||
await container.read(myQuarantineProvider.future);
|
||||
|
||||
Reference in New Issue
Block a user