<commit_before>var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone');
var Model = require('../../../src/core/model');
var Map = require('../../../src/geo/map');
var TorqueLayer = require('../../../src/geo/map/torque-layer');
var CartoDBLayer = require('../../../src/geo/map/cartodb-layer');
var HistogramDataviewModel = require('../../../src/dataviews/histogram-dataview-model');
var WindshaftMap = require('../../../src/windshaft/windshaft-map');
var WindshaftClient = require('../../../src/windshaft/client');
var CategoryFilter = require('../../../src/windshaft/filters/category');

describe('windshaft/map', function () {
  beforeEach(function () {
    jasmine.clock().install();

    // Disable ajax and debounce for these tests
    spyOn($, 'ajax').and.callFake(function () {});
    spyOn(_, 'debounce').and.callFake(function (func) { return function () { func.apply(this, arguments); }; });

    this.windshaftMapInstance = {
      layergroupid: 'layergroupid',
      metadata: {
        layers: [
          {
            'type': 'mapnik',
            'meta': {},
            'widgets': {
              'dataviewId': {
                'url': {
                  'http': 'http://example.com',
                  'https': 'https://example.com'
                }
              }
            }
          }
        ]
      }
    };
    this.dataviews = new Backbone.Collection();

    this.client = new WindshaftClient({
      endpoint: 'v1',
      urlTemplate: 'http://{user}.wadus.com',
      userName: 'rambo'
    });
    spyOn(this.client, 'instantiateMap').and.callFake(function (options) {
      options.success(this.windshaftMapInstance);
    }.bind(this));

    this.configGenerator = {
      generate: function () {}
    };

    this.map = new Map({
      view_bounds_sw: [],
      view_bounds_ne: []
    });

    this.cartoDBLayerGroup = new Model();
    this.cartoDBLayer1 = new CartoDBLayer({ id: '12345-67890' });
    this.cartoDBLayer2 = new CartoDBLayer({ id: '09876-54321' });
    this.torqueLayer = new TorqueLayer();
  });

  afterEach(function () {
    jasmine.clock().uninstall();
  });

  describe('createInstance', function () {
    beforeEach(function () {
      this.windshaftMap = new WindshaftMap(null, { // eslint-disable-line
        client: this.client,
        configGenerator: this.configGenerator,
        statTag: 'stat_tag'
      });

      var filter = new CategoryFilter({
        layer: this.cartoDBLayer1
      });
      spyOn(filter, 'isEmpty').and.returnValue(false);
      spyOn(filter, 'toJSON').and.returnValue({ accept: 'category' });

      var dataview = new HistogramDataviewModel({
        id: 'dataviewId',
        type: 'list'
      }, {
        map: this.map,
        windshaftMap: this.windshaftMap,
        layer: this.cartoDBLayer1,
        filter: filter
      });

      this.dataviews.add(dataview);
    });

    it('should create an instance of the windshaft map', function () {
      spyOn(this.configGenerator, 'generate').and.returnValue({ foo: 'bar' });

      this.windshaftMap.createInstance({
        layers: [ this.cartoDBLayer1, this.cartoDBLayer2, this.torqueLayer ],
        dataviews: this.dataviews,
        sourceLayerId: 'sourceLayerId'
      });

      var args = this.client.instantiateMap.calls.mostRecent().args[0];
      expect(args.mapDefinition).toEqual({ foo: 'bar' });
      expect(args.statTag).toEqual('stat_tag');
      expect(args.filters).toEqual({ layers: [{ accept: 'category' }, {}, {}] });
    });

    it('should not send filters linked to hidden layers', function () {
      // Hide this.cartoDBLayer1 -> Filters of that layer should be ignored
      this.cartoDBLayer1.set('visible', false, { silent: true });

      this.windshaftMap.createInstance({
        layers: [ this.cartoDBLayer1, this.cartoDBLayer2, this.torqueLayer ],
        dataviews: this.dataviews,
        sourceLayerId: 'sourceLayerId'
      });

      var args = this.client.instantiateMap.calls.mostRecent().args[0];
      expect(args.filters).toEqual({ });
    });

    it('should trigger an event when the instance is created', function () {
      var onWindshaftInstanceCreated = jasmine.createSpy('callback');
      this.windshaftMap.bind('instanceCreated', onWindshaftInstanceCreated);

      this.windshaftMap.createInstance({
        layers: [ this.cartoDBLayer1, this.cartoDBLayer2, this.torqueLayer ],
        dataviews: this.dataviews,
        sourceLayerId: 'sourceLayerId'
      });

      expect(onWindshaftInstanceCreated).toHaveBeenCalledWith(this.windshaftMap, 'sourceLayerId', undefined);
    });

    it('should set the attributes of the new instance', function () {
      this.windshaftMap.createInstance({
        layers: [ this.cartoDBLayer1, this.cartoDBLayer2, this.torqueLayer ],
        dataviews: this.dataviews,
        sourceLayerId: 'sourceLayerId'
      });

      expect(this.windshaftMap.get('layergroupid')).toEqual('layergroupid');
      expect(this.windshaftMap.get('metadata')).toEqual(this.windshaftMapInstance.metadata);
    });

    describe('metadata', function () {
      beforeEach(function () {
        this.dataviews = new Backbone.Collection();

        this.client = new WindshaftClient({
          endpoint: 'v1',
          urlTemplate: 'http://{user}.wadus.com',
          userName: 'rambo'
        });
        spyOn(this.client, 'instantiateMap').and.callFake(function (options) {
          options.success(this.windshaftMapInstance);
        }.bind(this));
      });

      describe("when there's a tiled layer as the first layer of the Windshaft response (Named Map)", function () {
        beforeEach(function () {
          this.windshaftMapInstance = {
            layergroupid: 'layergroupid',
            metadata: {
              layers: [
                {
                  'type': 'tiled'
                },
                {
                  'type': 'mapnik',
                  'meta': 'cartodb-metadata',
                  'widgets': {
                    'dataviewId': {
                      'url': {
                        'http': 'http://example.com',
                        'https': 'https://example.com'
                      }
                    }
                  }
                },
                {
                  'type': 'torque',
                  'meta': 'torque-metadata'
                }
              ]
            }
          };
        });

        it('should set the meta attribute of CartoDB layers from the Windshaft response', function () {
          this.windshaftMap.createInstance({
            layers: [ this.cartoDBLayer1, this.torqueLayer ],
            dataviews: this.dataviews,
            sourceLayerId: 'sourceLayerId'
          });

          // 'meta' attribute of the CartoDB layer has been set
          expect(this.cartoDBLayer1.get('meta')).toEqual('cartodb-metadata');
        });

        it('should set the meta and url attributes of Torque layers from the Windshaft response', function () {
          this.windshaftMap.createInstance({
            layers: [ this.cartoDBLayer1, this.torqueLayer ],
            dataviews: this.dataviews,
            sourceLayerId: 'sourceLayerId'
          });

          // 'meta' and 'urls' of the Torque layer has been set
          expect(this.torqueLayer.get('meta')).toEqual('torque-metadata');
          expect(this.torqueLayer.get('urls')).toEqual({
            tiles: [
              'http://rambo.wadus.com/api/v1/map/layergroupid/2/{z}/{x}/{y}.json.torque',
              'http://rambo.wadus.com/api/v1/map/layergroupid/2/{z}/{x}/{y}.json.torque',
              'http://rambo.wadus.com/api/v1/map/layergroupid/2/{z}/{x}/{y}.json.torque',
              'http://rambo.wadus.com/api/v1/map/layergroupid/2/{z}/{x}/{y}.json.torque'
            ],
            grids: []
          });
        });
      });

      describe("when there isn't a tiled layer as the first layer of the Windshaft response (Named Map)", function () {
        beforeEach(function () {
          this.windshaftMapInstance = {
            layergroupid: 'layergroupid',
            metadata: {
              layers: [
                {
                  'type': 'mapnik',
                  'meta': 'cartodb-metadata',
                  'widgets': {
                    'dataviewId': {
                      'url': {
                        'http': 'http://example.com',<commit_msg>Fixed typo in test description<commit_after>var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone');
var Model = require('../../../src/core/model');
var Map = require('../../../src/geo/map');
var TorqueLayer = require('../../../src/geo/map/torque-layer');
var CartoDBLayer = require('../../../src/geo/map/cartodb-layer');
var HistogramDataviewModel = require('../../../src/dataviews/histogram-dataview-model');
var WindshaftMap = require('../../../src/windshaft/windshaft-map');
var WindshaftClient = require('../../../src/windshaft/client');
var CategoryFilter = require('../../../src/windshaft/filters/category');

describe('windshaft/map', function () {
  beforeEach(function () {
    jasmine.clock().install();

    // Disable ajax and debounce for these tests
    spyOn($, 'ajax').and.callFake(function () {});
    spyOn(_, 'debounce').and.callFake(function (func) { return function () { func.apply(this, arguments); }; });

    this.windshaftMapInstance = {
      layergroupid: 'layergroupid',
      metadata: {
        layers: [
          {
            'type': 'mapnik',
            'meta': {},
            'widgets': {
              'dataviewId': {
                'url': {
                  'http': 'http://example.com',
                  'https': 'https://example.com'
                }
              }
            }
          }
        ]
      }
    };
    this.dataviews = new Backbone.Collection();

    this.client = new WindshaftClient({
      endpoint: 'v1',
      urlTemplate: 'http://{user}.wadus.com',
      userName: 'rambo'
    });
    spyOn(this.client, 'instantiateMap').and.callFake(function (options) {
      options.success(this.windshaftMapInstance);
    }.bind(this));

    this.configGenerator = {
      generate: function () {}
    };

    this.map = new Map({
      view_bounds_sw: [],
      view_bounds_ne: []
    });

    this.cartoDBLayerGroup = new Model();
    this.cartoDBLayer1 = new CartoDBLayer({ id: '12345-67890' });
    this.cartoDBLayer2 = new CartoDBLayer({ id: '09876-54321' });
    this.torqueLayer = new TorqueLayer();
  });

  afterEach(function () {
    jasmine.clock().uninstall();
  });

  describe('createInstance', function () {
    beforeEach(function () {
      this.windshaftMap = new WindshaftMap(null, { // eslint-disable-line
        client: this.client,
        configGenerator: this.configGenerator,
        statTag: 'stat_tag'
      });

      var filter = new CategoryFilter({
        layer: this.cartoDBLayer1
      });
      spyOn(filter, 'isEmpty').and.returnValue(false);
      spyOn(filter, 'toJSON').and.returnValue({ accept: 'category' });

      var dataview = new HistogramDataviewModel({
        id: 'dataviewId',
        type: 'list'
      }, {
        map: this.map,
        windshaftMap: this.windshaftMap,
        layer: this.cartoDBLayer1,
        filter: filter
      });

      this.dataviews.add(dataview);
    });

    it('should create an instance of the windshaft map', function () {
      spyOn(this.configGenerator, 'generate').and.returnValue({ foo: 'bar' });

      this.windshaftMap.createInstance({
        layers: [ this.cartoDBLayer1, this.cartoDBLayer2, this.torqueLayer ],
        dataviews: this.dataviews,
        sourceLayerId: 'sourceLayerId'
      });

      var args = this.client.instantiateMap.calls.mostRecent().args[0];
      expect(args.mapDefinition).toEqual({ foo: 'bar' });
      expect(args.statTag).toEqual('stat_tag');
      expect(args.filters).toEqual({ layers: [{ accept: 'category' }, {}, {}] });
    });

    it('should not send filters linked to hidden layers', function () {
      // Hide this.cartoDBLayer1 -> Filters of that layer should be ignored
      this.cartoDBLayer1.set('visible', false, { silent: true });

      this.windshaftMap.createInstance({
        layers: [ this.cartoDBLayer1, this.cartoDBLayer2, this.torqueLayer ],
        dataviews: this.dataviews,
        sourceLayerId: 'sourceLayerId'
      });

      var args = this.client.instantiateMap.calls.mostRecent().args[0];
      expect(args.filters).toEqual({ });
    });

    it('should trigger an event when the instance is created', function () {
      var onWindshaftInstanceCreated = jasmine.createSpy('callback');
      this.windshaftMap.bind('instanceCreated', onWindshaftInstanceCreated);

      this.windshaftMap.createInstance({
        layers: [ this.cartoDBLayer1, this.cartoDBLayer2, this.torqueLayer ],
        dataviews: this.dataviews,
        sourceLayerId: 'sourceLayerId'
      });

      expect(onWindshaftInstanceCreated).toHaveBeenCalledWith(this.windshaftMap, 'sourceLayerId', undefined);
    });

    it('should set the attributes of the new instance', function () {
      this.windshaftMap.createInstance({
        layers: [ this.cartoDBLayer1, this.cartoDBLayer2, this.torqueLayer ],
        dataviews: this.dataviews,
        sourceLayerId: 'sourceLayerId'
      });

      expect(this.windshaftMap.get('layergroupid')).toEqual('layergroupid');
      expect(this.windshaftMap.get('metadata')).toEqual(this.windshaftMapInstance.metadata);
    });

    describe('metadata', function () {
      beforeEach(function () {
        this.dataviews = new Backbone.Collection();

        this.client = new WindshaftClient({
          endpoint: 'v1',
          urlTemplate: 'http://{user}.wadus.com',
          userName: 'rambo'
        });
        spyOn(this.client, 'instantiateMap').and.callFake(function (options) {
          options.success(this.windshaftMapInstance);
        }.bind(this));
      });

      describe("when there's a tiled layer as the first layer of the Windshaft response (Named Map)", function () {
        beforeEach(function () {
          this.windshaftMapInstance = {
            layergroupid: 'layergroupid',
            metadata: {
              layers: [
                {
                  'type': 'tiled'
                },
                {
                  'type': 'mapnik',
                  'meta': 'cartodb-metadata',
                  'widgets': {
                    'dataviewId': {
                      'url': {
                        'http': 'http://example.com',
                        'https': 'https://example.com'
                      }
                    }
                  }
                },
                {
                  'type': 'torque',
                  'meta': 'torque-metadata'
                }
              ]
            }
          };
        });

        it('should set the meta attribute of CartoDB layers from the Windshaft response', function () {
          this.windshaftMap.createInstance({
            layers: [ this.cartoDBLayer1, this.torqueLayer ],
            dataviews: this.dataviews,
            sourceLayerId: 'sourceLayerId'
          });

          // 'meta' attribute of the CartoDB layer has been set
          expect(this.cartoDBLayer1.get('meta')).toEqual('cartodb-metadata');
        });

        it('should set the meta and url attributes of Torque layers from the Windshaft response', function () {
          this.windshaftMap.createInstance({
            layers: [ this.cartoDBLayer1, this.torqueLayer ],
            dataviews: this.dataviews,
            sourceLayerId: 'sourceLayerId'
          });

          // 'meta' and 'urls' of the Torque layer has been set
          expect(this.torqueLayer.get('meta')).toEqual('torque-metadata');
          expect(this.torqueLayer.get('urls')).toEqual({
            tiles: [
              'http://rambo.wadus.com/api/v1/map/layergroupid/2/{z}/{x}/{y}.json.torque',
              'http://rambo.wadus.com/api/v1/map/layergroupid/2/{z}/{x}/{y}.json.torque',
              'http://rambo.wadus.com/api/v1/map/layergroupid/2/{z}/{x}/{y}.json.torque',
              'http://rambo.wadus.com/api/v1/map/layergroupid/2/{z}/{x}/{y}.json.torque'
            ],
            grids: []
          });
        });
      });

      describe("when there isn't a tiled layer as the first layer of the Windshaft response (Anonymous Map)", function () {
        beforeEach(function () {
          this.windshaftMapInstance = {
            layergroupid: 'layergroupid',
            metadata: {
              layers: [
                {
                  'type': 'mapnik',
                  'meta': 'cartodb-metadata',
                  'widgets': {
                    'dataviewId': {
                      'url': {
                        'http': 'http://example.com',
{'fraction_non_alphanumeric': 0.14926082051890993, 'fraction_numerical': 0.005818440895327436, 'mean_word_length': 2.0475845847656955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5888528', 'n_tokens_mistral': 5101, 'n_tokens_neox': 4981, 'n_words': 861}