58 lines
3.2 KiB
JavaScript
58 lines
3.2 KiB
JavaScript
|
|
// End-to-end check of the WS API with the frames @blax-software/networking sends.
|
||
|
|
// Usage: node client.mjs <ws url> <bearer token>
|
||
|
|
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)
|