"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const esprima_1 = require("esprima");
const vitest_1 = require("vitest");
const index_js_1 = require("../types/index.js");
const refactor_js_1 = require("./refactor.js");
(0, vitest_1.describe)('Refactoring', () => {
    (0, vitest_1.describe)('Page Scope', () => {
        (0, vitest_1.describe)('refactorNameInCode', () => {
            (0, vitest_1.it)('should refactor the old name to the new name in the code', async () => {
                const value = 'return oldName;';
                const oldName = 'oldName';
                const newName = 'newName';
                const dataTree = {
                    oldName: {}
                };
                const result = await (0, refactor_js_1.refactorNameInCode)({
                    value,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractJsEvaluationPairs
                });
                (0, vitest_1.expect)(result).toEqual('return newName;');
            });
        });
        (0, vitest_1.describe)('refactorNameInString', () => {
            (0, vitest_1.it)('should refactor the old name to the new name in the string', async () => {
                const value = 'Hello, {{oldName}}!';
                const oldName = 'oldName';
                const newName = 'newName';
                const dataTree = {
                    oldName: {}
                };
                const result = await (0, refactor_js_1.refactorNameInString)({
                    value,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractJsEvaluationPairs
                });
                (0, vitest_1.expect)(result).toEqual('Hello, {{newName}}!');
            });
        });
        (0, vitest_1.describe)('refactorNameInValue', () => {
            (0, vitest_1.it)('in object', async () => {
                const oldValue = {
                    name: '{{oldName}}',
                    age: 30
                };
                const oldName = 'oldName';
                const newName = 'newName';
                const dataTree = {
                    oldName: {}
                };
                const result = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractJsEvaluationPairs
                });
                (0, vitest_1.expect)(result).toEqual({
                    name: '{{newName}}',
                    age: 30
                });
            });
        });
        (0, vitest_1.it)('in array', async () => {
            const oldValue = ['{{oldName}}', 30];
            const oldName = 'oldName';
            const newName = 'newName';
            const dataTree = {
                oldName: {}
            };
            const result = await (0, refactor_js_1.refactorNameInValue)({
                oldValue,
                oldName,
                newName,
                dataTree,
                extractPairs: extractJsEvaluationPairs
            });
            (0, vitest_1.expect)(result).toEqual(['{{newName}}', 30]);
        });
        (0, vitest_1.it)('in string', async () => {
            const oldValue = '{{oldName}}';
            const oldName = 'oldName';
            const newName = 'newName';
            const dataTree = {
                oldName: {}
            };
            const result = await (0, refactor_js_1.refactorNameInValue)({
                oldValue,
                oldName,
                newName,
                dataTree,
                extractPairs: extractJsEvaluationPairs
            });
            (0, vitest_1.expect)(result).toEqual('{{newName}}');
        });
    });
    (0, vitest_1.describe)('App Scope', () => {
        (0, vitest_1.describe)('refactorNameInCode', () => {
            (0, vitest_1.it)('should refactor the old name to the new name in the code', async () => {
                const value = 'return App.oldName;';
                const oldName = 'oldName';
                const newName = 'newName';
                const dataTree = {
                    oldName: {}
                };
                const result = await (0, refactor_js_1.refactorNameInCode)({
                    value,
                    oldName,
                    newName,
                    dataTree,
                    namespace: 'App',
                    extractPairs: extractJsEvaluationPairs
                });
                (0, vitest_1.expect)(result).toEqual('return App.newName;');
            });
        });
        (0, vitest_1.describe)('refactorNameInString', () => {
            (0, vitest_1.it)('should refactor the old name to the new name in the string', async () => {
                const value = 'Hello, {{App.oldName}}!';
                const oldName = 'oldName';
                const newName = 'newName';
                const dataTree = {
                    oldName: {}
                };
                const result = await (0, refactor_js_1.refactorNameInString)({
                    value,
                    oldName,
                    newName,
                    dataTree,
                    namespace: 'App',
                    extractPairs: extractJsEvaluationPairs
                });
                (0, vitest_1.expect)(result).toEqual('Hello, {{App.newName}}!');
            });
        });
        (0, vitest_1.describe)('refactorNameInValue', () => {
            (0, vitest_1.it)('in object', async () => {
                const oldValue = {
                    name: '{{App.oldName}}',
                    age: 30
                };
                const oldName = 'oldName';
                const newName = 'newName';
                const dataTree = {
                    oldName: {}
                };
                const result = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue,
                    oldName,
                    newName,
                    dataTree,
                    namespace: 'App',
                    extractPairs: extractJsEvaluationPairs
                });
                (0, vitest_1.expect)(result).toEqual({
                    name: '{{App.newName}}',
                    age: 30
                });
            });
        });
        (0, vitest_1.it)('in array', async () => {
            const oldValue = ['{{App.oldName}}', 30];
            const oldName = 'oldName';
            const newName = 'newName';
            const dataTree = {
                oldName: {}
            };
            const result = await (0, refactor_js_1.refactorNameInValue)({
                oldValue,
                oldName,
                newName,
                dataTree,
                namespace: 'App',
                extractPairs: extractJsEvaluationPairs
            });
            (0, vitest_1.expect)(result).toEqual(['{{App.newName}}', 30]);
        });
        (0, vitest_1.it)('in string', async () => {
            const oldValue = '{{App.oldName}}';
            const oldName = 'oldName';
            const newName = 'newName';
            const dataTree = {
                oldName: {}
            };
            const result = await (0, refactor_js_1.refactorNameInValue)({
                oldValue,
                oldName,
                newName,
                dataTree,
                namespace: 'App',
                extractPairs: extractJsEvaluationPairs
            });
            (0, vitest_1.expect)(result).toEqual('{{App.newName}}');
        });
    });
    (0, vitest_1.describe)('refactorNameInStep', () => {
        (0, vitest_1.it)('should refactor names in JS steps', async () => {
            const oldName = 'oldName';
            const newName = 'newName';
            const dataTree = {
                oldName: {}
            };
            const configuration = {
                body: 'return {{oldName}};'
            };
            await (0, refactor_js_1.refactorNameInStep)({
                configuration,
                oldName,
                newName,
                dataTree,
                namespace: undefined,
                pluginId: index_js_1.LanguagePluginID.JavaScript,
                extractor: extractJsEvaluationPairs
            });
            (0, vitest_1.expect)(configuration.body).toEqual('return {{newName}};');
        });
        (0, vitest_1.it)('should refactor names in steps with nested configurations, i.e. Databricks, Kinesis, etc', async () => {
            const oldName = 'oldName';
            const newName = 'newName';
            const dataTree = {
                oldName: {}
            };
            const configuration = {
                get: {
                    limit: 10,
                    pollingCooldownMs: 1000,
                    shardIteratorType: 'SHARD_ITERATOR_TYPE_LATEST',
                    streamIdentifierType: 'STREAM_IDENTIFIER_STREAM_NAME'
                },
                put: {
                    data: '"test": {{oldName.text}}',
                    streamName: '{{oldName.text}}',
                    streamIdentifierType: 'STREAM_IDENTIFIER_STREAM_NAME'
                },
                operationType: 'OPERATION_TYPE_PUT'
            };
            await (0, refactor_js_1.refactorNameInStep)({
                configuration,
                oldName,
                newName,
                dataTree,
                namespace: undefined,
                pluginId: '12345',
                extractor: extractJsEvaluationPairs
            });
            (0, vitest_1.expect)(configuration).toEqual({
                get: {
                    limit: 10,
                    pollingCooldownMs: 1000,
                    shardIteratorType: 'SHARD_ITERATOR_TYPE_LATEST',
                    streamIdentifierType: 'STREAM_IDENTIFIER_STREAM_NAME'
                },
                put: {
                    data: '"test": {{newName.text}}',
                    streamName: '{{newName.text}}',
                    streamIdentifierType: 'STREAM_IDENTIFIER_STREAM_NAME'
                },
                operationType: 'OPERATION_TYPE_PUT'
            });
        });
    });
});
async function extractJsEvaluationPairs(jsSnippet, entitiesToExtract, dataTree, namespacedEntitiesToExtract) {
    return (0, index_js_1.extractJsEvaluationPairsWithTokenizer)(jsSnippet, entitiesToExtract, dataTree, esprima_1.tokenize, namespacedEntitiesToExtract);
}
//# sourceMappingURL=refactor.test.js.map