/**
 * Integration tests for health and readiness endpoints.
 *
 * /health and /ready are used by Docker HEALTHCHECK and must:
 *  - Require no authentication
 *  - Not be rate-limited
 *  - Return the correct status codes
 */

import request from 'supertest';
import { createApp } from '../../server/app.js';

describe('GET /health', () => {
  let app;

  beforeEach(() => {
    app = createApp();
  });

  it('returns 200 with status ok', async () => {
    const res = await request(app).get('/health');
    expect(res.status).toBe(200);
    expect(res.body.status).toBe('ok');
  });

  it('includes uptime as a number', async () => {
    const res = await request(app).get('/health');
    expect(typeof res.body.uptime).toBe('number');
    expect(res.body.uptime).toBeGreaterThanOrEqual(0);
  });
});

describe('GET /ready', () => {
  let app;

  afterEach(() => {
    delete process.env.EMBY_URL;
  });

  it('returns 200 when EMBY_URL is configured', async () => {
    process.env.EMBY_URL = 'https://emby.local';
    app = createApp();
    const res = await request(app).get('/ready');
    expect(res.status).toBe(200);
    expect(res.body.status).toBe('ready');
  });

  it('returns 503 when EMBY_URL is not configured', async () => {
    delete process.env.EMBY_URL;
    app = createApp();
    const res = await request(app).get('/ready');
    expect(res.status).toBe(503);
    expect(res.body.status).toBe('not ready');
  });
});
