Merge remote-tracking branch 'origin/main'
# Conflicts: # apps/app_seeds/lib/i18n/strings.g.dart # apps/app_seeds/lib/state/inventory_cubit.dart
This commit is contained in:
commit
00db9d4b6a
13 changed files with 218 additions and 17 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:commons_core/commons_core.dart';
|
||||
import 'package:fake_async/fake_async.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tane/data/variety_repository.dart';
|
||||
import 'package:tane/db/database.dart';
|
||||
|
|
@ -8,6 +10,25 @@ import 'package:tane/state/inventory_cubit.dart';
|
|||
|
||||
import '../support/test_support.dart';
|
||||
|
||||
/// A repository whose inventory stream errors for the first
|
||||
/// [failuresBeforeHealthy] subscriptions, then behaves normally — mimics the
|
||||
/// transient DB-not-ready failure that used to leave the spinner stuck forever.
|
||||
/// The default never recovers, for the "give up" path.
|
||||
class _FailingRepository extends VarietyRepository {
|
||||
// ignore: use_super_parameters
|
||||
_FailingRepository(AppDatabase db, {this.failuresBeforeHealthy = 1 << 30})
|
||||
: super(db, idGen: IdGen(), nodeId: 'test-node');
|
||||
|
||||
final int failuresBeforeHealthy;
|
||||
int subscriptions = 0;
|
||||
|
||||
@override
|
||||
Stream<({List<VarietyListItem> items, List<VarietyListItem> drafts})>
|
||||
watchInventoryView() => subscriptions++ < failuresBeforeHealthy
|
||||
? Stream.error(StateError('boom'))
|
||||
: super.watchInventoryView();
|
||||
}
|
||||
|
||||
/// 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 +60,53 @@ void main() {
|
|||
expect(state.items.single.label, 'Maize');
|
||||
});
|
||||
|
||||
group('stream failure', () {
|
||||
test('a transient failure recovers on its own — no error, no manual retry',
|
||||
() async {
|
||||
// Fails the first two subscribes, then the DB is ready.
|
||||
final failing = _FailingRepository(db, failuresBeforeHealthy: 2);
|
||||
await failing.addQuickVariety(label: 'Maize');
|
||||
final failCubit = InventoryCubit(failing);
|
||||
addTearDown(failCubit.close);
|
||||
|
||||
// Auto-retry with backoff brings it back without ever showing an error.
|
||||
final state = await waitFor(failCubit, (s) => !s.loading && s.error == null);
|
||||
expect(state.items.single.label, 'Maize');
|
||||
expect(failing.subscriptions, greaterThan(1));
|
||||
});
|
||||
|
||||
test('stays in loading (spinner), never flips to error, while auto-retrying',
|
||||
() async {
|
||||
final failing = _FailingRepository(db, failuresBeforeHealthy: 2);
|
||||
await failing.addQuickVariety(label: 'Maize');
|
||||
final failCubit = InventoryCubit(failing);
|
||||
addTearDown(failCubit.close);
|
||||
|
||||
final errors = <String?>[];
|
||||
final sub = failCubit.stream.listen((s) => errors.add(s.error));
|
||||
addTearDown(sub.cancel);
|
||||
|
||||
await waitFor(failCubit, (s) => !s.loading && s.error == null);
|
||||
expect(errors.every((e) => e == null), isTrue,
|
||||
reason: 'no error state should be emitted during auto-recovery');
|
||||
});
|
||||
|
||||
test('gives up after the retry budget and surfaces a manual-retry error',
|
||||
() {
|
||||
fakeAsync((async) {
|
||||
final failing = _FailingRepository(db); // never recovers
|
||||
final failCubit = InventoryCubit(failing);
|
||||
addTearDown(failCubit.close);
|
||||
|
||||
// Fast-forward past all backoff windows (~12s of retries).
|
||||
async.elapse(const Duration(seconds: 30));
|
||||
|
||||
expect(failCubit.state.loading, isFalse);
|
||||
expect(failCubit.state.error, contains('boom'));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue