/**
* ADR-190 Zone A — blogSeo SSR-lite render + escaping proof.
*
* Covers the security-critical surface (AC-A3/A4/A5 escaping) and the pure
* render outputs (AC-A6/A7/A8). The DB helper is mocked so this stays a fast,
* hermetic unit test — no combat-master access (ADR-0009).
*/
import { describe, it, expect, vi } from 'vitest';
// Avoid pulling the public.js → db graph; the render fns don't use it.
vi.mock('../../routes/v3/public.js', () => ({ fetchPublicRows: vi.fn() }));
const {
esc, safeUrl, jsonLd, renderMarkdown,
renderPostPage, renderIndexPage, renderSitemap, renderRobots,
} = await import('../blogSeo.js');
const TEMPLATE = [
'
',
'GOD CRM — the self-configuring CRM',
'',
'',
'',
'',
'',
'',
'',
'',
].join('\n');
const POST = {
slug: 'the-factory-floor',
title: 'The Factory Floor',
excerpt: 'How we rebuilt the plant.',
body: '# Heading\n\nSome **bold** text with a [link](https://example.com).',
date: '2026-08-10T12:00:00Z',
tag: 'ops',
cover: 'https://cdn.example.com/cover.png',
ctaUrl: 'https://example.com/learn',
url: 'https://app.godcrm.ai/blog/the-factory-floor',
};
describe('escaping helpers', () => {
it('esc neutralises HTML metacharacters', () => {
expect(esc('' });
expect(out).not.toContain('');
expect(out).toContain('\\u003c/script');
});
});
describe('renderMarkdown linkify scheme', () => {
it('schemeless hosts autolink to https, not http', () => {
expect(renderMarkdown('see godcrm.ai/s/help/mcp now'))
.toContain('href="https://godcrm.ai/s/help/mcp"');
expect(renderMarkdown('see www.godcrm.ai/x')).toContain('href="https://www.godcrm.ai/x"');
expect(renderMarkdown('see godcrm.ai/s/help/mcp')).not.toContain('http://');
});
it('an explicitly authored scheme is preserved', () => {
expect(renderMarkdown('http://legacy.example.com/a')).toContain('href="http://legacy.example.com/a"');
expect(renderMarkdown('[x](http://legacy.example.com/a)')).toContain('href="http://legacy.example.com/a"');
});
it('mailto autolinks are untouched', () => {
expect(renderMarkdown('write to hi@godcrm.ai')).toContain('href="mailto:hi@godcrm.ai"');
});
});
describe('renderPostPage', () => {
const html = renderPostPage(TEMPLATE, POST);
it('AC-A3: per-post title replaces the generic one (only one )', () => {
expect(html).toContain('The Factory Floor — GOD CRM Blog');
expect(html).not.toContain('self-configuring CRM');
expect((html.match(//g) || []).length).toBe(1);
});
it('AC-A3: canonical + OG point at the post, not the root', () => {
expect(html).toContain('');
expect(html).toContain('');
expect(html).toContain('');
expect(html).not.toContain('content="https://godcrm.ai/"');
expect(html).toContain('');
expect(html).toContain('');
});
it('AC-A4: valid JSON-LD BlogPosting is injected', () => {
const m = html.match(/\n\nok',
cover: 'javascript:alert(1)',
});
expect(evil).not.toContain('');
expect(evil).not.toContain('');
expect(evil).toContain('<script>');
// javascript: cover dropped → no og:image emitted
expect(evil).not.toContain('og:image');
});
});
describe('renderIndexPage (AC-A6)', () => {
const html = renderIndexPage(TEMPLATE, [POST]);
it('emits ItemList JSON-LD + linked titles', () => {
expect(html).toContain('"@type":"ItemList"');
expect(html).toContain('href="https://app.godcrm.ai/blog/the-factory-floor"');
expect(html).toContain('Blog — GOD CRM');
});
});
describe('renderSitemap (AC-A7)', () => {
const xml = renderSitemap([POST]);
it('lists the index + each post with lastmod', () => {
expect(xml.startsWith('')).toBe(true);
expect(xml).toContain('https://app.godcrm.ai/blog');
expect(xml).toContain('https://app.godcrm.ai/blog/the-factory-floor');
expect(xml).toContain('2026-08-10');
});
});
describe('renderRobots (AC-A8)', () => {
it('allows all + advertises the sitemap', () => {
expect(renderRobots()).toBe(
'User-agent: *\nAllow: /\nSitemap: https://app.godcrm.ai/sitemap.xml\n'
);
});
});