364 lines
14 KiB
TypeScript
364 lines
14 KiB
TypeScript
/**
|
||
* COMMON-53..78 — region/bound resolution (§5.8..5.11):
|
||
* resolveRegionByLocation, resolveBound, regionSettings, activeBound,
|
||
* essentialConsts.
|
||
*
|
||
* All are `publicProcedure`: an anonymous client works and, where a signed-in
|
||
* user is relevant (`activeBound`), the thrown-away user's token is passed.
|
||
*
|
||
* Rows that describe behaviour the API cannot reach are pinned `(current)`:
|
||
* - COMMON-66 (a region with no bound) — the API refuses to create one.
|
||
* - COMMON-78 (`webviewHtml` for a raw/mf Host) — `fetch` cannot set `Host`.
|
||
*/
|
||
|
||
import {
|
||
apiClient,
|
||
expectTrpcError,
|
||
firstBatchResult,
|
||
rawQuery,
|
||
uniqueSuffix,
|
||
} from '../helpers/api'
|
||
import { anonClient, superadminClient } from '../helpers/sessions'
|
||
import {
|
||
COORDS,
|
||
createAddress,
|
||
createRegion,
|
||
getBoundSetting,
|
||
registerThrowawayUser,
|
||
squareGeojson,
|
||
withBoundSetting,
|
||
} from '../helpers/fixtures'
|
||
import { TEST_OTHER_BOUND_ID, TEST_OTHER_REGION_ID } from '../helpers/env'
|
||
|
||
jest.setTimeout(180_000)
|
||
|
||
const REGION_1 = 1
|
||
|
||
/** A fresh region with two non-overlapping bounds (priority 1 and 2). */
|
||
async function createTwoBoundRegion(): Promise<{
|
||
regionId: number
|
||
boundA: { id: number; lat: number; lng: number }
|
||
boundB: { id: number; lat: number; lng: number }
|
||
}> {
|
||
const superadmin = await superadminClient()
|
||
const a = { lat: 15.0, lng: 77.0 }
|
||
const b = { lat: 16.0, lng: 78.0 }
|
||
const res: any = await superadmin.superadmin.regions.create.mutate({
|
||
name: `E2E 2Bound ${uniqueSuffix()}`,
|
||
description: 'Created by backend API tests',
|
||
center: a,
|
||
bounds: [
|
||
{ description: 'A', geojson: squareGeojson(a.lat, a.lng), priority: '1' },
|
||
{ description: 'B', geojson: squareGeojson(b.lat, b.lng), priority: '2' },
|
||
],
|
||
})
|
||
const bounds: any[] = res.bounds ?? []
|
||
const boundA = bounds.find((x: any) => String(x.priority) === '1')
|
||
const boundB = bounds.find((x: any) => String(x.priority) === '2')
|
||
if (!res?.id || !boundA || !boundB) {
|
||
throw new Error(`[api-tests] two-bound region create failed: ${JSON.stringify(res)}`)
|
||
}
|
||
return {
|
||
regionId: Number(res.id),
|
||
boundA: { id: Number(boundA.id), ...a },
|
||
boundB: { id: Number(boundB.id), ...b },
|
||
}
|
||
}
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* 5.8 resolveRegionByLocation */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('common.resolveRegionByLocation (COMMON-53..58)', () => {
|
||
it('COMMON-53: a point inside region 1 bound 1 resolves region + lowest-priority bound', async () => {
|
||
const res: any = await anonClient().common.resolveRegionByLocation.query({
|
||
lat: COORDS.region1Bound1.lat,
|
||
lng: COORDS.region1Bound1.lng,
|
||
})
|
||
expect(res).toBeTruthy()
|
||
expect(res.region.id).toBe(REGION_1)
|
||
expect(res.bound.id).toBe(1)
|
||
expect(res.bound.priority).toBe('1')
|
||
expect(res.region.center).toBeTruthy()
|
||
})
|
||
|
||
it('COMMON-54: a point outside every bound is null', async () => {
|
||
const res = await anonClient().common.resolveRegionByLocation.query({ lat: 0, lng: 0 })
|
||
expect(res).toBeNull()
|
||
})
|
||
|
||
it('COMMON-55: where bounds overlap, the lowest priority number wins', async () => {
|
||
// region1Bound2 sits inside both bound 1 and bound 2; bound 1 (priority 1) wins.
|
||
const res: any = await anonClient().common.resolveRegionByLocation.query({
|
||
lat: COORDS.region1Bound2.lat,
|
||
lng: COORDS.region1Bound2.lng,
|
||
})
|
||
expect(res.region.id).toBe(REGION_1)
|
||
expect(res.bound.id).toBe(1)
|
||
})
|
||
|
||
it('COMMON-56: an invalid lat/lng is BAD_REQUEST', async () => {
|
||
const anon = anonClient()
|
||
await expectTrpcError(
|
||
() => anon.common.resolveRegionByLocation.query({ lat: 91, lng: 78 }),
|
||
'BAD_REQUEST'
|
||
)
|
||
await expectTrpcError(
|
||
() => anon.common.resolveRegionByLocation.query({ lat: 16, lng: 181 }),
|
||
'BAD_REQUEST'
|
||
)
|
||
await expectTrpcError(
|
||
() => anon.common.resolveRegionByLocation.query({ lat: 'x', lng: 78 } as any),
|
||
'BAD_REQUEST'
|
||
)
|
||
})
|
||
|
||
it('COMMON-57: a boundary point resolves deterministically', async () => {
|
||
// A vertex of region 1 bound 1's polygon.
|
||
const input = { lat: 16.7224827, lng: 77.9802307 }
|
||
const anon = anonClient()
|
||
const first: any = await anon.common.resolveRegionByLocation.query(input)
|
||
const second: any = await anon.common.resolveRegionByLocation.query(input)
|
||
expect(second).toEqual(first)
|
||
})
|
||
|
||
it('COMMON-58: the resolver is public (no token)', async () => {
|
||
const res: any = await anonClient().common.resolveRegionByLocation.query({
|
||
lat: COORDS.region1Bound1.lat,
|
||
lng: COORDS.region1Bound1.lng,
|
||
})
|
||
expect(res.bound.id).toBe(1)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* 5.9 resolveBound */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('common.resolveBound (COMMON-59..64)', () => {
|
||
it('COMMON-59: a point in region 1 resolves {regionId,boundId}', async () => {
|
||
const res: any = await anonClient().common.resolveBound.query({
|
||
regionId: REGION_1,
|
||
lat: COORDS.region1Bound1.lat,
|
||
lng: COORDS.region1Bound1.lng,
|
||
})
|
||
expect(res).toEqual({ regionId: REGION_1, boundId: 1 })
|
||
})
|
||
|
||
it('COMMON-60: a point outside the region is 422', async () => {
|
||
await expectTrpcError(
|
||
() => anonClient().common.resolveBound.query({ regionId: REGION_1, lat: 0, lng: 0 }),
|
||
'UNPROCESSABLE_CONTENT'
|
||
)
|
||
})
|
||
|
||
it('COMMON-61: an unknown region is 422', async () => {
|
||
await expectTrpcError(
|
||
() =>
|
||
anonClient().common.resolveBound.query({
|
||
regionId: 999_999,
|
||
lat: COORDS.region1Bound1.lat,
|
||
lng: COORDS.region1Bound1.lng,
|
||
}),
|
||
'UNPROCESSABLE_CONTENT'
|
||
)
|
||
})
|
||
|
||
it('COMMON-62: overlapping bounds resolve to the lowest priority', async () => {
|
||
const res: any = await anonClient().common.resolveBound.query({
|
||
regionId: REGION_1,
|
||
lat: COORDS.region1Bound2.lat,
|
||
lng: COORDS.region1Bound2.lng,
|
||
})
|
||
expect(res.boundId).toBe(1)
|
||
})
|
||
|
||
it('COMMON-63: invalid inputs are BAD_REQUEST', async () => {
|
||
const anon = anonClient()
|
||
await expectTrpcError(
|
||
() => anon.common.resolveBound.query({ regionId: -1, lat: 16, lng: 78 }),
|
||
'BAD_REQUEST'
|
||
)
|
||
await expectTrpcError(
|
||
() => anon.common.resolveBound.query({ regionId: REGION_1, lat: 91, lng: 78 }),
|
||
'BAD_REQUEST'
|
||
)
|
||
})
|
||
|
||
it('COMMON-64: resolveBound is public (no token)', async () => {
|
||
const res: any = await anonClient().common.resolveBound.query({
|
||
regionId: REGION_1,
|
||
lat: COORDS.region1Bound1.lat,
|
||
lng: COORDS.region1Bound1.lng,
|
||
})
|
||
expect(res.boundId).toBe(1)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* 5.10 regionSettings / activeBound */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('common.regionSettings / activeBound (COMMON-65..73)', () => {
|
||
it('COMMON-65: regionSettings is the region merged with its effective bound', async () => {
|
||
const anon = anonClient()
|
||
const boundDeliveryCharge = await getBoundSetting(1, 'deliveryCharge')
|
||
const res: any = await anon.common.regionSettings.query({ regionId: REGION_1 })
|
||
|
||
expect(res.regionId).toBe(REGION_1)
|
||
expect(res.boundId).toBe(1)
|
||
expect(res.priority).toBe('1')
|
||
expect(res.settings).toEqual(expect.any(Object))
|
||
expect(String(res.settings.deliveryCharge)).toBe(String(boundDeliveryCharge))
|
||
|
||
// A temporary bound setting flows straight through (bound wins over region).
|
||
await withBoundSetting(1, 'deliveryCharge', '777', async () => {
|
||
const during: any = await anon.common.regionSettings.query({ regionId: REGION_1 })
|
||
expect(String(during.settings.deliveryCharge)).toBe('777')
|
||
})
|
||
expect(await getBoundSetting(1, 'deliveryCharge')).toBe(String(boundDeliveryCharge))
|
||
})
|
||
|
||
it('COMMON-66 (current): a region with no bound cannot be created through the API', async () => {
|
||
// `regions.create` always seeds at least one bound and refuses a boundless
|
||
// region, so the "no bound → 422" branch is only reachable for an unknown
|
||
// region id. Pinned against that same 422 path.
|
||
await expectTrpcError(
|
||
() => anonClient().common.regionSettings.query({ regionId: 999_999 }),
|
||
'UNPROCESSABLE_CONTENT'
|
||
)
|
||
})
|
||
|
||
it('COMMON-67: an unknown region is 422', async () => {
|
||
await expectTrpcError(
|
||
() => anonClient().common.regionSettings.query({ regionId: 999_999 }),
|
||
'UNPROCESSABLE_CONTENT'
|
||
)
|
||
})
|
||
|
||
it('COMMON-68: signed-out activeBound is the region’s effective bound', async () => {
|
||
const res: any = await anonClient().common.activeBound.query({ regionId: REGION_1 })
|
||
expect(res).toEqual({ regionId: REGION_1, boundId: 1 })
|
||
})
|
||
|
||
it('COMMON-69: a user address in the region drives activeBound', async () => {
|
||
const user = await registerThrowawayUser()
|
||
const address: any = await createAddress(user.token)
|
||
expect(address.boundId).toBe(1)
|
||
|
||
const res: any = await apiClient(user.token).common.activeBound.query({ regionId: REGION_1 })
|
||
expect(res).toEqual({ regionId: REGION_1, boundId: 1 })
|
||
})
|
||
|
||
it('COMMON-70: two addresses in a region resolve to the lowest priority bound', async () => {
|
||
const region = await createTwoBoundRegion()
|
||
const user = await registerThrowawayUser()
|
||
const a: any = await createAddress(user.token, {
|
||
regionId: region.regionId,
|
||
latitude: region.boundA.lat,
|
||
longitude: region.boundA.lng,
|
||
})
|
||
const b: any = await createAddress(user.token, {
|
||
regionId: region.regionId,
|
||
latitude: region.boundB.lat,
|
||
longitude: region.boundB.lng,
|
||
isDefault: false,
|
||
})
|
||
expect(a.boundId).toBe(region.boundA.id)
|
||
expect(b.boundId).toBe(region.boundB.id)
|
||
|
||
const res: any = await apiClient(user.token).common.activeBound.query({
|
||
regionId: region.regionId,
|
||
})
|
||
expect(res.boundId).toBe(region.boundA.id)
|
||
})
|
||
|
||
it('COMMON-71: a user with an address only in another region does not leak', async () => {
|
||
const user = await registerThrowawayUser()
|
||
const address: any = await createAddress(user.token, { regionId: TEST_OTHER_REGION_ID })
|
||
expect(address.regionId).toBe(TEST_OTHER_REGION_ID)
|
||
expect(address.boundId).toBe(TEST_OTHER_BOUND_ID)
|
||
|
||
const client = apiClient(user.token)
|
||
const region1: any = await client.common.activeBound.query({ regionId: REGION_1 })
|
||
expect(region1).toEqual({ regionId: REGION_1, boundId: 1 })
|
||
|
||
const region2: any = await client.common.activeBound.query({
|
||
regionId: TEST_OTHER_REGION_ID,
|
||
})
|
||
expect(region2.boundId).toBe(TEST_OTHER_BOUND_ID)
|
||
})
|
||
|
||
it('COMMON-72: no bound resolves to boundId null', async () => {
|
||
const res: any = await anonClient().common.activeBound.query({ regionId: 999_999 })
|
||
expect(res).toEqual({ regionId: 999_999, boundId: null })
|
||
})
|
||
|
||
it('COMMON-73: an invalid region is BAD_REQUEST', async () => {
|
||
const anon = anonClient()
|
||
await expectTrpcError(
|
||
() => anon.common.activeBound.query({ regionId: -1 }),
|
||
'BAD_REQUEST'
|
||
)
|
||
await expectTrpcError(
|
||
() => anon.common.activeBound.query({ regionId: 0 }),
|
||
'BAD_REQUEST'
|
||
)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* 5.11 essentialConsts procedure-level cases */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('common.essentialConsts procedure cases (COMMON-74..78)', () => {
|
||
it('COMMON-74: a missing boundId input is BAD_REQUEST', async () => {
|
||
await expectTrpcError(
|
||
() => anonClient().common.essentialConsts.query({} as any),
|
||
'BAD_REQUEST'
|
||
)
|
||
})
|
||
|
||
it('COMMON-75: boundId 0 / negative / float is BAD_REQUEST', async () => {
|
||
const anon = anonClient()
|
||
await expectTrpcError(() => anon.common.essentialConsts.query({ boundId: 0 }), 'BAD_REQUEST')
|
||
await expectTrpcError(() => anon.common.essentialConsts.query({ boundId: -1 }), 'BAD_REQUEST')
|
||
await expectTrpcError(() => anon.common.essentialConsts.query({ boundId: 1.5 }), 'BAD_REQUEST')
|
||
})
|
||
|
||
it('COMMON-76: no token still succeeds', async () => {
|
||
const res: any = await anonClient().common.essentialConsts.query({ boundId: 1 })
|
||
expect(res.regionId).toBe(1)
|
||
expect(res.boundId).toBe(1)
|
||
})
|
||
|
||
it('COMMON-77: the response is flat (all identity/version keys at the root)', async () => {
|
||
const res: any = await anonClient().common.essentialConsts.query({ boundId: 1 })
|
||
for (const key of [
|
||
'regionId',
|
||
'boundId',
|
||
'assetsDomain',
|
||
'apiCacheKey',
|
||
'products_version_num',
|
||
'slots_version_num',
|
||
'availability_version_num',
|
||
]) {
|
||
expect(res).toHaveProperty(key)
|
||
}
|
||
// No nesting: the handler spreads a flat const object and appends two keys.
|
||
expect(res.data).toBeUndefined()
|
||
expect(res.constants).toBeUndefined()
|
||
expect(res.settings).toBeUndefined()
|
||
expect(res.region).toBeUndefined()
|
||
})
|
||
|
||
it('COMMON-78 (current): webviewHtml is null for a normal host', async () => {
|
||
// The notice only renders when the request Host contains "raw" or "mf".
|
||
// `fetch` cannot set Host, so the positive branch is not reachable here; the
|
||
// observable contract for localhost is a null notice and a closable webview.
|
||
const res = await rawQuery('common.essentialConsts', { boundId: 1 })
|
||
const data = firstBatchResult<any>(res.body)
|
||
expect(data.webviewHtml).toBeNull()
|
||
expect(data.isWebviewClosable).toBe(true)
|
||
})
|
||
})
|