<commit_before>/* global describe, it */
/* jslint node: true, esnext: true */

"use strict";

const chai = require('chai');
chai.use(require("chai-as-promised"));
const assert = chai.assert;
const expect = chai.expect;
const should = chai.should();
const request = require('supertest');
const kronos = require('../lib/manager.js');

let testPort = 12345;

describe('service manager REST', function () {
  const flowDecl = {
    "flow1": {
      "steps": {
        "s1": {
          "type": "kronos-copy",
          "config": {
            "key1": "value1"
          },
          "endpoints": {
            "in": "stdin",
            "out": "stdout"
          }
        }
      }
    }
  };

  function shutdownManager (manager,done) {
    return function(error,result) {
      manager.shutdown().then(function () {
        if(error) { return done(error); }
        else done();
      });
    };
  }

  function initManager() {
    return kronos.manager({
      flows: flowDecl,
      port: testPort
    });

    testPort ++; // TODO somhow koa-websocket does not shutdown correctly
  }

  describe('health', function () {
    it('GET /health', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .get('/health')
          .expect(200)
          .expect(function(res) {
            if (res.text !== 'OK') throw Error("not OK");
          })
          .end(shutdownManager(manager,done));
      });
    });
  });

  describe('state', function () {
    it('GET /state', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .get('/state')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function(res) {
            const response = JSON.parse(res.text);
            if (!response.uptime > 0) throw Error("uptime > 0 ?");
          })
          .end(shutdownManager(manager,done));
      });
    });
  });

  describe('flows', function () {
    it('GET /flows', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .get('/flows')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(function(res) {
            const response = JSON.parse(res.text);
            //console.log(`RES: ${JSON.stringify(response)}`);
            if (response[0].name !== 'flow1') throw Error("flow flow1 missing");
          })
          .expect(200)
          .end(shutdownManager(manager,done));
      });
    });
    it('GET /flows/flow1', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .get('/flows/flow1')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function(res) {
            const response = JSON.parse(res.text);
            if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager,done));
      });
    });

    it('DELETE /flows/flow1', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .delete('/flows/flow1')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function(res) {
            const response = JSON.parse(res.text);
            if (Object.keys(response).length > 0) throw Error("delete error");
          })
/*
          .get('/flows')
          .expect(200)
          .expect(function(res) {
            const response = JSON.parse(res.text);
            console.log(`RES: ${JSON.stringify(response)}`);
          })
*/
          .end(shutdownManager(manager,done));
      });
    });

    it('POST /flows', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .post('/flows')
          .send({ "a" : { "steps" : {} } })
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function(res) {
            //const response = JSON.parse(res.text);
            //if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager,done));
      });
    });
  });
});<commit_msg>test: pass done as 2. arg to promise .then()<commit_after>/* global describe, it */
/* jslint node: true, esnext: true */

"use strict";

const chai = require('chai');
chai.use(require("chai-as-promised"));
const assert = chai.assert;
const expect = chai.expect;
const should = chai.should();
const request = require('supertest');
const kronos = require('../lib/manager.js');

let testPort = 12345;

describe('service manager REST', function () {
  const flowDecl = {
    "flow1": {
      "steps": {
        "s1": {
          "type": "kronos-copy",
          "config": {
            "key1": "value1"
          },
          "endpoints": {
            "in": "stdin",
            "out": "stdout"
          }
        }
      }
    }
  };

  function shutdownManager (manager,done) {
    return function(error,result) {
      manager.shutdown().then(function () {
        if(error) { return done(error); }
        else done();
      },done);
    };
  }

  function initManager() {
    return kronos.manager({
      flows: flowDecl,
      port: testPort
    });

    testPort ++; // TODO somhow koa-websocket does not shutdown correctly
  }

  describe('health', function () {
    it('GET /health', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .get('/health')
          .expect(200)
          .expect(function(res) {
            if (res.text !== 'OK') throw Error("not OK");
          })
          .end(shutdownManager(manager,done));
      },done);
    });
  });

  describe('state', function () {
    it('GET /state', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .get('/state')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function(res) {
            const response = JSON.parse(res.text);
            if (!response.uptime > 0) throw Error("uptime > 0 ?");
          })
          .end(shutdownManager(manager,done));
      },done);
    });
  });

  describe('flows', function () {
    it('GET /flows', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .get('/flows')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(function(res) {
            const response = JSON.parse(res.text);
            //console.log(`RES: ${JSON.stringify(response)}`);
            if (response[0].name !== 'flow1') throw Error("flow flow1 missing");
          })
          .expect(200)
          .end(shutdownManager(manager,done));
      },done);
    });
    it('GET /flows/flow1', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .get('/flows/flow1')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function(res) {
            const response = JSON.parse(res.text);
            if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager,done));
      },done);
    });

    it('DELETE /flows/flow1', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .delete('/flows/flow1')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function(res) {
            const response = JSON.parse(res.text);
            if (Object.keys(response).length > 0) throw Error("delete error");
          })
/*
          .get('/flows')
          .expect(200)
          .expect(function(res) {
            const response = JSON.parse(res.text);
            console.log(`RES: ${JSON.stringify(response)}`);
          })
*/
          .end(shutdownManager(manager,done));
      },done);
    });

    it('POST /flows', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .post('/flows')
          .send({ "a" : { "steps" : {} } })
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function(res) {
            //const response = JSON.parse(res.text);
            //if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager,done));
      },done);
    });
  });
});
{'fraction_non_alphanumeric': 0.17738309352517986, 'fraction_numerical': 0.010004496402877698, 'mean_word_length': 1.9627039627039626, 'pattern_counts': {'":': 18, '<': 3, '<?xml version=': 0, '>': 9, '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': '2701135', 'n_tokens_mistral': 2676, 'n_tokens_neox': 2612, 'n_words': 491}