var events = require('./events');
var test2 = require('./test-util').test2;
var dsl = require('./ringpop-assert');

// endpoints
// /admin/debugClear
// /admin/debugSet
// /admin/lookup
// /admin/stats

test2('endpoint: /admin/gossip/stop', 7, 5000, function(t, tc, n) {
    return [
        dsl.waitForJoins(t, tc, n),
        dsl.assertStats(t, tc, n+1, 0, 0),

        dsl.callEndpoint(t, tc, '/admin/gossip/stop'),
        dsl.validateEventBody(t, tc, {
            type: events.Types.AdminGossipStop,
            direction: 'response'
        }, "Wait for AdminGossipStop response", function (response) {
            return true;
        }),

        dsl.consumePings(t, tc),
        dsl.wait(1000), // normally you would expect pings to be transfered in this time

        dsl.expectOnlyPings(t, tc, 0)
    ];
});

test2('endpoint: /admin/gossip/start', 7, 5000, function(t, tc, n) {
    return [
        dsl.waitForJoins(t, tc, n),
        dsl.assertStats(t, tc, n+1, 0, 0),

        dsl.callEndpoint(t, tc, '/admin/gossip/stop'),
        dsl.validateEventBody(t, tc, {
            type: events.Types.AdminGossipStop,
            direction: 'response'
        }, "Wait for AdminGossipStop response", function (response) {
            return true;
        }),

        dsl.consumePings(t, tc),
        dsl.wait(1000), // normally you would expect pings to be transfered in this time

        dsl.expectOnlyPings(t, tc, 0),

        dsl.callEndpoint(t, tc, '/admin/gossip/start'),
        dsl.validateEventBody(t, tc, {
            type: events.Types.AdminGossipStart,
            direction: 'response'
        }, "Wait for AdminGossipStart response", function (response) {
            return true;
        }),

        dsl.waitForPing(t, tc),
        dsl.expectOnlyPings(t, tc)
    ];
});

test2('endpoint: /admin/gossip/tick', 7, 5000, function(t, tc, n) {
    return [
        dsl.waitForJoins(t, tc, n),
        dsl.assertStats(t, tc, n+1, 0, 0),

        // stop gossip before invoking tick to make sure the tick is from the invocation
        dsl.callEndpoint(t, tc, '/admin/gossip/stop'),
        dsl.validateEventBody(t, tc, {
            type: events.Types.AdminGossipStop,
            direction: 'response'
        }, "Wait for AdminGossipStop response", function (response) {
            return true;
        }),

        dsl.consumePings(t, tc),
        dsl.wait(1000), // normally you would expect pings to be transfered in this time

        dsl.expectOnlyPings(t, tc, 0),

        dsl.callEndpoint(t, tc, '/admin/gossip/tick'),
        dsl.validateEventBody(t, tc, {
            type: events.Types.AdminGossipTick,
            direction: 'response'
        }, "Wait for AdminGossipTick response", function (response) {
            console.log("responded!");
            return true;
        }),

        dsl.wait(1000), // make sure it only send 1 ping

        dsl.expectOnlyPings(t, tc, 1)
    ];
});

test2('endpoint: /admin/lookup', 7, 5000, function(t, tc, n) {
    return [
        dsl.waitForJoins(t, tc, n),
        dsl.assertStats(t, tc, n+1, 0, 0),

        dsl.callEndpoint(t, tc, '/admin/lookup', { key: 'Hello World' }),
        dsl.validateEventBody(t, tc, {
            type: events.Types.AdminLookup,
            direction: 'response'
        }, "Wait for AdminLookup response", function (response) {
            // TODO test if the key is mapped to the right node
            return true;
        }),

        dsl.expectOnlyPings(t, tc)
    ];
});

@@ -111,16 +111,21 @@
 t');%0A%0A//
+ TODO
  endpoin
@@ -129,16 +129,18 @@
 oints%0A//
+  
  /admin/
@@ -149,19 +149,69 @@
 bugClear
+ (NOOP in go, toggle between ping logs in node2)
 %0A//
+  
  /admin/
@@ -221,16 +221,49 @@
 ugSet%0A//
+ node only:%0A//   /admin/join%0A//  
  /admin/
@@ -267,24 +267,25 @@
 in/l
-ookup
+eave
 %0A//
+  
  /admin/
 stat
@@ -284,13 +284,14 @@
 min/
-stats
+reload
 %0A%0Ate
@@ -3550,28 +3550,585 @@
 OnlyPings(t, tc)%0A    %5D;%0A%7D);%0A
+%0Atest2('endpoint: /admin/stats', 7, 5000, function(t, tc, n) %7B%0A    return %5B%0A        dsl.waitForJoins(t, tc, n),%0A        dsl.assertStats(t, tc, n+1, 0, 0),%0A%0A        dsl.callEndpoint(t, tc, '/admin/stats'),%0A        dsl.validateEventBody(t, tc, %7B%0A            type: events.Types.Stats,%0A            direction: 'response'%0A        %7D, %22Wait for Stats response%22, function (response) %7B%0A            // TODO do validation of specific values, the payload structure is already validated%0A            return true;%0A        %7D),%0A%0A        dsl.expectOnlyPings(t, tc)%0A    %5D;%0A%7D);%0A

Add test invocation of /admin/stats.