import { GET } from '@/app/api/grpc-test/route';
import { pingBot } from '@/lib/grpc';

// Mock the pingBot function
jest.mock('@/lib/grpc', () => ({
  pingBot: jest.fn(),
}));

describe('GET /api/grpc-test', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('should return 200 and mocked reply on success', async () => {
    (pingBot as jest.Mock).mockResolvedValue({ reply: 'Mocked Reply from Bot' });

    const request = new Request('http://localhost:3000/api/grpc-test?msg=Hello');
    const response = await GET(request);

    expect(response.status).toBe(200);
    const json = await response.json();
    expect(json.success).toBe(true);
    expect(json.reply).toBe('Mocked Reply from Bot');
    expect(pingBot).toHaveBeenCalledWith('Hello');
  });

  it('should return 500 on gRPC failure', async () => {
    (pingBot as jest.Mock).mockRejectedValue(new Error('gRPC connection failed'));

    const request = new Request('http://localhost:3000/api/grpc-test');
    const response = await GET(request);

    expect(response.status).toBe(500);
    const json = await response.json();
    expect(json.success).toBe(false);
    expect(json.error).toBe('gRPC connection failed');
  });
});
