# Testing Workshare before handing it over

Four stages, each proving more. Stages 1–3 need nothing from your manager; do them today.
Every stage ends with the same check, the **smoke test**: a script that plays two users through the real API and prints
PASS / FAIL per step. It makes one temporary model and deletes it again.

| Stage | Where the server runs | Proves | Time |
|---|---|---|---|
| 1 | Your PC | The server and your Unity test copy work together: sync, ownership, reload, furniture | 30 min |
| 2 | Your PC, reachable over the internet with HTTPS (Cloudflare tunnel) | It works through HTTPS and a proxy, from another network, like a customer | 15 min |
| 3 | Linux inside your PC (WSL) | The Linux installer works: service, Caddy, backups (a rehearsal of the real install) | 45 min |
| 4 | The company server | The real thing | after your manager is ready |

---

## Stage 1: on your PC

### 1.1 Node.js on your PC (once)
cmd:
```cmd
node --version
```
If it says "not recognized": `winget install --id OpenJS.NodeJS.LTS -e`, close cmd, open a new one.

### 1.2 Run the automatic tests (about 2 minutes)
```cmd
cd /d D:\Projects\WorkshareServer
npm test
```
Every block should end with `0 failed`. On Windows the database tests print `SKIP` (PostgreSQL not installed, MySQL
not set) — that is expected; they run on Linux (Stage 3) and on GitHub with every push.

### 1.3 Start the server
```cmd
npm start
```
Leave this window open (it prints a line per request). Stop it with Ctrl+C.

### 1.4 Smoke test (new cmd window)
```cmd
cd /d D:\Projects\WorkshareServer
node tools\smoke-test.js http://127.0.0.1:8080
```
Expected last line: `20 passed, 0 failed  - this server works`.

### 1.5 Unity test copy
1. Copy the `Workshare` folder from `Kavion_Workshare_Handoff.zip` over `Assets/Workshare` (it has all of today's client changes).
2. Player Settings: **Product Name `Kavion Dev`** (keeps your tests apart from the real app).
3. Setup Object → **Add Component → Workshare Update Notice**, in both scenes.
4. For this local test only: Player Settings → Other Settings → **Allow downloads over HTTP = Always allowed**.
5. Build a Windows build of the test copy (File → Build Settings → Build).

You now have **two users on one PC**: the Unity Editor and the build. They store their settings separately, so they
are really two members.

### 1.6 Test scenario (Editor = Ann, build = Bob)

| # | Do | Expect |
|---|---|---|
| 1 | Ann: Play → Workshare → server `http://127.0.0.1:8080`, name Ann → **New central model** | Join code shown |
| 2 | Bob (build): same server, name Bob → **Join** with the code | Bob sees version 1 |
| 3 | Ann draws a room → **Synchronize with Central** | Version 2 |
| 4 | Bob → **Reload Latest** | Ann's room appears |
| 5 | Bob edits a door (it becomes Bob's) → Ann tries to move the same door | Ann: "Owned by Bob" |
| 6 | Ann → Worksets → **Request** the door → Bob → Requests → **Grant** | Ann can now edit it |
| 7 | Both edit different rooms, both Synchronize | Both changes in central, nothing lost |
| 8 | History tab | Every version with who and what |
| 9 | Home Designer scene: Ann places furniture → Synchronize → Bob Reload Latest | Furniture appears |
| 10 | Stop the server (Ctrl+C) → try Synchronize | Clear "cannot reach the server" message, no crash |
| 11 | Start the server again → Synchronize | Works, nothing lost |

**Version banner:** stop the server, start it with a newer "latest" version, and Play again:
```cmd
set WORKSHARE_LATEST_APP_VERSION=0.2.0
set WORKSHARE_DOWNLOAD_URL=https://example.com/KavionSetup-0.2.0.exe
npm start
```
Expect a blue "A new version (0.2.0) is available" banner. Then add `set WORKSHARE_MIN_APP_VERSION=0.2.0` and
restart: a red "Update required" banner, and Synchronize is refused. Close the window afterwards (the `set` values
only live in that window).

---

## Stage 2: over the internet with HTTPS (Cloudflare quick tunnel)

A free Cloudflare tunnel gives your PC's server a temporary public `https://...trycloudflare.com` address, with no
account, no router setup and no open ports. Only for testing with test data: the address changes every time and works
only while your PC and the tunnel run.

1. Once: `winget install --id Cloudflare.cloudflared -e` (new cmd window afterwards).
2. Window 1, the server, told that a proxy sits in front:
   ```cmd
   cd /d D:\Projects\WorkshareServer
   set WORKSHARE_TRUST_PROXY=1
   npm start
   ```
3. Window 2, the tunnel:
   ```cmd
   cloudflared tunnel --url http://localhost:8080
   ```
   After a few seconds it prints `https://<random-words>.trycloudflare.com`. Copy it.
4. Window 3: `node tools\smoke-test.js https://<random-words>.trycloudflare.com` → `20 passed` (the "push updates" step proves the tunnel doesn't buffer them).
5. Unity: the build's server field = that https address (Allow downloads over HTTP isn't needed for https).
   Best test: send the build to a colleague at home or use your phone's hotspot on a laptop, and repeat a few
   steps of the 1.6 scenario between you.

---

## Stage 3: rehearse the Linux installation (WSL on your PC)

WSL runs a real Ubuntu inside Windows, the same kind of system as the company server. Here you run the actual
installer once, so the real installation holds no surprises.

1. cmd **as administrator**: `wsl --install -d Ubuntu-24.04`, restart the PC when asked, then open "Ubuntu" from
   the Start menu and choose a Linux user name and password.
2. In Ubuntu:
   ```bash
   ps -p 1 -o comm=                      # must print: systemd
   curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
   sudo apt-get install -y nodejs unzip postgresql mysql-server
   cp -r /mnt/d/Projects/WorkshareServer ~/ && cd ~/WorkshareServer
   npm install                           # the MySQL driver
   npm test                              # all "0 failed" (the MySQL suite prints SKIP; GitHub runs it)
   # a MySQL database + user like the manager will make on the real server:
   sudo mysql -e "CREATE DATABASE workshare CHARACTER SET utf8mb4 COLLATE utf8mb4_bin; CREATE USER 'workshare'@'localhost' IDENTIFIED BY 'rehearsal-only-pw-123'; GRANT ALL PRIVILEGES ON workshare.* TO 'workshare'@'localhost';"
   echo "mysql://workshare:rehearsal-only-pw-123@127.0.0.1:3306/workshare" > ~/ws-db-url
   sudo sh deploy/linux/install-linux.sh --check --mysql-url-file ~/ws-db-url
   sudo sh deploy/linux/install-linux.sh --lan localhost --mysql-url-file ~/ws-db-url
   ```
   (If `ps` doesn't print `systemd`: `echo -e "[boot]\nsystemd=true" | sudo tee /etc/wsl.conf`, then in cmd
   `wsl --shutdown` and open Ubuntu again.)
3. Check the installed service:
   ```bash
   systemctl status workshare --no-pager
   node tools/smoke-test.js http://127.0.0.1:8080
   sudo NODE_EXTRA_CA_CERTS=/var/lib/caddy/.local/share/caddy/pki/authorities/local/root.crt node tools/smoke-test.js https://localhost
   ```
   Both: `20 passed`. The second run goes through Caddy and HTTPS, like the real server.
4. Check the database:
   ```bash
   sudo mysql workshare -e "SELECT name, latest_version FROM models"
   sudo mysql workshare -e "SELECT at, action FROM audit_log ORDER BY id DESC LIMIT 5"
   ```
   The smoke test's model was deleted again, so `models` may be empty, but the audit log shows its create and delete.
5. Database outage drill: `sudo systemctl stop mysql`, then `curl http://127.0.0.1:8080/ready` → not ready,
   and the smoke test fails with "503 / try again" instead of saying "saved". `sudo systemctl start mysql`, run
   the smoke test again → `20 passed`.
6. Rehearse the daily tasks from the guide (section 10): restart, logs, a manual backup, **a restore**, an update
   with `sudo sh deploy/linux/update.sh`.
7. Remove it all afterwards if you like (cmd): `wsl --unregister Ubuntu-24.04`.

---

## Stage 4: the company server

After the installation (SETUP_GUIDE_LINUX.md), from **your PC**:
```cmd
node tools\smoke-test.js https://workshare.globalinfocloud.in --token <create token>
```
(`sudo cat /etc/workshare/workshare.env` on the server shows the token.) Then the Unity build from outside the office
network (home / phone hotspot), and the 1.6 scenario once more with a colleague.

---

## If something fails

Send Dikshant:
- the smoke test output, and
- the server log around that time: on your PC the `npm start` window; on Linux `journalctl -u workshare -n 100 --no-pager`.

Each error answer contains a request id (`request 3f9a...`); the same id is in the server log line, so the two can be
matched.
