"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const crypto_1 = require("crypto");
const Datastore_1 = __importDefault(require("../../src/common/Datastore"));
const Utils_1 = __importDefault(require("../Utils"));
describe('Datastore', () => {
    beforeAll(Utils_1.default.initDatabase);
    describe('HealthCheckModel', () => {
        it('should be able to create and fetch entries', async () => {
            const model = new Datastore_1.default.HealthCheckModel();
            model.workerId = (0, crypto_1.randomUUID)();
            await model.save();
            const found = await Datastore_1.default.HealthCheckModel.findOne({ workerId: model.workerId });
            expect(found).not.toBeNull();
            expect(found.workerId).toEqual(model.workerId);
        });
    });
    describe('InputFileListModel', () => {
        it('should be able to create and fetch entries', async () => {
            const model = new Datastore_1.default.InputFileListModel();
            model.generationId = 'generationId';
            const fileInfo1 = { path: 'path1', start: 0, end: 0, size: 1024 };
            const fileInfo2 = { path: 'path2', start: 0, end: 0, size: 1024 };
            const fileInfo3 = { path: 'path3', start: 0, end: 0, size: 1024 };
            model.fileList = [fileInfo1, fileInfo2, fileInfo3];
            await model.save();
            const found = await Datastore_1.default.InputFileListModel.findOne({ generationId: model.generationId });
            expect(found.fileList.length).toEqual(3);
            // Make sure the order is preserved
            expect(found.fileList[0]).toEqual(jasmine.objectContaining(fileInfo1));
            expect(found.fileList[1]).toEqual(jasmine.objectContaining(fileInfo2));
            expect(found.fileList[2]).toEqual(jasmine.objectContaining(fileInfo3));
        });
    });
    describe('OutputFileListModel', () => {
        it('should be able to create and fetch entries', async () => {
            const model = new Datastore_1.default.OutputFileListModel();
            model.generationId = 'generationId';
            const fileInfo1 = { path: 'path1', start: 0, end: 0, size: 1024, dir: false, selector: [], cid: 'cid1' };
            const fileInfo2 = { path: 'path2', start: 0, end: 0, size: 1024, dir: false, selector: [], cid: 'cid2' };
            const fileInfo3 = { path: 'path3', start: 0, end: 0, size: 1024, dir: false, selector: [], cid: 'cid3' };
            model.generatedFileList = [fileInfo1, fileInfo2, fileInfo3];
            await model.save();
            const found = await Datastore_1.default.OutputFileListModel.findOne({ generationId: model.generationId });
            expect(found.generatedFileList.length).toEqual(3);
            // Make sure the order is preserved
            expect(found.generatedFileList[0]).toEqual(jasmine.objectContaining(fileInfo1));
            expect(found.generatedFileList[1]).toEqual(jasmine.objectContaining(fileInfo2));
            expect(found.generatedFileList[2]).toEqual(jasmine.objectContaining(fileInfo3));
        });
    });
    describe('GenerationRequestModel', () => {
        it('should be able to create and fetch entries', async () => {
            const model = new Datastore_1.default.GenerationRequestModel();
            model.datasetName = 'name';
            await model.save();
            const found = await Datastore_1.default.GenerationRequestModel.findOne({ name: model.datasetName });
            expect(found).not.toBeNull();
            expect(found.datasetName).toEqual(model.datasetName);
        });
    });
    describe('ScanningRequestModel', () => {
        it('should be able to create and fetch entries', async () => {
            const model = new Datastore_1.default.ScanningRequestModel();
            model.name = 'name';
            await model.save();
            const found = await Datastore_1.default.ScanningRequestModel.findOne({ name: model.name });
            expect(found).not.toBeNull();
            expect(found.name).toEqual(model.name);
            const model2 = new Datastore_1.default.ScanningRequestModel();
            model2.name = 'name';
            await expectAsync(model2.save()).toBeRejectedWithError(/E11000 duplicate key error collection/);
        });
    });
});
//# sourceMappingURL=Datastore.spec.js.map