"use strict";
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var chai_1 = require("chai");
var events_matcher_1 = require("../test-kit/drivers/events-matcher");
var slow_fs_1 = require("../test-kit/drivers/slow-fs");
var universal_1 = require("../src/universal");
var implementation_suite_1 = require("./implementation-suite");
describe("the cache file system implementation", function () {
    var eventMatcherOptions = { retries: 15, interval: 2, timeout: 40, noExtraEventsGrace: 10 };
    implementation_suite_1.assertFileSystemContract(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
        return [2 /*return*/, new universal_1.CacheFileSystem(new universal_1.MemoryFileSystem(undefined, [implementation_suite_1.ignoredDir, implementation_suite_1.ignoredFile]))];
    }); }); }, eventMatcherOptions);
    describe("using slow FileSystem", function () {
        var timeout = 200;
        var fs;
        var slow;
        var startTimestamp;
        var matcher;
        beforeEach(function () {
            startTimestamp = Date.now();
            slow = new slow_fs_1.SlowFs(timeout);
            fs = new universal_1.CacheFileSystem(slow);
            matcher = new events_matcher_1.EventsMatcher(eventMatcherOptions);
            matcher.track.apply(matcher, [fs.events].concat(universal_1.fileSystemEventNames));
        });
        it('loads file faster after it has been saved', function () {
            return fs.saveFile(implementation_suite_1.fileName, implementation_suite_1.content)
                .then(function () { return fs.loadTextFile(implementation_suite_1.fileName); })
                .then(function () { return chai_1.expect(Date.now() - startTimestamp).to.be.lessThan(timeout * 2); });
        });
        it('loads file faster after it has been saved from outside', function () {
            var onFileCreated = new Promise(function (resolve) {
                fs.events.once('fileCreated', function () {
                    fs.loadTextFile(implementation_suite_1.fileName)
                        .then(function () { return resolve(Date.now() - startTimestamp); });
                });
            });
            slow.saveFile(implementation_suite_1.fileName, implementation_suite_1.content);
            return chai_1.expect(onFileCreated).to.be.eventually.lessThan(timeout * 2);
        });
        it('loads tree faster after it has been loaded before', function () {
            return fs.loadDirectoryTree()
                .then(function () { return fs.loadDirectoryTree(); })
                .then(function () { return chai_1.expect(Date.now() - startTimestamp).to.be.lessThan(timeout * 2); });
        });
    });
    describe("unexpected error behaviour", function () {
        var fs;
        var original;
        var matcher;
        beforeEach(function () {
            original = new universal_1.MemoryFileSystem();
            fs = new universal_1.CacheFileSystem(original);
            matcher = new events_matcher_1.EventsMatcher({
                retries: 30,
                interval: 5,
                noExtraEventsGrace: 150,
                timeout: 300
            });
            matcher.track.apply(matcher, [fs.events].concat(universal_1.fileSystemEventNames));
        });
        it('emits `fileCreated` if there is not cached file after error', function () {
            original.events.removeAllListeners('fileCreated');
            return original.saveFile(implementation_suite_1.fileName, implementation_suite_1.content)
                .then(function () { return matcher.expect([]); })
                .then(function () { return original.events.emit('unexpectedError', { type: 'unexpectedError' }); })
                .then(function () { return matcher.expect([{ type: 'fileCreated', fullPath: implementation_suite_1.fileName, newContent: implementation_suite_1.content }]); });
        });
        it('emits `directoryCreated` if there is not cached dir after error', function () {
            original.events.removeAllListeners('directoryCreated');
            return original.ensureDirectory(implementation_suite_1.dirName)
                .then(function () { return matcher.expect([]); })
                .then(function () { return original.events.emit('unexpectedError', { type: 'unexpectedError' }); })
                .then(function () { return matcher.expect([{ type: 'directoryCreated', fullPath: implementation_suite_1.dirName }]); });
        });
        it('emits `fileDeleted` if there is cached file and no real file after error', function () {
            return fs.saveFile(implementation_suite_1.fileName, implementation_suite_1.content).then(function () {
                original.events.removeAllListeners('fileDeleted');
                return original.deleteFile(implementation_suite_1.fileName)
                    .then(function () { return matcher.expect([{ type: 'fileCreated', fullPath: implementation_suite_1.fileName }]); })
                    .then(function () { return original.events.emit('unexpectedError', { type: 'unexpectedError' }); })
                    .then(function () { return matcher.expect([{ type: 'fileDeleted', fullPath: implementation_suite_1.fileName }]); });
            });
        });
        it('emits `directoryDeleted` if there is cached dir and no real dir after error', function () {
            return fs.ensureDirectory(implementation_suite_1.dirName).then(function () {
                original.events.removeAllListeners('directoryDeleted');
                return original.deleteDirectory(implementation_suite_1.dirName)
                    .then(function () { return matcher.expect([{ type: 'directoryCreated', fullPath: implementation_suite_1.dirName }]); })
                    .then(function () { return original.events.emit('unexpectedError', { type: 'unexpectedError' }); })
                    .then(function () { return matcher.expect([{ type: 'directoryDeleted', fullPath: implementation_suite_1.dirName }]); });
            });
        });
        it('emits `unexpectedError` if cache created with `rescanOnError = false` flag', function () {
            var fs = new universal_1.CacheFileSystem(original, false);
            var matcher = new events_matcher_1.EventsMatcher(eventMatcherOptions);
            matcher.track.apply(matcher, [fs.events].concat(universal_1.fileSystemEventNames));
            original.events.emit('unexpectedError', { type: 'unexpectedError' });
            return matcher.expect([{ type: 'unexpectedError' }]);
        });
    });
});
//# sourceMappingURL=cache-fs.spec.js.map