diff --git a/packages/bruno-converters/tests/postman/postman-translations.spec.js b/packages/bruno-converters/tests/postman/postman-translations.spec.js deleted file mode 100644 index a2fcf1560..000000000 --- a/packages/bruno-converters/tests/postman/postman-translations.spec.js +++ /dev/null @@ -1,139 +0,0 @@ -const { default: postmanTranslation } = require("../../src/postman/postman-translations"); - -describe('postmanTranslation function', () => { - test('should translate pm commands correctly', () => { - const inputScript = ` - pm.environment.get('key'); - pm.environment.set('key', 'value'); - pm.variables.get('key'); - pm.variables.set('key', 'value'); - pm.collectionVariables.get('key'); - pm.collectionVariables.set('key', 'value'); - const data = pm.response.json(); - pm.expect(pm.environment.has('key')).to.be.true; - `; - const expectedOutput = ` - bru.getEnvVar('key'); - bru.setEnvVar('key', 'value'); - bru.getVar('key'); - bru.setVar('key', 'value'); - bru.getVar('key'); - bru.setVar('key', 'value'); - const data = res.getBody(); - expect(bru.getEnvVar('key') !== undefined && bru.getEnvVar('key') !== null).to.be.true; - `; - expect(postmanTranslation(inputScript)).toBe(expectedOutput); - }); - - test('should not translate non-pm commands', () => { - const inputScript = ` - console.log('This script does not contain pm commands.'); - const data = pm.environment.get('key'); - pm.collectionVariables.set('key', data); - `; - const expectedOutput = ` - console.log('This script does not contain pm commands.'); - const data = bru.getEnvVar('key'); - bru.setVar('key', data); - `; - expect(postmanTranslation(inputScript)).toBe(expectedOutput); - }); - - test('should comment non-translated pm commands', () => { - const inputScript = "pm.test('random test', () => postman.variables.replaceIn('{{$guid}}'));"; - const expectedOutput = "// test('random test', () => postman.variables.replaceIn('{{$guid}}'));"; - expect(postmanTranslation(inputScript)).toBe(expectedOutput); - }); - test('should handle multiple pm commands on the same line', () => { - const inputScript = "pm.environment.get('key'); pm.environment.set('key', 'value');"; - const expectedOutput = "bru.getEnvVar('key'); bru.setEnvVar('key', 'value');"; - expect(postmanTranslation(inputScript)).toBe(expectedOutput); - }); - test('should handle comments and other JavaScript code', () => { - const inputScript = ` - // This is a comment - const value = 'test'; - pm.environment.set('key', value); - /* - Multi-line comment - */ - const result = pm.environment.get('key'); - console.log('Result:', result); - `; - const expectedOutput = ` - // This is a comment - const value = 'test'; - bru.setEnvVar('key', value); - /* - Multi-line comment - */ - const result = bru.getEnvVar('key'); - console.log('Result:', result); - `; - expect(postmanTranslation(inputScript)).toBe(expectedOutput); - }); - - test('should handle nested commands and edge cases', () => { - const inputScript = ` - const sampleObjects = [ - { - key: pm.environment.get('key'), - value: pm.variables.get('value') - }, - { - key: pm.collectionVariables.get('key'), - value: pm.collectionVariables.get('value') - } - ]; - const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => { - // this is a comment - acc[key] = pm.collectionVariables.get(pm.environment.get(value)); - return acc; // Return the accumulator - }, {}); - Object.values(dataTesting).forEach((data) => { - pm.environment.set(data.key, pm.variables.get(data.value)); - }); - `; - const expectedOutput = ` - const sampleObjects = [ - { - key: bru.getEnvVar('key'), - value: bru.getVar('value') - }, - { - key: bru.getVar('key'), - value: bru.getVar('value') - } - ]; - const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => { - // this is a comment - acc[key] = bru.getVar(bru.getEnvVar(value)); - return acc; // Return the accumulator - }, {}); - Object.values(dataTesting).forEach((data) => { - bru.setEnvVar(data.key, bru.getVar(data.value)); - }); - `; - expect(postmanTranslation(inputScript)).toBe(expectedOutput); - }); - - test('should handle test commands', () => { - const inputScript = ` - pm.test('Status code is 200', () => { - pm.response.to.have.status(200); - }); - pm.test('this test will fail', () => { - return false - }); - `; - const expectedOutput = ` - test('Status code is 200', () => { - expect(res.getStatus()).to.equal(200); - }); - test('this test will fail', () => { - return false - }); - `; - expect(postmanTranslation(inputScript)).toBe(expectedOutput); - }); -}); diff --git a/packages/bruno-converters/tests/postman/postman-translations/postman-comments.spec.js b/packages/bruno-converters/tests/postman/postman-translations/postman-comments.spec.js new file mode 100644 index 000000000..6d98ce6e8 --- /dev/null +++ b/packages/bruno-converters/tests/postman/postman-translations/postman-comments.spec.js @@ -0,0 +1,53 @@ +const { default: postmanTranslation } = require("../../../src/postman/postman-translations"); + +describe('postmanTranslations - comment handling', () => { + test('should not translate non-pm commands', () => { + const inputScript = ` + console.log('This script does not contain pm commands.'); + const data = pm.environment.get('key'); + pm.collectionVariables.set('key', data); + `; + const expectedOutput = ` + console.log('This script does not contain pm commands.'); + const data = bru.getEnvVar('key'); + bru.setVar('key', data); + `; + expect(postmanTranslation(inputScript)).toBe(expectedOutput); + }); + + test('should comment non-translated pm commands', () => { + const inputScript = "pm.test('random test', () => postman.variables.replaceIn('{{$guid}}'));"; + const expectedOutput = "// test('random test', () => postman.variables.replaceIn('{{$guid}}'));"; + expect(postmanTranslation(inputScript)).toBe(expectedOutput); + }); + + test('should handle multiple pm commands on the same line', () => { + const inputScript = "pm.environment.get('key'); pm.environment.set('key', 'value');"; + const expectedOutput = "bru.getEnvVar('key'); bru.setEnvVar('key', 'value');"; + expect(postmanTranslation(inputScript)).toBe(expectedOutput); + }); + + test('should handle comments and other JavaScript code', () => { + const inputScript = ` + // This is a comment + const value = 'test'; + pm.environment.set('key', value); + /* + Multi-line comment + */ + const result = pm.environment.get('key'); + console.log('Result:', result); + `; + const expectedOutput = ` + // This is a comment + const value = 'test'; + bru.setEnvVar('key', value); + /* + Multi-line comment + */ + const result = bru.getEnvVar('key'); + console.log('Result:', result); + `; + expect(postmanTranslation(inputScript)).toBe(expectedOutput); + }); +}); \ No newline at end of file diff --git a/packages/bruno-converters/tests/postman/postman-translations/postman-edge-cases.spec.js b/packages/bruno-converters/tests/postman/postman-translations/postman-edge-cases.spec.js new file mode 100644 index 000000000..d304026ea --- /dev/null +++ b/packages/bruno-converters/tests/postman/postman-translations/postman-edge-cases.spec.js @@ -0,0 +1,47 @@ +const { default: postmanTranslation } = require("../../../src/postman/postman-translations"); + +describe('postmanTranslations - edge cases', () => { + test('should handle nested commands and edge cases', () => { + const inputScript = ` + const sampleObjects = [ + { + key: pm.environment.get('key'), + value: pm.variables.get('value') + }, + { + key: pm.collectionVariables.get('key'), + value: pm.collectionVariables.get('value') + } + ]; + const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => { + // this is a comment + acc[key] = pm.collectionVariables.get(pm.environment.get(value)); + return acc; // Return the accumulator + }, {}); + Object.values(dataTesting).forEach((data) => { + pm.environment.set(data.key, pm.variables.get(data.value)); + }); + `; + const expectedOutput = ` + const sampleObjects = [ + { + key: bru.getEnvVar('key'), + value: bru.getVar('value') + }, + { + key: bru.getVar('key'), + value: bru.getVar('value') + } + ]; + const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => { + // this is a comment + acc[key] = bru.getVar(bru.getEnvVar(value)); + return acc; // Return the accumulator + }, {}); + Object.values(dataTesting).forEach((data) => { + bru.setEnvVar(data.key, bru.getVar(data.value)); + }); + `; + expect(postmanTranslation(inputScript)).toBe(expectedOutput); + }); +}); \ No newline at end of file diff --git a/packages/bruno-converters/tests/postman/postman-translations/postman-test-commands.spec.js b/packages/bruno-converters/tests/postman/postman-translations/postman-test-commands.spec.js new file mode 100644 index 000000000..7c75b518e --- /dev/null +++ b/packages/bruno-converters/tests/postman/postman-translations/postman-test-commands.spec.js @@ -0,0 +1,23 @@ +const { default: postmanTranslation } = require("../../../src/postman/postman-translations"); + +describe('postmanTranslations - test commands', () => { + test('should handle test commands', () => { + const inputScript = ` + pm.test('Status code is 200', () => { + pm.response.to.have.status(200); + }); + pm.test('this test will fail', () => { + return false + }); + `; + const expectedOutput = ` + test('Status code is 200', () => { + expect(res.getStatus()).to.equal(200); + }); + test('this test will fail', () => { + return false + }); + `; + expect(postmanTranslation(inputScript)).toBe(expectedOutput); + }); +}); \ No newline at end of file diff --git a/packages/bruno-converters/tests/postman/postman-translations/postman-variables.spec.js b/packages/bruno-converters/tests/postman/postman-translations/postman-variables.spec.js new file mode 100644 index 000000000..70801f295 --- /dev/null +++ b/packages/bruno-converters/tests/postman/postman-translations/postman-variables.spec.js @@ -0,0 +1,25 @@ +const { default: postmanTranslation } = require("../../../src/postman/postman-translations"); + +describe('postmanTranslations - variables commands', () => { + test('should translate variable commands correctly', () => { + const inputScript = ` + pm.environment.get('key'); + pm.environment.set('key', 'value'); + pm.variables.get('key'); + pm.variables.set('key', 'value'); + pm.collectionVariables.get('key'); + pm.collectionVariables.set('key', 'value'); + pm.expect(pm.environment.has('key')).to.be.true; + `; + const expectedOutput = ` + bru.getEnvVar('key'); + bru.setEnvVar('key', 'value'); + bru.getVar('key'); + bru.setVar('key', 'value'); + bru.getVar('key'); + bru.setVar('key', 'value'); + expect(bru.getEnvVar('key') !== undefined && bru.getEnvVar('key') !== null).to.be.true; + `; + expect(postmanTranslation(inputScript)).toBe(expectedOutput); + }); +}); \ No newline at end of file