e38189470b
The cacheFirst fix in 5511f87 added a yield after fetchAndPopulate
so streams never hang when populate is a no-op for this filter
(the liked-tab spinner-forever bug). Test expectation updated: the
first emission after an empty drift is now the still-empty yield
("we tried, nothing to show yet"), and the simulated drift re-emit
yields the populated rows as the second emission.
79 lines
2.6 KiB
Dart
79 lines
2.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:minstrel/cache/cache_first.dart';
|
|
|
|
void main() {
|
|
test('non-empty drift emission passes through toResult', () async {
|
|
final controller = StreamController<List<int>>();
|
|
final results = cacheFirst<int, int>(
|
|
driftStream: controller.stream,
|
|
fetchAndPopulate: () async => fail('should not fetch when rows present'),
|
|
toResult: (rows) => rows.fold<int>(0, (a, b) => a + b),
|
|
isOnline: () async => true,
|
|
);
|
|
|
|
final firstFuture = results.first;
|
|
controller.add([1, 2, 3]);
|
|
expect(await firstFuture, 6);
|
|
await controller.close();
|
|
});
|
|
|
|
test('empty drift emission + online triggers fetchAndPopulate', () async {
|
|
final controller = StreamController<List<int>>();
|
|
var fetchCalled = 0;
|
|
final results = cacheFirst<int, int>(
|
|
driftStream: controller.stream,
|
|
fetchAndPopulate: () async {
|
|
fetchCalled++;
|
|
controller.add([42]); // simulate populate causing re-emission
|
|
},
|
|
toResult: (rows) => rows.fold<int>(0, (a, b) => a + b),
|
|
isOnline: () async => true,
|
|
);
|
|
|
|
// Post-coldFetchAttempted guard, cacheFirst yields the current
|
|
// (still-empty) rows after the fetch returns, so the stream
|
|
// never hangs when populate is a no-op for this filter. The
|
|
// simulated drift re-emit then yields the populated rows.
|
|
final emissions = await results.take(2).toList();
|
|
expect(emissions, [0, 42]);
|
|
expect(fetchCalled, 1);
|
|
await controller.close();
|
|
});
|
|
|
|
test('empty drift + offline yields empty result without fetch', () async {
|
|
final controller = StreamController<List<int>>();
|
|
var fetchCalled = 0;
|
|
final results = cacheFirst<int, int>(
|
|
driftStream: controller.stream,
|
|
fetchAndPopulate: () async {
|
|
fetchCalled++;
|
|
},
|
|
toResult: (rows) => rows.fold<int>(0, (a, b) => a + b),
|
|
isOnline: () async => false,
|
|
);
|
|
|
|
final firstFuture = results.first;
|
|
controller.add([]);
|
|
expect(await firstFuture, 0);
|
|
expect(fetchCalled, 0);
|
|
await controller.close();
|
|
});
|
|
|
|
test('REST failure falls through to empty result', () async {
|
|
final controller = StreamController<List<int>>();
|
|
final results = cacheFirst<int, int>(
|
|
driftStream: controller.stream,
|
|
fetchAndPopulate: () async => throw Exception('REST 500'),
|
|
toResult: (rows) => rows.fold<int>(0, (a, b) => a + b),
|
|
isOnline: () async => true,
|
|
);
|
|
|
|
final firstFuture = results.first;
|
|
controller.add([]);
|
|
expect(await firstFuture, 0);
|
|
await controller.close();
|
|
});
|
|
}
|