<commit_before>const assert = require('power-assert');
const express = require('express');
const request = require('supertest');

describe('packages/health', () => {
  it('should have outcome "UP" without any checks', done => {
    const app = express();

    const health = require('../index')();
    require('@dnode/controllers')(app, [health]);

    request(app)
      .get('/health')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
      .then(res => {
        assert.equal(res.body.outcome, 'UP');
        assert.equal(res.body.checks.length, 0);
        done();
      });
  });

  it('should have outcome "UP" with positive checks', done => {
    const app = express();

    const health = require('../index')({
      a: () => {},
      b: () => {},
    });
    require('@dnode/controllers')(app, [health]);

    request(app)
      .get('/health')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
      .then(res => {
        assert.equal(res.body.outcome, 'UP');
        assert.equal(res.body.checks.length, 2);

        assert.equal(res.body.checks[0].name, 'a');
        assert.equal(res.body.checks[0].state, 'UP');
        assert.deepEqual(res.body.checks[0].data, {});

        assert.equal(res.body.checks[1].name, 'b');
        assert.equal(res.body.checks[1].state, 'UP');
        assert.deepEqual(res.body.checks[1].data, {});

        done();
      });
  });

  it('should have outcome "UP" with positive checks and data', done => {
    const app = express();

    const health = require('../index')({
      a: { check: () => {}, data: { c: 'd' } },
      b: { check: () => {}, data: { e: 'f' } },
    });
    require('@dnode/controllers')(app, [health]);

    request(app)
      .get('/health')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
      .then(res => {
        assert.equal(res.body.outcome, 'UP');
        assert.equal(res.body.checks.length, 2);

        assert.equal(res.body.checks[0].name, 'a');
        assert.equal(res.body.checks[0].state, 'UP');
        assert.deepEqual(res.body.checks[0].data, { c: 'd' });

        assert.equal(res.body.checks[1].name, 'b');
        assert.equal(res.body.checks[1].state, 'UP');
        assert.deepEqual(res.body.checks[1].data, { e: 'f' });

        done();
      });
  });

  it('should have outcome "DOWN" if at least one check throws an error', done => {
    const app = express();

    const health = require('../index')({
      a: () => {
        throw new Error('test');
      },
      b: () => {},
    });
    require('@dnode/controllers')(app, [health]);

    request(app)
      .get('/health')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(500)
      .then(res => {
        assert.equal(res.body.outcome, 'DOWN');
        assert.equal(res.body.checks.length, 2);

        assert.equal(res.body.checks[0].name, 'a');
        assert.equal(res.body.checks[0].state, 'DOWN');
        assert.deepEqual(res.body.checks[0].data, {});

        assert.equal(res.body.checks[1].name, 'b');
        assert.equal(res.body.checks[1].state, 'UP');
        assert.deepEqual(res.body.checks[1].data, {});

        done();
      });
  });

  it('should support async checks', done => {
    const app = express();

    const health = require('../index')({
      a: () => {
        return new Promise((resolve, reject) => {
          process.nextTick(() => {
            reject(new Error('test'));
          });
        });
      },
      b: () => {},
    });
    require('@dnode/controllers')(app, [health]);

    request(app)
      .get('/health')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(500)
      .then(res => {
        assert.equal(res.body.outcome, 'DOWN');
        assert.equal(res.body.checks.length, 2);

        assert.equal(res.body.checks[0].name, 'a');
        assert.equal(res.body.checks[0].state, 'DOWN');
        assert.deepEqual(res.body.checks[0].data, {});

        assert.equal(res.body.checks[1].name, 'b');
        assert.equal(res.body.checks[1].state, 'UP');
        assert.deepEqual(res.body.checks[1].data, {});

        done();
      });
  });
});<commit_msg>Simplify the tests<commit_after>const assert = require('power-assert');
const express = require('express');
const request = require('supertest');

describe('packages/health', () => {
  it('should have outcome "UP" without any checks', done => {
    const app = express();
    require('@dnode/controllers')(app, [require('../index')()]);
    request(app)
      .get('/health')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
      .then(res => {
        assert.deepEqual(res.body, { outcome: 'UP', checks: [] });
        done();
      });
  });

  it('should have outcome "UP" with positive checks', done => {
    const app = express();
    require('@dnode/controllers')(app, [require('../index')({
      a: () => {},
      b: () => {},
    })]);
    request(app)
      .get('/health')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
      .then(res => {
        assert.deepEqual(res.body, { outcome: 'UP', checks: [
          { name: 'a', state: 'UP', data: {} },
          { name: 'b', state: 'UP', data: {} },
        ] });
        done();
      });
  });

  it('should have outcome "UP" with positive checks and data', done => {
    const app = express();
    require('@dnode/controllers')(app, [require('../index')({
      a: { check: () => {}, data: { c: 'd' } },
      b: { check: () => {}, data: { e: 'f' } },
    })]);
    request(app)
      .get('/health')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
      .then(res => {
        assert.deepEqual(res.body, { outcome: 'UP', checks: [
          { name: 'a', state: 'UP', data: { c: 'd' } },
          { name: 'b', state: 'UP', data: { e: 'f' } },
        ] });
        done();
      });
  });

  it('should have outcome "DOWN" if at least one check throws an error', done => {
    const app = express();
    require('@dnode/controllers')(app, [require('../index')({
      a: () => {
        throw new Error('test');
      },
      b: () => {},
    })]);
    request(app)
      .get('/health')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(500)
      .then(res => {
        assert.deepEqual(res.body, { outcome: 'DOWN', checks: [
          { name: 'a', state: 'DOWN', data: {} },
          { name: 'b', state: 'UP', data: {} },
        ] });
        done();
      });
  });

  it('should support async checks', done => {
    const app = express();
    require('@dnode/controllers')(app, [require('../index')({
      a: () => {
        return new Promise((resolve, reject) => {
          process.nextTick(() => {
            reject(new Error('test'));
          });
        });
      },
      b: () => {},
    })]);
    request(app)
      .get('/health')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(500)
      .then(res => {
        assert.deepEqual(res.body, { outcome: 'DOWN', checks: [
          { name: 'a', state: 'DOWN', data: {} },
          { name: 'b', state: 'UP', data: {} },
        ] });
        done();
      });
  });
});
{'fraction_non_alphanumeric': 0.22380440623320796, 'fraction_numerical': 0.007925846319183236, 'mean_word_length': 2.232739904472427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7032589', 'n_tokens_mistral': 2478, 'n_tokens_neox': 2397, 'n_words': 445}