"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const csvParser = __importStar(require("../csvParser"));
const validators = __importStar(require("../validators"));
describe('csvParser', () => {
    test('should parse file with header correctly', () => __awaiter(this, void 0, void 0, function* () {
        const mockCsvHeader = `column 1,column 2
  1,value 1
  f,faulty value
  2, value 2`;
        const columns = [
            {
                name: 'column 1',
                validators: [validators.isInt, validators.isNotEmpty],
            },
            {
                name: 'column 2',
                validators: [validators.isString, validators.isNotEmpty],
            },
        ];
        const mockValidator = jest.fn(row => true);
        const result = yield csvParser.parseCsv({
            data: mockCsvHeader,
            columns,
            header: true,
            validateRow: mockValidator,
            maxRows: 2,
        });
        expect(result.result.length).toBe(2);
        expect(result.totalRows).toBe(3);
        expect(result.errorRows).toBe(1);
        expect(result.ignoredRows).toBe(1);
        expect(result.validRows).toBe(1);
        expect(mockValidator.mock.calls.length).toBe(1);
    }));
    test('should mark file as invalid if missing required field', () => __awaiter(this, void 0, void 0, function* () {
        const mockCsvHeader = `column 1,column 2
  1,value 1
  f,faulty value
  2, value 2`;
        const columns = [
            {
                name: 'column 1',
                validators: [validators.isInt, validators.isNotEmpty],
                required: true,
            },
            {
                name: 'column 2',
                validators: [validators.isString, validators.isNotEmpty],
                required: true,
            },
            {
                name: 'column 3',
                validators: [validators.isString, validators.isNotEmpty],
                required: true,
            },
        ];
        const mockValidator = jest.fn(row => true);
        const result = yield csvParser.parseCsv({
            data: mockCsvHeader,
            columns,
            header: true,
            validateRow: mockValidator,
        });
        expect(result.isFileValid).toBe(false);
    }));
    test('should mark file as valid if missing non-required field', () => __awaiter(this, void 0, void 0, function* () {
        const mockCsvHeader = `column 1,column 2
  1,value 1
  f,faulty value
  2, value 2`;
        const columns = [
            {
                name: 'column 1',
                validators: [validators.isInt, validators.isNotEmpty],
                required: true,
            },
            {
                name: 'column 2',
                validators: [validators.isString, validators.isNotEmpty],
                required: true,
            },
            {
                name: 'column 3',
                validators: [validators.isString, validators.isNotEmpty],
            },
        ];
        const mockValidator = jest.fn(row => true);
        const result = yield csvParser.parseCsv({
            data: mockCsvHeader,
            columns,
            header: true,
            validateRow: mockValidator,
        });
        expect(result.isFileValid).toBe(true);
    }));
    test('should mark file as invalid if it has extra fields in strict mode', () => __awaiter(this, void 0, void 0, function* () {
        const mockCsvHeader = `column 1,column 2,column 3
  1,value 1,12
  f,faulty value,12
  2, value 2, 13`;
        const columns = [
            {
                name: 'column 1',
                validators: [validators.isInt, validators.isNotEmpty],
                required: true,
            },
            {
                name: 'column 2',
                validators: [validators.isString, validators.isNotEmpty],
                required: true,
            },
        ];
        const mockValidator = jest.fn(row => true);
        const result = yield csvParser.parseCsv({
            data: mockCsvHeader,
            columns,
            header: true,
            strict: true,
            validateRow: mockValidator,
        });
        expect(result.isFileValid).toBe(false);
    }));
    test('should mark file as valid if it has extra fields in non strict mode', () => __awaiter(this, void 0, void 0, function* () {
        const mockCsvHeader = `column 1,column 2,column 3
  1,value 1,12
  f,faulty value,12
  2, value 2, 13`;
        const columns = [
            {
                name: 'column 1',
                validators: [validators.isInt, validators.isNotEmpty],
                required: true,
            },
            {
                name: 'column 2',
                validators: [validators.isString, validators.isNotEmpty],
                required: true,
            },
        ];
        const mockValidator = jest.fn(row => true);
        const result = yield csvParser.parseCsv({
            data: mockCsvHeader,
            columns,
            header: true,
            validateRow: mockValidator,
        });
        expect(result.isFileValid).toBe(true);
    }));
    test('should parse file without header correctly', () => __awaiter(this, void 0, void 0, function* () {
        const mockCsvNoHeader = `1,value 1
f,faulty value
2, value 2`;
        const columns = [
            {
                name: 'column 1',
                validators: [validators.isInt, validators.isNotEmpty],
            },
            {
                name: 'column 2',
                validators: [validators.isString, validators.isNotEmpty],
            },
        ];
        const mockValidator = jest.fn(row => true);
        const result = yield csvParser.parseCsv({
            data: mockCsvNoHeader,
            columns,
            validateRow: mockValidator,
            maxRows: 2,
        });
        expect(result.result.length).toBe(2);
        expect(result.totalRows).toBe(3);
        expect(result.errorRows).toBe(1);
        expect(result.ignoredRows).toBe(1);
        expect(result.validRows).toBe(1);
        expect(mockValidator.mock.calls.length).toBe(1);
    }));
});
//# sourceMappingURL=csvParser.js.map