/** * ADR-190-A — docs-hub SSR-lite: guards, render output, escaping, sitemap. * * AC-4 is the test that matters: a password-protected or non-external space is * force-noindex and force-excluded from the sitemap EVEN WITH indexable:true. * AC-9 re-asserts the ADR-190 escaping contract on this new surface. * * Both the DB and the public loaders are mocked — hermetic, no master (ADR-0009). */ import { describe, it, expect, vi, beforeEach } from 'vitest'; const getPublicSpaceBySlug = vi.fn(); const listIndexableSpaces = vi.fn(); const loadPublicDocumentsRegistry = vi.fn(); const loadPublicDocument = vi.fn(); // Keep the real readPublicSeoPrefs — the guard logic under test depends on it. vi.mock('../SpaceVisibilityService.js', async (importOriginal) => { const actual = await importOriginal(); return { ...actual, getPublicSpaceBySlug, listIndexableSpaces }; }); // blogSeo pulls fetchPublicRows from public.js; stub the whole module so the // DB graph never loads (same trick as blogSeo.test.js). vi.mock('../../routes/v3/public.js', () => ({ fetchPublicRows: vi.fn(), loadPublicDocumentsRegistry, loadPublicDocument, })); vi.mock('../../database/connection.js', () => ({ dbGet: vi.fn(), dbRun: vi.fn(), dbAll: vi.fn(), })); const mod = await import('../publicDocsSeo.js'); const { renderDocPage, renderHubPage, renderDocBody, collectDocsSitemapUrls, registerPublicDocsSeoRoutes, } = mod; const SEO = { indexable: true, in_sitemap: true, site_origin: 'https://godcrm.ai', site_name: 'GOD CRM', title_template: '%s — GOD CRM Docs', default_description: 'GOD CRM documentation — spaces, tables, agents, modules and widgets.', og_image: '/og/docs.png', default_lang: 'en', }; const DOC = { id: 46043, slug: '01-what-is-god-crm', name: 'What is GOD CRM?', description: 'A CRM you describe in plain language instead of configuring.', status: 'published', seo_title: '', seo_description: '', seo_noindex: false, created_at: '2026-06-01T10:00:00Z', updated_at: '2026-06-22T10:55:38Z', }; const ITEMS = [ { level: 'text', content_en: 'GOD CRM is a **universal** system.', content_ru: 'Это система.' }, { level: 'h2', content_en: 'The One-Sentence Answer', content_ru: 'Ответ' }, { level: 'divider', content_en: '', content_ru: '' }, { level: 'text', content_en: '', content_ru: 'Только по-русски.' }, ]; const TEMPLATE = [ '', 'GOD CRM — the self-configuring CRM', '', '', '
', ].join('\n'); const ctx = (over = {}) => ({ space: { id: 7773, name: 'GOD CRM Public', public_slug: 'help' }, seo: SEO, origin: 'https://godcrm.ai', slug: 'help', doc: DOC, items: ITEMS, ...over, }); const parseLd = (html) => [...html.matchAll(/', description: '', }, seo: { ...SEO, og_image: 'javascript:alert(1)' }, items: [{ level: 'text', content_en: '\n\nok' }, { level: 'h2', content_en: '' }], })); it('neutralises authored HTML in head, body and JSON-LD', () => { expect(evil).not.toContain(''); expect(evil).not.toContain(''); expect(evil).not.toContain(''); expect(evil).not.toContain(''); expect(evil).toContain('<script>'); // Only the two ld+json blocks this module emits — no injected third. expect((evil.match(/'); }); }); describe('collectDocsSitemapUrls (AC-6)', () => { it('lists the hub + every published, listable doc with lastmod', async () => { listIndexableSpaces.mockResolvedValue([ { id: 7773, name: 'GOD CRM Public', public_slug: 'help', public_seo: SEO }, ]); loadPublicDocumentsRegistry.mockResolvedValue({ documents: [ DOC, { ...DOC, slug: 'draft-one', status: 'draft' }, { ...DOC, slug: 'hidden-one', seo_noindex: true }, { ...DOC, slug: '02-four-layers', updated_at: '2026-07-01T00:00:00Z' }, ], }); const urls = await collectDocsSitemapUrls(); expect(urls.map((u) => u.loc)).toEqual([ 'https://godcrm.ai/s/help', 'https://godcrm.ai/s/help/docs/01-what-is-god-crm', 'https://godcrm.ai/s/help/docs/02-four-layers', ]); expect(urls[0].lastmod).toBe('2026-07-01T00:00:00Z'); // hub = newest chapter }); it('skips a space that opted out of the sitemap but stays indexable', async () => { listIndexableSpaces.mockResolvedValue([ { id: 1, name: 'X', public_slug: 'x', public_seo: { ...SEO, in_sitemap: false } }, ]); expect(await collectDocsSitemapUrls()).toEqual([]); expect(loadPublicDocumentsRegistry).not.toHaveBeenCalled(); }); it('never throws — a failing registry contributes nothing (AC-8 safety)', async () => { listIndexableSpaces.mockRejectedValue(new Error('db down')); expect(await collectDocsSitemapUrls()).toEqual([]); }); }); describe('route guards (AC-4) — the test that matters', () => { /** Minimal Express double: records handlers, drives one through req/res/next. */ function mount() { const routes = {}; const app = { get: (path, handler) => { routes[path] = handler; } }; registerPublicDocsSeoRoutes(app, { clientDistPath: '/nonexistent' }); return { routes, call: async (path, params) => { const res = { _status: 200, _body: null, _headers: {}, setHeader(k, v) { this._headers[k] = v; }, status(s) { this._status = s; return this; }, type() { return this; }, send(b) { this._body = b; return this; }, }; let nexted = false; await routes[path]({ params }, res, () => { nexted = true; }); return { res, nexted }; }, }; } it('registers exactly the three shapes and never /s/:slug/mcp', () => { expect(Object.keys(mount().routes).sort()) .toEqual(['/s/:slug', '/s/:slug/docs', '/s/:slug/docs/:docSlug']); }); it('password-protected space → falls through, even with indexable:true', async () => { getPublicSpaceBySlug.mockResolvedValue({ id: 7773, name: 'X', public_slug: 'help', has_password: true, settings: JSON.stringify({ public_seo: { indexable: true, in_sitemap: true } }), }); const { nexted } = await mount().call('/s/:slug/docs/:docSlug', { slug: 'help', docSlug: 'a' }); expect(nexted).toBe(true); expect(loadPublicDocumentsRegistry).not.toHaveBeenCalled(); }); it('non-external space → falls through (the SQL never returns it)', async () => { getPublicSpaceBySlug.mockResolvedValue(null); expect((await mount().call('/s/:slug', { slug: 'internal-space' })).nexted).toBe(true); }); it('external + no password but not opted in → falls through', async () => { getPublicSpaceBySlug.mockResolvedValue({ id: 7773, name: 'X', public_slug: 'help', has_password: false, settings: null, }); expect((await mount().call('/s/:slug', { slug: 'help' })).nexted).toBe(true); }); it('an unpublished doc inside an indexed space is never rendered', async () => { getPublicSpaceBySlug.mockResolvedValue({ id: 7773, name: 'X', public_slug: 'help', has_password: false, settings: JSON.stringify({ public_seo: { indexable: true } }), }); loadPublicDocumentsRegistry.mockResolvedValue({ documents: [{ ...DOC, slug: 'secret-draft', status: 'draft' }], }); // Template read fails on the fake dist path → handler must still not leak // the draft; it either 404s the shell or falls through, never renders it. const { res, nexted } = await mount() .call('/s/:slug/docs/:docSlug', { slug: 'help', docSlug: 'secret-draft' }); expect(nexted || res._status === 404).toBe(true); expect(String(res._body ?? '')).not.toContain('secret-draft'); expect(loadPublicDocument).not.toHaveBeenCalled(); }); });