<commit_before>"use strict";

const assert = require('../utils/assert.js');
const server = require('../utils/server.js');
const preq   = require('preq');
const P = require('bluebird');

function assertStorageRequest(requests, method, bucket, expected) {
    const storageRequests = requests.filter((log) =>
        log.req && log.req.uri === `/en.wikipedia.org/sys/table/${bucket}/`
            && log.req.method === method);
    if (expected) {
        assert.deepEqual(storageRequests.length > 0, true, `Should have made ${method} request to ${bucket}`);
    } else {
        assert.deepEqual(storageRequests.length === 0, true, `Should NOT have made ${method} request to ${bucket}`)
    }
}

function assertServiceRequest(requests, content, date, expected) {
    let serviceURI = `http://appservice.wmflabs.org/en.wikipedia.org/v1/${content}`;
    if (date) {
        serviceURI += `/${date}`;
    }
    const storageRequests = requests.filter((log) =>
        log.req && log.req.uri === serviceURI);
    if (expected) {
        assert.deepEqual(storageRequests.length > 0, true, `Should have made request to service for ${content}`);
    } else {
        assert.deepEqual(storageRequests.length === 0, true, `Should NOT have made request to service for ${content}`);
    }
}



describe('Feed', () => {

    before(() => server.start());

    it('Should render non-available historic content', () => {
        const date = '2016/10/01';
        const slice = server.config.logStream.slice();
        return preq.get({
            uri: `${server.config.baseURL}/feed/featured/${date}`
        })
        .then(function () {
            slice.halt();
            const requests = slice.get().map(JSON.parse);
            assertStorageRequest(requests, 'get', 'feed.aggregated.historic', true);
            assertStorageRequest(requests, 'put', 'feed.aggregated.historic', true);
            assertStorageRequest(requests, 'get', 'feed.aggregated', false);
            assertStorageRequest(requests, 'put', 'feed.aggregated', false);
            assertServiceRequest(requests, 'page/featured', date, true);
            assertServiceRequest(requests, 'page/most-read', date, true);
            assertServiceRequest(requests, 'media/image/featured', date, true);
            assertServiceRequest(requests, 'page/news', undefined, false);
        });
    });

    it('Should not rerender available historic content', () => {
        const date = '2016/10/01';
        const slice = server.config.logStream.slice();
        return preq.get({
            uri: `${server.config.baseURL}/feed/featured/2016/10/01`
        })
        .then(function () {
            slice.halt();
            const requests = slice.get().map(JSON.parse);
            assertStorageRequest(requests, 'get', 'feed.aggregated.historic', true);
            assertStorageRequest(requests, 'put', 'feed.aggregated.historic', false);
            assertStorageRequest(requests, 'get', 'feed.aggregated', false);
            assertStorageRequest(requests, 'put', 'feed.aggregated', false);
            assertServiceRequest(requests, 'page/featured', date, false);
            assertServiceRequest(requests, 'page/most-read', date, false);
            assertServiceRequest(requests, 'media/image/featured', date, false);
            assertServiceRequest(requests, 'page/news', undefined, false);
        });
    });

    it('Should render non-available current content', () => {
        const now = new Date();
        const date = `${now.getUTCFullYear()}/${now.getUTCMonth() + 1}/${now.getUTCDate()}`;
        const slice = server.config.logStream.slice();
        return preq.get({
            uri: `${server.config.baseURL}/feed/featured/${date}`
        })
        .then(function () {
            slice.halt();
            const requests = slice.get().map(JSON.parse);
            assertStorageRequest(requests, 'get', 'feed.aggregated', true);
            assertStorageRequest(requests, 'put', 'feed.aggregated', true);
            assertStorageRequest(requests, 'put', 'feed.aggregated.historic', true);
            assertServiceRequest(requests, 'page/featured', date, true);
            assertServiceRequest(requests, 'page/most-read', date, true);
            assertServiceRequest(requests, 'media/image/featured', date, true);
            assertServiceRequest(requests, 'page/news', undefined, true);
        });
    });

    it('Should not rerender available current content', () => {
        const now = new Date();
        const date = `${now.getUTCFullYear()}/${now.getUTCMonth() + 1}/${now.getUTCDate()}`;
        const slice = server.config.logStream.slice();
        return preq.get({
            uri: `${server.config.baseURL}/feed/featured/${date}`
        })
        .then(function () {
            slice.halt();
            const requests = slice.get().map(JSON.parse);
            assertStorageRequest(requests, 'get', 'feed.aggregated', true);
            assertServiceRequest(requests, 'page/featured', date, false);
            assertServiceRequest(requests, 'page/most-read', date, false);
            assertServiceRequest(requests, 'media/image/featured', date, false);
            assertServiceRequest(requests, 'page/news', undefined, false);
        });
    });
});<commit_msg>Added a delay to the tests as storing is done async<commit_after>"use strict";

const assert = require('../utils/assert.js');
const server = require('../utils/server.js');
const preq   = require('preq');
const P = require('bluebird');

function assertStorageRequest(requests, method, bucket, expected) {
    const storageRequests = requests.filter((log) =>
        log.req && log.req.uri === `/en.wikipedia.org/sys/table/${bucket}/`
            && log.req.method === method);
    if (expected) {
        assert.deepEqual(storageRequests.length > 0, true, `Should have made ${method} request to ${bucket}`);
    } else {
        assert.deepEqual(storageRequests.length === 0, true, `Should NOT have made ${method} request to ${bucket}`)
    }
}

function assertServiceRequest(requests, content, date, expected) {
    let serviceURI = `http://appservice.wmflabs.org/en.wikipedia.org/v1/${content}`;
    if (date) {
        serviceURI += `/${date}`;
    }
    const storageRequests = requests.filter((log) =>
        log.req && log.req.uri === serviceURI);
    if (expected) {
        assert.deepEqual(storageRequests.length > 0, true, `Should have made request to service for ${content}`);
    } else {
        assert.deepEqual(storageRequests.length === 0, true, `Should NOT have made request to service for ${content}`);
    }
}



describe('Feed', () => {

    before(() => server.start());

    it('Should render non-available historic content', () => {
        const date = '2016/10/01';
        const slice = server.config.logStream.slice();
        return preq.get({
            uri: `${server.config.baseURL}/feed/featured/${date}`
        })
        .delay(1000)
        .then(() => {
            slice.halt();
            const requests = slice.get().map(JSON.parse);
            assertStorageRequest(requests, 'get', 'feed.aggregated.historic', true);
            assertStorageRequest(requests, 'put', 'feed.aggregated.historic', true);
            assertStorageRequest(requests, 'get', 'feed.aggregated', false);
            assertStorageRequest(requests, 'put', 'feed.aggregated', false);
            assertServiceRequest(requests, 'page/featured', date, true);
            assertServiceRequest(requests, 'page/most-read', date, true);
            assertServiceRequest(requests, 'media/image/featured', date, true);
            assertServiceRequest(requests, 'page/news', undefined, false);
        });
    });

    it('Should not rerender available historic content', () => {
        const date = '2016/10/01';
        const slice = server.config.logStream.slice();
        return preq.get({
            uri: `${server.config.baseURL}/feed/featured/2016/10/01`
        })
        .delay(1000)
        .then(() => {
            slice.halt();
            const requests = slice.get().map(JSON.parse);
            assertStorageRequest(requests, 'get', 'feed.aggregated.historic', true);
            assertStorageRequest(requests, 'put', 'feed.aggregated.historic', false);
            assertStorageRequest(requests, 'get', 'feed.aggregated', false);
            assertStorageRequest(requests, 'put', 'feed.aggregated', false);
            assertServiceRequest(requests, 'page/featured', date, false);
            assertServiceRequest(requests, 'page/most-read', date, false);
            assertServiceRequest(requests, 'media/image/featured', date, false);
            assertServiceRequest(requests, 'page/news', undefined, false);
        });
    });

    it('Should render non-available current content', () => {
        const now = new Date();
        const date = `${now.getUTCFullYear()}/${now.getUTCMonth() + 1}/${now.getUTCDate()}`;
        const slice = server.config.logStream.slice();
        return preq.get({
            uri: `${server.config.baseURL}/feed/featured/${date}`
        })
        .delay(1000)
        .then(() => {
            slice.halt();
            const requests = slice.get().map(JSON.parse);
            assertStorageRequest(requests, 'get', 'feed.aggregated', true);
            assertStorageRequest(requests, 'put', 'feed.aggregated', true);
            assertStorageRequest(requests, 'put', 'feed.aggregated.historic', true);
            assertServiceRequest(requests, 'page/featured', date, true);
            assertServiceRequest(requests, 'page/most-read', date, true);
            assertServiceRequest(requests, 'media/image/featured', date, true);
            assertServiceRequest(requests, 'page/news', undefined, true);
        });
    });

    it('Should not rerender available current content', () => {
        const now = new Date();
        const date = `${now.getUTCFullYear()}/${now.getUTCMonth() + 1}/${now.getUTCDate()}`;
        const slice = server.config.logStream.slice();
        return preq.get({
            uri: `${server.config.baseURL}/feed/featured/${date}`
        })
        .delay(1000)
        .then(() => {
            slice.halt();
            const requests = slice.get().map(JSON.parse);
            assertStorageRequest(requests, 'get', 'feed.aggregated', true);
            assertServiceRequest(requests, 'page/featured', date, false);
            assertServiceRequest(requests, 'page/most-read', date, false);
            assertServiceRequest(requests, 'media/image/featured', date, false);
            assertServiceRequest(requests, 'page/news', undefined, false);
        });
    });
});
{'fraction_non_alphanumeric': 0.15745960165351372, 'fraction_numerical': 0.007328072153325817, 'mean_word_length': 2.7390235335440813, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '3672045', 'n_tokens_mistral': 2966, 'n_tokens_neox': 2924, 'n_words': 618}