import assert from 'node:assert/strict'; import test from 'node:test'; import express from 'express'; import { attachPortalSeoDiscoveryRoutes } from './portal-seo-discovery-routes.mjs'; function createApp(deps) { const app = express(); attachPortalSeoDiscoveryRoutes(app, deps); return app; } async function getText(app, path) { const server = app.listen(0); try { const { port } = server.address(); const response = await fetch(`http://127.0.0.1:${port}${path}`); const body = await response.text(); return { status: response.status, body }; } finally { await new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); } } test('GET /robots.txt returns closed policy when disabled', async () => { const app = createApp({ getMindSpaceConfig: async () => ({ seoGeo: { enabled: false } }), }); const result = await getText(app, '/robots.txt'); assert.equal(result.status, 200); assert.match(result.body, /Disallow: \//); }); test('GET /sitemap.xml returns 404 when sitemap disabled', async () => { const app = createApp({ getMindSpaceConfig: async () => ({ seoGeo: { enabled: true, seo: { enabled: true, sitemap: false } }, }), }); const result = await getText(app, '/sitemap.xml'); assert.equal(result.status, 404); }); test('GET /sitemap.xml renders xml when enabled', async () => { const app = createApp({ getAuthPool: () => ({}), getMindSpaceConfig: async () => ({ seoGeo: { enabled: true, seo: { enabled: true, sitemap: true } }, }), getSeoDiscoveryService: () => ({ listIndexablePublications: async () => [ { publicUrl: '/u/john/pages/demo', updatedAt: Date.now() }, ], renderSitemapXml: (entries, ctx) => `${entries.length}`, }), resolveRequestOrigin: () => 'https://m.tkmind.cn', }); const result = await getText(app, '/sitemap.xml'); assert.equal(result.status, 200); assert.match(result.body, /origin="https:\/\/m\.tkmind\.cn">1<\/urlset>/); });