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

"use strict";

const chai = require('chai'),
  assert = chai.assert,
  expect = chai.expect,
  should = chai.should(),
  request = require("supertest-as-promised")(Promise),
  kronos = require('kronos-service-manager'),
  admin = require('../lib/adminService'),
  wt = require('koa-jwt');

chai.use(require("chai-as-promised"));

let testPort = 12345;

describe('service manager admin', function () {
  const flowDecl = {
    "name": "flow1",
    "type": "kronos-flow",
    "steps": {
      "s0": {
        "type": "kronos-stdin",
        "endpoints": {
          "out": "s1/in"
        }
      },
      "s1": {
        "type": "kronos-copy",
        "endpoints": {
          "out": "s2/in"
        }
      },
      "s2": {
        "type": "kronos-stdout"
      }
    }
  };

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

  function initManager() {
    return kronos.manager().then(manager => {
      require('kronos-step-stdio').registerWithManager(manager);
      admin.registerWithManager(manager);
      manager.registerFlow(manager.getStepInstance(flowDecl));

      testPort++; // TODO somehow koa-websocket does not shutdown correctly
      return manager;
    });
  }

  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[1].url !== 'flow1') throw Error("flow 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) {
            //console.log(`reponse: ${res.text}`);
            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({
            "name": "a",
            "type": "kronos-flow",
            "steps": {}
          })
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function (res) {
            console.log(res.text);
            //const response = JSON.parse(res.text);
            //if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager, done));
      }, done);
    });

    it('POST /flows with error', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .post('/flows')
          .send({
            "name": "a",
            "type": "kronos-flow",
            "steps": {
              "s1": {
                "type": "no-such-type"
              }
            }
          })
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function (res) {
            console.log("RES:" + res.text);
            //const response = JSON.parse(res.text);
            //if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager, done));
      }, done);
    });

  });
});<commit_msg>test: trying to resolve some initialization issues<commit_after>/* global describe, it */
/* jslint node: true, esnext: true */

"use strict";

const chai = require('chai'),
  assert = chai.assert,
  expect = chai.expect,
  should = chai.should(),
  request = require("supertest-as-promised")(Promise),
  kronos = require('kronos-service-manager'),
  admin = require('../lib/adminService');

chai.use(require("chai-as-promised"));

describe('service manager admin', function () {
  const flowDecl = {
    "name": "flow1",
    "type": "kronos-flow",
    "steps": {
      "s0": {
        "type": "kronos-stdin",
        "endpoints": {
          "out": "s1/in"
        }
      },
      "s1": {
        "type": "kronos-copy",
        "endpoints": {
          "out": "s2/in"
        }
      },
      "s2": {
        "type": "kronos-stdout"
      }
    }
  };

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

  function initManager() {
    return kronos.manager().then(manager => {
      require('kronos-step-stdio').registerWithManager(manager);

      admin.registerWithManager(manager);

      manager.registerFlow(manager.getStepInstance(flowDecl));

      const as = manager.serviceGet('admin');
      return as.start().then(service => Promise.resolve(manager));
    });
  }

  describe('flows', function () {
    it('GET /flows', function (done) {
      initManager().then(function (manager) {

        console.log(
          `Admin: ${manager.services.admin} ${manager.services.admin.state} ${manager.services.admin.server}`
        );

        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[1].url !== 'flow1') throw Error("flow 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) {
            //console.log(`reponse: ${res.text}`);
            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({
            "name": "a",
            "type": "kronos-flow",
            "steps": {}
          })
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function (res) {
            console.log(res.text);
            //const response = JSON.parse(res.text);
            //if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager, done));
      }, done);
    });

    it('POST /flows with error', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .post('/flows')
          .send({
            "name": "a",
            "type": "kronos-flow",
            "steps": {
              "s1": {
                "type": "no-such-type"
              }
            }
          })
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function (res) {
            console.log("RES:" + res.text);
            //const response = JSON.parse(res.text);
            //if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager, done));
      }, done);
    });
  });
});
{'fraction_non_alphanumeric': 0.17488653555219363, 'fraction_numerical': 0.008169440242057489, 'mean_word_length': 1.7916666666666667, 'pattern_counts': {'":': 42, '<': 3, '<?xml version=': 0, '>': 10, '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': '6668149', 'n_tokens_mistral': 2959, 'n_tokens_neox': 2891, 'n_words': 526}