2016-12-06 08:07:46 +08:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2017-02-03 06:00:06 +08:00
|
|
|
const LocalDatabaseConnector = require('../src/shared/local-database-connector')
|
2016-12-06 08:07:46 +08:00
|
|
|
|
|
|
|
const FIXTURES_PATH = path.join(__dirname, 'fixtures');
|
|
|
|
const ACCOUNT_ID = 'test-account-id';
|
|
|
|
|
|
|
|
function forEachJSONFixture(relativePath, callback) {
|
|
|
|
const fixturesDir = path.join(FIXTURES_PATH, relativePath);
|
|
|
|
const filenames = fs.readdirSync(fixturesDir).filter(f => f.endsWith('.json'));
|
|
|
|
filenames.forEach((filename) => {
|
|
|
|
const json = JSON.parse(fs.readFileSync(path.join(fixturesDir, filename)));
|
|
|
|
callback(filename, json);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-14 04:42:38 +08:00
|
|
|
function forEachHTMLAndTXTFixture(relativePath, callback) {
|
|
|
|
const fixturesDir = path.join(FIXTURES_PATH, relativePath);
|
|
|
|
const filenames = fs.readdirSync(fixturesDir).filter(f => f.endsWith('.html'));
|
|
|
|
filenames.forEach((filename) => {
|
|
|
|
const html = fs.readFileSync(path.join(fixturesDir, filename)).toString();
|
|
|
|
const basename = path.parse(filename).name;
|
|
|
|
const txt = fs.readFileSync(path.join(fixturesDir, `${basename}.txt`)).toString().replace(/\n$/, '');
|
|
|
|
callback(filename, html, txt);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-03 06:00:06 +08:00
|
|
|
async function getTestDatabase(accountId = ACCOUNT_ID) {
|
|
|
|
await LocalDatabaseConnector.ensureAccountDatabase(accountId)
|
|
|
|
return LocalDatabaseConnector.forAccount(accountId)
|
|
|
|
}
|
|
|
|
|
|
|
|
function destroyTestDatabase(accountId = ACCOUNT_ID) {
|
|
|
|
LocalDatabaseConnector.destroyAccountDatabase(accountId)
|
|
|
|
}
|
|
|
|
|
|
|
|
function mockImapBox() {
|
|
|
|
return {
|
|
|
|
setLabels: jasmine.createSpy('setLabels'),
|
|
|
|
removeLabels: jasmine.createSpy('removeLabels'),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 08:07:46 +08:00
|
|
|
const silentLogger = {
|
|
|
|
info: () => {},
|
|
|
|
warn: () => {},
|
|
|
|
debug: () => {},
|
|
|
|
error: () => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
FIXTURES_PATH,
|
|
|
|
ACCOUNT_ID,
|
|
|
|
silentLogger,
|
|
|
|
forEachJSONFixture,
|
2016-12-14 04:42:38 +08:00
|
|
|
forEachHTMLAndTXTFixture,
|
2017-02-03 06:00:06 +08:00
|
|
|
mockImapBox,
|
|
|
|
getTestDatabase,
|
|
|
|
destroyTestDatabase,
|
2016-12-06 08:07:46 +08:00
|
|
|
}
|