import { describe, expect, it, vi } from 'vitest';
import { buildBrandVoiceAnalysisPrompt, fenceBrandVoiceSample, gatherBrandVoiceSamples, parseBrandVoiceAnalysisResponse, stripJsonFences, toBrandVoiceAnalyzeResult, } from '../../ai/brand-voice-analyze.js';
describe('fenceBrandVoiceSample', () => {
    it('wraps content in delimiters and clamps length', () => {
        const out = fenceBrandVoiceSample('Hello world', 5);
        expect(out).toContain('<<<SAMPLE');
        expect(out).toContain('Hello');
    });
});
describe('buildBrandVoiceAnalysisPrompt', () => {
    it('includes fenced samples and asks for JSON keys', () => {
        const { system, prompt } = buildBrandVoiceAnalysisPrompt([
            {
                collection: 'posts',
                documentId: 'd1',
                title: 'Launch post',
                text: 'We help teams ship faster with clear, practical guidance.',
            },
        ]);
        expect(system).toContain('name');
        expect(system).toContain('guidelines');
        expect(prompt).toContain('posts / Launch post');
        expect(prompt).toContain('<<<SAMPLE');
    });
});
describe('parseBrandVoiceAnalysisResponse', () => {
    it('parses valid JSON and normalizes fields', () => {
        const draft = parseBrandVoiceAnalysisResponse(JSON.stringify({
            name: 'Acme',
            tone: 'confident, warm',
            audience: 'B2B marketers',
            guidelines: '- Do: lead with benefits',
            sampleText: 'Ship with confidence.',
        }));
        expect(draft).toMatchObject({
            name: 'Acme',
            tone: 'confident, warm',
            audience: 'B2B marketers',
        });
    });
    it('strips markdown fences before parsing', () => {
        const draft = parseBrandVoiceAnalysisResponse('```json\n{"name":"X","tone":"plain","guidelines":"- Be clear"}\n```');
        expect(draft?.name).toBe('X');
        expect(draft?.tone).toBe('plain');
    });
    it('returns null when tone and guidelines are both empty', () => {
        expect(parseBrandVoiceAnalysisResponse('{"name":"X"}')).toBeNull();
        expect(parseBrandVoiceAnalysisResponse('not json')).toBeNull();
    });
});
describe('stripJsonFences', () => {
    it('removes code fences', () => {
        expect(stripJsonFences('```json\n{}\n```')).toBe('{}');
    });
});
describe('gatherBrandVoiceSamples', () => {
    it('loads published docs across collections up to maxSamples', async () => {
        const findMany = vi
            .fn()
            .mockResolvedValueOnce([
            {
                id: 'p1',
                collection: 'pages',
                title: 'About',
                plainText: 'We build tools for busy teams who need clarity and speed in every release cycle.',
                data: {},
            },
        ])
            .mockResolvedValueOnce([
            {
                id: 'b1',
                collection: 'posts',
                title: 'Hello',
                plainText: 'Our blog shares practical advice for product leaders who want measurable outcomes without hype.',
                data: {},
            },
        ]);
        const samples = await gatherBrandVoiceSamples({ document: { findMany } }, { collections: ['pages', 'posts'], maxSamples: 4 });
        expect(samples).toHaveLength(2);
        expect(samples[0]?.documentId).toBe('p1');
        expect(findMany).toHaveBeenCalledTimes(2);
    });
    it('skips very short documents', async () => {
        const findMany = vi.fn().mockResolvedValue([
            {
                id: 'p1',
                collection: 'pages',
                title: 'Tiny',
                plainText: 'Too short',
                data: {},
            },
        ]);
        const samples = await gatherBrandVoiceSamples({ document: { findMany } }, { collections: ['pages'] });
        expect(samples).toHaveLength(0);
    });
});
describe('toBrandVoiceAnalyzeResult', () => {
    it('maps samples to source metadata', () => {
        const result = toBrandVoiceAnalyzeResult({
            name: 'Acme',
            tone: 'warm',
            audience: 'teams',
            guidelines: '- Be clear',
            sampleText: 'Example copy.',
        }, [
            {
                collection: 'pages',
                documentId: 'p1',
                title: 'About',
                text: 'About us copy that is long enough to analyze properly for tone and style.',
            },
        ]);
        expect(result.sampleCount).toBe(1);
        expect(result.sources[0]).toMatchObject({ documentId: 'p1', title: 'About' });
    });
});
//# sourceMappingURL=brand-voice-analyze.test.js.map