import test from 'ava';
// import { createIndex, searchString, searchFor } from 'src/search';
// import * as search from 'src/search';
import createSearchIndex from '../src/js/search';
import search_data from './test_data.json';
import data from '../build/data_de.json';

test('generated search strings should be equal', t => {
    let index = createSearchIndex(search_data, 'index', ['label'], 'de');
    let input1 = 'Arzt zg';
    let input2 = 'zg Arzt';

    t.is(index.compileString(input1), index.compileString(input2), 'Deterministic search string generation');
});

test('generated search strings should not be equal', t => {
    let index = createSearchIndex(search_data, 'index', ['label'], 'de');
    let input1 = 'Arzt zgx';
    let input2 = 'zgx Arzt';

    t.not(index.compileString(input1), index.compileString(input2), 'Deterministic search string generation');
});

test('length of search string should be greater than 0', t => {
    let index = createSearchIndex(search_data, 'index', ['label'], 'de');
    let input = 'Leitende/r Sekretär/in oder leitende';
    let res = index.compileString(input);
    // console.log(res);

    t.not(res.length, 0, 'Deterministic search string generation');
});

// Unfortunately, lunr search neither works when the search string contains unicode nor latin1 characters. ridiculous
// Needs further investigation: "Café" is found, but neither "Ärztin" nor "Öffentlich" 
test('search on test data in german', t => {
    // let index = searchIndex();
    let idx = createSearchIndex(search_data, 'index', ['label'], 'de');

    // Draw random entry
    let ridx = Math.floor(Math.random() * search_data.length);
    let rnd = search_data[ridx];

    let label = rnd.label;
    let rs = Math.round(Math.random() * (label.length - 3)) + 3;  // Random string length, at least 3
    let rsstring = label.substring(0, rs);

    // let result = idx.searchFor(idx, 'arzt');
    let result = idx.searchFor(rsstring);
    let inResult = result.indexOf(rnd);
    console.log(ridx, rnd, label, rs, rsstring, inResult);
    t.not(inResult, -1, 'Search term should be found in data');
});

test('probabilities of similar entries should be lower than probability of entry', t => {

    // Draw random entry
    let ridx = Math.floor(Math.random() * data.length);
    let rnd = data[ridx];
    // console.log('random entry', rnd);

    rnd.similar.forEach((e) => {
        // console.log('entry, similar, probs', rnd.prob, e.prob, e.index);
        // console.log('data[e.index]', data[e.index].label);
        t.assert(rnd.prob > e.prob);
    });
});
