/* global describe, it*/
/* jslint node: true, esnext: true */

"use strict";

const chai = require('chai'),
  assert = chai.assert,
  expect = chai.expect,
  should = chai.should(),
  ksm = require('kronos-service-manager'),
  ServiceConsul = require('../service.js');


describe('consul service', function () {
  this.timeout(30000);
  it("create", () =>
    ksm.manager([{}, {
      name: "consul",
      port: 4713
    }], [ServiceConsul, require('kronos-service-health-check'), require('kronos-service-koa')]).then(
      manager => {
        const cs = manager.services.registry;

        assert.equal(cs.name, "registry");

        return cs.start().then(f => {
          assert.equal(cs.state, "running");
          assert.deepEqual(cs.tags, []);

          cs.registerService('myService', {
            url: cs.listener.url + "/somepath"
          }).then(() => {
            const us = cs.serviceURLs('myService');

            setInterval(() =>
              us.next().value.then(u => console.log(`myService: ${u}`)),
              500);

            setTimeout(() => {
                cs.unregisterService('myService').then(() => {
                  assert.ok("unregistered");
                });
              },
              5000);
          }).catch(console.log);

          /*
                    cs.consul.kv.set('hello', 'world').then(f => {
                      return cs.consul.kv.get('hello').then(f => {
                        console.log(`GET ${f[0].Key} = ${f[0].Value}`);
                      });
                    }).catch(console.log);
          */

          return new Promise((f, r) =>
            setTimeout(f, 20000));
        });
      })
  );
});

@@ -935,65 +935,210 @@
     
-setInterval(() =%3E%0A              us.next().value.then(u =%3E
+assert.equal(us, cs.listener.url + %22/somepath%22);%0A%0A            setInterval(() =%3E%0A              us.next().value.then(u =%3E %7B%0A                assert.equal(us, cs.listener.url + %22/somepath%22);%0A               
  con
@@ -1164,16 +1164,33 @@
 : $%7Bu%7D%60)
+;%0A              %7D
 ),%0A     
@@ -1198,17 +1198,18 @@
         
-5
+10
 00);%0A%0A  
@@ -1349,322 +1349,134 @@
 %22);%0A
-                %7D);%0A              %7D,%0A              5000);%0A          %7D).catch(console.log);%0A%0A          /*%0A                    cs.consul.kv.set('hello', 'world').then(f =%3E %7B%0A                      return cs.consul.kv.get('hello').then(f =%3E %7B%0A                        console.log(%60GET $%7Bf%5B0%5D.Key%7D = $%7Bf%5B0%5D.Value%7D%60);%0A   
+%0A                  const us = cs.serviceURLs('myService');%0A                  console.log(%60after: $%7Bus%7D%60);%0A                %7D);%0A
     
@@ -1481,32 +1481,26 @@
             
-     %7D);
+%7D,
 %0A           
@@ -1490,32 +1490,43 @@
    %7D,%0A          
+    5000);%0A
           %7D).cat
@@ -1545,21 +1545,8 @@
 og);
-%0A          */
 %0A%0A  

assert service url