<commit_before>import { delay } from '../src/main';
import { Future, never, reject, fulfill } from '../src/Promise';
import { silenceError, isNever, isRejected, isPending, getValue } from '../src/inspect';
import { assertSame } from './lib/test-util';
import assert from 'assert';

describe('delay', function() {

    it('should be identity for 0 ms', () => {
        let p = fulfill();
        assert.strictEqual(p, delay(0, p));
    });

    it('should be identity for rejected', () => {
        let p = reject();
        silenceError(p);
        assert.strictEqual(p, delay(1, p));
    });

    it('should not delay rejected', () => {
        let p = new Future();
        let d = delay(1, p);

        assert(isPending(d));

        let x = {};
        p._reject(x);

        return d.then(assert.ifError, e => assert.strictEqual(x, e));
    });

    it('should return never for never', () => {
        assert(isNever(delay(0, never())));
    });

    it('should delay value', () => {
        let x = {};
        let t = 10;
        let p = delay(t, x);

        let now = Date.now();
        return assertSame(fulfill(x), p)
            .then(() => assert(Date.now() - now >= t));
    });

    it('should delay fulfilled', () => {
        let x = {};
        let t = 10;
        let p = delay(t, fulfill(x));

        let now = Date.now();
        return assertSame(fulfill(x), p)
            .then(() => assert(Date.now() - now >= t));
    });
});<commit_msg>Add epsilon to time comparisons, since JS clock is inaccurate<commit_after>import { delay } from '../src/main';
import { Future, never, reject, fulfill } from '../src/Promise';
import { silenceError, isNever, isRejected, isPending, getValue } from '../src/inspect';
import { assertSame } from './lib/test-util';
import assert from 'assert';

function lte(a, b) {
    return (a - 1) <= b;
}

describe('delay', function() {

    it('should be identity for 0 ms', () => {
        let p = fulfill();
        assert.strictEqual(p, delay(0, p));
    });

    it('should be identity for rejected', () => {
        let p = reject();
        silenceError(p);
        assert.strictEqual(p, delay(1, p));
    });

    it('should not delay rejected', () => {
        let p = new Future();
        let d = delay(1, p);

        assert(isPending(d));

        let x = {};
        p._reject(x);

        return d.then(assert.ifError, e => assert.strictEqual(x, e));
    });

    it('should return never for never', () => {
        assert(isNever(delay(0, never())));
    });

    it('should delay value', () => {
        let x = {};
        let t = 10;
        let p = delay(t, x);

        let now = Date.now();
        return assertSame(fulfill(x), p)
            .then(() => assert(lte(t, Date.now() - now)));
    });

    it('should delay fulfilled', () => {
        let x = {};
        let t = 10;
        let p = delay(t, fulfill(x));

        let now = Date.now();
        return assertSame(fulfill(x), p)
            .then(() => assert(lte(t, Date.now() - now)));
    });
});
{'fraction_non_alphanumeric': 0.18793273986152326, 'fraction_numerical': 0.006264424662050775, 'mean_word_length': 2.1375387797311274, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 23, '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': '2052530', 'n_tokens_mistral': 968, 'n_tokens_neox': 933, 'n_words': 266}