'use strict';

const test = require('ava');
const TTLList = require('./ttl-list');
const moment = require('moment');

test.cb('should emit expire', (t) => {
    const list = new TTLList({
        interval: 20,
        expireAmount: 10,
        expireType: 'milliseconds',
    });

    list.on('expire', (entry) => {
        list.destroy();
        t.true(entry.n === 1);
        t.end();
    });

    list.add({ n: 1 });
});

test.cb('should slice off list', (t) => {
    const list = new TTLList({
        interval: 10,
        expireAmount: 10,
        expireType: 'milliseconds',
    });


    list.add({ n: '1' }, moment().add(1, 'milliseconds'));
    list.add({ n: '2' }, moment().add(50, 'milliseconds'));
    list.add({ n: '3' }, moment().add(200, 'milliseconds'));
    list.add({ n: '4' }, moment().add(300, 'milliseconds'));

    const expired = [];

    list.on('expire', (entry) => {
        // console.timeEnd(entry.n);
        expired.push(entry);
    });

    setTimeout(() => {
        t.true(expired.length === 1);
    }, 30);
    setTimeout(() => {
        t.true(expired.length === 2);
    }, 71);
    setTimeout(() => {
        t.true(expired.length === 3);
    }, 221);
    setTimeout(() => {
        t.true(expired.length === 4);
        list.destroy();
        t.end();
    }, 330);
});

@@ -109,16 +109,48 @@
 oment');
+%0Aconst sinon = require('sinon');
 %0A%0Atest.c
@@ -485,32 +485,74 @@
 list', (t) =%3E %7B%0A
+    const clock = sinon.useFakeTimers();%0A%0A
     const list =
@@ -1045,35 +1045,28 @@
     
-setTimeout(() =%3E %7B%0A 
+clock.tick(21);%0A
     
-   
 t.tr
@@ -1095,198 +1095,145 @@
 1);%0A
-    %7D, 30);%0A    setTimeout(() =%3E %7B%0A        t.true(expired.length === 2);%0A    %7D, 71);%0A    setTimeout(() =%3E %7B%0A        t.true(expired.length === 3);%0A    %7D, 221);%0A    setTimeout(() =%3E %7B%0A   
+%0A    clock.tick(51);%0A    t.true(expired.length === 2);%0A%0A    clock.tick(201);%0A    t.true(expired.length === 3);%0A%0A    clock.tick(301);%0A
     
- 
 t.tr
@@ -1258,20 +1258,17 @@
 === 4);%0A
-    
+%0A
     list
@@ -1287,31 +1287,57 @@
     
-    t.end
+clock.restore();%0A    sinon.restore
 ();%0A
+%0A
     
-%7D, 330
+t.end(
 );%0A%7D

use fake timers for ttl test