Files
FFF_vtt_frozen/tools/smoke.test.mjs
T

83 lines
3.5 KiB
JavaScript

// M3 slice 1 smoke (#16): the bare server starts under withserver, answers /healthz, binds
// loopback only, and a fresh DB is created and migrated by the runner (re-open applies nothing).
import test from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import { spawnSync } from 'node:child_process'
import { DatabaseSync } from 'node:sqlite'
import { listeningSockets } from './lib.mjs'
import { buildServer, makeStateDir, startServer, stopServer, withServer } from './withserver.mjs'
test('withserver brings the server up and /healthz answers 200 JSON', async () => {
await withServer(async s => {
const r = await fetch(`${s.origin}/healthz`)
assert.equal(r.status, 200)
assert.equal(r.headers.get('content-type'), 'application/json')
assert.deepEqual(await r.json(), { status: 'ok' })
assert.equal((await fetch(`${s.origin}/nope`)).status, 404)
assert.equal((await fetch(`${s.origin}/healthz`, { method: 'POST' })).status, 405)
// flags reached the config and the start line: the harness origin and the fixed loopback bind
assert.match(s.log(), new RegExp(`server_start origin=${s.origin} .* bind=127\\.0\\.0\\.1`))
})
})
test('the server listens on 127.0.0.1 only', async () => {
await withServer(async s => {
const sockets = listeningSockets(s.port)
assert.ok(sockets.length > 0, `no LISTEN socket found for port ${s.port}`)
for (const sock of sockets) {
assert.equal(sock.addr, '127.0.0.1', `bound ${sock.proto} ${sock.addr}, not loopback-only`)
}
})
})
test('negative control: the loopback check sees a 0.0.0.0 listener', async () => {
const net = await import('node:net')
const srv = net.createServer()
await new Promise((res, rej) => { srv.once('error', rej); srv.listen(0, '0.0.0.0', res) })
try {
const addrs = listeningSockets(srv.address().port).map(s => s.addr)
assert.deepEqual(addrs, ['0.0.0.0'])
} finally {
await new Promise(res => srv.close(res))
}
})
test('a fresh DB is created and migrated; re-opening applies nothing', async () => {
const stateDir = makeStateDir()
try {
const first = await startServer({ stateDir })
const firstLog = first.log()
await stopServer(first)
assert.match(firstLog, /db_ready .*schema_version=2 applied=2/, firstLog)
assert.ok(fs.existsSync(first.dbPath), 'state.db was not created')
const db = new DatabaseSync(first.dbPath)
try {
assert.equal(Number(db.prepare('PRAGMA user_version').get().user_version), 2)
const rows = db.prepare('SELECT version, name FROM schema_migration ORDER BY version').all()
assert.deepEqual(rows.map(r => [Number(r.version), r.name]), [[1, 'meta'], [2, 'auth']])
assert.equal(Number(db.prepare('SELECT count(*) AS n FROM meta').get().n), 0)
assert.equal(Number(db.prepare('SELECT count(*) AS n FROM player').get().n), 0)
assert.equal(db.prepare('PRAGMA journal_mode').get().journal_mode, 'wal')
} finally {
db.close()
}
const second = await startServer({ stateDir })
const secondLog = second.log().slice(firstLog.length)
await stopServer(second)
assert.match(secondLog, /db_ready .*schema_version=2 applied=0/, secondLog)
assert.doesNotMatch(secondLog, /applied=[1-9]/)
} finally {
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
test('a bad flag is a clean exit 2 with a message, not a crash', () => {
const r = spawnSync(buildServer(), ['--port=0'], { encoding: 'utf8', timeout: 10000 })
assert.equal(r.status, 2)
assert.match(r.stderr, /--port/)
assert.doesNotMatch(r.stderr, /Traceback|SIGSEGV/)
})