import { expect, test, type APIRequestContext } from '@playwright/test' /** * MITHOME-127 — production launch SEO checks (run at go-live and after every * production release): `BASE_URL=https://mozdit.hu npm run test:seo:production`. * * The site must be indexable, every sitemap URL must be final (200, no * redirect), canonical/hreflang must point at those final URLs, and content + * internal links must be in the server-rendered HTML (no JS needed). * Host-level redirects (http→https, www→apex) are nginx's job (MITHOME-15): * set SEO_CHECK_HOST_REDIRECTS=1 once the production domain is live. */ const text = async (request: APIRequestContext, path: string) => { const response = await request.get(path, { maxRedirects: 0 }) expect(response.status(), `${path} status`).toBe(200) return response.text() } const locs = (xml: string) => [...xml.matchAll(/([^<]+)<\/loc>/g)].map((m) => m[1]) const attr = (html: string, pattern: RegExp) => html.match(pattern)?.[1] test('robots.txt allows crawling and points to the sitemap', async ({ request, baseURL }) => { const robots = await text(request, '/robots.txt') expect(robots).not.toMatch(/^Disallow:\s*\/\s*$/im) expect(robots).toContain(`Sitemap: ${baseURL}/sitemap.xml`) }) test('every sitemap URL is final, indexable and correctly annotated', async ({ request, baseURL }) => { const urls = locs(await text(request, '/sitemap.xml')) expect(urls).toHaveLength(12) for (const url of urls) { expect(url.startsWith(`${baseURL}/`), `${url} is on ${baseURL}`).toBe(true) const html = await text(request, url) expect(html, `${url} has no noindex`).not.toMatch(/]*name="robots"[^>]*noindex/i) expect(attr(html, / { const html = await text(request, '/hu') expect(html).toMatch(/]*>[^<]+/) for (const path of ['/hu/rolunk', '/hu/szolgaltatasok', '/hu/kapcsolat', '/en']) { expect(html, `link to ${path}`).toContain(`href="${path}"`) } }) test('the site root redirects to /hu in one step', async ({ request }) => { const response = await request.get('/', { maxRedirects: 0 }) expect([301, 302, 307, 308]).toContain(response.status()) expect(new URL(response.headers()['location'], 'http://x').pathname).toBe('/hu') }) test('unknown pages return a real 404', async ({ request }) => { const response = await request.get('/hu/nincs-ilyen-oldal', { maxRedirects: 0 }) expect(response.status()).toBe(404) }) test('host variants redirect to the canonical origin in one step', async ({ request, baseURL }) => { test.skip(!process.env.SEO_CHECK_HOST_REDIRECTS, 'nginx host redirects are checked once the production domain is live (MITHOME-15)') const canonical = new URL(baseURL!) const variants = [`http://${canonical.host}/hu`, `https://www.${canonical.host}/hu`] for (const variant of variants) { const response = await request.get(variant, { maxRedirects: 0 }) expect([301, 308], `${variant} status`).toContain(response.status()) expect(response.headers()['location'], `${variant} target`).toBe(`${canonical.origin}/hu`) } })