"use strict";

// var assert = require("assert");
var request = require("supertest"),
    apiApp  = require("../");

request = request(apiApp());

describe("/books", function () {

  describe("/:isbn", function () {

    it("should redirect to normalised isbn13", function (done) {
      request
        .get("/books/0340831499")
        .expect(301)
        .expect("Location", "/books/9780340831496")
        .end(done);
    });

    it("should return 404 when the isbn is not valid", function (done) {
      request
        .get("/books/123456789")
        .expect(404)
        .expect("Content-Type", "application/json; charset=utf-8")
        .expect({ error: "isbn '123456789' is not valid" })
        .end(done);
    });

    it("should return correct details for valid isbn", function (done) {
      request
        .get("/books/9780340831496")
        .expect(200)
        .expect("Content-Type", "application/json; charset=utf-8")
        .expect({
          isbn: "9780340831496",
          title: "McGee on Food and Cooking: An Encyclopedia of Kitchen Science, History and Culture",
          authors: ["Harold McGee"],
        })
        .end(done);
    });

    it.skip("should use cache");

  });

});

"use strict";

var assert = require("assert"),
    request = require("supertest"),
    getter  = require("../src/getter"),
    fetcher = require("l2b-price-fetchers"),
    sinon  = require("sinon"),
    async = require("async"),
    apiApp  = require("../"),
    samples = require("./samples");

request = request(apiApp());

describe("/books", function () {

  var sandbox;
  var fetchStub;

  beforeEach(function (done) {
    getter.enterTestMode(done);
    sandbox = sinon.sandbox.create();

    // stub the fetch so that it does not do a scrape
    fetchStub = sandbox
      .stub(fetcher, "fetch")
      .yields(null, samples.fetch["9780340831496"]);

  });

  afterEach(function () {
    sandbox.restore();
  });

  describe("/:isbn", function () {

    it("should redirect to normalised isbn13", function (done) {
      request
        .get("/books/0340831499")
        .expect(301)
        .expect("Location", "/books/9780340831496")
        .end(function (err) {
          assert(!fetchStub.called);
          done(err);
        });
    });

    it("should return 404 when the isbn is not valid", function (done) {
      request
        .get("/books/123456789")
        .expect(404)
        .expect("Content-Type", "application/json; charset=utf-8")
        .expect({ error: "isbn '123456789' is not valid" })
        .end(function (err) {
          assert(!fetchStub.called);
          done(err);
        });
    });

    it("should return correct details for valid isbn", function (done) {

      var testRequest = function (cb) {
        request
          .get("/books/9780340831496")
          .expect(200)
          .expect("Content-Type", "application/json; charset=utf-8")
          .expect(samples.getBookDetails["9780340831496"])
          .end(cb);
      };

      async.series(
        [
          testRequest,
          function (cb) { setTimeout(cb, 50); },
          testRequest,
        ],
        function (err) {
          assert(fetchStub.calledOnce);
          done(err);
        }
      );

    });

  });

});

Test that /books/:isbn uses cache