import {
  renderService,
  managementService,
  contentService,
  managementServiceRoot,
  ci_buildVersion,
  ci_buildBranch,
} from './helper';

interface HealthInfo {
  status: string;
  buildVersion: string;
  buildBranch: string;
}

describe('Verify latest services deployments', () => {
  it('Should have latest Management API service', async () => {
    const response: HealthInfo = (await managementServiceRoot.get('/health')).data;
    expect(response.buildVersion).toBe(ci_buildVersion);
    expect(response.buildBranch).toBe(ci_buildBranch);
  });

  it('Should return 200 for Management API docs', async () => {
    const req = await managementService.get('/docs');
    expect(req.status).toBe(200);
    expect(req.headers['content-type']).toEqual('text/html; charset=utf-8');
  });

  it('Should return 200 for Management API docs.json', async () => {
    const req = await managementService.get('/docs.json');
    expect(req.status).toBe(200);
    expect(req.headers['content-type']).toEqual('application/json; charset=UTF-8');
  });

  it('Should have latest Render Runtime service', async () => {
    const response: HealthInfo = (await renderService.get('/health')).data;
    expect(response.buildVersion).toBe(ci_buildVersion);
    expect(response.buildBranch).toBe(ci_buildBranch);
  });

  it('Should return 200 for Render Runtime API docs', async () => {
    const req = await renderService.get('/docs');
    expect(req.status).toBe(200);
    expect(req.headers['content-type']).toEqual('text/html; charset=utf-8');
  });

  it('Should return 200 for Render Runtime API docs.json', async () => {
    const req = await renderService.get('/docs.json');
    expect(req.status).toBe(200);
    expect(req.headers['content-type']).toEqual('application/json; charset=UTF-8');
  });

  it('Should have latest Content API service', async () => {
    const response: HealthInfo = (await contentService.get('/health')).data;
    expect(response.buildVersion).toBe(ci_buildVersion);
    expect(response.buildBranch).toBe(ci_buildBranch);
  });

  it('Should return 200 for Content API docs', async () => {
    const req = await contentService.get('/docs');
    expect(req.status).toBe(200);
    expect(req.headers['content-type']).toEqual('text/html; charset=utf-8');
  });

  it('Should return 200 for Content API docs.json', async () => {
    const req = await contentService.get('/docs.json');
    expect(req.status).toBe(200);
    expect(req.headers['content-type']).toEqual('application/json; charset=UTF-8');
  });
});
