Files
doormile_milderapp/test/completed_store_test.dart
2026-08-11 13:16:33 +05:30

144 lines
5.3 KiB
Dart

import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:miler/Models/stop_status.dart';
import 'package:miler/data/accepted_store.dart';
/// Finishing a stop is optimistic — the rider is moved on whether or not the
/// status call lands, and the queue endpoints will not report the stop as
/// finished for a poll or two (never, on demo data). This store is the only
/// record in between, and the Activity tab is empty without it.
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
Map<String, dynamic> booking(String id) => {
'orderid': id,
'pickupcustomer': 'Sri Balaji Stores',
'pickupaddress': '19, Thadagam Road, Coimbatore',
};
String today() {
final t = DateTime.now();
return '${t.year}-${t.month.toString().padLeft(2, '0')}-'
'${t.day.toString().padLeft(2, '0')}';
}
setUp(() => SharedPreferences.setMockInitialValues({}));
test('a finished stop is readable, and reads as finished', () async {
await addCompletedBookings([booking('A')]);
final done = await getCompletedBookings();
expect(done, hasLength(1));
expect(done.first['orderid'], 'A');
expect(
stopStatusOf(done.first).isFinishedPickup,
isTrue,
reason: 'Activity filters on the same test Bookings drops a stop with',
);
expect(done.first['completedat'], isNotNull);
});
test('a cancelled stop is stored as cancelled, not picked', () async {
await addCompletedBookings([booking('B')], cancelled: true);
final done = await getCompletedBookings();
expect(done.first['orderstatus'], 'cancelled');
expect(stopStatusOf(done.first).isCancelled, isTrue);
});
test('newest first', () async {
await addCompletedBookings([booking('A')]);
await Future<void>.delayed(const Duration(milliseconds: 5));
await addCompletedBookings([booking('B')]);
final done = await getCompletedBookings();
expect(done.map((b) => b['orderid']).toList(), ['B', 'A']);
});
test('the same stop twice is one row', () async {
await addCompletedBookings([booking('A')]);
await addCompletedBookings([booking('A')]);
expect(await getCompletedBookings(), hasLength(1));
});
// ── The timings ──
//
// Activity's stop detail can only answer "when did I get there" and "how long
// did that door take" if the two stamps survive the stop being closed. They
// live in prefs keys the *next* stop overwrites, so completion is the only
// moment they can be captured — and capturing a stale one is worse than
// capturing none, because another stop's clock reads as fact.
group('timings', () {
Map<String, dynamic> withPickup(String id) =>
{...booking(id), 'pickupid': '77'};
test('the departure and arrival stamps are carried onto the record',
() async {
SharedPreferences.setMockInitialValues({
kStopStartedKey('77'): '2026-08-06T09:12:00.000',
kStopArrivedKey('77'): '2026-08-06T09:31:00.000',
});
await addCompletedBookings([withPickup('A')]);
final done = await getCompletedBookings();
expect(done.single['startedat'], '2026-08-06T09:12:00.000');
expect(done.single['arrivedat'], '2026-08-06T09:31:00.000');
});
test('the keys are cleared, so the next stop cannot inherit them',
() async {
SharedPreferences.setMockInitialValues({
kStopStartedKey('77'): '2026-08-06T09:12:00.000',
kStopArrivedKey('77'): '2026-08-06T09:31:00.000',
});
await addCompletedBookings([withPickup('A')]);
final prefs = await SharedPreferences.getInstance();
expect(prefs.getString(kStopStartedKey('77')), isNull);
expect(prefs.getString(kStopArrivedKey('77')), isNull);
// A second stop sharing the pickup id — a resumed skip worked twice in
// one day — must not report the first attempt's clock.
await addCompletedBookings([withPickup('B')]);
final second = (await getCompletedBookings())
.firstWhere((b) => b['orderid'] == 'B');
expect(second['startedat'], isNull);
});
test('a stop closed without ever opening the map claims no timings',
() async {
// Bulk-completed from Home: nothing wrote a departure, so nothing is
// claimed, and the detail panel says "not recorded" instead of inventing
// a figure that cannot be caught.
await addCompletedBookings([withPickup('A')]);
final done = await getCompletedBookings();
expect(done.single['startedat'], isNull);
expect(done.single['arrivedat'], isNull);
});
});
// The store is per-day: yesterday's work is not today's activity, and a store
// that never forgets grows for the life of the install.
test('yesterday is dropped, today is kept', () async {
SharedPreferences.setMockInitialValues({
'completed_bookings': jsonEncode([
{...booking('OLD'), 'completedday': '2020-01-01', 'orderstatus': 'picked'},
{...booking('NEW'), 'completedday': today(), 'orderstatus': 'picked'},
]),
});
final done = await getCompletedBookings();
expect(done.map((b) => b['orderid']).toList(), ['NEW']);
// And the prune is written back, not recomputed on every read.
final prefs = await SharedPreferences.getInstance();
expect(prefs.getString('completed_bookings'), isNot(contains('OLD')));
});
}