diff --git a/scripts/e2e-edgerealm.sh b/scripts/e2e-edgerealm.sh new file mode 100644 index 0000000..f399ca1 --- /dev/null +++ b/scripts/e2e-edgerealm.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +# End-to-end check of the WS API on the deployed code, without touching production data: +# a throwaway container (no network) copies the backend checkout, builds its own SQLite +# world, user, WS server and game:events worker, and runs scripts/e2e/client.mjs against +# them (join, village.show, village.build with its push, map.view). Run after a deploy. +# On Windows, put the Windows OpenSSH client first: PATH="/c/Windows/System32/OpenSSH:$PATH". +set -euo pipefail + +HOST=root@static.64.40.140.128.clients.your-server.de +DIR=/mnt/HC_Volume_102040804/srv/game-village-politics-backend +IMAGE=blaxsoftware/laravel:laravel13-php8.4 + +ssh -o BatchMode=yes "$HOST" "docker run --rm --network none -v $DIR:/src:ro --entrypoint bash $IMAGE /src/scripts/e2e/run.sh 2>&1 | tail -25" diff --git a/scripts/e2e/client.mjs b/scripts/e2e/client.mjs new file mode 100644 index 0000000..a66f7b5 --- /dev/null +++ b/scripts/e2e/client.mjs @@ -0,0 +1,57 @@ +// End-to-end check of the WS API with the frames @blax-software/networking sends. +// Usage: node client.mjs +const [url, token] = process.argv.slice(2) +const ws = new WebSocket(url) +const waiters = [] +const log = (...a) => console.log(...a) + +ws.onmessage = (m) => { + const msg = JSON.parse(m.data) + if (msg.event === 'village:updated') log('PUSH village:updated', JSON.stringify(msg.data)) + for (const w of [...waiters]) if (w.match(msg)) { waiters.splice(waiters.indexOf(w), 1); w.resolve(msg) } +} +const next = (match, ms = 8000) => new Promise((resolve, reject) => { + waiters.push({ match, resolve }) + setTimeout(() => reject(new Error('timeout waiting')), ms) +}) +async function call(event, data = {}, withToken = true) { + const tagged = `${event}[${Math.random().toString(36).slice(7)}]` + const reply = next((m) => m.event === `${tagged}:response` || m.event === `${tagged}:error`) + ws.send(JSON.stringify({ event: tagged, data: withToken ? { ...data, authtoken: token } : data, channel: 'websocket' })) + const msg = await reply + return { ok: msg.event.endsWith(':response'), data: msg.data } +} + +await next((m) => m.event === 'websocket.connection_established') +log('connected') + +ws.send(JSON.stringify({ event: 'websocket.subscribe', data: { channel: 'websocket', authtoken: token }, channel: null })) +await next((m) => m.event?.startsWith('websocket.subscribe')) +log('subscribed') + +const guest = await call('world.index', {}, false) +log('world.index without token ->', guest.ok ? 'answered (unexpected)' : `refused: ${JSON.stringify(guest.data)}`) + +const worlds = await call('world.index') +log('world.index ->', JSON.stringify(worlds.data)) +const dev = worlds.data.find((w) => w.slug === 'dev') +const joined = await call('world.join', { world_id: dev.id, direction: 'ne' }) +log('world.join ->', JSON.stringify(joined.data)) +const id = joined.data.village_id + +const shown = await call('village.show', { village_id: id }) +log('village.show ->', shown.ok, 'buildings', shown.data.buildings.map((b) => `${b.type}(space ${b.space})`).join(' '), 'space', JSON.stringify(shown.data.space), 'continent', shown.data.continent, 'points', shown.data.points) + +const refused = await call('village.build', { village_id: id, type: 'headquarters' }) +log('village.build HQ ->', refused.ok ? 'answered (unexpected)' : `refused: ${JSON.stringify(refused.data)}`) + +const built = await call('village.build', { village_id: id, type: 'timber_camp' }) +log('village.build timber_camp ->', built.ok, JSON.stringify(built.data.queue?.[0])) + +const pushed = await next((m) => m.event === 'village:updated', 10000).then(() => true, () => false) +const after = await call('village.show', { village_id: id }) +log('after finish -> push received:', pushed, '| queue', after.data.queue.length, '| timber camp', JSON.stringify(after.data.buildings.find((b) => b.type === 'timber_camp'))) +const map = await call('map.view', { world_id: dev.id, w: 41, h: 41 }) +log('map.view ->', map.ok, 'centre', JSON.stringify(map.data.center), 'window', map.data.x, map.data.y, map.data.w, map.data.h, '| villages', map.data.villages.length, '| own', JSON.stringify(map.data.villages.find((v) => v.own)), '| barbarians', map.data.villages.filter((v) => !v.player).length) +ws.close() +process.exit(0) diff --git a/scripts/e2e/run.sh b/scripts/e2e/run.sh new file mode 100644 index 0000000..24a7193 --- /dev/null +++ b/scripts/e2e/run.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# Started by scripts/e2e-edgerealm.sh. Runs inside a throwaway container: a copy of the deployed +# code with its own SQLite DB, test user, WS server (port 6099) and game:events worker. +# Production data is not touched. +set -e +mkdir /tmp/app && (cd /src && tar cf - --exclude=./docker-data --exclude=./.git .) | tar xf - -C /tmp/app && cd /tmp/app +rm -f .env bootstrap/cache/*.php +export APP_ENV=local APP_DEBUG=false APP_KEY="base64:$(head -c 32 /dev/urandom | base64)" +export DB_CONNECTION=sqlite DB_DATABASE=/tmp/e2e.sqlite CACHE_STORE=file SESSION_DRIVER=array QUEUE_CONNECTION=sync LOG_CHANNEL=stderr +export PUSHER_APP_ID=e2e PUSHER_APP_KEY=e2e PUSHER_APP_SECRET=e2e-secret +touch /tmp/e2e.sqlite +php artisan migrate --force -q && php artisan db:seed --force -q +TOKEN=$(php artisan tinker --execute 'echo App\Models\User::create(["name"=>"E2E","email"=>"e2e@example.test","password"=>"e2e-only-pass"])->createToken("e2e")->plainTextToken;' | tail -1) +php artisan websockets:serve --port=6099 > /tmp/ws.log 2>&1 & +php artisan game:events > /tmp/worker.log 2>&1 & +sleep 4 +node /src/scripts/e2e/client.mjs ws://127.0.0.1:6099/app/e2e "$TOKEN" || { echo "--- ws.log"; tail -30 /tmp/ws.log; echo "--- worker.log"; tail -20 /tmp/worker.log; exit 1; }