From e8affcfde9437cd3dfd63d28374f97d038cc984e Mon Sep 17 00:00:00 2001 From: Sanjai Kumar <161328623+sanjaikumar-bruno@users.noreply.github.com> Date: Tue, 15 Apr 2025 17:24:24 +0530 Subject: [PATCH] feat: add tests for mock variable interpolation in interpolate function (#4507) * feat: add tests for mock variable interpolation in interpolate function * test: enhance mock variable interpolation tests for additional types and JSON validation --------- Co-authored-by: sanjai0py --- .../src/interpolate/index.spec.ts | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/packages/bruno-common/src/interpolate/index.spec.ts b/packages/bruno-common/src/interpolate/index.spec.ts index 9dc76b7f1..40ca49416 100644 --- a/packages/bruno-common/src/interpolate/index.spec.ts +++ b/packages/bruno-common/src/interpolate/index.spec.ts @@ -354,3 +354,68 @@ describe('interpolate - recursive', () => { }`); }); }); + +describe('interpolate - mock variable interpolation', () => { + it('should replace mock variables with generated values', () => { + const inputString = '{{$randomInt}}, {{$randomIP}}, {{$randomIPV4}}, {{$randomIPV6}}, {{$randomBoolean}}'; + + const result = interpolate(inputString, {}); + + // Validate the result using regex patterns + const randomIntPattern = /^\d+$/; + const randomIPPattern = /^([\da-f]{1,4}:){7}[\da-f]{1,4}$|^(\d{1,3}\.){3}\d{1,3}$/; + const randomIPV4Pattern = /^(\d{1,3}\.){3}\d{1,3}$/; + const randomIPV6Pattern = /^([\da-f]{1,4}:){7}[\da-f]{1,4}$/; + const randomBooleanPattern = /^(true|false)$/; + + const [randomInt, randomIP, randomIPV4, randomIPV6, randomBoolean] = result.split(', '); + + expect(randomIntPattern.test(randomInt)).toBe(true); + expect(randomIPPattern.test(randomIP)).toBe(true); + expect(randomIPV4Pattern.test(randomIPV4)).toBe(true); + expect(randomIPV6Pattern.test(randomIPV6)).toBe(true); + expect(randomBooleanPattern.test(randomBoolean)).toBe(true); + });; + + it('should leave mock variables unchanged if no corresponding function exists', () => { + const inputString = 'Random number: {{$nonExistentMock}}'; + + const result = interpolate(inputString, {}); + + expect(result).toBe('Random number: {{$nonExistentMock}}'); + }); + + it('should escape special characters in mock variable values and produce valid JSON when escapeJSONStrings is true', () => { + const inputString = '{"escapedValue": "{{$randomLoremParagraphs}}"}'; + + expect(() => { + const result = interpolate(inputString, {}, { escapeJSONStrings: true }); + JSON.parse(result); // This should not throw an error + }).not.toThrow(); + }); + + it('should not produce valid JSON when escapeJSONStrings is false', () => { + const inputString = '{"escapedValue": "{{$randomLoremParagraphs}}"}'; + + expect(() => { + const result = interpolate(inputString, {}, { escapeJSONStrings: false }); + JSON.parse(result); // This should throw an error + }).toThrow(); + }); + + it('should throw an error when producing invalid JSON regardless of escapeJSONStrings option', () => { + const inputString = '{"escapedValue": "{{$randomLoremParagraphs}}"}'; + + // Test without providing the options argument + expect(() => { + const result = interpolate(inputString, {}); + JSON.parse(result); // This should throw an error + }).toThrow(); + + // Test with escapeJSONStrings explicitly set to false + expect(() => { + const result = interpolate(inputString, {}, { escapeJSONStrings: false }); + JSON.parse(result); // This should throw an error + }).toThrow(); + }); +});