diff --git a/apps/backend/tests/README.md b/apps/backend/tests/README.md index f4a7bca..385f83c 100644 --- a/apps/backend/tests/README.md +++ b/apps/backend/tests/README.md @@ -118,17 +118,188 @@ Rules: These are properties of this seeded local D1, not of the contract. Pin them as `(current)` instead of failing the suite: -- The dev DB contains ~490 legacy `E2E *` stores. A full cache rebuild uploads - `stores.json` and `products.json`, but the per-store loop +- A full suite run leaves hundreds of `E2E *` / fixture stores, products, tags, + regions, users and slots in the local D1 (plus a few cache/const rows). A full + cache rebuild uploads `stores.json` and `products.json`, but the per-store loop (`createAllStoresFilesInternal`) can stop partway through, so `stores/.json` may be missing for stores created later. `stores.json` - itself still reflects them. + itself still reflects them. See [Cleaning up test data](#cleaning-up-test-data) + after a run. - `common.essentialConsts` is served from the KV mirror; `products_version_num` there can be ahead of `region_consts` in D1 during/after a rebuild. Assert version *movement*, not equality across the two stores. - `webviewHtml` for `raw`/`mf` hosts cannot be exercised with `fetch` (the `Host` header is forbidden). Normal hosts return `null` + closable. +## Cleaning up test data + +The suite writes into the **local** D1 (`freshyo-dev`), not a throwaway DB. After +a run it leaves test regions/bounds, ~300 stores, ~300 products and their SKUs, +tags, groups, slots, staff, users, orders, coupons, banners, snippets and +`global_consts` probes behind. It also bumps the versioned cache files and the KV +mirror. Clean up after a full run (or before handing the DB to someone). + +> Local only. Never point this at `wrangler.prod.toml`, `--remote`, or a shared +> D1. The helper script refuses `--remote`; keep it that way. + +### 1. Back up and stop the dev server + +`wrangler dev` holds the DB; stop it first so SQLite writes and `VACUUM` do not +hit a lock. Restart it when done. + +```bash +cd apps/backend +pkill -f "wrangler dev --config wrangler.dev.toml" || true + +DB=$(find .wrangler/state/v3/d1 -name '*.sqlite' ! -name 'metadata.sqlite' | head -1) +sqlite3 "$DB" ".backup '/tmp/freshyo-dev-before-cleanup.sqlite'" +``` + +### 2. Delete the rows the script knows about + +`scripts/remove-test-data.ts` removes everything created at/after `--since` +(default `2026-10-18`) while preserving the keep user (default mobile +`9676651496`) and all older real data. + +```bash +bun run remove-test-data -- --dry-run # prints the SQL + before counts +bun run remove-test-data # applies it +bun run remove-test-data -- --since "2026-10-18" --keep-mobile 9676651496 +``` + +### 3. Delete what the script does not cover + +These are the leftovers observed after a full run. Run them in one transaction; +`delivery_slot_info` has no `created_at`, so the fixture dates are the marker. + +```bash +sqlite3 "$DB" <<'SQL' +PRAGMA busy_timeout=15000; +BEGIN IMMEDIATE; + +-- fixture-date slots (no orders reference them) +DELETE FROM delivery_slot_info + WHERE delivery_time LIKE '2030-06-01%' OR delivery_time LIKE '2020-01-01%'; + +-- probe keys written by the consts suites +DELETE FROM global_consts + WHERE key LIKE '\_\_e2e%' ESCAPE '\' OR key = 'zzz_probe'; + +-- test placeholder welcome message (re-enter it in the admin UI if it was real) +DELETE FROM region_consts + WHERE key = 'welcomeMessage' AND value = 'E2E probec'; + +-- test bound + its settings (check addresses first: SELECT COUNT(*) FROM addresses WHERE bound_id = ) +DELETE FROM region_bound_consts WHERE bound_id = 17; +DELETE FROM region_bounds WHERE id = 17; + +-- old test product that predates the cutoff (cascade) +DELETE FROM sku_features WHERE sku_id IN (SELECT id FROM product_skus WHERE product_id = 150); +DELETE FROM product_market_stats WHERE sku_id IN (SELECT id FROM product_skus WHERE product_id = 150); +DELETE FROM product_combos WHERE sku_id IN (SELECT id FROM product_skus WHERE product_id = 150) + OR combo_sku_id IN (SELECT id FROM product_skus WHERE product_id = 150); +DELETE FROM special_deals WHERE sku_id IN (SELECT id FROM product_skus WHERE product_id = 150); +DELETE FROM cart_items WHERE sku_id IN (SELECT id FROM product_skus WHERE product_id = 150); +DELETE FROM order_items WHERE sku_id IN (SELECT id FROM product_skus WHERE product_id = 150); +DELETE FROM product_tags WHERE product_id = 150; +DELETE FROM product_group_membership WHERE product_id = 150; +DELETE FROM product_reviews WHERE product_id = 150; +DELETE FROM product_skus WHERE product_id = 150; +DELETE FROM product_info WHERE id = 150; + +COMMIT; +SQL +``` + +Skip the product-150 block if that row was already removed. If a test bound has a +different id, look it up first +(`SELECT id, region_id, description FROM region_bounds WHERE description LIKE 'e2e%';`). +Slots created by tests in a real region with an ordinary delivery date and an +empty `sku_ids` cannot be told apart from real slots; review +`SELECT id, delivery_time, region_id FROM delivery_slot_info ORDER BY id DESC;` +if that matters for your DB. + +`$DB` only exists in the shell that ran step 1 — re-export it if you split the +steps across terminals: +`DB=$(find .wrangler/state/v3/d1 -name '*.sqlite' ! -name 'metadata.sqlite' | head -1)` + +### 4. Reclaim the file size + +The deletes leave free pages behind; `VACUUM` shrinks the file. The dev server is +still stopped from step 1. + +```bash +sqlite3 "$DB" "VACUUM;" +sqlite3 "$DB" "PRAGMA integrity_check; PRAGMA foreign_key_check;" +``` + +### 5. Clear the KV mirror and rebuild the cache files + +`common.essentialConsts` is served from KV (`freshyo_gen_dev`), so stale test +values survive a DB cleanup until the mirror is rewritten. The cache files +(`products.json`, `stores.json`, …) are versioned in S3 and were built during the +run, so bump them too — otherwise the app keeps fetching the test-built version. + +```bash +# stop the dev server first (step 1), then wipe the mirror (all keys are consts) +./node_modules/.bin/wrangler kv key list --binding freshyo_gen_dev --local --config wrangler.dev.toml \ + | python3 -c 'import sys,json; raw=sys.stdin.read(); print(json.dumps([k["name"] for k in json.loads(raw[raw.index("["):])]))' \ + > /tmp/freshyo-kv-keys.json +./node_modules/.bin/wrangler kv bulk delete /tmp/freshyo-kv-keys.json \ + --binding freshyo_gen_dev --local --config wrangler.dev.toml --force +``` + +Restart the dev server (`bun run dev`), then rebuild each live region through the +guarded test route. It is behind `authenticateUser` + `ENABLE_TEST_ROUTES=true`, +so send a bearer token. Mint a short-lived one with the `JWT_SECRET` from +`wrangler.dev.toml` (use any row that still exists in `users`, e.g. the keep user +`388`): + +```bash +# from apps/backend +cat > /tmp/freshyo-mint-jwt.ts <<'TS' +import { SignJWT } from 'jose' +import { readFileSync } from 'node:fs' +const m = readFileSync('wrangler.dev.toml', 'utf8').match(/^\s*JWT_SECRET\s*=\s*["']([^"']+)["']/m)! +const token = await new SignJWT({ userId: 388 }) + .setProtectedHeader({ alg: 'HS256' }) + .setExpirationTime('10m') + .sign(new TextEncoder().encode(m[1])) +console.log(token) +TS + +TOKEN=$(bun /tmp/freshyo-mint-jwt.ts) +for R in 1 2 3; do + curl -s -X POST "http://127.0.0.1:8787/api/test/rebuild?regionId=$R" \ + -H "Authorization: Bearer $TOKEN" +done +``` + +This rewrites `essentialConsts_` for the live bounds only and bumps +`products_version_num`, so the app points at the freshly built files. + +### 6. Verify the result + +```bash +sqlite3 -column "$DB" " + SELECT 'regions', COUNT(*) FROM regions + UNION ALL SELECT 'stores', COUNT(*) FROM store_info + UNION ALL SELECT 'products', COUNT(*) FROM product_info + UNION ALL SELECT 'tags', COUNT(*) FROM product_tag_info + UNION ALL SELECT 'slots', COUNT(*) FROM delivery_slot_info + UNION ALL SELECT 'users', COUNT(*) FROM users + UNION ALL SELECT 'staff', COUNT(*) FROM staff_users;" +``` + +A clean local DB looked like: regions 3, stores 6, products 153, tags 9, slots +495, users 1320, staff 7, `integrity_check: ok`, `foreign_key_check` empty, +file ≈1.3 MB (from ≈3.9 MB). The keep user's own orders/complaints are preserved +by design. `common.essentialConsts` should return `welcomeMessage: ""` and only +the live bounds should remain as `essentialConsts_*` keys. + +Restart the dev server when finished. Remember: **the run itself is expected to +add all of this again** — clean up after the suite, not before. + ## Current known gaps (pin, don't "fix" silently) See `API-TEST-CASES.md` §13. In particular: `protectedProcedure` admin reads diff --git a/apps/backend/tests/api/admin-order.test.ts b/apps/backend/tests/api/admin-order.test.ts index dc3cd7f..a96046d 100644 --- a/apps/backend/tests/api/admin-order.test.ts +++ b/apps/backend/tests/api/admin-order.test.ts @@ -98,6 +98,13 @@ describe('admin.order.getAll (AORD-01..06)', () => { expect(typeof row.orderId).toBe('string') expect(typeof row.status).toBe('string') expect(Array.isArray(row.items)).toBe(true) + // List rows carry the same per-line numbers as the detail view. + for (const item of row.items) { + expect(typeof item.id).toBe('number') + expect(typeof item.quantity).toBe('number') + expect(typeof item.price).toBe('number') + expect(item.amount).toBe(item.quantity * item.price) + } }) it('AORD-02: cursor pagination returns a new page with no overlap', async () => { @@ -219,9 +226,20 @@ describe('admin.order.getOrderDetails (AORD-07..09)', () => { ) expect(d.slotInfo.time).toEqual(expect.any(String)) expect(d.items).toHaveLength(1) - expect(d.items[0]).toEqual( - expect.objectContaining({ id: expect.any(Number), name: expect.any(String) }) + const item = d.items[0] + expect(item).toEqual( + expect.objectContaining({ + id: expect.any(Number), + name: expect.any(String), + skuName: '1pc', + // order_items.quantity / .price are TEXT columns, so the admin payload + // keeps them as strings; `amount` is computed as a number. + quantity: '1', + price: '250', + amount: 250, + }) ) + expect(Number(item.amount)).toBe(Number(item.quantity) * Number(item.price)) expect(d.orderStatus).toEqual( expect.objectContaining({ isCancelled: false, isDelivered: false, isPackaged: false }) ) diff --git a/apps/backend/tests/api/user-order-lifecycle.test.ts b/apps/backend/tests/api/user-order-lifecycle.test.ts index 348c32f..9989898 100644 --- a/apps/backend/tests/api/user-order-lifecycle.test.ts +++ b/apps/backend/tests/api/user-order-lifecycle.test.ts @@ -239,20 +239,23 @@ describe('user.order detail shape (UORD-81..92)', () => { addressId: fx.address.id, skuId: fx.product.skuId, slotId: fx.slot.slotId, + quantity: 3, }) const d = await detail(fx.user.token, Number(placed[0].id)) expect(d.items[0]).toEqual( expect.objectContaining({ productName: expect.any(String), skuName: expect.anything(), - quantity: expect.any(Number), + quantity: 3, unitNotation: expect.anything(), - price: expect.any(Number), - discountedPrice: expect.any(Number), - amount: expect.any(Number), + price: 199, + discountedPrice: 199, + amount: 199 * 3, image: null, }) ) + // The returned amount is the line total, not the unit price. + expect(d.items[0].amount).toBe(d.items[0].price * d.items[0].quantity) }) it('UORD-82: the order list only contains the caller’s own orders', async () => { diff --git a/apps/backend/tests/api/user-order-place.test.ts b/apps/backend/tests/api/user-order-place.test.ts index 9fe61ef..f7a1b90 100644 --- a/apps/backend/tests/api/user-order-place.test.ts +++ b/apps/backend/tests/api/user-order-place.test.ts @@ -683,6 +683,17 @@ describe('user.order.placeOrder — splitting/flash/combo (UORD-25..38)', () => expect(res.data[0].isFlashDelivery).toBe(true) expect(res.data[0].slotId).toBeNull() expect(Number(res.data[0].totalAmount)).toBe(149 + 69) // flash price + flash delivery charge + + // The line stores the flash price, not the regular 199. + const d = await detail(user.token, Number(res.data[0].id)) + expect(d.items[0]).toEqual( + expect.objectContaining({ + quantity: 1, + price: 149, + discountedPrice: 149, + amount: 149, + }) + ) }) it('UORD-30: flash enabled but an ineligible SKU is rejected', async () => { @@ -741,6 +752,13 @@ describe('user.order.placeOrder — splitting/flash/combo (UORD-25..38)', () => const res = await placeCall(user.token, { addressId: address.id, skuId: combo.skuId, slotId: slot.slotId }) expect(res.success).toBe(true) expect(Number(res.data[0].totalAmount)).toBe(250 + 20) + + // The combo line keeps its own SKU price; child items are not expanded. + const d = await detail(user.token, Number(res.data[0].id)) + expect(d.items).toHaveLength(1) + expect(d.items[0]).toEqual( + expect.objectContaining({ quantity: 1, price: 250, amount: 250 }) + ) }) it('UORD-33 (current): a combo-only SKU ordered directly is accepted (no isComboOnly guard)', async () => { diff --git a/change-log.txt b/change-log.txt index 7dc860b..030e29e 100644 --- a/change-log.txt +++ b/change-log.txt @@ -8005,3 +8005,72 @@ Final diffs (on top of the per-suite migrations already logged above): - admin-ui tests/pages/banner-edit.test.tsx: ABANE-03 awaits the ProductsSelector SKU label. No application source changed. + +================================================================================ +[2026-10-20 12:25:00 IST] CHORE-2026-10-20-12 remove API-test data from local D1 +================================================================================ +DB: apps/backend/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/da3a...sqlite +Backup: $TMPDIR/opencode/freshyo-dev-before-cleanup.sqlite (sqlite .backup) + +Removed rows created by the API suite (created_at >= 2026-10-18, excluding the +keep user mobile 9676651496), following scripts/remove-test-data.ts, plus: + - delivery_slot_info rows with fixture dates 2030-06-01 / 2020-01-01 (test-only, + no orders reference them) + - notifications for test users (none present) and key_val_store key 'tester' + +Kept: regions 1-3, stores 1,2,4,8,9,10, the 154 pre-cutoff products, tags 1-9, +pre-cutoff users/staff/banners/snippets/coupons. +Executed in one BEGIN IMMEDIATE / COMMIT; PRAGMA foreign_key_check clean. + +Follow-up (same cleanup): config-table pollution +- global_consts: removed 40 probe keys (__e2e_*, zzz_probe). Kept the 4 real keys. +- region_consts: removed welcomeMessage='E2E probec' from regions 1 and 2 + (test placeholder; region 3 keeps its real welcome message). +- region_bounds: removed id 17 ('e2e b14', region 1) and its region_bound_consts + rows. No addresses reference it. + +Final cleanup steps (same task): +- Removed the last pre-cutoff test product: product_info id 150 + "E2E Test Item 14Sep-2342" (+ its sku/features/market-stat; no orders/slots). +- KV rewrite cache (freshyo_gen_dev): purged all 695 stale essentialConsts_* keys + (one per bound, incl. deleted test bounds), then rebuilt regions 1-3 through + POST /api/test/rebuild, leaving only the 6 live bound keys. +- Cache rebuild bumped the clean cache version (products v423 -> v424) so the app + no longer points at test-built files. +- VACUUM: DB file 3.9 MB -> 1.3 MB, freelist 0, integrity_check ok. + +Final local D1: regions 3, stores 6, products 153, tags 9, slots 495, +users 1320, staff 7, orders 1072, banners 4, snippets 4, foreign_key_check clean. +Kept: the keep user's 9 orders + 8 complaints dated 2026-10-18 (mobile +9676651496), all pre-cutoff real data. Dev server restarted on :8787. + +================================================================================ +[2026-10-20 13:00:00 IST] TEST-2026-10-20-13 order-item quantity/price assertions +================================================================================ +User request: add the missing assertions for an order's item quantity and price. + +apps/backend/tests/api/user-order-lifecycle.test.ts (UORD-81) + places the fixture with quantity: 3 instead of 1 and pins exact values: + quantity 3, price 199, discountedPrice 199, amount 597, plus + amount === price * quantity. Shape checks stay. + +apps/backend/tests/api/admin-order.test.ts (AORD-07) + pins the admin detail item contract instead of id/name only: + skuName '1pc', quantity '1' (TEXT), price '250' (TEXT), amount 250, + amount === Number(quantity) * Number(price). + +apps/backend/tests/api/admin-order.test.ts (AORD-01) + inspects list-row items: numeric id/quantity/price and amount = quantity * price. + +apps/backend/tests/api/user-order-place.test.ts (UORD-29 flash) + fetches detail and pins the stored flash price: price 149, quantity 1, + amount 149, discountedPrice 149. + +apps/backend/tests/api/user-order-place.test.ts (UORD-32 combo) + fetches detail and pins the combo line price: price 250, amount 250. + +No application source changed. Run: only these three modules. +Run: bun run test:api -- user-order-lifecycle admin-order user-order-place + -> 3/3 suites, 145/145 tests passed (105 s); test:api:typecheck clean. +The run re-added live suite data (79 E2E stores, 87 products) — clean with +tests/README.md "Cleaning up test data" when needed.