/** * COMMON-17..29 — `common.getStoresSummary` (§5.3) and * `common.checkLocationInPolygon` (§5.4). * * `getStoresSummary` is `protectedProcedure` scoped to the staff region and * returns the region's own stores plus globals. The catalogue expects a product * count per store; the picker returns none — pinned `(current)`. */ import { apiClient, expectTrpcError, uniqueSuffix, type ApiClient } from '../helpers/api' import { anonClient, staffClient, superadminClient } from '../helpers/sessions' import { createStore, COORDS, PASSWORD } from '../helpers/fixtures' import { TEST_OTHER_REGION_ID } from '../helpers/env' jest.setTimeout(180_000) async function createStaffToken(regionId: number): Promise { const superadmin = await superadminClient() const roles: any = await superadmin.superadmin.staff.roles.query() const roleId = Number(roles.roles.find((r: any) => r.name === 'admin')?.id ?? roles.roles[0].id) const name = `e2e_store_${regionId}_${uniqueSuffix()}` await superadmin.superadmin.staff.create.mutate({ name, password: PASSWORD, roleId, regionId }) const login: any = await apiClient().admin.staffUser.login.mutate({ name, password: PASSWORD }) if (!login?.token) throw new Error('[api-tests] region staff login returned no token') return login.token } let region2Promise: Promise<{ token: string; client: ApiClient; storeId: number }> | null = null function region2(): Promise<{ token: string; client: ApiClient; storeId: number }> { region2Promise ??= (async () => { const token = await createStaffToken(TEST_OTHER_REGION_ID) const client = apiClient(token) const res: any = await client.admin.store.createStore.mutate({ name: `E2E R2 Store ${uniqueSuffix()}`, }) return { token, client, storeId: Number(res.store.id) } })() return region2Promise } /* -------------------------------------------------------------------------- */ /* 5.3 getStoresSummary */ /* -------------------------------------------------------------------------- */ describe('common.getStoresSummary (COMMON-17..21)', () => { it('COMMON-17 (current): staff call returns id/name/description/isGlobal (no productCount)', async () => { const admin = await staffClient() const store = await createStore() const res: any = await admin.common.getStoresSummary.query() expect(Array.isArray(res.stores)).toBe(true) const entry = res.stores.find((s: any) => s.id === store.storeId) expect(entry).toEqual( expect.objectContaining({ id: store.storeId, name: store.storeName }) ) expect(entry).toHaveProperty('description') expect(entry).toHaveProperty('isGlobal') // The catalogue expects `productCount`; the summary does not expose one. expect(entry.productCount).toBeUndefined() }) it('COMMON-18 (current): a region with no own stores still lists global stores', async () => { // `getStoresSummary` always includes globals, so a brand-new region is never // empty; the pinned contract is that every row is a global store. const { createRegion } = await import('../helpers/fixtures') const region = await createRegion() const token = await createStaffToken(region.regionId) const res: any = await apiClient(token).common.getStoresSummary.query() expect(res.stores.every((s: any) => s.isGlobal === true)).toBe(true) }) it('COMMON-19 (current): a user token passes the guard but null staffUser → 500, no leak', async () => { const { userSession } = await import('../helpers/sessions') const user = apiClient((await userSession()).token) const error = await expectTrpcError( () => user.common.getStoresSummary.query(), 'INTERNAL_SERVER_ERROR', 'regionId' ) expect(JSON.stringify(error)).not.toContain('Meat Store') }) it('COMMON-20: no token is 401', async () => { await expectTrpcError(() => anonClient().common.getStoresSummary.query(), 'UNAUTHORIZED') }) it('COMMON-21: cross-region staff never sees another region’s store', async () => { const r2 = await region2() const local = await createStore() const r1: any = await (await staffClient()).common.getStoresSummary.query() expect(r1.stores.some((s: any) => s.id === local.storeId)).toBe(true) expect(r1.stores.some((s: any) => s.id === r2.storeId)).toBe(false) const r2Res: any = await r2.client.common.getStoresSummary.query() expect(r2Res.stores.some((s: any) => s.id === r2.storeId)).toBe(true) expect(r2Res.stores.some((s: any) => s.id === local.storeId)).toBe(false) }) }) /* -------------------------------------------------------------------------- */ /* 5.4 checkLocationInPolygon */ /* -------------------------------------------------------------------------- */ describe('common.checkLocationInPolygon (COMMON-22..29)', () => { it('COMMON-22: a point inside the Mahabubnagar polygon is inside', async () => { const res: any = await anonClient().common.checkLocationInPolygon.query({ lat: COORDS.region1Bound1.lat, lng: COORDS.region1Bound1.lng, }) expect(res).toEqual({ isInside: true }) }) it('COMMON-23: a point outside every polygon is not inside', async () => { const res: any = await anonClient().common.checkLocationInPolygon.query({ lat: 0, lng: 0 }) expect(res).toEqual({ isInside: false }) }) it('COMMON-24: a boundary vertex has a deterministic result', async () => { // A vertex of the mbnr polygon (lng,lat): [78.0540565157155, 16.750355644371382]. const input = { lat: 16.750355644371382, lng: 78.0540565157155 } const first: any = await anonClient().common.checkLocationInPolygon.query(input) const second: any = await anonClient().common.checkLocationInPolygon.query(input) expect(typeof first.isInside).toBe('boolean') expect(second).toEqual(first) }) it('COMMON-25: extreme-but-valid coordinates are accepted', async () => { const anon = anonClient() const north: any = await anon.common.checkLocationInPolygon.query({ lat: 90, lng: 180 }) const south: any = await anon.common.checkLocationInPolygon.query({ lat: -90, lng: -180 }) expect(typeof north.isInside).toBe('boolean') expect(typeof south.isInside).toBe('boolean') }) it('COMMON-26: lat > 90 or lng > 180 is BAD_REQUEST', async () => { const anon = anonClient() await expectTrpcError( () => anon.common.checkLocationInPolygon.query({ lat: 90.0001, lng: 78 }), 'BAD_REQUEST' ) await expectTrpcError( () => anon.common.checkLocationInPolygon.query({ lat: 16, lng: 180.0001 }), 'BAD_REQUEST' ) }) it('COMMON-27: a non-number coordinate is BAD_REQUEST', async () => { await expectTrpcError( () => anonClient().common.checkLocationInPolygon.query({ lat: 'x', lng: 78 } as any), 'BAD_REQUEST' ) }) it('COMMON-28: (0,0) is false without crashing', async () => { const res: any = await anonClient().common.checkLocationInPolygon.query({ lat: 0, lng: 0 }) expect(res.isInside).toBe(false) }) it('COMMON-29: the check is public (no token required)', async () => { const res: any = await anonClient().common.checkLocationInPolygon.query({ lat: COORDS.region1Bound2.lat, lng: COORDS.region1Bound2.lng, }) expect(res).toEqual({ isInside: true }) }) })