import { createServer } from 'node:http' const logo = `` const pages = [ { id: 1, path: 'docs', locale: 'en', title: 'Documentation', description: 'Documentation root', contentType: 'markdown', isPublished: true, updatedAt: '2026-01-02T10:00:00.000Z', render: `
Start with Install. The Linux details are in Linux requirements.
DOCS_MARKER
`, }, { id: 2, path: 'docs/install', locale: 'en', title: 'Installation', description: 'Install guide', contentType: 'markdown', isPublished: true, updatedAt: '2026-01-03T10:00:00.000Z', render: `Back to Documentation.
Choose Linux.
INSTALL_MARKER
`, }, { id: 3, path: 'docs/install/linux', locale: 'en', title: 'Linux requirements', description: 'Linux install', contentType: 'markdown', isPublished: true, updatedAt: '2026-01-04T10:00:00.000Z', render: `LINUX_MARKER
`, }, { id: 4, path: 'docs/other', locale: 'en', title: 'Other notes', description: 'Other notes', contentType: 'markdown', isPublished: true, updatedAt: '2026-01-05T10:00:00.000Z', render: `OTHER_MARKER
`, }, { id: 5, path: 'docs/fallback', locale: 'en', title: 'Fallback Markdown', description: 'Rendered from source', contentType: 'markdown', isPublished: true, updatedAt: '2026-01-06T10:00:00.000Z', render: null, content: `# Fallback Markdown This page is rendered from its markdown source. See [Installation](install). FALLBACK_MARKER `, }, { id: 6, path: 'docs/draft', locale: 'en', title: 'Draft page', description: 'Unpublished', contentType: 'markdown', isPublished: false, updatedAt: '2026-01-07T10:00:00.000Z', render: `DRAFT_MARKER
`, }, { id: 7, path: 'docs-other', locale: 'en', title: 'Excluded sibling', description: 'Not below docs', contentType: 'markdown', isPublished: true, updatedAt: '2026-01-08T10:00:00.000Z', render: `EXCLUDED_SIBLING_MARKER
`, }, { id: 8, path: 'guide', locale: 'en', title: 'Outside guide page', description: 'Outside the subtree', contentType: 'markdown', isPublished: true, updatedAt: '2026-01-09T10:00:00.000Z', render: `GUIDE_OUTSIDE_MARKER
`, }, ] function pageListItem(page) { return { id: page.id, path: page.path, locale: page.locale, title: page.title, description: page.description, contentType: page.contentType, isPublished: page.isPublished, updatedAt: page.updatedAt, } } function sendJson(response, status, payload) { const body = JSON.stringify(payload) response.writeHead(status, { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body), }) response.end(body) } export async function startMockWiki() { const state = { assetHits: 0, graphqlHits: 0 } const server = createServer((request, response) => { const url = new URL(request.url, 'http://127.0.0.1') if (url.pathname === '/assets/logo.svg') { state.assetHits += 1 response.writeHead(200, { 'Content-Type': 'image/svg+xml' }) response.end(logo) return } if (url.pathname !== '/graphql' || request.method !== 'POST') { response.writeHead(404, { 'Content-Type': 'text/plain' }) response.end('not found') return } if (request.headers.authorization !== 'Bearer test-key') { sendJson(response, 401, { error: 'unauthorized' }) return } state.graphqlHits += 1 let raw = '' request.setEncoding('utf8') request.on('data', (chunk) => { raw += chunk }) request.on('end', () => { let payload try { payload = JSON.parse(raw) } catch { sendJson(response, 400, { error: 'bad json' }) return } const query = payload.query || '' const variables = payload.variables || {} if (query.includes('WikiPrintLocalization')) { sendJson(response, 200, { data: { localization: { config: { locale: 'en', namespacing: true, namespaces: ['en'] }, }, }, }) return } if (query.includes('WikiPrintPages')) { sendJson(response, 200, { data: { pages: { list: pages.map(pageListItem) } }, }) return } if (query.includes('WikiPrintPageSource')) { const page = pages.find((candidate) => candidate.id === variables.id) sendJson(response, 200, { data: { pages: { single: page ? { content: page.content || '', contentType: page.contentType } : null, }, }, }) return } if (query.includes('WikiPrintPage')) { const page = pages.find( (candidate) => candidate.path === variables.path && candidate.locale === variables.locale, ) sendJson(response, 200, { data: { pages: { singleByPath: page ? { id: page.id, path: page.path, locale: page.locale, title: page.title, description: page.description, contentType: page.contentType, render: page.render, createdAt: page.updatedAt, updatedAt: page.updatedAt, } : null, }, }, }) return } sendJson(response, 400, { errors: [{ message: 'Unknown mock query' }] }) }) }) await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) const address = server.address() return { url: `http://127.0.0.1:${address.port}`, state, pages, close: () => new Promise((resolve) => server.close(resolve)), } }