import { jsx as _jsx } from "react/jsx-runtime";
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
/**
 * Focused SeoEditorPane behaviours:
 *   - The header labels the two numbers unambiguously ("SEO score" vs "Content
 *     analysis N/100") instead of the ambiguous "· Live analysis N".
 *   - Generating an OG title/description writes into the OG field itself and
 *     never overwrites the (search) meta title/description — a previously
 *     destructive, undo-less action.
 */
const RECORD = {
    id: 'seo1',
    entityType: 'page',
    entityId: 'p1',
    title: 'Home',
    url: 'https://example.com/',
    canonicalUrl: null,
    metaTitle: 'Home — Example',
    metaTitlePage: 'Home',
    siteName: 'Example',
    titleTemplate: '%page% — %site%',
    metaDescription: 'Welcome to Example.',
    focusKeyword: null,
    keyTakeaway: null,
    ogTitle: null,
    ogDescription: null,
    ogImageId: null,
    ogImage: null,
    twitterTitle: null,
    twitterDescription: null,
    twitterImageId: null,
    noindex: false,
    nofollow: false,
    structuredDataType: 'WebPage',
    structuredData: null,
    llmsInclude: 'AUTO',
    llmsDescription: null,
    llmsSection: null,
    llmsOptional: false,
    seoScore: 82,
    seoGrade: 'good',
    updatedAt: '2026-06-01T00:00:00Z',
};
const fetchSeoContentRecord = vi.fn(async () => RECORD);
const generateSeoField = vi.fn(async (_field, _ctx) => ({
    text: 'Generated OG Title',
}));
vi.mock('@actuate-media/cms-core/client', async (importOriginal) => {
    const actual = await importOriginal();
    return { ...actual };
});
vi.mock('../../lib/seo-service.js', () => ({
    fetchSeoContentRecord: () => fetchSeoContentRecord(),
    fetchDocumentSeoAnalysis: vi.fn(async () => ({ score: 72, checks: [] })),
    updateSeoFields: vi.fn(async () => ({})),
    generateSeoField: (field, ctx) => generateSeoField(field, ctx),
    fetchLinkSuggestions: vi.fn(async () => ({ data: { suggestions: [] } })),
    SEO_SCHEMA_TYPES: ['Article', 'WebPage'],
}));
vi.mock('../../lib/brand-voice-client.js', () => ({
    fixBrandVoiceContent: vi.fn(async () => ({ ok: false })),
    brandAlignmentTone: () => 'text-success',
    brandAlignmentLabel: () => 'On brand',
}));
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn(), loading: vi.fn() } }));
const { SeoEditorPane } = await import('../../components/seo/SeoEditorPane.js');
beforeEach(() => {
    fetchSeoContentRecord.mockClear();
    generateSeoField.mockClear();
});
describe('SeoEditorPane header', () => {
    it('labels the analysis number as "Content analysis N/100", not "Live analysis"', async () => {
        render(_jsx(SeoEditorPane, { entityType: "page", entityId: "p1", onClose: () => { } }));
        // Score header renders once the record loads.
        expect(await screen.findByText(/SEO score:/)).toBeTruthy();
        await waitFor(() => expect(screen.getByText(/Content analysis 72\/100/)).toBeTruthy());
        expect(screen.queryByText(/Live analysis/)).toBeNull();
    });
});
describe('SeoEditorPane OG generation', () => {
    it('writes an AI OG title into the OG field and leaves the meta title intact', async () => {
        render(_jsx(SeoEditorPane, { entityType: "page", entityId: "p1", onClose: () => { } }));
        // Wait for the record → the SEO (meta) title field shows the page portion.
        expect(await screen.findByDisplayValue('Home')).toBeTruthy();
        // Expand the collapsed "Social appearance" accordion to reveal OG fields.
        fireEvent.click(screen.getByRole('button', { name: /Social appearance/ }));
        // Click the AI Generate button that belongs to the "OG title" field.
        const ogLabel = await screen.findByText('OG title');
        const ogFieldHeader = ogLabel.closest('div');
        fireEvent.click(within(ogFieldHeader).getByRole('button', { name: /AI Generate/ }));
        await waitFor(() => expect(generateSeoField).toHaveBeenCalledWith('ogTitle', expect.anything()));
        // The suggestion lands in the OG field...
        await waitFor(() => expect(screen.getByDisplayValue('Generated OG Title')).toBeTruthy());
        // ...and the search meta title is untouched (was NOT overwritten/cleared).
        expect(screen.getByDisplayValue('Home')).toBeTruthy();
    });
});
//# sourceMappingURL=seo-editor-pane.render.test.js.map