"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../src/utils");
const vscode_languageserver_1 = require("vscode-languageserver");
const assert = __importStar(require("assert"));
const sinon_1 = __importDefault(require("sinon"));
describe('utils', () => {
    describe('#isBeforeOrEqualToPosition', () => {
        it('is before position (line, character lower)', () => {
            const result = (0, utils_1.isBeforeOrEqualToPosition)(vscode_languageserver_1.Position.create(1, 1), vscode_languageserver_1.Position.create(0, 0));
            assert.equal(result, true);
        });
        it('is before position (line lower, character higher)', () => {
            const result = (0, utils_1.isBeforeOrEqualToPosition)(vscode_languageserver_1.Position.create(1, 1), vscode_languageserver_1.Position.create(0, 2));
            assert.equal(result, true);
        });
        it('is equal to position', () => {
            const result = (0, utils_1.isBeforeOrEqualToPosition)(vscode_languageserver_1.Position.create(1, 1), vscode_languageserver_1.Position.create(1, 1));
            assert.equal(result, true);
        });
        it('is after position (line, character higher)', () => {
            const result = (0, utils_1.isBeforeOrEqualToPosition)(vscode_languageserver_1.Position.create(1, 1), vscode_languageserver_1.Position.create(2, 2));
            assert.equal(result, false);
        });
        it('is after position (line lower, character higher)', () => {
            const result = (0, utils_1.isBeforeOrEqualToPosition)(vscode_languageserver_1.Position.create(1, 1), vscode_languageserver_1.Position.create(2, 0));
            assert.equal(result, false);
        });
    });
    describe('#regexLastIndexOf', () => {
        it('should work #1', () => {
            assert.equal((0, utils_1.regexLastIndexOf)('1 2 3', /\s/g), 3);
        });
        it('should work #2', () => {
            assert.equal((0, utils_1.regexLastIndexOf)('1_2:- 3', /\W/g), 5);
        });
        it('should work #3', () => {
            assert.equal((0, utils_1.regexLastIndexOf)('<bla blubb={() => hello', /[\W\s]/g), 17);
        });
    });
    describe('#modifyLines', () => {
        it('should work', () => {
            assert.equal((0, utils_1.modifyLines)('a\nb\r\nc\nd', (line) => 1 + line), '1a\n1b\r\n1c\n1d');
        });
        it('should pass correct line numbers', () => {
            const idxs = [];
            (0, utils_1.modifyLines)('a\nb\r\nc\nd', (_, idx) => {
                idxs.push(idx);
                return _;
            });
            assert.deepStrictEqual(idxs, [0, 1, 2, 3]);
        });
    });
    describe('path normalization on Windows', () => {
        it('should lowercase drive letters and normalize slashes on Windows', () => {
            assert.strictEqual((0, utils_1.normalizePath)('C:\\Users\\Test\\project\\file.ts'), 'c:/Users/Test/project/file.ts');
            assert.strictEqual((0, utils_1.normalizePath)('D:/Some/Other/Path/file.ts'), 'd:/Some/Other/Path/file.ts');
            assert.strictEqual((0, utils_1.normalizePath)('e:\\Mixed/Slashes\\Path/file.ts'), 'e:/Mixed/Slashes/Path/file.ts');
            assert.strictEqual((0, utils_1.normalizePath)('/already/normalized/path/file.ts'), '/already/normalized/path/file.ts');
        });
    });
    describe('#unique', () => {
        it('removes duplicate primitives', () => {
            assert.deepStrictEqual((0, utils_1.unique)([1, 2, 1, 3, 2]), [1, 2, 3]);
            assert.deepStrictEqual((0, utils_1.unique)(['a', 'b', 'a']), ['a', 'b']);
        });
        it('removes deeply equal objects', () => {
            assert.deepStrictEqual((0, utils_1.unique)([{ a: 1 }, { b: 2 }, { a: 1 }]), [{ a: 1 }, { b: 2 }]);
        });
        it('removes deeply equal nested objects', () => {
            assert.deepStrictEqual((0, utils_1.unique)([{ x: { y: 1 } }, { x: { y: 2 } }, { x: { y: 1 } }]), [
                { x: { y: 1 } },
                { x: { y: 2 } }
            ]);
        });
        it('keeps all items when none are equal', () => {
            const input = [{ a: 1 }, { a: 2 }, { a: 3 }];
            assert.strictEqual((0, utils_1.unique)(input).length, 3);
        });
        it('returns empty array for empty input', () => {
            assert.deepStrictEqual((0, utils_1.unique)([]), []);
        });
    });
    describe('#groupBy', () => {
        it('produces one key per unique value returned by predicate', () => {
            const result = (0, utils_1.groupBy)(['apple', 'banana', 'avocado'], (s) => s[0]);
            assert.ok('a' in result);
            assert.ok('b' in result);
            assert.strictEqual(Object.keys(result).length, 2);
            assert.deepStrictEqual(result['a'], ['apple', 'avocado']);
            assert.deepStrictEqual(result['b'], ['banana']);
        });
        it('groups all items sharing a key into an array', () => {
            const items = [
                { file: 'a.ts', msg: 'first' },
                { file: 'a.ts', msg: 'second' },
                { file: 'b.ts', msg: 'only' }
            ];
            const result = (0, utils_1.groupBy)(items, (i) => i.file);
            assert.strictEqual(Object.keys(result).length, 2);
            assert.deepStrictEqual(result['a.ts'], [
                { file: 'a.ts', msg: 'first' },
                { file: 'a.ts', msg: 'second' }
            ]);
            assert.deepStrictEqual(result['b.ts'], [{ file: 'b.ts', msg: 'only' }]);
        });
        it('returns an empty object for an empty array', () => {
            assert.deepStrictEqual((0, utils_1.groupBy)([], () => 'key'), {});
        });
    });
    describe('#debounce', () => {
        it('only calls the function after the delay has elapsed', () => {
            const clock = sinon_1.default.useFakeTimers();
            try {
                let callCount = 0;
                const fn = (0, utils_1.debounce)(() => callCount++, 100);
                fn();
                assert.strictEqual(callCount, 0, 'should not fire immediately');
                clock.tick(99);
                assert.strictEqual(callCount, 0, 'should not fire before delay');
                clock.tick(1);
                assert.strictEqual(callCount, 1, 'should fire after delay');
            }
            finally {
                clock.restore();
            }
        });
        it('resets the timer when called again before the delay elapses', () => {
            const clock = sinon_1.default.useFakeTimers();
            try {
                let callCount = 0;
                const fn = (0, utils_1.debounce)(() => callCount++, 100);
                fn();
                clock.tick(50);
                fn(); // reset
                clock.tick(50); // 50ms after second call — should not have fired yet
                assert.strictEqual(callCount, 0, 'should not fire before reset delay');
                clock.tick(50); // now 100ms after second call
                assert.strictEqual(callCount, 1, 'should fire once after reset delay');
            }
            finally {
                clock.restore();
            }
        });
        it('fires exactly once even when called many times in quick succession', () => {
            const clock = sinon_1.default.useFakeTimers();
            try {
                let callCount = 0;
                const fn = (0, utils_1.debounce)(() => callCount++, 100);
                fn();
                fn();
                fn();
                clock.tick(100);
                assert.strictEqual(callCount, 1);
            }
            finally {
                clock.restore();
            }
        });
    });
});
//# sourceMappingURL=utils.test.js.map