freshyo/apps/test-improvements.md
2026-10-20 11:19:25 +05:30

13 KiB
Raw Blame History

Test improvements

Summary and scope

The suites have broad feature coverage. The main weakness is that some tests give more confidence than they should—not a shortage of test cases. Strengthen those before adding more screen-rendering tests.

This report is based on a static review of tests, harnesses, and relevant implementation code in user-ui, admin-ui, and backend. No tests were run and no application or test code was changed. Findings about missing coverage are based on the inspected suites, not measured runtime coverage. OTP and push notifications are excluded per AGENTS.md.

Paths below are relative to this file's directory (apps/). Line references describe the files at review time and may move.

1. Biggest coverage gaps

A. The real cache pipeline is largely bypassed

Many backend cache tests now call test-only scaffold endpoints rather than fetching the actual published cache files.

Examples:

  • backend/tests/helpers/cache.ts:76
  • backend/tests/api/cache-products.test.ts:71

These prove that the backend can construct the right document, but not that:

  • A mutation schedules the rebuild.
  • The Durable Object runs it.
  • Storage receives every file.
  • Discovery advertises a readable version.
  • Public/CDN responses contain the latest data.
  • A global edit reaches every region.

The Durable Object unit tests are useful, but mock initializeAllStores, so they do not close that gap.

Add a small real-pipeline integration suite covering:

  1. Regional product create/update/delete → public cache changes.
  2. Global update → both regions change.
  3. Store detail files exist at the advertised version.
  4. Temporary 404 → eventual successful read.
  5. Failed rebuild → recovery without losing pending work.

Keep the scaffold tests—they are valuable—but do not describe them as full cache end-to-end coverage.

B. UI tests mostly bypass the real tRPC/React Query lifecycle

Both UI mocks replace the query hooks themselves:

  • user-ui/tests/mocks/trpc-client.ts:141
  • admin-ui/tests/mocks/trpc-client.ts:141

In particular:

  • refetch does not actually fetch.
  • Invalidations only record calls.
  • Infinite queries always have hasNextPage: false.
  • fetchNextPage does nothing.
  • Mutation callback exceptions are swallowed.
  • Per-call mutation onSettled is not implemented.

Consequently, these suites cannot reliably establish pagination, cache refresh, or mutation lifecycle correctness.

Add a few integration tests using real tRPC hooks and React Query, mocking HTTP responses instead. Prioritize:

  • Mutation → invalidation → visibly updated screen.
  • First page → next page → no duplicate request.
  • Failed refresh retains existing data and can recover.
  • Mutation completion clears pending state.
  • Logout while a request is pending.

There is no need to rewrite every page test.

C. No connected customer/admin journey

Individual screens and backend procedures are tested separately, but the seams between them remain exposed.

A concrete example:

  • admin-ui/tests/pages/orders.test.tsx:240 accepts navigation to an ORD-... identifier.
  • admin-ui/tests/pages/order-details.test.tsx:43 mounts details with numeric '11'.
  • The actual details page uses parseInt(id).

Those separate tests do not prove that an order selected from the list can actually be opened.

Add connected journeys:

  • Customer: product → cart → checkout → order details.
  • Admin: orders list → details → package/deliver → refreshed status.
  • Customer subsequently sees the admin's change.
  • Product/SKU ID and internal/display order ID remain correct throughout.

Use actual API-shaped responses, not independently adapted fixtures.

2. Missing or insufficiently tested edge cases

user-ui

Priority Case to add
High Order succeeds, but the response is lost. Retry/reopen the app without accidentally creating another order. The double-press test does not cover this. Backend non-idempotency is already documented, so this needs an explicit product decision.
High Concurrent cart writes. Two rapid additions, additions from different mounted components, update versus removal, and slot reconciliation versus quantity changes. The storage implementation performs read–modify–write operations, while current cart-hook tests are sequential.
High Rapid region switching with delayed responses. A → B → C, then A's products/availability arrive last. Assert actual displayed products, prices, central stores, and persisted carts—not merely the requested URL.
High Partial region-switch failure. Snapshot write, cart clear, or restore fails halfway through. Ensure the previous cart is not lost and the new region is not displayed with the old region's items.
High Checkout becomes stale while open. Slot closes, product becomes unavailable, coupon expires, address is deleted, or bound/price changes before Place Order. Test a recoverable customer-facing result.
Medium Background/resume and offline recovery. Resume after the slot cutoff; network disappears during checkout; connectivity returns without remounting the whole app.
Medium Storage contains valid JSON with an invalid shape. Malformed JSON is tested, but also try null, {}, wrong collection types, missing IDs, and invalid quantities in cart/snapshot data.
Medium Authentication races. Logout while profile/login is pending, then a late success arrives; account A logs out and B logs in without A's data returning.

Existing region-switch, coupon-selection, cart CRUD, and checkout payload tests are good foundations. These additions target timing and recovery rather than repeating happy paths.

admin-ui

Priority Case to add
High Real pagination: page two succeeds/fails/retries; repeated end-of-list events; filter changes while an old page is pending.
High Actual delivery reordering: reorder at least three orders, preserve other staff assignments, switch staff/slot with unsaved changes, and handle an order disappearing after cancellation.
High Competing edits: another admin changes stock, prices, packaging, or delivery status while this screen is open. Decide whether stale saves overwrite or conflict.
High Session persistence failures: JWT write succeeds but staff write fails; token deletion fails during logout; saved token exists but staff data is missing/corrupt.
Medium Partial bulk-operation failures: some banner assignments or other multi-request updates succeed and another fails. Do not report complete success or discard unsaved work.
Medium Location attachment failures: permission denied, unavailable location, location timeout, and rejected coordinate update. Delivery-sequence tests currently supply a successful location.

backend

1. Failure after persistence has begun

Several transaction tests reject input before the interesting write, or exercise an unrelated happy path. Add controlled failure injection after:

  • First split order is inserted.
  • Order exists but coupon usage recording fails.
  • Default-address clearing occurs.
  • An image is claimed but the associated write fails.

Assert persisted invariants afterward. A rejected API response alone does not prove rollback.

2. Stronger concurrency invariants

backend/tests/api/concurrency.test.ts:77 accepts at least one successful use of a max-one coupon—even if both succeed. The default-address race accepts either one or two defaults.

These do not protect the intended invariants. Test:

  • Exactly one allowed coupon consumption.
  • Exactly one default address.
  • No duplicate unique entities after concurrent creation.
  • Concurrent first orders do not both receive first-order-only benefits.

3. Durable Object failure/restart boundaries

Extend backend/tests/unit/cache-creator.test.ts with:

  • Alarm fires exactly at the debounce deadline.
  • Rebuild rejects after pending state is cleared.
  • New schedule arrives during rebuilding.
  • Instance restarts with persisted pending work.
  • One region fails while others remain pending.

4. Flash-delivery scheduler

No tests were found for backend/src/lib/flash-delivery-cron.ts. Cover:

  • On/off cron mapping.
  • UTC/IST interpretation.
  • Unknown cron.
  • Every bound being updated.
  • Constants/KV refresh.

5. More demanding pricing combinations

Basic stacking and rounding are covered. Add:

  • Percentage then flat versus flat then percentage through the API.
  • Duplicate coupon IDs.
  • Unequal split orders with rounding remainders.
  • Targeted coupons across multiple slots.
  • Exact threshold/max-discount boundaries.
  • Partial cancellation of the sub-order carrying delivery/coupon usage.

Assert that discounts and totals reconcile across every sub-order.

6. Upload security boundaries

Existing tests cover fabricated and already-claimed keys. Add:

  • Another principal/region's unclaimed key.
  • Unrelated deletedImageKeys.
  • Wrong upload context.
  • Actual expired signatures.
  • Simultaneous same-MIME key generation.

7. Existing sessions after staff changes

Staff move/delete tests verify records, but not previously issued sessions. Verify old tokens after reassignment/removal, including region isolation.

8. Production configuration safety

Verify test-only cache/rebuild/tracker-reset routes are unavailable when ENABLE_TEST_ROUTES is unset or false.

3. Tests to replace, merge, or downgrade

Concrete false-confidence tests

Test Problem Better replacement
admin-ui/tests/pages/login.test.tsx:179 — ALOGIN-14 Says “storage save failure” but never injects a storage failure. Reject the relevant storage write and assert consistent auth/storage/navigation state.
admin-ui/tests/pages/delivery-sequences.test.tsx:148 — ADSEQ-10/11 “Reorders” a single item into the same order. Move [11,12,13] to [13,11,12]; assert displayed and submitted order.
admin-ui/tests/pages/orders.test.tsx:423 — AORDUI-30 Pagination test only exercises “no next page.” Fetch and append a genuine second page. Keep the no-next-page guard as a separate case.
admin-ui/tests/pages/orders.test.tsx:433 — AORDUI-31/32 Refresh handler exists/screen survives; no proof data was refetched. Change server response and verify the new data appears.
backend/tests/api/cache-products.test.ts:530 — CACHE-48 Global-edit case performs a global read. Actual global write followed by convergence across regions.
backend/tests/api/concurrency.test.ts:215 — CONC-05 Global-versus-regional scheduling case never schedules a global write. Unit-test the interleaving, plus a small real-pipeline integration check.
backend/tests/api/transactions.test.ts:179 — TX-06 Cancels a nonexistent order, then checks a different order survived. Fail cancellation midway for the same existing order and inspect its state.

Similarly, “S3 failure cannot be induced, so a valid upload succeeds” is another happy-path test—not failure coverage. Rename/merge it or inject the failure.

Lower-value tests to consolidate

  • Repeated “floating cart/location bar exists” assertions across many pages.
  • Simple store setter/getter tests already exercised through meaningful flows.
  • SVG counts, exact colors, and long static-copy assertions in user-ui/tests/components/static-chrome.test.tsx.
  • Multiple live tests proving the same generic request validation or response shape.
  • Separate cases for every small assertion from one expensive shared setup.

These are not all useless. Keep representative wiring tests and important legal/payment copy checks. However, “one catalogue row = one test” is creating maintenance overhead without proportional protection.

4. Existing tests that deliberately approve bugs

This is not missing coverage, but it deserves attention:

Behavior Reference
User tokens performing admin suspension backend/tests/api/auth-matrix.test.ts:618
Suspended users retaining tRPC access backend/tests/api/auth-matrix.test.ts:786
Account deletion partially destroying data before failing backend/tests/api/user-profile-auth.test.ts:367
Missing availability producing ₹0 product prices user-ui/tests/lib/prominent-api-hooks.test.tsx:297
An inert admin Packaged toggle admin-ui/tests/pages/orders.test.tsx:206

Characterization tests are useful for documenting current behavior. They should not make these behaviors look like accepted requirements. Track them explicitly, then change their expectations alongside fixes.

  1. Replace the concrete false-positive tests above.
  2. Add real HTTP-level UI integration coverage.
  3. Restore a small genuine public-cache pipeline suite.
  4. Add cart/region/auth race tests and mid-write failure tests.
  5. Consolidate repeated render/style tests.

Bottom line: fewer superficial cases, more connected flows, delayed responses, partial failures, and strict persisted-state invariants.