import { NextResponse } from 'next/server';

jest.mock('@/lib/prisma', () => ({
  prisma: {
    project: {
      findMany: jest.fn(async () => [
        {
          id: 1,
          slug: 'just-doing-some-testing',
          title: 'Just Doing Some Testing',
          updatedAt: new Date('2025-02-13T14:25:38.000Z'),
          metaDescription: 'Hello bla bla bla bla',
        },
        {
          id: 2,
          slug: 'blockchain-based-voting-system',
          title: 'Blockchain Based Voting System',
          updatedAt: new Date('2025-02-13T16:54:42.000Z'),
          metaDescription:
            'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
        },
      ]),
    },
  },
}));

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

describe('GET /api/fetchAllProjects', () => {
  it('should return a list of projects (partial match)', async () => {
    const { GET } = await import('@/app/api/fetchAllProjects/route');
    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: '1',
          title: 'Just Doing Some Testing',
        }),
        expect.objectContaining({
          id: '2',
          title: 'Blockchain Based Voting System',
        }),
      ]),
    });
  });
});