/* eslint-disable @typescript-eslint/no-explicit-any */ /** * The launch decision as a table. Each case maps to one row of the * docs/neo-region-refactor.md decision table. */ import { decideNeoRegion } from '@/src/lib/neo-region-decision' import type { NeoAddress } from '@/src/lib/neo-address' const CACHED_A = { regionId: 1, boundId: 7, regionName: 'Region A', coords: { latitude: 16.7, longitude: 77.9 }, } const FIX_A = { latitude: 16.71, longitude: 77.91 } const FIX_B = { latitude: 18.0, longitude: 79.0 } const RESOLVED_A = { regionId: 1, boundId: 7 } const RESOLVED_B = { regionId: 2, boundId: 8 } function address(overrides: Partial = {}): NeoAddress { return { id: 11, regionId: 1, boundId: 7, name: 'Home', phone: '9876543210', addressLine1: '12 Main Road', addressLine2: null, city: 'Mahabubnagar', state: 'Telangana', isDefault: true, latitude: 16.7, longitude: 77.9, googleMapsUrl: null, ...overrides, } } const A_IN_A = address() const A_IN_B = address({ id: 12, regionId: 2, boundId: 8, isDefault: false, name: 'Work', latitude: 18.0, longitude: 79.0, }) describe('decideNeoRegion', () => { it('1. no cache, no fix, default address exists -> use the address', () => { const decision = decideNeoRegion({ cached: null, fix: null, resolved: null, isAuthenticated: true, addresses: [A_IN_A], }) expect(decision).toMatchObject({ action: 'ready', regionId: 1, boundId: 7, origin: 'address', selectedAddressId: 11, }) }) it('2. no cache, no fix, no address -> address flow', () => { const decision = decideNeoRegion({ cached: null, fix: null, resolved: null, isAuthenticated: true, addresses: [], }) expect(decision).toEqual({ action: 'needs-address-flow' }) }) it('3. no cache, device in B, address in A -> address wins', () => { const decision = decideNeoRegion({ cached: null, fix: FIX_B, resolved: RESOLVED_B, isAuthenticated: true, addresses: [A_IN_A], }) expect(decision).toMatchObject({ action: 'ready', regionId: 1, origin: 'address', selectedAddressId: 11, }) }) it('4. no cache, device in B, no address -> use B', () => { const decision = decideNeoRegion({ cached: null, fix: FIX_B, resolved: RESOLVED_B, isAuthenticated: true, addresses: [], }) expect(decision).toMatchObject({ action: 'ready', regionId: 2, boundId: 8, origin: 'device', selectedAddressId: null, coords: FIX_B, }) }) it('5. cached A, no fix, address in A -> keep A with the address', () => { const decision = decideNeoRegion({ cached: CACHED_A, fix: null, resolved: null, isAuthenticated: true, addresses: [A_IN_A], }) expect(decision).toMatchObject({ action: 'ready', regionId: 1, origin: 'address', selectedAddressId: 11, }) }) it('6. cached A, no fix, no address -> keep the cached region', () => { const decision = decideNeoRegion({ cached: CACHED_A, fix: null, resolved: null, isAuthenticated: true, addresses: [], }) expect(decision).toMatchObject({ action: 'ready', regionId: 1, boundId: 7, origin: 'cache', selectedAddressId: null, }) }) it('7. cached A, device in A, no address in A -> device keeps the region', () => { const decision = decideNeoRegion({ cached: CACHED_A, fix: FIX_A, resolved: RESOLVED_A, isAuthenticated: true, addresses: [A_IN_B], }) expect(decision).toMatchObject({ action: 'ready', regionId: 1, origin: 'device', selectedAddressId: null, }) }) it('8. cached A, device in B, address in B -> use that address', () => { const decision = decideNeoRegion({ cached: CACHED_A, fix: FIX_B, resolved: RESOLVED_B, isAuthenticated: true, addresses: [A_IN_A, A_IN_B], }) expect(decision).toMatchObject({ action: 'ready', regionId: 2, boundId: 8, origin: 'address', selectedAddressId: 12, }) }) it('9. cached A, device in B, addresses only in A -> confirmation', () => { const decision = decideNeoRegion({ cached: CACHED_A, fix: FIX_B, resolved: RESOLVED_B, isAuthenticated: true, addresses: [A_IN_A], }) expect(decision).toEqual({ action: 'needs-confirmation', regionId: 2, boundId: 8, }) }) it('10. cached A, device in B, no addresses -> go ahead with B', () => { const decision = decideNeoRegion({ cached: CACHED_A, fix: FIX_B, resolved: RESOLVED_B, isAuthenticated: true, addresses: [], }) expect(decision).toMatchObject({ action: 'ready', regionId: 2, origin: 'device', selectedAddressId: null, }) }) it('11. cached A, device in B, guest with address data -> go ahead with B', () => { const decision = decideNeoRegion({ cached: CACHED_A, fix: FIX_B, resolved: RESOLVED_B, isAuthenticated: false, addresses: [A_IN_A], }) expect(decision).toMatchObject({ action: 'ready', regionId: 2, origin: 'device', selectedAddressId: null, }) }) it('13. cached A keeps the persisted selection over the default', () => { const office = address({ id: 14, isDefault: false, name: 'Office' }) const decision = decideNeoRegion({ cached: CACHED_A, fix: null, resolved: null, isAuthenticated: true, addresses: [A_IN_A, office], selectedAddressId: 14, }) expect(decision).toMatchObject({ action: 'ready', regionId: 1, origin: 'address', selectedAddressId: 14, }) }) it('14. a persisted selection outside the cached region is ignored', () => { const decision = decideNeoRegion({ cached: CACHED_A, fix: null, resolved: null, isAuthenticated: true, addresses: [A_IN_A, A_IN_B], selectedAddressId: A_IN_B.id, }) expect(decision).toMatchObject({ action: 'ready', regionId: 1, selectedAddressId: A_IN_A.id, }) }) it('12. cached A, device in A, address in A -> the address is selected', () => { const decision = decideNeoRegion({ cached: CACHED_A, fix: FIX_A, resolved: RESOLVED_A, isAuthenticated: true, addresses: [A_IN_A], }) expect(decision).toMatchObject({ action: 'ready', regionId: 1, origin: 'address', selectedAddressId: 11, }) }) })