<commit_before>import {expect} from 'chai';
import loki from 'lokijs';

import LokiPersist from '../../backend/stores/lokiPersist.js';

let testPath = 'testdb.js';

function setup() {
    const lokidb = new loki('testdb.js');
    lokidb.addCollection('tests');
    lokidb.saveDatabase;
    console.log('setup done');
}

setup();

describe('loki persist wrapper', () => {

    describe('new', () => {
        it('connects a new persistent loki', () => {
            let lp = new LokiPersist(testPath);
            expect(lp).to.exist;
        });
    });

    describe('add something', () => {
        it('adds something to the db', (done) => {
            let lp = new LokiPersist(testPath);
            var result = lp.persist( (db) => {
                let cln = db.addCollection('tests'); // because it doesn't exist yet
                cln.add({ added: 'stuff'});
                return 'bonanza';
            });
            return result.then( (data) => {
                expect(data).to.equal('bonanza');
                done();
            })
        });
    });

});<commit_msg>Test actual persistence.<commit_after>import {expect} from 'chai';
import loki from 'lokijs';
import fs   from 'fs';

import LokiPersist from '../../backend/stores/lokiPersist.js';

let testPath = 'testdb.js';

function setup() {
    fs.truncateSync(testPath, 0);
    console.log('setup done');
}

setup();

describe('loki persist wrapper', () => {

    describe('new', () => {
        it('connects a new persistent loki', () => {
            let lp = new LokiPersist(testPath);
            expect(lp).to.exist;
        });
    });

    describe('add something', () => {
        it('adds something to the db', (done) => {
            let lp = new LokiPersist(testPath);
            var result = lp.persist( (db) => {
                let cln = db.addCollection('tests'); // because it doesn't exist yet
                cln.insert({ added: 'stuff'});
                return 'bonanza';
            });
            return result.then( (data) => {
                expect(data).to.equal('bonanza');
                let cln = lp.db.getCollection('tests');
                expect(cln).to.exist;
                expect(cln.data).to.exist;
                let added = cln.findOne({added: 'stuff'});
                expect(added).to.exist;
                done();
            }).catch( (err) => console.error(err));
        });
    });

    describe('something is still there', () => {
        it('gets something back from the db', (done) => {
            let lp = new LokiPersist(testPath);
            var result = lp.persist( (db) => {
                let cln = db.getCollection('tests');
                return cln.data;
            });
            return result.then( (data) => {
                expect(data.length).to.equal(1);
                done();
            })
        });
    });

});
{'fraction_non_alphanumeric': 0.1646362687086669, 'fraction_numerical': 0.0006961364427427776, 'mean_word_length': 1.7714561234329798, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '500903', 'n_tokens_mistral': 871, 'n_tokens_neox': 836, 'n_words': 192}