OverviewFilesCommitsIssuesDocsReleasesPeers

godot-keet / tests / check-ait.mjs

50 lines · javascript ref:
import Pipe from 'bare-pipe'
import FramedStream from 'framed-stream'
import b4a from 'b4a'

const pipe = new Pipe('/tmp/keet-daemon.sock')
const framed = new FramedStream(pipe)
let id = 0

function call (m, p) {
  return new Promise((res, rej) => {
    const i = ++id
    const t = setTimeout(() => rej(new Error('timeout')), 10000)
    framed.on('data', function h (buf) {
      const msg = JSON.parse(b4a.toString(buf))
      if (msg.id === i) {
        framed.removeListener('data', h)
        clearTimeout(t)
        if (msg.error) rej(new Error(msg.error.message || JSON.stringify(msg.error)))
        else res(msg.result || msg.data)
      }
    })
    framed.write(b4a.from(JSON.stringify({ jsonrpc: '2.0', id: i, method: m, params: p || {} })))
  })
}

async function run () {
  const rooms = await call('rooms')
  const ait = rooms.find(r => r.title === 'AI Testing Town')
  if (!ait) { console.log('Not found'); framed.destroy(); pipe.destroy(); return }
  const AIT = ait.roomId || ait.id
  console.log('roomId:', AIT)
  const msgs = await call('read', { roomId: AIT, limit: 20, reverse: true })
  const messages = Array.isArray(msgs) ? msgs : (msgs?.messages || msgs?.data || [])

  console.log('Last 15 messages in AI Testing Town:\n')
  for (const m of messages) {
    const text = m.chat?.text || m.text || m.message?.text || ''
    const ts = m.chat?.timestamp || m.timestamp || 0
    const date = ts ? new Date(ts).toISOString().slice(0, 19) : 'unknown'
    const prefix = text.startsWith('{"_hs":') ? '[_hs]' : '[chat]'
    console.log(`${date} ${prefix} ${text.slice(0, 120)}`)
  }

  framed.destroy()
  pipe.destroy()
  setTimeout(() => Bare.exit(0), 500)
}

run().catch(err => { console.error('FAIL:', err.message); Bare.exit(1) })