freshyo/apps/user-ui/tests/KNOWN-GAPS.md
2026-10-11 09:33:11 +05:30

189 lines
8.1 KiB
Markdown

# Known gaps — apps/user-ui
Each item is a real defect or a missing guarantee that the test suite **pins
today**. The tests stay green on purpose: they document what the code does now
and will fail loudly when the behaviour changes. When you fix one, update the
matching test to assert the correct behaviour and remove it from this list.
Location of the pinning test is given for each entry.
---
## 1. A 401 does not log the user out
`src/trpc-client.ts:33` emits `FORCE_LOGOUT_EVENT` on a `401 UNAUTHORIZED`
response, but **nothing in `apps/user-ui` subscribes to it** (only
`apps/admin-ui` does). A session whose token the server has rejected stays
signed in; the next screen the user opens fails again instead of redirecting to
login.
**Fix:** subscribe to `FORCE_LOGOUT_EVENT` in `AuthProvider` (or the root
layout) and call `logout()`.
**Pinned by:** `tests/lib/force-logout.test.tsx` — "leaves the signed-in session
untouched (no subscriber for the event)". It asserts the session survives the
401. Rewrite it to assert the redirect once a listener exists.
---
## 2. A rejected token is deleted from memory but not from storage
`src/contexts/AuthContext.tsx:105` — `deleteAuthToken()` is commented out in the
`selfDataError` branch. The in-memory session is cleared, but the dead token and
user id stay in secure storage and `initializeAuth` re-authenticates with them
on the next cold start.
**Fix:** uncomment `deleteAuthToken()` (and clear the stored user id).
**Pinned by:** `tests/contexts/auth-lifecycle.test.tsx` — "clears the session
when getSelfData fails but leaves the stale token in storage".
---
## 3. The delivery threshold is checked after the discount
`components/cart-page.tsx:270` and `components/checkout-page.tsx:213` both do:
```ts
return finalTotal < threshold ? charge : 0 // finalTotal is POST-discount
```
**Resolved (2026-10-10):** the backend now uses this exact model — discount the
cart subtotal, then `finalTotal < freeDeliveryThreshold` — so the client and the
persisted order agree. Previously the backend waived delivery from the
**pre-discount** total and from a different constant (`minRegularOrderValue` vs
the client's `freeDeliveryThreshold`), which is what produced the mismatch.
`minRegularOrderValue` has been removed entirely.
Note the post-discount rule is now the single, intended behaviour on both sides:
a coupon that drops the total under the threshold re-adds the delivery fee, so a
small coupon can still make the payable amount rise above the un-couponed total.
That is by design (it matches the cart), not a client/backend divergence.
**Pinned by:** `tests/screens/cart-coupons.test.tsx` — "re-adds the delivery fee
when the discount drops the total below the threshold" (the same behaviour is
asserted on the checkout side by `tests/screens/checkout-coupons.test.tsx` —
"charges delivery again when the discount drops the total below the threshold").
---
## 4. Order success shows the frontend's total, not the backend's charge
`components/PaymentAndOrderComponent.tsx:86` writes
`totalAmount=${finalTotalWithDelivery}` into the success URL, and
`app/(drawer)/(tabs)/home/order-success.tsx` renders `params.totalAmount`
verbatim. The backend stores a different number: it scales the coupon discount
by `orderGroupProportion` for multi-slot orders and the client does not
(`orderGroupProportion` has zero occurrences in `apps/user-ui`). For a multi-slot
group the two disagree.
**FIXED (amount) 2026-10-11:** `PaymentAndOrderComponent` now sums the
server-returned orders' `totalAmount` and writes that into the success URL,
falling back to `finalTotalWithDelivery` only when the response omits it. The
cart/checkout discount is also computed with the shared `computeCartDiscount`
helper, so the local preview now matches the server for the single-coupon case.
The screen still shows only `orders[0].id` for a group of orders created by one
`placeOrder` call (not yet addressed).
**Pinned by:** `tests/screens/order-amounts.test.tsx` — "renders whatever amount
the caller put in the query string"; `tests/components/payment-and-order-money.test.tsx`
— "uses the server-stored order amount for the success URL when present".
---
## 5. Cart lines vanish when the product cache is incomplete
`hooks/cart-query-hooks.tsx` — `useGetCart` maps a stored line to `null` when its
`skuId` is missing from `productsById` or `productSlotsMap`, then filters it out.
The line disappears from the view while still on disk, so the cart shows fewer
rows than it holds and the user cannot see or remove the hidden items.
**FIXED (visibility):** the enriched cart is now derived in a `useMemo` over
`query.data + productsById + productSlotsMap`, so lines reappear as soon as the
central stores populate (no manual refresh). Lines whose product is genuinely
absent from the catalog are still hidden — that remainder is intentional.
**Pinned by:** `tests/hooks/cart-storage.test.tsx` — "drops lines whose product
is missing from the central stores" (asserts the line is still on disk).
---
## 6. `useReconcileCartSlots` deletes cart lines and never repairs them again
**FIXED.** The reconciler no longer drops a line whose product has no slots — it
keeps the line untouched. It also runs whenever `productSlotsMap` changes (the
once-per-mount `hasRun` ref is gone), so expired slots are repaired on the next
slot refresh, and it bails when the slots map is empty so it can never wipe the
cart.
**Pinned by:** `tests/hooks/cart-storage.test.tsx` — "keeps a line whose product
has no slots instead of deleting it" and "re-reconciles when the slots map
changes".
---
## 7. Two adds in the same tick lose the first one
`hooks/cart-query-hooks.tsx:132` — `addToLocalCart` is read-modify-write with no
locking or serialisation. Two `addToCart` calls fired together both read the
pre-add cart and the second write erases the first.
**Fix:** serialise the writes (a per-cart promise chain, or a reducer).
**Pinned by:** `tests/hooks/cart-storage.test.tsx` — "loses the first of two
concurrent adds".
---
## 8. A zero quantity is written through to storage
`hooks/cart-query-hooks.tsx:158` — `updateLocalCartItem` assigns
`item.quantity = quantity` with no lower bound. A 0-qty line stays in storage
and keeps counting as a cart row. The UI always routes deletion through
`removeFromCart`, so this is only reachable from a direct call today.
**Fix:** clamp to `>= 1`, or treat 0 as a removal.
**Pinned by:** `tests/hooks/cart-storage.test.tsx` — "persists a zero quantity
with no guard".
---
## 9. Cart totals follow the live catalog price
Carts store only `skuId`/`quantity`; the price is read from `productsById` at
read time (`hooks/cart-query-hooks.tsx:205`). A repriced product changes the
bill underneath the user with no notice and no stale-price marker. This may be
intended, but it is unpinned business behaviour — worth an explicit decision.
**Pinned by:** `tests/hooks/cart-storage.test.tsx` — "prices a line from the
live product record, not the price at add time".
---
## 10. `current` store map is rebuilt wholesale on refresh
`src/store/centralSlotStore.ts` `setSlotsData` builds `productSlotsMap` from
scratch and calls `set`. A product that drops out of `productAvailability`
disappears from the map entirely, and its `isOutOfStock` flag resets to `false`
when its availability row is missing. Everything downstream (cart reconciler,
out-of-stock rules) then treats it as unknown.
**Pinned by:** `tests/stores/store-staleness.test.ts` — "replaces the map
wholesale on a refresh, dropping vanished products" and "keeps the availability
flags when the same product is refreshed".
---
## Scope notes
Excluded from this pass on request: online payments (Razorpay
`verifyPayment`/`markPaymentFailed`/`initiateRazorpayPayment`) and the
notifications service (`services/notif-service/`, `services/toaster.tsx`).
Backend-side gaps found during the same review (coupon math uncapped for
percentage discounts, coupon scoping not enforced at `placeOrder`, coupon usage
not rolled back on cancellation, out-of-stock items orderable, admin writes
reachable with a user token, `user.payment.*` untested) are tracked separately
in the backend review notes and are not covered here.