var _ = require('underscore');
var expect = require('chai').expect;

var test_utils = require('./elastic-test-utils');

var modes = test_utils.modes;

function generate_live_data() {
    return test_utils.generate_sample_data({
        start: Date.now() + 2000
    });
}

describe('elastic source', function() {
    modes.forEach(function(type) {
        describe('live reads -- ' + type, function() {
            afterEach(function() {
                return test_utils.clear_data(type);
            });

            function test_live(points_to_write, points_to_expect) {
                points_to_expect = points_to_expect || points_to_write;
                var last_time = new Date(_.last(points_to_write).time).getTime();
                var deactivateAfter = last_time - Date.now() + 5000;
                var options = {id: type, from: 0, to: 'end', lag: '2s'};

                var read = test_utils.read(options, '', deactivateAfter)
                .then(function(result) {
                    expect(result.sinks.table).deep.equal(points_to_expect);
                });

                return test_utils.write(points_to_write, {id: type})
                .then(function(result) {
                    expect(result.errors).deep.equal([]);
                    return test_utils.verify_import(points_to_write, type);
                })
                .then(function() {
                    return read;
                });
            }

            it('reads live points', function() {
                var points = generate_live_data();

                return test_live(points);
            });

            it('superquery', function() {
                var historical = test_utils.generate_sample_data({
                    tags: {name: ['historical']}
                });

                return test_utils.write(historical, {id: type})
                    .then(function(result) {
                        expect(result.errors).deep.equal([]);
                        return test_utils.verify_import(historical, type);
                    })
                    .then(function() {
                        var live = generate_live_data();
                        return test_live(live, historical.concat(live));
                    });
            });
        });
    });
});

@@ -253,17 +253,73 @@
 ) + 2000
+,%0A        tags: %7B%0A            name: %5B'a', 'b'%5D%0A        %7D
 %0A
-
     %7D);%0A
@@ -619,16 +619,23 @@
 o_expect
+, extra
 ) %7B%0A    
@@ -702,16 +702,53 @@
 _write;%0A
+                extra = extra %7C%7C '';%0A
         
@@ -1016,18 +1016,21 @@
 ptions, 
-''
+extra
 , deacti
@@ -1642,24 +1642,24 @@
 ve_data();%0A%0A
-
             
@@ -1697,32 +1697,354 @@
            %7D);%0A%0A
+            it('reads live points with a filter', function() %7B%0A                var points = generate_live_data();%0A                var expected = points.filter(function(pt) %7B%0A                    return pt.name === 'a';%0A                %7D);%0A%0A                return test_live(points, expected, 'name = %22a%22');%0A            %7D);%0A%0A
             it('

test with filter