text: /*eslint-env node, mocha */
  /*eslint no-unused-vars: 0 */
  
  var Prismic = require('../lib/prismic.js');
  var chai = require('chai');
  
  // Test snippets for the documentation, and keep them in sync with Gist
  
  var testRepository = 'https://lesbonneschoses.prismic.io/api',
      assert = chai.assert;
  
  function getLinkResolver(ref) {
    return function(doc, isBroken) {
      if (isBroken) return '#broken';
      return "/testing_url/" + doc.id + "/" + doc.slug + (ref ? ('?ref=' + ref) : '');
    };
  }
  
  describe('API', function() {
  
    it('prismic-api.js', function (done) {
      Prismic.api('https://lesbonneschoses.cdn.prismic.io/api', function (err, Api) {
        if (err) done(err); // gisthide
        // You can use the Api object inside this block
        console.log("References: ", Api.data.refs);
        assert.operator(Api.data.refs.length, '>', 0, 'at least one reference'); // gisthide
        done(); // gisthide
      });
    });
  
    it('prismic-apiPrivate.js', function (done) {
      Prismic.api('https://lesbonneschoses.cdn.prismic.io/api', function (err) {
        // This will fail because the token is invalid, but this is how to access a private API
        assert.equal(err.message, "Unexpected status code [401] on URL https://lesbonneschoses.cdn.prismic.io/api?access_token=MC5-XXXXXXX-vRfvv70"); // gisthide
        done(); // gisthide
      }, "MC5-XXXXXXX-vRfvv70");
    });
  
    it('prismic-references.js', function (done) {
      var previewToken = 'MC5VbDdXQmtuTTB6Z0hNWHF3.c--_vVbvv73vv73vv73vv71EA--_vS_vv73vv70T77-9Ke-_ve-_vWfvv70ebO-_ve-_ve-_vQN377-9ce-_vRfvv70';
      Prismic.api('https://lesbonneschoses.cdn.prismic.io/api', function (err, Api) {
        if (err) done(err); // gisthide
        var stPatrickRef = Api.ref("St-Patrick specials");
        // Now we'll use this reference for all our calls
        Api.query(Prismic.Predicates.at("document.type", "product"), {
          ref: stPatrickRef
        }, function (err, response) {
          if (err) {
            console.log(err);
            done();
          } // gisthide
          // The documents object contains a Response object with all documents of type "product"
          // including the new "Saint-Patrick's Cupcake"
          assert.equal(response.results.length, 17); // gisthide
          done(); // gisthide
        });
      }, previewToken);
    });
  
  });
  
  describe('Query', function() {
  
    it('prismic-simplequery.js', function (done) {
      Prismic.api('https://lesbonneschoses.prismic.io/api', function (err, Api) {
        if (err) done(err); // gisthide
        Api.query(Prismic.Predicates.at("document.type", "product"), {}, function (err, response) {
          if (err) { console.log(err); done(); } // gisthide
          // The documents object contains a Response object with all documents of type "product".
          var page = response.page; // The current page number, the first one being 1
          assert.equal(page, 1); // gisthide
          var results = response.results; // An array containing the results of the current page;
          // you may need to retrieve more pages to get all results
          assert.equal(results.length, 16); // gisthide
          var prev_page = response.prev_page; // the URL of the previous page (may be null)
          var next_page = response.next_page; // the URL of the next page (may be null)
          var results_per_page = response.results_per_page; // max number of results per page
          var results_size = response.results_size; // the size of the current page
          var total_pages = response.total_pages; // the number of pages
          var total_results_size = response.total_results_size; // the total size of results across all pages
          done(); // gisthide
        });
      });
    });
  
    it('prismic-orderings.js', function (done) {
      Prismic.api('https://lesbonneschoses.prismic.io/api', function (err, Api) {
        if (err) { done(err); return; } // gisthide
        Api.form('everything')
          .ref(Api.master())
          .query(Prismic.Predicates.at("document.type", "product"))
          .pageSize(100)
          .orderings('[my.product.price desc]')
          .submit(function (err, response) {
            if (err) { done(err); return; } // gisthide
            // The products are now ordered by price, highest first
            var results = response.results;
            assert.equal(response.results_per_page, 100); // gisthide
            done(); // gisthide
          });
      });
    });
  
    it('prismic-predicates.js', function (done) {
      Prismic.api('https://lesbonneschoses.prismic.io/api', function (err, Api) {
        if (err) { done(err); return; } // gisthide
        Api.form('everything').ref(Api.master()).query(
          Prismic.Predicates.at("document.type", "blog-post"),
          Prismic.Predicates.dateAfter("my.blog-post.date", new Date(2014, 6, 1))
        ).submit(function (err, response) {
          if (err) { done(err); return; } // gisthide
          // All documents of type "product", updated after June 1st, 2014
          assert.equal(response.results.length, 0); // gisthide
          done(); // gisthide
        });
      });
    });
  
    it('prismic-allPredicates.js', function () {
      // "at" predicate: equality of a fragment to a value.
      var at = Prismic.Predicates.at("document.type", "article");
      assert.deepEqual(at, ["at", "document.type", "article"]); // gisthide
      // "any" predicate: equality of a fragment to a value.
      var any = Prismic.Predicates.any("document.type", ["article", "blog-post"]);
      assert.deepEqual(any, ["any", "document.type", ["article", "blog-post"]]); // gisthide
      // "missing" predicate: documents having a non-empty field
      var missing = Prismic.Predicates.missing("my.article.author");
      // "has" predicate: documents having a non-empty field
      var has = Prismic.Predicates.has("my.article.author");
  
      // "fulltext" predicate: fulltext search in a fragment.
      var fulltext = Prismic.Predicates.fulltext("my.article.body", "sausage");
  
      // "similar" predicate, with a document id as reference
      var similar = Prismic.Predicates.similar("UXasdFwe42D", 10);
    });
  
  });
  
  describe('Fragments', function() {
  
    it('prismic-htmlSerializer.js', function (done) {
      Prismic.api(testRepository, function (err, Api) {
        if (err) { done(err); return; }
        Api.form('everything').query(Prismic.Predicates.at("document.id", "UlfoxUnM0wkXYXbl"))
          .ref(Api.master()).submit(function (err, documents) {
            if (err) { done(err); return; }
            var doc = documents.results[0];
            var htmlSerializer = function (element, content) {
              // Don't wrap images in a <p> tag
              if (element.type == "image") {
                return '<img src="' + element.url + '" alt="' + (element.alt || "") + '">';
              }
  
              // Add a class to hyperlinks
              if (element.type == "hyperlink") {
                return '<a class="some-link" href="' + element.url + '">' + content + '</a>';
              }
  
              // Return null to stick with the default behavior
              return null;
            };
            var html = doc.getStructuredText('blog-post.body').asHtml(getLinkResolver(), htmlSerializer);
            assert.equal(html,
                         '<h1>Our world-famous Pastry Art Brainstorm event</h1>' +
                         '<img src=\"https://prismic-io.s3.amazonaws.com/lesbonneschoses/c38f9e5a1a6c43aa7aae516c154013a2cee2bc75.jpg\" alt=\"\">' +
                         '<p>Each year, <em>Les Bonnes Choses</em> organizes a world-famous two-day event called the \"Pastry Art Brainstorm\", and which is the perfect excuse for every fine pastry artist in the world to exercise their art, and build their skills up. The event is a multiple win-win operation, at many levels: see what the event is, as seen by many point of views.</p>' +
                         '<h2>As seen by the top pastry artists worldwide</h2>' +
                         '<p>The event always starts with half a day of conference talks, given by the most insightful pastry artists in the world, selected for having made tremendous achievements in pastry that year. The list of invited guest speakers is decided jointly by the <em>Les Bonnes Choses</em> staff and the Fine Pastry Magazine editors.</p>' +
                         '<p>This is great for the speakers, who get an occasion to share their work, and have people build up on it with them.</p>' +
                         '<h2>As seen by the pastry professionals</h2>' +
                         '<p>After half a day of thoughtful conference, the professionals will get to put what they learned to good use, and mingle with the best artists worldwide to make the most daring pastries together. There are no set rules about who does what during this giant innovation workshop, and many crazy ideas get created out of thin air. As a virtually infinite amount of ingredients is provided by the <em>Les Bonnes Choses</em> staff, many unexpected pastries happen on that day, and professionals taste each other\'s creations, and provide relevant feedback to each other. Most pieces get showcased to the amateur audience as well, who get invited to taste some of the pieces.</p>' +
                                  '<p>At noon on the second day, teams are expected to subscribe to our Pastry Art Challenge, during which they will make the best possible pastry,  judged on many aspects (originality, taste, looks, ...) by a jury of amateurs and professionals. The team members of the three winning pieces share a substantial prize, and their pastries may even join the Les Bonnes Choses catalogue, and be offered in all the <em>Les Bonnes Choses</em> shops worldwide!</p>' +
                                  '<h2>As seen by the pastry amateurs</h2>' +
                                  '<p>The conference is limited with a reasonable fee; but the showcase is open to everyone, although visitors are often expected to pay the pastry chefs for the pastries they taste. The educated amateurs spend their day tasting the most daring pieces, giving some appreciated feedback to their chefs, and challenging their own tastebuds. The novice amateurs usually get a once-in-a-lifetime experience, and often mention being blown away by how rich the fine pastry art can be. All in all, every one goes home with a smile on their faces!</p>' +
                                  '<h2>As seen by the Les Bonnes Choses interns</h2>' +
                                  '<p>Every year, we recruit a very limited amount of interns, who get aboard a <a class="some-link" href=\"/testing_url/UlfoxUnM0wkXYXbu/les-bonnes-chosess-internship-a-testimony\">life-defining adventure around fine pastries</a>, discovering <em>Les Bonnes Choses</em> during half a year, with part of this time spent in one of our shops abroad. We always manage to get them on board at a time when we know they will be able to attend a Fine Pastry Brainstorm, because we consider it is a very defining element in the experience of being part of <em>Les Bonnes Choses</em>.</p>' +
                                  '<p>Not only do we invite them to the event (whatever the country they are stationed in when the event happens), but we give them a front-row seat! They are part of the jury for the Fine Pastry Challenge, they are introduced to every speaker as the next generation of pastry (thus having the occasion to learn even more, directly from them).</p>' +
                                  '<h2>As seen by fine pastry as a field</h2>' +
                                  '<p>There wasn\'t really an international occasion for pastry artists to join and share, before <em>Les Bonnes Choses</em> came up with the first Fine Pastry Brainstorm, in 2006. Fine Pastry Magazine\'s first edition was out in 2004, and initiated the idea that pastry art needed to be shared better between professionals. But a proper event to meet up in person was missing, and <em>Les Bonnes Choses</em> is proud to be the one to have come up with it first.</p>' +
                                  '<p>Since then, more local initiatives have been started (notably in Argentina, and Canada), but none comes close to the size of <em>Les Bonnes Choses</em>\'s international Fine Pastry Brainstorm.</p>' +
                         '<h2>As seen by <em>Les Bonnes Choses</em></h2>' +
                         '<p>As the almost only sponsor of every edition of the event, <em>Les Bonnes Choses</em> makes sure enough ingredients are available for everyone, rents the premises, makes sure the speakers are as comfortable as possible, and takes care of the whole organization! But through the operation, <em>Les Bonnes Choses</em> gains much more than any sponsoring can buy: not only does it get to secure <em>Les Bonnes Choses</em> as the world reference in pastry arts, but it also allows them to claim rightfully that they do offer in their shops the best pastries, created by the world top artists indeed.</p>');
  
            done();
          });
      });
    });
  
    it('prismic-getText.js', function (done) {
      Prismic.api('https://lesbonneschoses.prismic.io/api', function (err, Api) {
        if (err) throw err; // gisthide
        Api.form('everything').query(Prismic.Predicates.at("document.id", "UlfoxUnM0wkXYXbl")).ref(Api.master()).submit(function (err, documents) {
          if (err) throw err; // gisthide
          var doc = documents.results[0];
          var author = doc.getText("blog-post.author");
          if (!author) author = "Anonymous";
          assert.equal(author, "John M. Martelle, Fine Pastry Magazine"); // gisthide
          done();
        });
      });
    });
  
    it('prismic-getNumber.js', function (done) {
      Prismic.api('https://lesbonneschoses.prismic.io/api', function (err, Api) {
        if (err) throw err;
        Api.form('everything').query(Prismic.Predicates.at("document.id", "UlfoxUnM0wkXYXbO")).ref(Api.master()).submit(function (err, documents) {
          if (err) throw err;
          var doc = documents.results[0];
          // Number predicates
          var gt = Prismic.Predicates.gt("my.product.price", 10);
          var lt = Prismic.Predicates.lt("my.product.price", 20);
          var inRange = Prismic.Predicates.inRange("my.product.price", 10, 20);
  
          // Accessing number fields
          var price = doc.getNumber("product.price");
          assert.equal(price, 2.5);
          done();
        });
      });
    });
  
    it('prismic-images.js', function (done) {
      Prismic.api('https://lesbonneschoses.prismic.io/api', function (err, Api) {
        if (err) throw err;
        Api.form('everything').query(Prismic.Predicates.at("document.id", "UlfoxUnM0wkXYXbO")).ref(Api.master()).submit(function (err, documents) {
          if (err) throw err;
          var doc = documents.results[0];
          // Accessing image fields
          var image = doc.getImage("product.image");
          // Most of the time you will be using the "main" view
          var url = image.main.url;
          assert.equal(url, "https://prismic-io.s3.amazonaws.com/lesbonneschoses/f606ad513fcc2a73b909817119b84d6fd0d61a6d.png");
          done();
        });
      });
    });
  
    it('prismic-dateTimestamp.js', function (done) {
      Prismic.api('https://lesbonneschoses.prismic.io/api', function (err, Api) {
        if (err) throw err;
        Api.form('everything').query(Prismic.Predicates.at("document.id", "UlfoxUnM0wkXYXbl")).ref(Api.master()).submit(function (err, documents) {
          if (err) throw err;
          var doc = documents.results[0];
          var Predicates = Prismic.Predicates;
          // Date and Timestamp predicates
          var dateBefore = Predicates.dateBefore("my.product.releaseDate", new Date(2014, 6, 1));
          var dateAfter = Predicates.dateAfter("my.product.releaseDate", new Date(2014, 1, 1));
          var dateBetween = Predicates.dateBetween("my.product.releaseDate", new Date(2014, 1, 1), new Date(2014, 6, 1));
          var dayOfMonth = Predicates.dayOfMonth("my.product.releaseDate", 14);
          var dayOfMonthAfter = Predicates.dayOfMonthAfter("my.product.releaseDate", 14);
          var dayOfMonthBefore = Predicates.dayOfMonthBefore("my.product.releaseDate", 14);
          var dayOfWeek = Predicates.dayOfWeek("my.product.releaseDate", "Tuesday");
          var dayOfWeekAfter = Predicates.dayOfWeekAfter("my.product.releaseDate", "Wednesday");
          var dayOfWeekBefore = Predicates.dayOfWeekBefore("my.product.releaseDate", "Wednesday");
          var month = Predicates.month("my.product.releaseDate", "June");
          var monthBefore = Predicates.monthBefore("my.product.releaseDate", "June");
          var monthAfter = Predicates.monthAfter("my.product.releaseDate", "June");
          var year = Predicates.year("my.product.releaseDate", 2014);
          var hour = Predicates.hour("my.product.releaseDate", 12);
          var hourBefore = Predicates.hourBefore("my.product.releaseDate", 12);
          var hourAfter = Predicates.hourAfter("my.product.releaseDate", 12);
  
          // Accessing Date and Timestamp fields
          var date = doc.getDate("blog-post.date");
          var resultYear = date ? date.getFullYear() : null;
          var updateTime = doc.getTimestamp("blog-post.update");
          var updateHour = updateTime ? updateTime.getHours() : 0;
          assert.equal(resultYear, 2013); // gisthide
          done();
        });
      });
    });
  
    it('prismic-group.js', function () {
      var doc = Prismic.parseDoc({
        id: "abcd",
        type: "article",
        data: {
          article: {
            documents: {
              type: "Group",
              "value": [{
                "linktodoc": {
                  "type": "Link.document",
                  "value": {
                    "document": {
                      "id": "UrDejAEAAFwMyrW9",
                      "type": "doc",
                      "tags": [ ],
                      "slug": "installing-meta-micro"
                    },
                    "isBroken": false
                  }
                },
                "desc": {
                  "type": "StructuredText",
                  "value": [{
                    "type": "paragraph",
                    "text": "A detailed step by step point of view on how installing happens.",
                    "spans": []
                  }]
                }
              }, {
                "linktodoc": {
                  "type": "Link.document",
                  "value": {
                    "document": {
                      "id": "UrDmKgEAALwMyrXA",
                      "type": "doc",
                      "tags": [ ],
                      "slug": "using-meta-micro"
                    },
                    "isBroken": false
                  }
                }
              }]
            }
          }
        }
      });
      var group = doc.getGroup("article.documents");
      var docs = group ? group.toArray() : [];
      for (var i = 0; i < docs.length; i++) {
        // Desc and Link are Fragments, their type depending on what's declared in the Document Mask
        var desc = docs[i].getStructuredText("desc");
        var link = docs[i].getLink("linktodoc");
      }
      assert.equal(
        docs[0].getStructuredText("desc").asHtml(),
        "<p>A detailed step by step point of view on how installing happens.</p>"
      );
    });
  
    it('prismic-link.js', function () {
      var doc = Prismic.parseDoc({
        id: "abcd",
        type: "article",
        data: {
          article: {
            source: {
              type: "Link.document",
              value: {
                document: {
                  id: "UlfoxUnM0wkXYXbE",
                  type: "product",
                  tags: ["Macaron"],
                  slug: "dark-chocolate-macaron"
                },
                isBroken: false
              }
            }
          }
        }
      });
      var resolver = function (doc, isBroken) {
        if (isBroken) return '#broken';
        return "/testing_url/" + doc.id + "/" + doc.slug;
      };
      var source = doc.getLink("article.source");
      var url = source ? source.url(resolver) : null;
      assert.equal(url, "/testing_url/UlfoxUnM0wkXYXbE/dark-chocolate-macaron");
    });
  
    it('prismic-embed.js', function () {
      var doc = Prismic.parseDoc({
        id: "abcd",
        type: "article",
        data: {
          article: {
            "video": {
              "type": "Embed",
              "value": {
                "oembed": {
                  "provider_url": "http://www.youtube.com/",
                  "type": "video",
                  "thumbnail_height": 360,
                  "height": 270,
                  "thumbnail_url": "http://i1.ytimg.com/vi/baGfM6dBzs8/hqdefault.jpg",
                  "width": 480,
                  "provider_name": "YouTube",
                  "html": "<iframe width=\"480\" height=\"270\" src=\"http://www.youtube.com/embed/baGfM6dBzs8?feature=oembed\" frameborder=\"0\" allowfullscreen></iframe>",
                  "author_name": "Siobhan Wilson",
                  "version": "1.0",
                  "author_url": "http://www.youtube.com/user/siobhanwilsonsongs",
                  "thumbnail_width": 480,
                  "title": "Siobhan Wilson - All Dressed Up",
                  "embed_url": "https://www.youtube.com/watch?v=baGfM6dBzs8"
                }
              }
            }
          }
        }
      });
      var video = doc.get("article.video");
      // Html is the code to include to embed the object, and depends on the embedded service
      var html = video ? video.asHtml() : "";
      assert.equal(html, "<iframe width=\"480\" height=\"270\" src=\"http://www.youtube.com/embed/baGfM6dBzs8?feature=oembed\" frameborder=\"0\" allowfullscreen></iframe>"
                  );
    });
  
    it('prismic-color.js', function () {
      var doc = Prismic.parseDoc({
        id: "abcd",
        type: "article",
        data: {
          article: {
            "background": {
              "type": "Color",
              "value": "#000000"
            }
          }
        }
      });
      var $ = function (x) {
        return { css: function (y, z) {
        } };
      };
      var bgcolor = doc.getColor("article.background");
      $("#article").css("background-color", bgcolor);
      assert.equal(bgcolor, "#000000");
    });
  
    it('prismic-geopoint.js', function () {
      var doc = Prismic.parseDoc({
        id: "abcd",
        type: "article",
        data: {
          article: {
            "location": {
              "type": "GeoPoint",
              "value": {
                "latitude": 48.877108,
                "longitude": 2.3338790
              }
            }
          }
        }
      });
      // "near" predicate for GeoPoint fragments
      var near = Prismic.Predicates.near("my.store.location", 48.8768767, 2.3338802, 10);
  
      // Accessing GeoPoint fragments
      var place = doc.getGeoPoint("article.location");
      var coordinates;
      if (place) {
        coordinates = place.latitude + "," + place.longitude;
      }
      assert.equal(coordinates, "48.877108,2.333879");
    });
  
    it('prismic-asHtml.js', function (done) {
      Prismic.api('https://lesbonneschoses.cdn.prismic.io/api', function (err, Api) {
        if (err) {
          console.log(err);
          done();
        }
        Api.form('everything').ref(Api.master()).query(Prismic.Predicates.at("document.id", "UlfoxUnM0wkXYXbX")).submit(function (err, response) {
          if (err) {
            console.log(err);
            done();
          }
          var doc = response.results[0];
          var html = doc.getStructuredText('blog-post.body').asHtml({
            linkResolver: function (ctx, doc, isBroken) {
              if (isBroken) return '#broken';
              return "/testing_url/" + doc.id + "/" + doc.slug + ( ctx.maybeRef ? '?ref=' + ctx.maybeRef : '' );
            }
          });
          assert.equal(
            html,
            '<h1>Get the right approach to ganache</h1><p>A lot of people touch base with us to know about one of our key ingredients, and the essential role it plays in our creations: ganache.</p><p>Indeed, ganache is the macaron\'s softener, or else, macarons would be but tough biscuits; it is the cupcake\'s wrapper, or else, cupcakes would be but plain old cake. We even sometimes use ganache within our cupcakes, to soften the cake itself, or as a support to our pies\' content.</p><h2>How to approach ganache</h2><p class=\"block-img\"><img src=\"https://d2aw36oac6sa9o.cloudfront.net/lesbonneschoses/ee7b984b98db4516aba2eabd54ab498293913c6c.jpg\" alt=\"\"></p><p>Apart from the taste balance, which is always a challenge when it comes to pastry, the tough part about ganache is about thickness. It is even harder to predict through all the phases the ganache gets to meet (how long will it get melted? how long will it remain in the fridge?). Things get a hell of a lot easier to get once you consider that there are two main ways to get the perfect ganache:</p><ul><li><strong>working from the top down</strong>: start with a thick, almost hard material, and soften it by manipulating it, or by mixing it with a more liquid ingredient (like milk)</li><li><strong>working from the bottom up</strong>: start from a liquid-ish state, and harden it by miwing it with thicker ingredients, or by leaving it in the fridge longer.</li></ul><p>We do hope this advice will empower you in your ganache-making skills. Let us know how you did with it!</p><h2>Ganache at <em>Les Bonnes Choses</em></h2><p>We have a saying at Les Bonnes Choses: \"Once you can make ganache, you can make anything.\"</p><p>As you may know, we like to give our workshop artists the ability to master their art to the top; that is why our Preparation Experts always start off as being Ganache Specialists for Les Bonnes Choses. That way, they\'re given an opportunity to focus on one exercise before moving on. Once they master their ganache, and are able to provide the most optimal delight to our customers, we consider they\'ll thrive as they work on other kinds of preparations.</p><h2>About the chocolate in our ganache</h2><p>Now, we\'ve also had a lot of questions about how our chocolate gets made. It\'s true, as you might know, that we make it ourselves, from Columbian cocoa and French cow milk, with a process that much resembles the one in the following Discovery Channel documentary.</p><div data-oembed=\"undefined\" data-oembed-type=\"embed\" data-oembed-provider=\"undefined\"><iframe width=\"459\" height=\"344\" src=\"http://www.youtube.com/embed/Ye78F3-CuXY?feature=oembed\" frameborder=\"0\" allowfullscreen></iframe></div>'
          );
          done();
        });
      });
    });
  
  });
  
  describe('Cache', function() {
  
    it('prismic-cache.js', function(done) {
      var cache = {
        get: function (key, cb) {
          // Retrieve a value from the key
          return cb();
        },
  
        set: function (key, value, ttl, cb) {
          return cb();
        },
  
        remove: function (key, cb) {
          // Remove a value
          return cb();
        },
  
        clear: function (key, cb) {
          return cb();
        }
      };
  
      Prismic.api('https://lesbonneschoses.prismic.io/api', function (err, Api) {
        if (err) { done(err); return; } // gisthide
        // The Api in this block will use the custom cache object
        assert.notEqual(Api, null); // gisthide
        done(); // gisthide
      }, null, null, cache);
    });
  
  });
  