Under the fan-out a test that greps server.log right after an ack can lose: the logger writes the line after the ack. waitForLog polls to a deadline and the assertions are unchanged (the line must still appear; absence checks still run on the same snapshot). Fixes the one observed assertion failure (fault.test.mjs's intent_failed line) and the same latent race in the opaque_refused and startup-line checks.
104 lines
5.1 KiB
JavaScript
104 lines
5.1 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, waitForLog } 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
|
|
const startLog = await waitForLog(s, new RegExp(`server_start origin=${s.origin} .* bind=127\\.0\\.0\\.1`))
|
|
assert.match(startLog, 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=8 applied=8/, 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), 8)
|
|
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'], [3, 'world'], [4, 'tokens'], [5, 'explored'],
|
|
[6, 'token details'], [7, 'characters'], [8, 'wall record']])
|
|
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)
|
|
// step 3 seeds the single v1 scene (Q3) with no walls and a counter at 1
|
|
assert.deepEqual(db.prepare('SELECT id, next_wall_id FROM scene').all()
|
|
.map(r => [Number(r.id), Number(r.next_wall_id)]), [[1, 1]])
|
|
assert.equal(Number(db.prepare('SELECT count(*) AS n FROM wall').get().n), 0)
|
|
// step 4 creates the token table (DESIGN 4.4 `entity`), empty
|
|
assert.equal(Number(db.prepare('SELECT count(*) AS n FROM entity').get().n), 0)
|
|
// step 5 creates the chunked explored store (DESIGN 4.4, #21), empty
|
|
assert.equal(Number(db.prepare('SELECT count(*) AS n FROM explored_chunk').get().n), 0)
|
|
// step 6 adds the three prop buckets and the status set (DESIGN 5.5, 8; #22), empty
|
|
assert.equal(Number(db.prepare('SELECT count(*) AS n FROM entity_status').get().n), 0)
|
|
for (const col of ['props_public', 'props_owner', 'props_gm', 'character_id', 'reveal_all']) {
|
|
assert.equal(Number(db.prepare(`SELECT count(*) AS n FROM pragma_table_info('entity')
|
|
WHERE name = '${col}'`).get().n), 1, col)
|
|
}
|
|
// step 7 creates the character world (DESIGN 8, #23), empty
|
|
for (const table of ['character', 'name_reveal', 'encountered']) {
|
|
assert.equal(Number(db.prepare(`SELECT count(*) AS n FROM ${table}`).get().n), 0, table)
|
|
}
|
|
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=8 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/)
|
|
})
|