// src/routes/price.routes.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import supertest from 'supertest';
import priceRouter from './price.routes';
import { createTestApp } from '../tests/utils/createTestApp';

// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
  logger: {
    info: vi.fn(),
    error: vi.fn(),
  },
}));

describe('Price Routes (/api/price-history)', () => {
  const app = createTestApp({ router: priceRouter, basePath: '/api/price-history' });
  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe('POST /', () => {
    it('should return 200 OK with an empty array for a valid request', async () => {
      const response = await supertest(app)
        .post('/api/price-history')
        .send({ masterItemIds: [1, 2, 3] });

      expect(response.status).toBe(200);
      expect(response.body).toEqual([]);
    });

    it('should return 400 if masterItemIds is not an array', async () => {
      const response = await supertest(app)
        .post('/api/price-history')
        .send({ masterItemIds: 'not-an-array' });
      expect(response.status).toBe(400);
      expect(response.body.errors[0].message).toMatch(/Expected array, received string/i);
    });

    it('should return 400 if masterItemIds is an empty array', async () => {
      const response = await supertest(app)
        .post('/api/price-history')
        .send({ masterItemIds: [] });
      expect(response.status).toBe(400);
      expect(response.body.errors[0].message).toBe('masterItemIds must be a non-empty array of positive integers.');
    });
  });
});