import { describe, expect, it, vi } from 'vitest';
import { homepageContent } from '$lib/content/homepage';

const { getHomepageContent, isHomepageHowItWorksEnabled } = vi.hoisted(() => ({
  getHomepageContent: vi.fn(),
  isHomepageHowItWorksEnabled: vi.fn()
}));

vi.mock('$lib/server/content', () => ({
  getHomepageContent
}));

vi.mock('$lib/server/feature-flags', () => ({
  isHomepageHowItWorksEnabled
}));

import { load } from './+page.server';

function createLoadEvent(url = 'https://www.goodwalk.co.nz/') {
  return {
    url: new URL(url)
  } as Parameters<typeof load>[0];
}

describe('home page server load', () => {
  it('returns homepage content', async () => {
    getHomepageContent.mockResolvedValue(homepageContent);
    isHomepageHowItWorksEnabled.mockReturnValue(false);

    await expect(load(createLoadEvent())).resolves.toEqual({
      siteVariant: 'marketing',
      content: homepageContent,
      howItWorksEnabled: false
    });
  });

  it('returns the how it works flag when enabled', async () => {
    getHomepageContent.mockResolvedValue(homepageContent);
    isHomepageHowItWorksEnabled.mockReturnValue(true);

    await expect(load(createLoadEvent())).resolves.toEqual({
      siteVariant: 'marketing',
      content: homepageContent,
      howItWorksEnabled: true
    });
  });
});
