
import SKClient from "sk-client";
import expect from "expect.js";

describe("broadcasts", function() {
  const SK = new SKClient({server: "http://localhost:80"});

  let testBroadcastId = "nope";
  let testBroadcastName = "Test Broadcast";

  const fail = function(err) {
    if (err instanceof Error) {
      throw err;
    }
    else {
      throw new Error(err.message);
    }
  };

  it("should create", function() {
    return SK.broadcasts.create({name: testBroadcastName}).then(function(broadcast) {
      expect(broadcast).to.be.ok();
      expect(broadcast.name).to.be(testBroadcastName);
      testBroadcastId = broadcast.id;
    })
    .catch(fail);
  });

  it("should findOne", function() {
    return SK.broadcasts.findOne(testBroadcastId).then(function(broadcast) {
      expect(broadcast).to.be.ok();
      expect(broadcast.name).to.be(testBroadcastName);
    })
    .catch(fail);
  });

  it("should update", function() {
    testBroadcastName = "Altered Name";
    return SK.broadcasts.update(testBroadcastId, {name: testBroadcastName})
    .then(function(broadcast) {
      expect(broadcast).to.be.ok();
      expect(broadcast.name).to.be(testBroadcastName);
    })
    .catch(fail);
  });

  it("should find", function() {
    return SK.broadcasts.find().then(function(broadcasts) {
      expect(broadcasts).to.be.an(Array);
      const myBroadcast = broadcasts.filter((b) => b.id === testBroadcastId)[0];
      expect(myBroadcast).to.be.ok();
      expect(myBroadcast.name).to.be(testBroadcastName);
    })
    .catch(fail);
  });

  it("should watch", function() {
    let data;
    let watchBroadcastName = "broadcast watch test";
    let watchBroadcastId;

    // Given a cursor and an eventName, return a promise that has resolved after the event has
    // fired once.
    let eventPromise = function(eventName) {
      return new Promise((resolve, reject) => {
        broadcastCursor.on(eventName, (...args) => {
          if (resolve) {
            resolve([...args]);
            resolve = null;
          }
        });
      });
    };

    let broadcastCursor = SK.broadcasts.watch({id: testBroadcastId})

    .then(function() {
      // Now we want to do a create, so we're going to send a PUT and register a "updated"
      // handler. But those things do not necessarily to resolve in any particular order, so
      // we'll do them both and handle it with a Promise.all
      const createdEventPromise = eventPromise("created");
      const doCreatePromise = SK.broadcasts.create({name: watchBroadcastName});
      return Promise.all([createdEventPromise, doCreatePromise]);
    })

    .then(function([docs], newDoc) {
      expect(docs).to.equal([newDoc]);
      watchBroadcastId = newDoc.id;

      // Same deal, now modify it
      testBroadcastName = "Updated in Watch";
      const updateEventPromise = eventPromise("updated");
      const doUpdatePromise = SK.broadcasts.update(testBroadcastName, {name: testBroadcastName});
      return Promise.all([updateEventPromise, doUpdatePromise]);
    })

    .then(function([docs], doc) {
      expect(docs).to.equal([doc]);
      expect(docs[0].name).to.equal(testBroadcastName);

      // Same deal, now delete it.
      const deleteEventPromise = eventPromise("deleted");
      const doDeletePromise = SK.broadcasts.delete(testBroadcastName);
      return Promise.all([deleteEventPromise, doDeletePromise]);
    })

    .then(function([ids]) {
      expect(ids).to.equal([watchBroadcastId]);
    });

    return broadcastCursor;
  });

  it("should delete", function() {
    return SK.broadcasts.delete(testBroadcastId)
    .then(function() {
      return SK.broadcasts.findOne(testBroadcastId).then(function() {
        throw new Error("Uh oh -- success on findOne after delete");
      })
      .catch(function(err) {
        expect(err.status).to.be(404);
      });
    })
    .catch(fail);
  });
});

@@ -1549,27 +1549,26 @@
  %7D);%0A%0A  
-it(%22should 
+describe(%22
 watch%22, 
@@ -1672,16 +1672,41 @@
 dcastId;
+%0A    let broadcastCursor;
 %0A%0A    //
@@ -2086,27 +2086,65 @@
     %7D;%0A%0A    
-let
+it(%22should watch%22, function(done) %7B%0A     
  broadcastCu
@@ -2192,22 +2192,16 @@
 castId%7D)
-%0A%0A    
 .then(fu
@@ -2221,246 +2221,111 @@
     
-// Now we want to do a create, so we're going to send a PUT and register a %22updated%22%0A      // handler. But those things do not necessarily to resolve in any particular order, so%0A      // we'll do them both and handle it with a Promise.all
+  done();%0A      %7D);%0A      return broadcastCursor;%0A    %7D);%0A%0A%0A    it(%22should watch created%22, function() %7B
 %0A   
@@ -2520,38 +2520,24 @@
 atePromise%5D)
-;%0A    %7D)%0A%0A    
 .then(functi
@@ -2553,24 +2553,26 @@
 , newDoc) %7B%0A
+  
       expect
@@ -2604,16 +2604,18 @@
 ;%0A      
+ 
+ 
 watchBro
@@ -2636,16 +2636,78 @@
 Doc.id;%0A
+      %7D);%0A    %7D);%0A%0A    it(%22should watch updated%22, function() %7B
 %0A      /
@@ -2994,38 +2994,24 @@
 atePromise%5D)
-;%0A    %7D)%0A%0A    
 .then(functi
@@ -3030,24 +3030,26 @@
 oc) %7B%0A      
+  
 expect(docs)
@@ -3062,24 +3062,26 @@
 ual(%5Bdoc%5D);%0A
+  
       expect
@@ -3124,16 +3124,79 @@
 tName);%0A
+      %7D);%0A%0A    %7D);%0A%0A    it(%22should watch deleted%22, function() %7B
 %0A      /
@@ -3419,22 +3419,8 @@
 se%5D)
-;%0A    %7D)%0A%0A    
 .the
@@ -3435,24 +3435,26 @@
 on(%5Bids%5D) %7B%0A
+  
       expect
@@ -3497,48 +3497,30 @@
     
+  
 %7D);%0A
-%0A
     
-return broadcastCursor;
+%7D);%0A
 %0A  %7D);%0A%0A
   it
@@ -3507,32 +3507,33 @@
     %7D);%0A%0A  %7D);%0A%0A
+%0A
   it(%22should del

Improve tests