/**
 * Container Log Route Tests
 *
 * Tests Docker container log listing and retrieval endpoints
 * Note: These tests run against the real Docker socket if available,
 * or will gracefully handle Docker being unavailable in CI.
 */

const request = require('supertest');
const fs = require('fs');
const path = require('path');
const os = require('os');

const testServicesFile = path.join(os.tmpdir(), `logs-services-${Date.now()}.json`);
const testConfigFile = path.join(os.tmpdir(), `logs-config-${Date.now()}.json`);

process.env.SERVICES_FILE = testServicesFile;
process.env.CONFIG_FILE = testConfigFile;
process.env.ENABLE_HEALTH_CHECKER = 'false';
process.env.NODE_ENV = 'test';

fs.writeFileSync(testServicesFile, '[]', 'utf8');
fs.writeFileSync(testConfigFile, '{}', 'utf8');

const app = require('../server');

describe('Container Log Routes', () => {
  afterAll(() => {
    try { fs.unlinkSync(testServicesFile); } catch (e) { /* ignore */ }
    try { fs.unlinkSync(testConfigFile); } catch (e) { /* ignore */ }
  });

  describe('GET /api/logs/containers', () => {
    test('should return 200 with containers array', async () => {
      const res = await request(app).get('/api/logs/containers');

      expect(res.statusCode).toBe(200);
      expect(res.body.success).toBe(true);
      expect(Array.isArray(res.body.containers)).toBe(true);
    });
  });

  describe('GET /api/logs/container/:id', () => {
    test('should return 404 or 500 for nonexistent container', async () => {
      const res = await request(app).get('/api/logs/container/nonexistent');

      // Docker will throw a not-found error for an invalid container ID
      expect([404, 500]).toContain(res.statusCode);
    });
  });
});
