/**
 * Tests for Site Definition Loader
 * Phase 5: Sites Renderer & Bulk Generation
 */
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { loadSiteDefinition } from '../loaders/loadSiteDefinition';

// Mock fetch
global.fetch = vi.fn();

describe('Site Definition Loader', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('loads site definitions from API', async () => {
    // Test: Sites renderer loads site definitions
    const mockSiteDefinition = {
      id: 1,
      name: 'Test Site',
      pages: [
        { id: 1, slug: 'home', title: 'Home' },
        { id: 2, slug: 'about', title: 'About' },
      ],
    };

    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({ data: mockSiteDefinition }),
    });

    const result = await loadSiteDefinition(1);

    expect(result).toBeDefined();
    expect(result.name).toBe('Test Site');
    expect(result.pages).toHaveLength(2);
    expect(global.fetch).toHaveBeenCalledWith(
      expect.stringContaining('/api/v1/site-builder/blueprints/1/')
    );
  });

  it('handles API errors gracefully', async () => {
    // Test: Sites renderer loads site definitions
    (global.fetch as any).mockRejectedValueOnce(new Error('API Error'));

    await expect(loadSiteDefinition(1)).rejects.toThrow('API Error');
  });
});

