import { afterEach, describe, expect, it, vi } from 'vitest';
import { getActuateConfig, setActuateConfig } from '../../config/runtime.js';
/**
 * Regression tests for the boot-time `fields`-shape guard.
 *
 * cms-core reads `collection.fields` as an object map keyed by field name. An
 * array silently drops every custom field on write, so `setActuateConfig` warns
 * (loudly, once) when it sees one — see `warnOnArrayFields`. These tests pin
 * that behaviour: warn on arrays, stay quiet on the correct object-map shape,
 * and never throw on malformed input.
 */
describe('setActuateConfig — fields-shape guard', () => {
    afterEach(() => {
        vi.restoreAllMocks();
        delete globalThis.__actuateConfig;
    });
    it('warns when a collection declares `fields` as an array', () => {
        const warn = vi.spyOn(console, 'warn').mockImplementation(() => { });
        setActuateConfig({
            collections: {
                posts: {
                    slug: 'posts',
                    fields: [{ name: 'title', type: 'text' }],
                },
            },
        });
        expect(warn).toHaveBeenCalledTimes(1);
        const message = String(warn.mock.calls[0]?.[0] ?? '');
        expect(message).toContain('posts');
        expect(message).toContain('array');
        // The config is still registered — the guard never blocks boot.
        expect(getActuateConfig()?.collections?.posts).toBeDefined();
    });
    it('does not warn for the correct object-map shape', () => {
        const warn = vi.spyOn(console, 'warn').mockImplementation(() => { });
        setActuateConfig({
            collections: {
                pages: {
                    slug: 'pages',
                    fields: { title: { type: 'text' }, slug: { type: 'slug' } },
                },
            },
        });
        expect(warn).not.toHaveBeenCalled();
    });
    it('warns once per offending collection and ignores valid ones', () => {
        const warn = vi.spyOn(console, 'warn').mockImplementation(() => { });
        setActuateConfig({
            collections: {
                good: { slug: 'good', fields: { a: { type: 'text' } } },
                bad1: { slug: 'bad1', fields: [] },
                bad2: { slug: 'bad2', fields: [{ name: 'x', type: 'text' }] },
            },
        });
        expect(warn).toHaveBeenCalledTimes(2);
    });
    it('does not throw on malformed config', () => {
        const warn = vi.spyOn(console, 'warn').mockImplementation(() => { });
        expect(() => setActuateConfig({})).not.toThrow();
        expect(() => setActuateConfig({ collections: null })).not.toThrow();
        expect(() => setActuateConfig({ collections: 'nope' })).not.toThrow();
        expect(warn).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=runtime.test.js.map