<commit_before>const Gio = imports.gi.Gio;
const Soup = imports.gi.Soup;

const ArticleObjectModel = imports.search.articleObjectModel;
const ContentObjectModel = imports.search.contentObjectModel;
const Engine = imports.search.engine;
const InstanceOfMatcher = imports.tests.InstanceOfMatcher;
const MediaObjectModel = imports.search.mediaObjectModel;
const MockShard = imports.tests.mockShard;
const QueryObject = imports.search.queryObject;
const SetObjectModel = imports.search.setObjectModel;
const Utils = imports.search.utils;

const MOCK_CONTENT_RESULTS = [
    {
        "@id": "ekn://asoiaf/House_Stark",
        "@type": "ekn://_vocab/ContentObject",
        "title": "House Stark",
        "synopsis": "Winter is Coming",
    },
    {
        "@id": "ekn://marvel/Tony_Stark",
        "@type": "ekn://_vocab/ContentObject",
        "title": "T<NAME>",
        "synopsis": "is actually Iron Man",
    },
];
const MOCK_ARTICLE_RESULTS = [
    {
        "@id": "ekn://asoiaf/House_Stark",
        "@type": "ekn://_vocab/ArticleObject",
        "title": "House Stark",
        "synopsis": "Winter is Coming",
        "articleBody": "Everything's coming up Stark!",
    },
    {
        "@id": "ekn://marvel/Tony_Stark",
        "@type": "ekn://_vocab/ArticleObject",
        "title": "<NAME>",
        "synopsis": "is actually Iron Man",
        "articleBody": "Fun fact: Iron Man was actually based on <NAME> Jr.'s life",
    },
];
const MOCK_MEDIA_RESULTS = [
    {
        "@context": "ekn://_context/ImageObject",
        "@type": "ekn://_vocab/ImageObject",
        "@id": "http://img1.wikia.nocookie.net/__cb20130318151721/epicrapbattlesofhistory/images/6/6d/Rick-astley.jpg",
        "title": "<NAME>: The Man, The Myth, The Legend",
        "tags": ["inspiring", "beautiful"],
        "caption": "Great musician, or greatest?",
        "encodingFormat": "jpg",
        "height": "666",
        "width": "666",
    },
    {
        "@context": "ekn://_context/VideoObject",
        "@type": "ekn://_vocab/VideoObject",
        "@id": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
        "title": "Never Gonna Give You Up (Never Gonna Let You Down)",
        "transcript": "We're no strangers to love, etc etc etc",
        "tags": ["inspiring", "beautiful"],
        "caption": "If this song was sushi, it would be a Rick Roll",
        "encodingFormat": "mov",
        "duration": "P666S",
        "height": "666",
        "width": "666",
    },
];

describe('Knowledge Engine Module', () => {
    let engine;
    let noop = function () {};

    // Setup a spy in place of the Soup-based request function
    function engine_request_spy() {
        let mock_request = jasmine.createSpy('request');
        engine._send_json_ld_request = mock_request;
        return mock_request;
    }

    // Setup a mocked request function which just returns the mock data.
    // From EOS 2.4 onward, xapian-bridge will return string results instead of
    // JSON, so we stringify all mock_data to emulate this.
    function mock_engine_query(mock_err, mock_results) {
        let mock_data;
        if (mock_results)
            mock_data = { results: mock_results.map(JSON.stringify) };
        mock_engine_request(mock_err, mock_data);
    }

    function mock_engine_request(mock_err, mock_data) {
        spyOn(engine, '_send_json_ld_request').and.callFake((uri,
                                                             cancellable,
                                                             callback) => {
            callback(engine, null);
        });
        spyOn(engine, '_send_json_ld_request_finish');
        if (mock_data) {
            engine._send_json_ld_request_finish.and.returnValue(mock_data);
        } else if (mock_err) {
            engine._send_json_ld_request_finish.and.callFake(() => {
                throw mock_err;
            });
        }
    }

    // Fakes out the get_fixed_query method to simply pass through the query
    // object completely untouched.
    function fake_get_fixed_query() {
        spyOn(engine, 'get_fixed_query').and.callFake((query_obj, _, callback) => {
            callback(undefined, query_obj);
        });
        spyOn(engine, 'get_fixed_query_finish').and.callFake((task) => {
            return task;
        });
    }

    function mock_engine_query_with_multiple_values(return_values) {
        spyOn(engine, '_send_json_ld_request').and.callFake((uri,
                                                             cancellable,
                                                             callback) => {
            callback(engine, null);
        });<commit_msg>Use .and.throwError() instead of function<commit_after>const Gio = imports.gi.Gio;
const Soup = imports.gi.Soup;

const ArticleObjectModel = imports.search.articleObjectModel;
const ContentObjectModel = imports.search.contentObjectModel;
const Engine = imports.search.engine;
const InstanceOfMatcher = imports.tests.InstanceOfMatcher;
const MediaObjectModel = imports.search.mediaObjectModel;
const MockShard = imports.tests.mockShard;
const QueryObject = imports.search.queryObject;
const SetObjectModel = imports.search.setObjectModel;
const Utils = imports.search.utils;

const MOCK_CONTENT_RESULTS = [
    {
        "@id": "ekn://asoiaf/House_Stark",
        "@type": "ekn://_vocab/ContentObject",
        "title": "House Stark",
        "synopsis": "Winter is Coming",
    },
    {
        "@id": "ekn://marvel/Tony_Stark",
        "@type": "ekn://_vocab/ContentObject",
        "title": "Tony Stark",
        "synopsis": "is actually Iron Man",
    },
];
const MOCK_ARTICLE_RESULTS = [
    {
        "@id": "ekn://asoiaf/House_Stark",
        "@type": "ekn://_vocab/ArticleObject",
        "title": "House Stark",
        "synopsis": "Winter is Coming",
        "articleBody": "Everything's coming up Stark!",
    },
    {
        "@id": "ekn://marvel/Tony_Stark",
        "@type": "ekn://_vocab/ArticleObject",
        "title": "T<NAME>",
        "synopsis": "is actually Iron Man",
        "articleBody": "Fun fact: Iron Man was actually based on <NAME> Jr.'s life",
    },
];
const MOCK_MEDIA_RESULTS = [
    {
        "@context": "ekn://_context/ImageObject",
        "@type": "ekn://_vocab/ImageObject",
        "@id": "http://img1.wikia.nocookie.net/__cb20130318151721/epicrapbattlesofhistory/images/6/6d/Rick-astley.jpg",
        "title": "Rick Astley: The Man, The Myth, The Legend",
        "tags": ["inspiring", "beautiful"],
        "caption": "Great musician, or greatest?",
        "encodingFormat": "jpg",
        "height": "666",
        "width": "666",
    },
    {
        "@context": "ekn://_context/VideoObject",
        "@type": "ekn://_vocab/VideoObject",
        "@id": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
        "title": "Never Gonna Give You Up (Never Gonna Let You Down)",
        "transcript": "We're no strangers to love, etc etc etc",
        "tags": ["inspiring", "beautiful"],
        "caption": "If this song was sushi, it would be a Rick Roll",
        "encodingFormat": "mov",
        "duration": "P666S",
        "height": "666",
        "width": "666",
    },
];

describe('Knowledge Engine Module', () => {
    let engine;
    let noop = function () {};

    // Setup a spy in place of the Soup-based request function
    function engine_request_spy() {
        let mock_request = jasmine.createSpy('request');
        engine._send_json_ld_request = mock_request;
        return mock_request;
    }

    // Setup a mocked request function which just returns the mock data.
    // From EOS 2.4 onward, xapian-bridge will return string results instead of
    // JSON, so we stringify all mock_data to emulate this.
    function mock_engine_query(mock_err, mock_results) {
        let mock_data;
        if (mock_results)
            mock_data = { results: mock_results.map(JSON.stringify) };
        mock_engine_request(mock_err, mock_data);
    }

    function mock_engine_request(mock_err, mock_data) {
        spyOn(engine, '_send_json_ld_request').and.callFake((uri,
                                                             cancellable,
                                                             callback) => {
            callback(engine, null);
        });
        spyOn(engine, '_send_json_ld_request_finish');
        if (mock_data) {
            engine._send_json_ld_request_finish.and.returnValue(mock_data);
        } else if (mock_err) {
            engine._send_json_ld_request_finish.and.throwError(mock_err);
        }
    }

    // Fakes out the get_fixed_query method to simply pass through the query
    // object completely untouched.
    function fake_get_fixed_query() {
        spyOn(engine, 'get_fixed_query').and.callFake((query_obj, _, callback) => {
            callback(undefined, query_obj);
        });
        spyOn(engine, 'get_fixed_query_finish').and.callFake((task) => {
            return task;
        });
    }

    function mock_engine_query_with_multiple_values(return_values) {
        spyOn(engine, '_send_json_ld_request').and.callFake((uri,
                                                             cancellable,
                                                             callback) => {
            callback(engine, null);
        });
{'fraction_non_alphanumeric': 0.13323998276604912, 'fraction_numerical': 0.007755277897457992, 'mean_word_length': 2.3019203413940255, 'pattern_counts': {'":': 76, '<': 9, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6862103', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2766, 'n_words': 608}