fix(inventory): recover from a stuck loading spinner

The inventory stream subscription had no onError handler, so a transient
failure at startup (e.g. the encrypted DB not yet ready) went unhandled
and left loading=true forever — the spinner that only a restart cleared.

Handle stream errors: drop out of loading, surface an error state, and
offer a retry that re-opens the stream. Add a _LoadError view (i18n en/
es/ast/pt) and cover both the failure and the retry-recovers paths.
This commit is contained in:
vjrj 2026-07-10 22:12:24 +02:00
parent bb4ee2fd89
commit f45c452615
12 changed files with 158 additions and 17 deletions

View file

@ -1,5 +1,6 @@
import 'dart:typed_data';
import 'package:commons_core/commons_core.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/data/variety_repository.dart';
import 'package:tane/db/database.dart';
@ -8,6 +9,22 @@ import 'package:tane/state/inventory_cubit.dart';
import '../support/test_support.dart';
/// A repository whose inventory stream errors until [healthy] is flipped
/// mimics the transient DB-not-ready failure that used to leave the spinner
/// stuck forever.
class _FailingRepository extends VarietyRepository {
// ignore: use_super_parameters
_FailingRepository(AppDatabase db)
: super(db, idGen: IdGen(), nodeId: 'test-node');
bool healthy = false;
@override
Stream<({List<VarietyListItem> items, List<VarietyListItem> drafts})>
watchInventoryView() =>
healthy ? super.watchInventoryView() : Stream.error(StateError('boom'));
}
/// Waits until the cubit's state satisfies [predicate], whether it already
/// does or a later stream emission gets it there.
Future<InventoryState> waitFor(
@ -39,6 +56,34 @@ void main() {
expect(state.items.single.label, 'Maize');
});
group('stream failure', () {
test('a failing stream drops loading and surfaces an error, not a '
'forever-spinner', () async {
final failing = _FailingRepository(db);
final failCubit = InventoryCubit(failing);
addTearDown(failCubit.close);
final state = await waitFor(failCubit, (s) => s.error != null);
expect(state.loading, isFalse);
expect(state.error, contains('boom'));
});
test('retry re-opens the stream and recovers once the DB is healthy',
() async {
final failing = _FailingRepository(db);
final failCubit = InventoryCubit(failing);
addTearDown(failCubit.close);
await waitFor(failCubit, (s) => s.error != null);
await failing.addQuickVariety(label: 'Maize');
failing.healthy = true;
failCubit.retry();
final state = await waitFor(failCubit, (s) => s.error == null && !s.loading);
expect(state.items.single.label, 'Maize');
});
});
test('drafts stay in the tray and never mix into the list', () async {
await repo.addDraftVariety(Uint8List.fromList([1, 2, 3]));
var state = await waitFor(cubit, (s) => s.drafts.isNotEmpty);