<commit_before>require('../support/test_helper');

var assert        = require('../support/assert');
var _             = require('underscore');
var redis         = require('redis');
var Windshaft     = require('../../lib/windshaft');
var ServerOptions = require('../support/server_options');

describe('multilayer interactivity and layers order', function() {

    var server = new Windshaft.Server(ServerOptions);
    var redisClient = redis.createClient(ServerOptions.redis.port);

    function layerType(layer) {
        return layer.type || 'undefined';
    }

    function testInteractivityLayersOrderScenario(testScenario) {
        it(testScenario.desc, function(done) {
            var layergroup =  {
                version: '1.3.0',
                layers: testScenario.layers
            };

            server.afterLayergroupCreateCalls = 0;

            assert.response(server,
                {
                    url: '/database/windshaft_test/layergroup',
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    data: JSON.stringify(layergroup)
                },
                {
                    //status: 200, don't use status here to have a more meaningful error message
                    headers: {
                        'content-type': 'application/json; charset=utf-8'
                    }
                },
                function(response) {
                    assert.equal(
                        response.statusCode,
                        200,
                            'Expected status code 200, got ' + response.statusCode +
                            '\n\tResponse body: ' + response.body +
                            '\n\tLayer types: ' + layergroup.layers.map(layerType).join(', ')
                    );

                    assert.equal(server.afterLayergroupCreateCalls, 1);

                    var layergroupResponse = JSON.parse(response.body);
                    assert.ok(layergroupResponse);

                    var layergroupId = layergroupResponse.layergroupid;
                    assert.ok(layergroupId);
                    assert.equal(layergroupResponse.layercount, layergroup.layers.length);

                    redisClient.exists("map_cfg|" +  layergroupId, function(err, exists) {
                        if (err) {
                            return done(err);
                        }
                        assert.ok(exists, "Missing expected token " + layergroupId + " from redis");
                        redisClient.del("map_cfg|" +  layergroupId, function(err) {
                            done(err);
                        });
                    });
                }
            );
        });
    }

    var cartocssVersion = '2.3.0';
    var cartocss = '#layer { line-width:16; }';
    var sql = "select 1 as i, st_setsrid('LINESTRING(0 0, 1 0)'::geometry, 4326) as the_geom";
    var sqlWadus = "select 1 as wadus, st_setsrid('LINESTRING(0 0, 1 0)'::geometry, 4326) as the_geom";

    var httpLayer = {
        type: 'http',<commit_msg>Tests layers metadata in multilayer suite<commit_after>require('../support/test_helper');

var assert        = require('../support/assert');
var _             = require('underscore');
var redis         = require('redis');
var Windshaft     = require('../../lib/windshaft');
var ServerOptions = require('../support/server_options');

describe('multilayer interactivity and layers order', function() {

    var server = new Windshaft.Server(ServerOptions);
    var redisClient = redis.createClient(ServerOptions.redis.port);

    function layerType(layer) {
        return layer.type || 'undefined';
    }

    function testInteractivityLayersOrderScenario(testScenario) {
        it(testScenario.desc, function(done) {
            var layergroup =  {
                version: '1.3.0',
                layers: testScenario.layers
            };

            server.afterLayergroupCreateCalls = 0;

            assert.response(server,
                {
                    url: '/database/windshaft_test/layergroup',
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    data: JSON.stringify(layergroup)
                },
                {
                    //status: 200, don't use status here to have a more meaningful error message
                    headers: {
                        'content-type': 'application/json; charset=utf-8'
                    }
                },
                function(response) {
                    assert.equal(
                        response.statusCode,
                        200,
                            'Expected status code 200, got ' + response.statusCode +
                            '\n\tResponse body: ' + response.body +
                            '\n\tLayer types: ' + layergroup.layers.map(layerType).join(', ')
                    );

                    assert.equal(server.afterLayergroupCreateCalls, 1);

                    var layergroupResponse = JSON.parse(response.body);
                    assert.ok(layergroupResponse);

                    var layergroupId = layergroupResponse.layergroupid;
                    assert.ok(layergroupId);
                    assert.equal(layergroupResponse.layercount, layergroup.layers.length);

                    // check layers metadata at least match in number
                    assert.equal(layergroupResponse.metadata.layers.length, layergroup.layers.length);
                    // check torque metadata at least match in number
                    var torqueLayers = layergroup.layers.filter(function(layer) { return layer.type === 'torque'; });
                    if (torqueLayers.length) {
                        assert.equal(Object.keys(layergroupResponse.metadata.torque).length, torqueLayers.length);
                    }

                    redisClient.exists("map_cfg|" +  layergroupId, function(err, exists) {
                        if (err) {
                            return done(err);
                        }
                        assert.ok(exists, "Missing expected token " + layergroupId + " from redis");
                        redisClient.del("map_cfg|" +  layergroupId, function(err) {
                            done(err);
                        });
                    });
                }
            );
        });
    }

    var cartocssVersion = '2.3.0';
    var cartocss = '#layer { line-width:16; }';
    var sql = "select 1 as i, st_setsrid('LINESTRING(0 0, 1 0)'::geometry, 4326) as the_geom";
    var sqlWadus = "select 1 as wadus, st_setsrid('LINESTRING(0 0, 1 0)'::geometry, 4326) as the_geom";

    var httpLayer = {
        type: 'http',
{'fraction_non_alphanumeric': 0.10644502770487023, 'fraction_numerical': 0.011081948089822106, 'mean_word_length': 1.419400352733686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '7239109', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1644, 'n_words': 344}