import { jsx as _jsx } from "react/jsx-runtime";
// @vitest-environment happy-dom
import { afterEach, describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
const fetchPageForEditor = vi.fn();
vi.mock('../../lib/page-editor-service.js', async (importOriginal) => {
    const actual = await importOriginal();
    return {
        ...actual,
        fetchPageForEditor: (...args) => fetchPageForEditor(...args),
    };
});
const { PageSectionEditor } = await import('../../views/page-editor/PageSectionEditor.js');
const validConfig = {
    collections: {
        pages: {
            slug: 'pages',
            fields: {
                title: { type: 'text' },
                slug: { type: 'slug' },
                path: { type: 'text' },
                sections: { type: 'json' },
                metaTitle: { type: 'text' },
                metaDescription: { type: 'text' },
            },
        },
    },
};
afterEach(() => {
    fetchPageForEditor.mockReset();
});
describe('PageSectionEditor schema guard', () => {
    it('refuses to operate when the pages collection is missing the sections field', async () => {
        const badConfig = {
            collections: { pages: { slug: 'pages', fields: { title: { type: 'text' } } } },
        };
        render(_jsx(PageSectionEditor, { documentId: "p1", config: badConfig, onNavigate: () => { }, session: {} }));
        expect(await screen.findByText(/missing required field/i)).toBeTruthy();
        expect(screen.getByText(/sections/)).toBeTruthy();
        // The guard must short-circuit before any network read.
        expect(fetchPageForEditor).not.toHaveBeenCalled();
    });
    it('does not block when no config is provided (embedding/test contexts)', async () => {
        render(_jsx(PageSectionEditor, { config: undefined, onNavigate: () => { }, session: {} }));
        // New page (no documentId) → empty editor shell renders.
        expect(await screen.findByText('Sections')).toBeTruthy();
    });
});
describe('PageSectionEditor new page', () => {
    it('renders the editor shell without fetching for /pages/new', async () => {
        render(_jsx(PageSectionEditor, { config: validConfig, onNavigate: () => { }, session: {} }));
        expect(await screen.findByText('Sections')).toBeTruthy();
        expect(fetchPageForEditor).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=page-section-editor.test.js.map