diff --git a/prototype-server-vision/README.md b/prototype-server-vision/README.md index 1047238..64958d1 100644 --- a/prototype-server-vision/README.md +++ b/prototype-server-vision/README.md @@ -20,12 +20,12 @@ Browser rendering was NOT verified (no browser in the build environment): only H |---|---| | `devbox run test` | regen map + `nimble test` (geometry: visibility, moves, cells, seed sanity) | | `devbox run client` | includes client reducer test (`npm run check`, incl. reconnect reset) | -| `PORT=8088 devbox run smoke` | fresh DB, server+nginx, auth/cookie/tile/steal/garbage checks | -| `PORT=8088 devbox run leaktest` | runs twice (SUBJECT=A, SUBJECT=B), fresh DB each; subject walks ~240 steps through rooms, corridor, open/closed doors while GM + B act; Nim checker (`tools/leakcheck.nim`) replays `run/leak-transcript.jsonl` | -| `PORT=8088 devbox run concurrency` | 60 s (`DUR=` secs) A, B, GM(6 monsters + doors) at 20-50 ms per token | +| `devbox run smoke` | fresh DB, server+nginx, auth/cookie/tile/steal/garbage checks | +| `devbox run leaktest` | runs twice (SUBJECT=A, SUBJECT=B), fresh DB each; subject walks ~240 steps through rooms, corridor, open/closed doors while GM + B act; Nim checker (`tools/leakcheck.nim`) replays `run/leak-transcript.jsonl` | +| `devbox run concurrency` | 60 s (`DUR=` secs) A, B, GM(6 monsters + doors) at 20-50 ms per token | | `devbox run bench` | vision microbench | -`smoke/leaktest/concurrency` start/stop their own server+nginx via `tools/withserver.sh` (wipes `run/state.db`). Server stats print on stop into `run/server.log`. +Tests are isolated: `tools/withserver.sh` uses `run/test/` (own DB, invites, nginx conf/logs) and ports 18088 (nginx) / 19000 (server), overridable via `TEST_PORT`/`TEST_BACKEND_PORT`; the live session on 8088/9000 and `run/` are never touched. Server stats print on stop into `run/test/server.log`. ### Leak test asserts Ground truth = GM socket (tokens + doors per gseq). A's expected vision/explored is recomputed with `geometry.nim`, never from A's own messages. Per gseq: diff --git a/prototype-server-vision/devbox.json b/prototype-server-vision/devbox.json index 027f810..e0e8e8a 100644 --- a/prototype-server-vision/devbox.json +++ b/prototype-server-vision/devbox.json @@ -24,7 +24,10 @@ ], "test": [ "node tools/genmap.mjs", - "nimble test" + "nimble test", + "devbox run smoke", + "devbox run leaktest", + "DUR=15 devbox run concurrency" ], "server": [ "nimble build -d:release -y", @@ -33,12 +36,12 @@ ], "nginx": [ "mkdir -p run/temp client/dist", - "sed \"s/NGX_PORT/${PORT:-8080}/\" nginx/nginx.conf > run/nginx.conf", + "sed \"s/NGX_PORT/${PORT:-8080}/;s/NGX_BACKEND/${BACKEND_PORT:-9000}/\" nginx/nginx.conf > run/nginx.conf", "nginx -p \"$PWD/\" -c run/nginx.conf -e run/error.log" ], "leaktest": [ - "nim c -d:release --hints:off -o:run/leakcheck tools/leakcheck.nim", - "for s in A B; do SUBJECT=$s tools/withserver.sh sh -c 'node tools/leak.mjs && run/leakcheck' || exit 1; done" + "mkdir -p run/test && nim c -d:release --hints:off -o:run/test/leakcheck tools/leakcheck.nim", + "for s in A B; do SUBJECT=$s tools/withserver.sh sh -c 'node tools/leak.mjs && run/test/leakcheck' || exit 1; done" ], "concurrency": [ "tools/withserver.sh node tools/concurrency.mjs" diff --git a/prototype-server-vision/nginx/nginx.conf b/prototype-server-vision/nginx/nginx.conf index a0cd85f..4ed8aab 100644 --- a/prototype-server-vision/nginx/nginx.conf +++ b/prototype-server-vision/nginx/nginx.conf @@ -14,14 +14,14 @@ http { include ../nginx/mime.types; # relative to the generated run/nginx.conf map $http_upgrade $conn_up { default upgrade; '' close; } server { - listen NGX_PORT; # sed-substituted from $PORT (default 8080) + listen NGX_PORT; # sed-substituted from $PORT (default 8080); NGX_BACKEND from $BACKEND_PORT (default 9000) server_name localhost; root client/dist; index index.html; - location /join/ { proxy_pass http://127.0.0.1:9000; } - location /tiles/ { proxy_pass http://127.0.0.1:9000; } + location /join/ { proxy_pass http://127.0.0.1:NGX_BACKEND; } + location /tiles/ { proxy_pass http://127.0.0.1:NGX_BACKEND; } location /ws { - proxy_pass http://127.0.0.1:9000; + proxy_pass http://127.0.0.1:NGX_BACKEND; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $conn_up; diff --git a/prototype-server-vision/src/server.nim b/prototype-server-vision/src/server.nim index 36841f6..f261fd8 100644 --- a/prototype-server-vision/src/server.nim +++ b/prototype-server-vision/src/server.nim @@ -31,6 +31,9 @@ type # nginx public port; 8080 per spec, PORT overrides when taken let origin = "http://localhost:" & getEnv("PORT", "8080") +# RUN_DIR: db/invites dir (tools/withserver.sh sets run/test); BACKEND_PORT: mummy listen port (nginx proxies to it) +let runDir = getEnv("RUN_DIR", "run") +let backendPort = parseInt(getEnv("BACKEND_PORT", "9000")) var lk: Lock @@ -280,8 +283,8 @@ proc loadState() = for t in sc["tokens"]: toks.add Tok(id: t["id"].getStr, name: t["name"].getStr, kind: t["kind"].getStr, pos: (t["pos"][0].getFloat, t["pos"][1].getFloat), gmOnly: t["gmOnly"].getBool) - createDir "run" - db = open("run/state.db", "", "", "") + createDir runDir + db = open(runDir / "state.db", "", "", "") discard db.getAllRows(sql"PRAGMA journal_mode=WAL") for s in ["CREATE TABLE IF NOT EXISTS meta (k TEXT PRIMARY KEY, v TEXT)", "CREATE TABLE IF NOT EXISTS tok (id TEXT PRIMARY KEY, x REAL, y REAL)", @@ -303,7 +306,7 @@ proc loadState() = roles[rn] = Role(ver: 0) for r in db.getAllRows(sql"SELECT c FROM exp WHERE role=?", rn): roles[rn].explored.incl parseInt(r[0]) echo invites - writeFile("run/invites.txt", invites) + writeFile(runDir / "invites.txt", invites) db.exec(sql"BEGIN") for rn in roles.keys: recompute(rn); persistCells(rn); roles[rn].fresh.setLen 0 @@ -315,6 +318,6 @@ setControlCHook(proc() {.noconv.} = printStats() # shortcut: reads stats without the lock; fine at exit quit 0) let server = newServer(handler, wsHandler, workerThreads = 8) -echo "listening 127.0.0.1:9000" +echo "listening 127.0.0.1:" & $backendPort stdout.flushFile -server.serve(Port(9000), "127.0.0.1") +server.serve(Port(backendPort), "127.0.0.1") diff --git a/prototype-server-vision/tools/leak.mjs b/prototype-server-vision/tools/leak.mjs index a741493..54ab4f8 100644 --- a/prototype-server-vision/tools/leak.mjs +++ b/prototype-server-vision/tools/leak.mjs @@ -2,8 +2,8 @@ // SUBJECT=A|B (default A) is the player whose tile sweeps/view are checked (leakcheck reads the same env); the other walks as noise. // Everything is sequential (one actor at a time) so every tile sweep sees a quiescent server at a known gseq. import fs from 'node:fs' -import { O, connect, sleep } from './lib.mjs' -const out = fs.createWriteStream(new URL('../run/leak-transcript.jsonl', import.meta.url)) +import { O, RUN, connect, sleep } from './lib.mjs' +const out = fs.createWriteStream(`${RUN}/leak-transcript.jsonl`) const log = o => out.write(JSON.stringify(o) + '\n') let gmG = 0 const S = process.env.SUBJECT ?? 'A' diff --git a/prototype-server-vision/tools/leakcheck.nim b/prototype-server-vision/tools/leakcheck.nim index f388371..2ab6e5b 100644 --- a/prototype-server-vision/tools/leakcheck.nim +++ b/prototype-server-vision/tools/leakcheck.nim @@ -31,7 +31,7 @@ proc scan(j: JsonNode, t: string) = var gm, am: seq[JsonNode] var tiles: seq[JsonNode] -for line in lines("run/leak-transcript.jsonl"): +for line in lines(getEnv("RUN_DIR", "run") / "leak-transcript.jsonl"): let j = parseJson(line) if j["k"].getStr == "tile": tiles.add j elif j["who"].getStr == "GM": gm.add j["m"] diff --git a/prototype-server-vision/tools/lib.mjs b/prototype-server-vision/tools/lib.mjs index 351e2c0..b23fbf5 100644 --- a/prototype-server-vision/tools/lib.mjs +++ b/prototype-server-vision/tools/lib.mjs @@ -1,7 +1,8 @@ // shared by leak.mjs / concurrency.mjs. Node 22 WebSocket (undici) accepts {headers}. import fs from 'node:fs' +export const RUN = process.env.RUN_DIR ?? 'run' export const O = `http://localhost:${process.env.PORT ?? 8080}` -export const inv = Object.fromEntries(fs.readFileSync(new URL('../run/invites.txt', import.meta.url), 'utf8').trim().split('\n').map(l => l.split(' '))) +export const inv = Object.fromEntries(fs.readFileSync(`${RUN}/invites.txt`, 'utf8').trim().split('\n').map(l => l.split(' '))) export const sleep = ms => new Promise(r => setTimeout(r, ms)) export const login = async role => (await fetch(inv[role], { redirect: 'manual' })).headers.get('set-cookie').split(';')[0] // resolves once hello+initial state arrived (socket quiet for 300 ms) diff --git a/prototype-server-vision/tools/smoke.mjs b/prototype-server-vision/tools/smoke.mjs index 0d7876c..2ee8cb3 100644 --- a/prototype-server-vision/tools/smoke.mjs +++ b/prototype-server-vision/tools/smoke.mjs @@ -1,7 +1,8 @@ // node tools/smoke.mjs [pos] -- needs server+nginx up. Node 22 WebSocket (undici) accepts {headers}. import fs from 'node:fs' const O = `http://localhost:${process.env.PORT ?? 8080}` -const inv = Object.fromEntries(fs.readFileSync(new URL('../run/invites.txt', import.meta.url), 'utf8').trim().split('\n').map(l => l.split(' '))) +const RUN = process.env.RUN_DIR ?? 'run' +const inv = Object.fromEntries(fs.readFileSync(`${RUN}/invites.txt`, 'utf8').trim().split('\n').map(l => l.split(' '))) const ok = (c, m) => { console.log(c ? 'PASS' : 'FAIL', m); if (!c) process.exitCode = 1 } const join = async u => { const r = await fetch(u, { redirect: 'manual' }); return r } const ws = (cookie, ms = 800) => new Promise(res => { diff --git a/prototype-server-vision/tools/withserver.sh b/prototype-server-vision/tools/withserver.sh index 19b952d..afebdfe 100755 --- a/prototype-server-vision/tools/withserver.sh +++ b/prototype-server-vision/tools/withserver.sh @@ -1,13 +1,15 @@ #!/bin/sh -# usage: tools/withserver.sh cmd... fresh DB, build, start server+nginx (PORT, default 8080), run cmd, stop both (SIGINT => server prints STATS) +# usage: tools/withserver.sh cmd... isolated stack in run/test (own DB/invites/nginx conf+logs/binary), own ports TEST_PORT (nginx, 18088) + TEST_BACKEND_PORT (server, 19000); never touches live run/ or :8088/:9000. SIGINT => server prints STATS cd "$(dirname "$0")/.." || exit 1 -PORT=${PORT:-8080}; export PORT -mkdir -p run/temp client/dist -rm -f run/state.db* run/invites.txt -nimble build -d:release -y >/dev/null || exit 1 -sed "s/NGX_PORT/$PORT/" nginx/nginx.conf > run/nginx.conf -./server > run/server.log 2>&1 & S=$! -nginx -p "$PWD/" -c run/nginx.conf -e run/error.log > run/nginx.out 2>&1 & N=$! +export RUN_DIR=run/test PORT=${TEST_PORT:-18088} BACKEND_PORT=${TEST_BACKEND_PORT:-19000} +R=$RUN_DIR +case "$PORT:$BACKEND_PORT" in 8088:*|*:9000|*:8088) echo "withserver: refusing live port (8088/9000)" >&2; exit 1;; esac +mkdir -p $R/temp client/dist +rm -f $R/state.db* $R/invites.txt +nim c -d:release --hints:off --nimblePath:"${NIMBLE_DIR:-.devbox/nimble}/pkgs2" -o:$R/server src/server.nim >/dev/null || exit 1 +sed -e "s/NGX_PORT/$PORT/" -e "s/NGX_BACKEND/$BACKEND_PORT/" -e "s#run/#$R/#g" -e "s#\.\./nginx/mime#$PWD/nginx/mime#" nginx/nginx.conf > $R/nginx.conf +./$R/server > $R/server.log 2>&1 & S=$! +nginx -p "$PWD/" -c $R/nginx.conf -e $R/error.log > $R/nginx.out 2>&1 & N=$! trap 'kill -INT $S; sleep 1; kill $N 2>/dev/null' EXIT -for i in $(seq 50); do [ -s run/invites.txt ] && curl -s -o /dev/null "localhost:$PORT/" && break; sleep 0.2; done +for i in $(seq 50); do [ -s $R/invites.txt ] && curl -s -o /dev/null "localhost:$PORT/" && break; sleep 0.2; done "$@"