Skip to content

The only tool you need to know what is happening and how to fix it.

fix: better tests

ddux3dfd34dffe7a
diff --git a/testing/shop/DEMO.md b/testing/shop/DEMO.md
index ab028d45..32351544 100644
--- a/testing/shop/DEMO.md
+++ b/testing/shop/DEMO.md
@@ -230,6 +230,61 @@ so a fresh event arrives, then open that issue. The same frame now reads
---
+## Manual click test cases
+
+Assumes the **Act 2 build** (Traceway re-added) is running and the dashboard is open. Base URL
+**http://localhost:8090**. Issue numbers are from [`BUGS.md`](./BUGS.md). Backend issues also have
+curl equivalents in [`demo-traffic.sh`](./demo-traffic.sh).
+
+| TC | Issue | Page | Click exactly | You'll see | Reliability |
+|----|-------|------|---------------|------------|-------------|
+| 1 | #1 slow products | Products | Click **Products** in the nav (or reload) a few times | Grid loads with a visible lag | ~3 of 4 loads |
+| 2 | #4 slow add-to-cart | Products | Click **Add to cart** on a few cards | Button lags on "Adding…", cart badge ticks up | ~3 of 4 |
+| 3 | **#11** quick view | Products | Click **Quick view** on any card | "<name> — No variants available" | every click |
+| 4 | #3 slow cart | Cart | Do TC2 first, then click **Cart** | Cart renders with a lag | ~3 of 4 |
+| 5 | #5 panic + **#10** | Checkout | Type `SAVE10`, click **Apply** | ~75% "Could not display coupon."; ~25% "saved 10%" | panic ~3 of 4 |
+| 6 | #5b + **#10** | Checkout | Type `FOO` (any unknown code), click **Apply** | "Could not display coupon." | every click |
+| 7 | #6 empty-cart panic | Checkout | Empty the cart, fill the form, click **Place order** | inline "request failed (500)" | ~5 of 6 (else 402) |
+| 8 | #7 slow + #8 declined | Checkout | With items, fill the form, click **Place order** | "Placing order…" ~1s, then confirmed or "declined" | slow most; decline ~1 of 6 |
+| 9 | **#9** unhandled add | Products | not a pure click — see note | button sticks on "Adding…" | n/a |
+
+Details that bite:
+
+- **TC3 / #11 is the cleanest frontend demo** — every product is missing a `variants` field, so
+ Quick view throws on every click and is captured manually. Start here.
+- **TC6 / #10 is the deterministic null-deref:** any **unknown** coupon code returns 400, which sets
+ `discount = null`, which makes the badge throw on render and the error boundary report it.
+ `EXPIRED` works too (400 "expired", no backend exception). `SAVE10` also triggers #10 but only on
+ the ~75% of applies that panic.
+- **Re-triggering #10:** once the coupon badge boundary catches once, it stays on the fallback. To
+ fire a fresh #10, leave Checkout (click **Products**) and come back, then Apply again.
+- **TC7 / #6:** "empty the cart" = Cart page → **Remove** every line, or just restart the server (the
+ in-memory DB reseeds empty). The Place-order fields are required: Name, a valid Email, a 4-digit
+ card (e.g. `Ada` / `[email protected]` / `4242`).
+- **TC8 / #8 (declined)** is ~1 in 6, and a successful order clears the cart, so to keep trying you
+ click **Back to shop**, re-add an item, return to Checkout. Easier via `demo-traffic.sh` phase 8.
+- **#2 (product-detail N+1) has no UI path** — there is no product-detail page. Hit it with
+ `curl .../api/products/1` or `demo-traffic.sh` phase 2.
+- **TC9 / #9 is not reproducible by clicking** a seeded product (add-to-cart always 201s, so
+ `handleAdd` never rejects). Two ways to force it:
+ - *No code change:* with the Products page already loaded, stop the shop backend (Ctrl-C the
+ `:8090` process — Traceway on `:8082` stays up). Click **Add to cart**: the fetch fails, the
+ button sticks on "Adding…", and the unhandled rejection is reported. Restart with
+ `./build-and-run.sh` afterward — don't reload the page while it's down.
+ - *One-line tweak for a pure click:* in `backend/cart.go` `addToCart`, inside the `if !fastPath()`
+ block, fail a fraction of slow adds:
+ ```go
+ if !fastPath() {
+ slowJitter(150, 500)
+ if rand.IntN(3) == 0 {
+ c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("inventory check failed for product %d", req.ProductId))
+ return
+ }
+ }
+ ```
+ (ensure `"math/rand/v2"` is imported). Now a few **Add to cart** clicks reliably 500 → #9, while
+ successful adds still fill the cart.
+
## Reset
- Back to "bugs, no observability": discard the Act 2 restores
diff --git a/testing/shop/backend/cart.go b/testing/shop/backend/cart.go
index 2770ceb9..0781f1a1 100644
--- a/testing/shop/backend/cart.go
+++ b/testing/shop/backend/cart.go
@@ -13,6 +13,9 @@ func getCart(c *gin.Context) {
ctx := c.Request.Context()
lines := []CartLine{}
+ var slept int
+ _ = db.QueryRowContext(ctx, `SELECT sleep_ms(?)`, 400).Scan(&slept)
+
if fastPath() {
rows, err := db.QueryContext(ctx, `
SELECT ci.id, ci.product_id, p.name, p.price_cents, p.image_url, ci.qty
diff --git a/testing/shop/backend/db.go b/testing/shop/backend/db.go
index 780d31bd..c2721a00 100644
--- a/testing/shop/backend/db.go
+++ b/testing/shop/backend/db.go
@@ -1,10 +1,26 @@
package main
-import "database/sql"
+import (
+ "database/sql"
+ "time"
+
+ sqlite3 "github.com/mattn/go-sqlite3"
+)
+
+func init() {
+ sql.Register("sqlite3_shop", &sqlite3.SQLiteDriver{
+ ConnectHook: func(conn *sqlite3.SQLiteConn) error {
+ return conn.RegisterFunc("sleep_ms", func(ms int64) int64 {
+ time.Sleep(time.Duration(ms) * time.Millisecond)
+ return ms
+ }, false)
+ },
+ })
+}
func initDB() {
var err error
- db, err = sql.Open("sqlite3", ":memory:")
+ db, err = sql.Open("sqlite3_shop", ":memory:")
if err != nil {
panic(err)
}
diff --git a/testing/shop/demo-traffic.sh b/testing/shop/demo-traffic.sh
new file mode 100755
index 00000000..dbae3038
--- /dev/null
+++ b/testing/shop/demo-traffic.sh
@@ -0,0 +1,84 @@
+#!/usr/bin/env bash
+# Drives every BACKEND bug so it shows up in Traceway. The three frontend bugs
+# (#9, #10, #11) are client-side JS and must be triggered in the browser — see the
+# checklist printed at the end. Re-run any time to accumulate more volume.
+#
+# Usage: ./demo-traffic.sh [base-url] (default http://localhost:8090)
+set -uo pipefail
+
+BASE="${1:-http://localhost:8090}"
+CHECKOUT='{"name":"Ada","email":"[email protected]","card_last4":"4242"}'
+
+hit() { # method path [json]
+ local m=$1 p=$2 d=${3:-}
+ if [ -n "$d" ]; then
+ curl -s -o /dev/null -w '%{http_code}' -X "$m" "$BASE$p" -H 'Content-Type: application/json' -d "$d"
+ else
+ curl -s -o /dev/null -w '%{http_code}' -X "$m" "$BASE$p"
+ fi
+}
+
+timed() { # method path [json] -> "code (1.234s)"
+ local m=$1 p=$2 d=${3:-}
+ if [ -n "$d" ]; then
+ curl -s -o /dev/null -w '%{http_code}(%{time_total}s)' -X "$m" "$BASE$p" -H 'Content-Type: application/json' -d "$d"
+ else
+ curl -s -o /dev/null -w '%{http_code}(%{time_total}s)' -X "$m" "$BASE$p"
+ fi
+}
+
+empty_cart() {
+ for id in $(curl -s "$BASE/api/cart" | grep -oE '"id":[0-9]+' | grep -oE '[0-9]+'); do
+ curl -s -o /dev/null -X DELETE "$BASE/api/cart/$id"
+ done
+}
+
+echo "== 0. Health =="
+echo " /api/health -> $(hit GET /api/health)"
+
+echo "== 1. Slow product list, N+1 (#1) — ~3 of 4 take the slow fan-out =="
+printf ' '; for i in $(seq 1 18); do printf '%s ' "$(timed GET /api/products)"; done; echo
+
+echo "== 2. Slow product detail, N+1 + recommendations span (#2) =="
+printf ' '; for id in 1 2 3 4 5 6 7 8; do printf 'id%s=%s ' "$id" "$(timed GET "/api/products/$id")"; done; echo
+
+echo "== 3. Coupon nil-map PANIC (#5) — POST /api/coupon SAVE10, ~75% -> 500 =="
+codes=""; for i in $(seq 1 12); do codes+="$(hit POST /api/coupon '{"code":"SAVE10"}') "; done
+echo " $codes"
+echo " -> $(grep -o 500 <<<"$codes" | wc -l | tr -d ' ')/12 panicked (assignment to entry in nil map)"
+
+echo "== 4. Unknown-coupon captured error (#5b) — code that isn't seeded -> 400 + c.Error =="
+printf ' '; for i in 1 2 3; do printf '%s ' "$(hit POST /api/coupon '{"code":"NOPE"}')"; done; echo
+
+echo "== 5. Empty-cart checkout PANIC (#6) — index out of range [0] -> 500 =="
+empty_cart
+codes=""; for i in $(seq 1 6); do codes+="$(hit POST /api/checkout "$CHECKOUT") "; done
+echo " $codes (500 = panic; an occasional 402 = the 1/6 decline firing first)"
+
+echo "== 6. Slow add-to-cart, inventory.check span (#4) — also fills the cart =="
+printf ' '; for i in $(seq 1 8); do printf '%s ' "$(timed POST /api/cart '{"product_id":4,"qty":1}')"; done; echo
+
+echo "== 7. Cart N+1 (#3) — GET /api/cart with items, ~75% slow per-line lookups =="
+printf ' '; for i in $(seq 1 10); do printf '%s ' "$(timed GET /api/cart)"; done; echo
+
+echo "== 8. Slow checkout (#7) + declined card (#8) — re-add an item each pass so it isn't empty =="
+printf ' '
+for i in $(seq 1 12); do
+ hit POST /api/cart '{"product_id":5,"qty":1}' >/dev/null
+ printf '%s ' "$(timed POST /api/checkout "$CHECKOUT")"
+done; echo
+echo " (slow 0.3-1.2s = #7 payment.charge; 402 = declined #8; 200 = order placed)"
+
+cat <<'EOF'
+
+== Done — open the Traceway dashboard, filter server "shop-demo" / version "0.1.0" ==
+ Issues: nil-map panic (#5), index-out-of-range panic (#6), declined-card (#8),
+ unknown-coupon (#5b)
+ Transactions: slow GET /api/products with the N+1 span fan-out, slow checkout payment.charge
+
+Frontend bugs are client-side JS — trigger these in the browser at the base URL:
+ #11 Products -> "Quick view" on any card (undefined .find, manual capture)
+ #9 Products -> "Add to cart" a few times (slow-path 500 -> unhandled promise rejection)
+ #10 Checkout -> enter a coupon, click "Apply" (failed apply -> null-deref in badge render,
+ until it errors caught by the error boundary)
+EOF