import { describe, it, expect, beforeEach } from 'vitest';
import { validateTree } from '../validate.js';
// ─── Test Helpers ────────────────────────────────────────────────────
let idCounter = 0;
function uid() {
    return `node-${++idCounter}`;
}
function makeBlock(overrides) {
    return {
        id: uid(),
        type: 'block',
        settings: { blockType: 'text' },
        data: {},
        ...overrides,
    };
}
function makeColumn(width, children = [makeBlock()]) {
    return {
        id: uid(),
        type: 'column',
        settings: { width },
        children: children,
    };
}
function makeRow(columns) {
    return {
        id: uid(),
        type: 'row',
        settings: {},
        children: columns ?? [makeColumn(12)],
    };
}
function makeContainer(rows) {
    return {
        id: uid(),
        type: 'container',
        settings: {},
        children: rows ?? [makeRow()],
    };
}
function makeSection(children) {
    return {
        id: uid(),
        type: 'section',
        settings: {},
        children: children ?? [makeContainer()],
    };
}
function makeRef(refId = 'saved-1') {
    return {
        id: uid(),
        type: 'savedSectionRef',
        settings: { refId },
    };
}
function makePage(children) {
    return {
        id: uid(),
        type: 'page',
        children: children ?? [makeSection()],
    };
}
// ─── Tests ───────────────────────────────────────────────────────────
describe('validateTree', () => {
    beforeEach(() => {
        idCounter = 0;
    });
    it('accepts a valid minimal page', () => {
        const tree = makePage([
            makeSection([makeContainer([makeRow([makeColumn(12, [makeBlock()])])])]),
        ]);
        const result = validateTree(tree);
        expect(result.valid).toBe(true);
        expect(result.errors).toHaveLength(0);
    });
    it('accepts a page with savedSectionRef', () => {
        const tree = makePage([makeSection(), makeRef('ref-abc')]);
        const result = validateTree(tree);
        expect(result.valid).toBe(true);
        expect(result.errors).toHaveLength(0);
    });
    it('rejects a tree without page root', () => {
        const section = makeSection();
        const result = validateTree(section);
        expect(result.valid).toBe(false);
        expect(result.errors.some((e) => e.includes('Root node must be of type "page"'))).toBe(true);
    });
    it('rejects a node missing an id', () => {
        const tree = { type: 'page', children: [] };
        const result = validateTree(tree);
        expect(result.valid).toBe(false);
        expect(result.errors.some((e) => e.includes('id'))).toBe(true);
    });
    it('rejects columns that do not sum to 12', () => {
        const tree = makePage([makeSection([makeContainer([makeRow([makeColumn(7), makeColumn(4)])])])]);
        const result = validateTree(tree);
        expect(result.valid).toBe(false);
        expect(result.errors.some((e) => e.includes('sum to 11'))).toBe(true);
    });
    it('accepts columns that sum to 12', () => {
        const tree = makePage([makeSection([makeContainer([makeRow([makeColumn(7), makeColumn(5)])])])]);
        const result = validateTree(tree);
        expect(result.valid).toBe(true);
        expect(result.errors).toHaveLength(0);
    });
    it('rejects invalid nesting (block inside page)', () => {
        const tree = {
            id: 'page-1',
            type: 'page',
            children: [makeBlock()],
        };
        const result = validateTree(tree);
        expect(result.valid).toBe(false);
        expect(result.errors.length).toBeGreaterThan(0);
    });
    it('rejects column width outside 1-12 range', () => {
        const tree = makePage([
            makeSection([
                makeContainer([
                    makeRow([
                        {
                            id: 'col-zero',
                            type: 'column',
                            settings: { width: 0 },
                            children: [makeBlock()],
                        },
                        makeColumn(12),
                    ]),
                ]),
            ]),
        ]);
        const result = validateTree(tree);
        expect(result.valid).toBe(false);
        expect(result.errors.some((e) => e.toLowerCase().includes('number') ||
            e.toLowerCase().includes('small') ||
            e.toLowerCase().includes('width') ||
            e.toLowerCase().includes('1'))).toBe(true);
    });
    it('rejects nesting deeper than max depth', () => {
        function nestContainers(depth) {
            if (depth <= 1) {
                return makeContainer([makeRow([makeColumn(12, [makeBlock()])])]);
            }
            const inner = nestContainers(depth - 1);
            return makeContainer([makeRow([makeColumn(12, [inner])])]);
        }
        const deepContainer = nestContainers(10);
        const tree = makePage([makeSection([deepContainer])]);
        const result = validateTree(tree);
        expect(result.valid).toBe(false);
        expect(result.errors.some((e) => e.includes('depth'))).toBe(true);
    });
    it('accepts responsive overrides on nodes', () => {
        const tree = makePage([
            makeSection([
                makeContainer([
                    makeRow([
                        {
                            ...makeColumn(12),
                            responsive: {
                                mobile: { width: 12 },
                                tablet: { width: 6 },
                            },
                        },
                    ]),
                ]),
            ]),
        ]);
        const result = validateTree(tree);
        expect(result.valid).toBe(true);
        expect(result.errors).toHaveLength(0);
    });
    it('rejects block node with children', () => {
        const blockWithChildren = {
            id: 'bad-block',
            type: 'block',
            settings: { blockType: 'text' },
            data: {},
            children: [makeBlock()],
        };
        const tree = makePage([
            makeSection([
                makeContainer([makeRow([makeColumn(12, [blockWithChildren])])]),
            ]),
        ]);
        const result = validateTree(tree);
        expect(result.valid).toBe(false);
        expect(result.errors.some((e) => e.includes('Block node') && e.includes('children'))).toBe(true);
    });
});
//# sourceMappingURL=schema.test.js.map