import { GET } from '@/app/api/fetchAllProjects/route';
import { NextResponse } from 'next/server';

// Wir mocken node-fetch direkt
jest.mock('node-fetch', () => {
  return jest.fn(() =>
    Promise.resolve({
      json: () =>
        Promise.resolve({
          posts: [
            {
              id: '67ac8dfa709c60000117d312',
              title: 'Just Doing Some Testing',
              meta_description: 'Hello bla bla bla bla',
              slug: 'just-doing-some-testing',
              updated_at: '2025-02-13T14:25:38.000+00:00',
            },
            {
              id: '67aaffc3709c60000117d2d9',
              title: 'Blockchain Based Voting System',
              meta_description:
                'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
              slug: 'blockchain-based-voting-system',
              updated_at: '2025-02-13T16:54:42.000+00:00',
            },
          ],
          meta: {
            pagination: {
              limit: 'all',
              next: null,
              page: 1,
              pages: 1,
              prev: null,
              total: 2,
            },
          },
        }),
    })
  );
});

jest.mock('next/server', () => ({
  NextResponse: {
    json: jest.fn(),
  },
}));

describe('GET /api/fetchAllProjects', () => {
  beforeAll(() => {
    process.env.GHOST_API_URL = 'http://localhost:2368';
    process.env.GHOST_API_KEY = 'some-key';
  });

  it('should return a list of projects (partial match)', async () => {
    await GET();

    // Den tatsächlichen Argumentwert extrahieren
    const responseArg = (NextResponse.json as jest.Mock).mock.calls[0][0];

    expect(responseArg).toMatchObject({
      posts: expect.arrayContaining([
        expect.objectContaining({
          id: '67ac8dfa709c60000117d312',
          title: 'Just Doing Some Testing',
        }),
        expect.objectContaining({
          id: '67aaffc3709c60000117d2d9',
          title: 'Blockchain Based Voting System',
        }),
      ]),
    });
  });
});