var reduce = require('../../lib/index').reduce;

var should = require('chai').should();

describe('reduce.latest', function () {
    var resource = {
        resourceType: 'Bundle',
        entry: [
            {
                id: 1,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '2469' }
                    ]},
                    issued: '2012-03-08T09:00:00.000Z'
                }
            },            {
                id: 2,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '246A' }
                    ]},
                    issued: '2012-03-08T09:00:00.000Z'
                }
            },
            {
                id: 3,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '2469' }
                    ]},
                    issued: '2014-03-08T09:00:00.000Z'
                }
            },
            {
                id: 4,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '246A' }
                    ]},
                    issued: '2014-03-08T09:00:00.000Z'
                }
            },
            {
                id: 5,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '2469' }
                    ]},
                    issued: '2010-03-08T09:00:00.000Z'
                }
            },
            {
                id: 6,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '246A' }
                    ]},
                    issued: '2010-03-08T09:00:00.000Z'
                }
            },
            {
                id: 7,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '2469' }
                    ]},
                    issued: '2008-03-08T09:00:00.000Z'
                }
            },
            {
                id: 8,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '246A' }
                    ]},
                    issued: '2008-03-08T09:00:00.000Z'
                }
            }
        ]
    };

    it('should return a filtered list of latest Observations and Conditions', function () {
        var result = resource.entry.reduce(reduce.latest());

        should.exist(result);
        result.length.should.equal(2);
        result[0].content.issued.should.equal('2014-03-08T09:00:00.000Z');
        result[1].content.issued.should.equal('2014-03-08T09:00:00.000Z');
    });
});

