const { getRequest } = require('../testServer');
const path = require('path');

describe('Upload API', () => {
  describe('POST /api/upload', () => {
    it('should reject upload without files', async () => {
      const response = await getRequest()
        .post('/api/upload')
        .field('groupName', 'TestGroup')
        .expect('Content-Type', /json/)
        .expect(400);

      expect(response.body).toHaveProperty('error');
      expect(response.body.error).toMatch(/datei|file/i);
    });

    it('should accept upload with file and groupName', async () => {
      // Create a simple test buffer (1x1 transparent PNG)
      const testImageBuffer = Buffer.from(
        'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
        'base64'
      );

      const response = await getRequest()
        .post('/api/upload')
        .attach('file', testImageBuffer, 'test.png')
        .field('groupName', 'TestGroup');

      // Log error for debugging
      if (response.status !== 200) {
        console.log('Upload failed:', response.body);
      }

      expect(response.status).toBe(200);
      expect(response.body).toHaveProperty('filePath');
      expect(response.body).toHaveProperty('fileName');
      expect(response.body).toHaveProperty('groupId');
      expect(response.body).toHaveProperty('groupName', 'TestGroup');
    });

    it('should use default group name if not provided', async () => {
      const testImageBuffer = Buffer.from(
        'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
        'base64'
      );

      const response = await getRequest()
        .post('/api/upload')
        .attach('file', testImageBuffer, 'test.png')
        .expect('Content-Type', /json/)
        .expect(200);

      expect(response.body).toHaveProperty('groupName');
      // Should use default: 'Unnamed Group'
      expect(response.body.groupName).toBeTruthy();
    });
  });
});
