// MITHOME-127: tests for scripts/check-indexability.sh against a local HTTP
// server that impersonates an indexable (production) or blocked (staging) site.
const { test, before, after } = require('node:test')
const assert = require('node:assert/strict')
const http = require('node:http')
const { spawn } = require('node:child_process')
const path = require('node:path')
const SCRIPT = path.join(__dirname, 'check-indexability.sh')
const SITES = {
indexable: {
'/robots.txt': 'User-Agent: *\nAllow: /\nDisallow: /admin\n\nSitemap: https://mozdit.hu/sitemap.xml\n',
'/sitemap.xml': 'https://mozdit.hu/huhttps://mozdit.hu/en',
'/hu': '
mozdITok',
},
blocked: {
'/robots.txt': 'User-Agent: *\nDisallow: /\n',
'/sitemap.xml': '',
'/hu': 'ok',
},
// production build that wrongly got NEXT_PUBLIC_DEPLOY_ENV=staging for the page only
leakyNoindex: {
'/robots.txt': 'User-Agent: *\nAllow: /\n\nSitemap: https://mozdit.hu/sitemap.xml\n',
'/sitemap.xml': 'https://mozdit.hu/hu',
'/hu': '',
},
}
let server
let origin
before(async () => {
server = http.createServer((req, res) => {
const [, site, ...rest] = req.url.split('/')
const body = SITES[site]?.['/' + rest.join('/')]
res.writeHead(body === undefined ? 404 : 200)
res.end(body ?? 'not found')
})
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
origin = `http://127.0.0.1:${server.address().port}`
})
after(() => server.close())
function check(site, expect) {
return new Promise((resolve) => {
const child = spawn('bash', [SCRIPT, `${origin}/${site}`, expect])
let out = ''
child.stdout.on('data', (d) => (out += d))
child.stderr.on('data', (d) => (out += d))
child.on('close', (code) => resolve({ code, out }))
})
}
test('accepts an indexable production site', async () => {
const { code, out } = await check('indexable', 'indexable')
assert.equal(code, 0, out)
assert.match(out, /sitemap: 2 URL/)
})
test('accepts a blocked staging site', async () => {
assert.equal((await check('blocked', 'blocked')).code, 0)
})
test('fails when production is blocked, listing every problem', async () => {
const { code, out } = await check('blocked', 'indexable')
assert.equal(code, 1)
assert.match(out, /mindent tilt/)
assert.match(out, /noindex/)
assert.match(out, /sitemap üres/)
})
test('fails on a stray noindex even if robots.txt looks fine', async () => {
const { code, out } = await check('leakyNoindex', 'indexable')
assert.equal(code, 1)
assert.match(out, /noindex/)
})
test('fails when staging became indexable', async () => {
const { code } = await check('indexable', 'blocked')
assert.equal(code, 1)
})
test('returns 2 for bad usage or an unreachable site', async () => {
assert.equal((await check('indexable', 'maybe')).code, 2)
const missing = await check('missing', 'indexable')
assert.equal(missing.code, 2, missing.out)
assert.match(missing.out, /robots\.txt nem érhető el/)
})