text: const unitUnderTest = require("../../src/index");
  const utilities = require("../../src/utilities");
  
  const expect = require("chai").expect;
  const importFresh = require('import-fresh');
  
  const context = {};
  
  describe("FindRestroomAtLocation API handler tests", function () {
    it("should delegate to skill with the right resolved search filters and zipcode (converted to string) loaded into session attributes", async () => {
      const eventWithAPL = importFresh("../../test-data/api/atlocation_api");
      const eventWithoutAPL = importFresh("../../test-data/api/atlocation_no_apl_api");
  
      const synonymsToIdMap = utilities.slotSynonymsToIdMap("RestRoomTypes");
      const allSynonyms = [...synonymsToIdMap.keys()];
  
      const events = [eventWithAPL, eventWithoutAPL];
      for (var index = 0; index < events.length; index++) {
        const event = events[index]
  
        for (var i = 0; i < 1000; i++) {
          // Simulate 0 to 3 search filters randomly.
          const randomTestSetSize = Math.floor(Math.random() * Math.floor(4));
          // Simulate a zipcode.
          const zipcode = 10000 + Math.floor(Math.random() * Math.floor(89999));
  
          const aTestSetOfSynonyms = getRandom(allSynonyms, randomTestSetSize);
  
          let expectedResolvedEntitiesSet = new Set();
          aTestSetOfSynonyms.forEach(synonym =>
            expectedResolvedEntitiesSet = expectedResolvedEntitiesSet.add(synonymsToIdMap.get(synonym))
          )
  
          event.request.apiRequest.arguments.SearchFiltersList = aTestSetOfSynonyms;
          event.request.apiRequest.arguments.Zipcode = zipcode;
  
          const responseContainer = await unitUnderTest.handler(event, context);
          const response = responseContainer.response;
          const directive = response.directives[0];
          expect(directive.type).to.equal(`Dialog.DelegateRequest`);
          expect(directive.target).to.equal(`skill`);
          expect(directive.period.until).to.equal(`EXPLICIT_RETURN`);
          expect(directive.updatedRequest.type).to.equal(`IntentRequest`);
          expect(directive.updatedRequest.intent.name).to.equal(`FindRestroomAtLocationIntent`);
  
          const actualResolvedEntitiesArray = context.search_filters;
          const expectedResolvedEntitiesArray = [...expectedResolvedEntitiesSet];
  
          expect(actualResolvedEntitiesArray).to.eql(expectedResolvedEntitiesArray);
  
          expect(context.zipcode).to.equal(zipcode.toString());
        }
      }
    });
  
    it("should be able to sanitize four digit zip codes. The US has zip codes like 07834 which will be given by Alexa as 7834. We should convert it back to a valid five digit zip code by prepending a '0'.", async () => {
      const fourDigitZipCode = 7834;
      const event = importFresh("../../test-data/api/atlocation_api");
      event.request.apiRequest.arguments.Zipcode = fourDigitZipCode;
  
      await unitUnderTest.handler(event, context);
  
      expect(context.zipcode).to.equal("07834");
    });
  
    it("should delegate to skill even when the zipcode is missing. We expiclity set a zipcode to null in the intent slots", async () => {
      const event = importFresh("../../test-data/api/atlocation_api");
  
      const synonymsToIdMap = utilities.slotSynonymsToIdMap("RestRoomTypes");
      const allSynonyms = [...synonymsToIdMap.keys()];
      const aTestSetOfSynonyms = getRandom(allSynonyms, 3);
  
      event.request.apiRequest.arguments.SearchFiltersList = aTestSetOfSynonyms;
      delete event.request.apiRequest.arguments.Zipcode;
  
      await unitUnderTest.handler(event, context);
  
      expect(context.zipcode).to.equal(null);
    });
  });
  
  /**
   * Helper method to fetch n random elements from an array.
   */
  function getRandom(array, n) {
    var result = new Array(n),
      len = array.length,
      taken = new Array(len);
    if (n > len)
      throw new RangeError("getRandom: more elements taken than available");
    while (n--) {
      var x = Math.floor(Math.random() * len);
      result[n] = array[x in taken ? taken[x] : x];
      taken[x] = --len in taken ? taken[len] : len;
    }
    return result;
  }