- index and category pages paginate with ?page=N and 404 out of range - category content renders above the list through the html gate - related posts fall back to the latest posts - --journal-* tokens and neutral base styles - pl/en strings registered only for locales the app enables
141 lines
5.9 KiB
JavaScript
Executable File
141 lines
5.9 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
// SSR smoke check: boots the fixture Journal API and a built Nuxt server,
|
|
// then checks each line of an expectations file.
|
|
//
|
|
// node smoke-ssr.mjs --output <.output dir> --fixtures <dir> --expect <file> [--env KEY=VALUE]...
|
|
//
|
|
// Expectation grammar, one check per line ('#' starts a comment line):
|
|
// STATUS PATH [contains=TEXT] [notcontains=TEXT] [type=PREFIX]
|
|
// A value runs until the next ` contains=`, ` notcontains=`, ` type=` or the
|
|
// end of the line, so it may contain spaces. Requests are plain GETs that do
|
|
// not follow redirects and carry `X-Forwarded-For: 203.0.113.7`. After the
|
|
// lines, at least one fixture API request must have carried that address
|
|
// (the SSR fetches forward the visitor's x-forwarded-for), and every
|
|
// categories request must have carried include_empty=1.
|
|
|
|
import { readFileSync } from 'node:fs'
|
|
import { resolve } from 'node:path'
|
|
import { startFixtureApi } from './fixture-api.mjs'
|
|
import { startNuxtServer } from './nuxt-server.mjs'
|
|
|
|
const VISITOR_IP = '203.0.113.7'
|
|
|
|
/** @param {string[]} argv */
|
|
function parseArgs(argv) {
|
|
/** @type {{ output?: string, fixtures?: string, expect?: string, env: Record<string, string> }} */
|
|
const out = { env: {} }
|
|
for (let i = 0; i < argv.length; i++) {
|
|
const arg = argv[i]
|
|
const next = () => {
|
|
const v = argv[++i]
|
|
if (v === undefined) throw new Error(`${arg} needs a value`)
|
|
return v
|
|
}
|
|
if (arg === '--output') out.output = next()
|
|
else if (arg === '--fixtures') out.fixtures = next()
|
|
else if (arg === '--expect') out.expect = next()
|
|
else if (arg === '--env') {
|
|
const kv = next()
|
|
const eq = kv.indexOf('=')
|
|
if (eq < 1) throw new Error(`--env expects KEY=VALUE, got ${kv}`)
|
|
out.env[kv.slice(0, eq)] = kv.slice(eq + 1)
|
|
} else throw new Error(`unknown argument ${arg}`)
|
|
}
|
|
if (!out.output || !out.fixtures || !out.expect) throw new Error('--output, --fixtures and --expect are required')
|
|
return /** @type {{ output: string, fixtures: string, expect: string, env: Record<string, string> }} */ (out)
|
|
}
|
|
|
|
/**
|
|
* @param {string} line
|
|
* @returns {{ status: number, path: string, contains?: string, notcontains?: string, type?: string }}
|
|
*/
|
|
export function parseExpectLine(line) {
|
|
const m = /^(\d{3})\s+(\S+)(.*)$/.exec(line.trim())
|
|
if (!m) throw new Error('expected "STATUS PATH [options]"')
|
|
/** @type {{ status: number, path: string, contains?: string, notcontains?: string, type?: string }} */
|
|
const check = { status: Number(m[1]), path: m[2] }
|
|
const rest = m[3]
|
|
const re = /\s(contains|notcontains|type)=/g
|
|
/** @type {Array<{ key: string, start: number, valueStart: number }>} */
|
|
const marks = []
|
|
let hit
|
|
while ((hit = re.exec(rest)) !== null) marks.push({ key: hit[1], start: hit.index, valueStart: re.lastIndex })
|
|
if (marks.length === 0 && rest.trim() !== '') throw new Error(`unknown option text "${rest.trim()}"`)
|
|
if (marks.length > 0 && rest.slice(0, marks[0].start).trim() !== '') {
|
|
throw new Error(`unknown option text "${rest.slice(0, marks[0].start).trim()}"`)
|
|
}
|
|
/** @type {Record<string, string>} */
|
|
const options = {}
|
|
marks.forEach((mark, i) => {
|
|
const end = i + 1 < marks.length ? marks[i + 1].start : rest.length
|
|
options[mark.key] = rest.slice(mark.valueStart, end).trim()
|
|
})
|
|
return { ...check, ...options }
|
|
}
|
|
|
|
async function main() {
|
|
const args = parseArgs(process.argv.slice(2))
|
|
const lines = readFileSync(resolve(args.expect), 'utf8')
|
|
.split('\n')
|
|
.map((l) => l.trim())
|
|
.filter((l) => l !== '' && !l.startsWith('#'))
|
|
|
|
const api = await startFixtureApi({ dir: resolve(args.fixtures) })
|
|
/** @type {{ url: string, close: () => Promise<void> } | null} */
|
|
let nuxt = null
|
|
const failures = /** @type {string[]} */ ([])
|
|
try {
|
|
nuxt = await startNuxtServer({ outputDir: args.output, apiBase: `${api.url}/_journal/api/v1`, env: args.env })
|
|
for (const line of lines) {
|
|
let check
|
|
try {
|
|
check = parseExpectLine(line)
|
|
} catch (err) {
|
|
failures.push(`${line}: ${/** @type {Error} */ (err).message}`)
|
|
continue
|
|
}
|
|
let res
|
|
try {
|
|
res = await fetch(nuxt.url + check.path, {
|
|
redirect: 'manual',
|
|
headers: { 'X-Forwarded-For': VISITOR_IP },
|
|
signal: AbortSignal.timeout(20000),
|
|
})
|
|
} catch (err) {
|
|
failures.push(`${line}: request failed: ${/** @type {Error} */ (err).message}`)
|
|
continue
|
|
}
|
|
const body = await res.text()
|
|
const type = res.headers.get('content-type') ?? ''
|
|
if (res.status !== check.status) failures.push(`${line}: status ${res.status}, want ${check.status}`)
|
|
else if (check.contains !== undefined && !body.includes(check.contains)) failures.push(`${line}: body lacks "${check.contains}"`)
|
|
else if (check.notcontains !== undefined && body.includes(check.notcontains)) failures.push(`${line}: body contains "${check.notcontains}"`)
|
|
else if (check.type !== undefined && !type.startsWith(check.type)) failures.push(`${line}: content-type "${type}", want prefix "${check.type}"`)
|
|
}
|
|
if (!api.requests.some((r) => (r.xff ?? '').includes(VISITOR_IP))) {
|
|
failures.push('x-forwarded-for: x-forwarded-for not forwarded')
|
|
}
|
|
// Categories are always requested with include_empty=1, so empty ones resolve.
|
|
for (const r of api.requests) {
|
|
const u = new URL(r.url, 'http://fixture.local')
|
|
if (u.pathname.endsWith('/categories') && u.searchParams.get('include_empty') !== '1') {
|
|
failures.push(`include_empty: ${r.url} requested without include_empty=1`)
|
|
}
|
|
}
|
|
} finally {
|
|
if (nuxt) await nuxt.close()
|
|
await api.close()
|
|
}
|
|
|
|
if (failures.length > 0) {
|
|
for (const f of failures) console.error(`smoke-ssr: FAIL ${f}`)
|
|
process.exit(1)
|
|
}
|
|
console.log(`smoke-ssr: ok (${lines.length} checks)`)
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(`smoke-ssr: FAIL setup: ${err instanceof Error ? err.message : String(err)}`)
|
|
process.exit(1)
|
|
})
|