# Test diagnostic: MongoDB connection during `npm run build`

**Date:** 2026-03-10  
**Issue:** MongoDB appears to go down / other services see connection issues when running `npm run build`.

---

## 1. Build script check

- **Test:** Inspect what `npm run build` runs.
- **Command:** Read `package.json` scripts.
- **Result:** `"build": "react-scripts build"` — frontend-only; no DB or server process is started or stopped by the build.

---

## 2. Production build run

- **Test:** Run production build to see if it completes and if any DB-related step runs.
- **Command:** `npm run build`
- **Result:** Build **failed** before completion. Cause: `process.env.CI = true` → "Treating warnings as errors" → ESLint warnings (e.g. no-unused-vars, react-hooks/exhaustive-deps) cause compile failure. No MongoDB or server process is invoked by the build.

---

## 3. Backend MongoDB connection

- **Test:** Confirm how the app connects to MongoDB.
- **Location:** `dashboard/server.js`
- **Result:** `mongoose.connect(process.env.MONGO_URI)` with no connection options. `.env`: `MONGO_URI` points to remote host `18.184.249.241:27017`.

---

## 4. TCP connectivity to MongoDB host

- **Test:** Verify basic TCP reachability to the Mongo host.
- **Command:** Python socket connect to `18.184.249.241:27017` with 5s timeout.
- **Result:** `TCP_OK 302.0 ms` — connection succeeded at time of test (intermittent failures still possible).

---

## 5. Server port and process

- **Test:** See what is listening on port 5000 and whether the backend was already running.
- **Commands:** `ss -ltnp`, `ps -fp <pid>` for process on `:5000`.
- **Result:** `node dashboard/server.js` (PID 46997) listening on `*:5000`. Second `npm run start:server` failed with `EADDRINUSE :::5000`.

---

## 6. Backend logs (existing session)

- **Test:** Inspect server terminal output for MongoDB errors.
- **Result:** `Booking list error: MongoServerSelectionError: connection <monitor> to 18.184.249.241:27017 timed out` and `MongoNetworkTimeoutError`. Indicates connection timeout to remote Mongo, not a local process stopping the DB.

---

## 7. System resources (context)

- **Test:** Quick check of memory and load during investigation.
- **Commands:** `free -h`, `uptime`
- **Result:** ~15 GiB RAM, ~6.4 GiB available; swap heavily used (~3.9 GiB of 4 GiB). Load average ~1.25–1.60. Suggests possible resource pressure elsewhere (e.g. on Mongo host or during heavy build).

---

## 8. CI environment

- **Test:** Check why build treats warnings as errors.
- **Command:** `printenv | awk -F= '$1=="CI"{print $0}'`
- **Result:** `CI=1`. So `react-scripts build` runs in CI mode and fails on lint warnings.

---

## Summary

| Check                    | Outcome                                                                 |
|--------------------------|-------------------------------------------------------------------------|
| Build touches MongoDB?   | No — build only runs `react-scripts build`.                             |
| Build completes?         | No — fails on ESLint when `CI=1`.                                      |
| Mongo reachable (once)   | Yes — TCP to 18.184.249.241:27017 OK (~302 ms).                         |
| Backend Mongo errors     | Yes — timeouts to 18.184.249.241:27017 in server logs.                  |
| Likely cause             | Remote MongoDB/network instability or resource pressure, not the build. |

---

## Recommended next steps

1. **Mongo host:** Check `systemctl status mongod`, logs, OOM/kills during the time build runs.
2. **Build vs DB:** If build and Mongo run on same machine, run build on another host or reduce concurrency (e.g. `nice -n 10 npm run build`).
3. **Get a clean build:** Use `CI=false npm run build` or fix ESLint warnings so build succeeds and you can test “build only” without lint failure.
4. **Resilience:** Add Mongo reconnect handling and optional connection options (e.g. timeouts, retries) in `dashboard/server.js` so the app recovers from short outages.
