import 'package:flutter_test/flutter_test.dart'; import 'package:miler/data/milk_run.dart'; import 'package:miler/data/pickup_locations.dart'; /// ───────────────────────────────────────────────────────────────────────── /// THE PICKUP IS NAMED BY THE TENANT'S LOCATION LIST /// /// The route card's heading — the biggest type on Home — read `Sudharsan`, /// a contact person, above a distance and an ETA to a place that name does not /// identify. It came from the booking row's `sourcename`, which the backend /// fills from `providercompany` / `providerlocation`. /// /// The app cannot tell a person from a shop by looking at the string, so the /// name is **joined** instead: the booking carries the location id, and /// `GET /admin/tenants/:id/locations` carries the names. /// /// The half that matters most is the fallback. That route is under `/admin`, /// and whether a rider's token is admitted to it is the backend's call — so /// every one of these proves the app is no worse off than before when the /// table is empty. /// ───────────────────────────────────────────────────────────────────────── void main() { setUp(PickupLocations.reset); tearDown(PickupLocations.reset); Map stop({ Object? locationId, String sourceName = '', }) => { 'orderid': 'A1', if (locationId != null) 'pickuplocationid': locationId, 'sourcename': sourceName, }; group('with the tenant list loaded', () { test('the location name replaces the person on the row', () { PickupLocations.seed({'7': 'Sri Balaji Stores'}); expect( MilkRun.sourceNameOf(stop(locationId: 7, sourceName: 'Sudharsan')), 'Sri Balaji Stores', ); }); test('it also fills a row that names nothing at all', () { PickupLocations.seed({'7': 'Gandhipuram Counter'}); expect( MilkRun.sourceNameOf(stop(locationId: 7)), 'Gandhipuram Counter', ); }); test('`sourceid` and `kitchenid` join on the same table', () { PickupLocations.seed({'9': 'Peelamedu Kitchen'}); expect( MilkRun.sourceNameOf({'orderid': 'A1', 'sourceid': 9}), 'Peelamedu Kitchen', ); expect( MilkRun.sourceNameOf({'orderid': 'A1', 'kitchenid': '9'}), 'Peelamedu Kitchen', ); }); test('an id the tenant list does not hold falls back to the row', () { PickupLocations.seed({'7': 'Sri Balaji Stores'}); expect( MilkRun.sourceNameOf(stop(locationId: 99, sourceName: 'Sudharsan')), 'Sudharsan', reason: 'a partial table must not blank out the names it does have', ); }); }); group('with no tenant list — the admin route refused, or has not answered', () { test('the row is read exactly as it was before', () { expect( MilkRun.sourceNameOf(stop(locationId: 7, sourceName: 'Sudharsan')), 'Sudharsan', ); expect(MilkRun.sourceNameOf(stop(locationId: 7)), ''); }); test('and grouping still works off the id', () { // The group key was never the name — it is the counter's id — so a // missing table cannot merge two kitchens into one card. final a = stop(locationId: 7); final b = stop(locationId: 8); expect(MilkRun.sameSource(a, a), isTrue); expect(MilkRun.sameSource(a, b), isFalse); }); }); group('the lookup itself', () { test('an absent, empty or zero id is never a name', () { PickupLocations.seed({'7': 'Sri Balaji Stores', '0': 'Nowhere'}); expect(PickupLocations.nameFor(null), ''); expect(PickupLocations.nameFor(''), ''); expect(PickupLocations.nameFor(' '), ''); expect( PickupLocations.nameFor(0), '', reason: '0 is how this backend spells "unset", not a location', ); expect(PickupLocations.nameFor(7), 'Sri Balaji Stores'); expect(PickupLocations.nameFor('7'), 'Sri Balaji Stores'); }); test('reset empties it, so a sign-out cannot leak names across riders', () { PickupLocations.seed({'7': 'Sri Balaji Stores'}); expect(PickupLocations.isLoaded, isTrue); PickupLocations.reset(); expect(PickupLocations.isLoaded, isFalse); expect(PickupLocations.nameFor(7), ''); }); }); }