Skip to content

Commit 1911e71

Browse files
author
Zvonimir Sabljic
committed
Added unit tests
1 parent 2a740b4 commit 1911e71

27 files changed

+1306
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const { cleanupGPTResponse } = require('../../../../../src/helpers/api');
2+
3+
describe('cleanupGPTResponse', () => {
4+
test('should remove code block markdown in the start and end', () => {
5+
const input = "```\nThis is a test\n```";
6+
const expected = 'This is a test\n';
7+
expect(cleanupGPTResponse(input)).toBe(expected);
8+
});
9+
10+
test('should return input unchanged if it does not begin with code block markdown', () => {
11+
const input = 'This is another test';
12+
const expected = 'This is another test';
13+
expect(cleanupGPTResponse(input)).toBe(expected);
14+
});
15+
16+
test('should handle empty input string', () => {
17+
const input = '';
18+
const expected = '';
19+
expect(cleanupGPTResponse(input)).toBe(expected);
20+
});
21+
22+
test('should handle input string with only code block markdown and nothing else', () => {
23+
const input = '```\n```';
24+
const expected = '';
25+
expect(cleanupGPTResponse(input)).toBe(expected);
26+
});
27+
28+
test('should handle multiple lines in code block markdown', () => {
29+
const input = '```\nLine 1\nLine 2\nLine 3```';
30+
const expected = 'Line 1\nLine 2\nLine 3';
31+
expect(cleanupGPTResponse(input)).toBe(expected);
32+
});
33+
34+
test('should not remove code block markdown from the middle of the string', () => {
35+
const input = 'This is a test with ```code inside``` the string';
36+
const expected = 'This is a test with ```code inside``` the string';
37+
expect(cleanupGPTResponse(input)).toBe(expected);
38+
});
39+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
describe("extractDataFromMongoRes tests", () => {
2+
const { extractDataFromMongoRes } = require("../../../../../src/helpers/mongodb");
3+
4+
test("should return empty object for empty input", () => {
5+
expect(extractDataFromMongoRes({})).toEqual({});
6+
});
7+
8+
test("should return input object when not a CommandResult or special object", () => {
9+
const input = { a: 1, b: 2 };
10+
expect(extractDataFromMongoRes(input)).toEqual(input);
11+
});
12+
13+
test("should return result object for CommandResult input", () => {
14+
const input = {
15+
result: { a: 1, b: 2 },
16+
electionId: "electionId",
17+
opTime: "opTime",
18+
$clusterTime: "clusterTime",
19+
operationTime: "operationTime",
20+
__proto__: { constructor: { name: "CommandResult" } },
21+
};
22+
const expected = { a: 1, b: 2 };
23+
expect(extractDataFromMongoRes(input)).toEqual(expected);
24+
});
25+
26+
test("should return value object for special object input", () => {
27+
const input = {
28+
value: { a: 1, b: 2 },
29+
lastErrorObject: "lastErrorObject",
30+
ok: "ok",
31+
__proto__: { constructor: { name: "Object" } },
32+
};
33+
const expected = { a: 1, b: 2 };
34+
expect(extractDataFromMongoRes(input)).toEqual(expected);
35+
});
36+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const { jsonObjToMongo } = require("../../../../../src/helpers/mongodb");
2+
const {ObjectId} = require("mongodb");
3+
4+
describe('jsonObjToMongo', () => {
5+
test('should return correct output for an array input', () => {
6+
const input = [{ _id: 'ObjectId("60f1e7b5d119076884d289ad")', createdAt: 'Date("2021-08-10T08:47:00.000Z")' }];
7+
const expected = [{ _id: new ObjectId("60f1e7b5d119076884d289ad"), createdAt: new Date("2021-08-10T08:47:00.000Z") }];
8+
expect(jsonObjToMongo(input)).toEqual(expected);
9+
});
10+
11+
test('should return correct output for a nested object input', () => {
12+
const input = { outer: { _id: 'ObjectId("60f1e7b5d119076884d289ad")', createdAt: 'Date("2021-08-10T08:47:00.000Z")' } };
13+
const expected = { outer: { _id: new ObjectId("60f1e7b5d119076884d289ad"), createdAt: new Date("2021-08-10T08:47:00.000Z") } };
14+
expect(jsonObjToMongo(input)).toEqual(expected);
15+
});
16+
17+
test('should return correct output for a string input', () => {
18+
const input = 'ObjectId("60f1e7b5d119076884d289ad")';
19+
const expected = new ObjectId("60f1e7b5d119076884d289ad");
20+
expect(jsonObjToMongo(input)).toEqual(expected);
21+
});
22+
23+
test('should return correct output for a date string input', () => {
24+
const input = 'Date("2021-08-10T08:47:00.000Z")';
25+
const expected = new Date("2021-08-10T08:47:00.000Z");
26+
expect(jsonObjToMongo(input)).toEqual(expected);
27+
});
28+
29+
test('should return correct output for a RegExp string input', () => {
30+
const input = '/test\\wstring/i';
31+
const expected = /test\wstring/i;
32+
expect(jsonObjToMongo(input)).toEqual(expected);
33+
});
34+
35+
test('should return correct output for an ObjectID string input', () => {
36+
const input = '60f1e7b5d119076884d289ad';
37+
const expected = new ObjectId("60f1e7b5d119076884d289ad");
38+
expect(jsonObjToMongo(input)).toEqual(expected);
39+
});
40+
41+
test('should return the input for non-matching string input', () => {
42+
const input = 'non-matching string input';
43+
const expected = 'non-matching string input';
44+
expect(jsonObjToMongo(input)).toEqual(expected);
45+
});
46+
47+
test('should return undefined for undefined input', () => {
48+
expect(jsonObjToMongo(undefined)).toBeUndefined();
49+
});
50+
51+
test('should return null for null input', () => {
52+
expect(jsonObjToMongo(null)).toBeNull();
53+
});
54+
55+
test('should return the input for non-object, non-array input', () => {
56+
const input = 123;
57+
const expected = 123;
58+
expect(jsonObjToMongo(input)).toEqual(expected);
59+
});
60+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {ObjectId} from "mongodb";
2+
import {mongoObjToJson} from "../../../../../src/helpers/mongodb";
3+
4+
describe("mongoObjToJson", () => {
5+
test('should convert ObjectId to string format', () => {
6+
const objectId = new ObjectId();
7+
expect(mongoObjToJson(objectId)).toEqual(`ObjectId("${objectId.toString()}")`);
8+
});
9+
10+
test('should convert Date to string format', () => {
11+
const date = new Date();
12+
expect(mongoObjToJson({ time: date })).toEqual({ time: `Date("${date.toISOString()}")` });
13+
});
14+
15+
test('should convert RegExp to string format', () => {
16+
const regex = /^test$/i;
17+
expect(mongoObjToJson({ pattern: regex })).toEqual({ pattern: `RegExp("${regex.toString()}")` });
18+
});
19+
20+
test('should handle nested objects and arrays', () => {
21+
const objectId = new ObjectId();
22+
const date = new Date();
23+
const regex = /^test$/i;
24+
const input = {
25+
_id: objectId,
26+
dates: [{ time: date }],
27+
regexArr: [regex],
28+
nestedObj: { _id: objectId, time: date, patternRegex: regex },
29+
};
30+
const expectedResult = {
31+
_id: `ObjectId("${objectId.toString()}")`,
32+
dates: [{ time: `Date("${date.toISOString()}")` }],
33+
regexArr: [`RegExp("${regex.toString()}")`],
34+
nestedObj: {
35+
_id: `ObjectId("${objectId.toString()}")`,
36+
time: `Date("${date.toISOString()}")`,
37+
patternRegex: `RegExp("${regex.toString()}")`,
38+
},
39+
};
40+
expect(mongoObjToJson(input)).toEqual(expectedResult);
41+
});
42+
43+
test('should return null if input is null', () => {
44+
expect(mongoObjToJson(null)).toBeNull();
45+
});
46+
47+
test('should return undefined if input is undefined', () => {
48+
expect(mongoObjToJson(undefined)).toBeUndefined();
49+
});
50+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
describe('searchAllModuleFolders', () => {
2+
const fs = require('fs');
3+
const path = require('path');
4+
const { searchAllModuleFolders } = require("../../../../../src/helpers/starting");
5+
6+
test('returns empty object when no modules', () => {
7+
const result = searchAllModuleFolders('.', []);
8+
expect(result).toEqual({});
9+
});
10+
11+
test('returns empty module paths when modules not found', () => {
12+
const result = searchAllModuleFolders('.', ['nonexistent']);
13+
expect(result).toEqual({ nonexistent: [] });
14+
});
15+
16+
test('finds nested node_modules directories', () => {
17+
const module1Path = path.join('.', 'testdir', 'node_modules', 'module1');
18+
fs.mkdirSync(module1Path, { recursive: true });
19+
const result = searchAllModuleFolders('.', ['module1']);
20+
expect(result).toEqual({ module1: [module1Path] });
21+
fs.rmdirSync(path.join('.', 'testdir'), { recursive: true });
22+
});
23+
24+
test('finds multiple node_modules directories for multiple modules', () => {
25+
const module1Path = path.join('.', 'testdir', 'node_modules', 'module1');
26+
const module2Path = path.join('.', 'testdir', 'node_modules', 'module2');
27+
28+
fs.mkdirSync(module1Path, { recursive: true });
29+
fs.mkdirSync(module2Path, { recursive: true });
30+
31+
const result = searchAllModuleFolders('.', ['module1', 'module2']);
32+
expect(result).toEqual({
33+
module1: [module1Path],
34+
module2: [module2Path]
35+
});
36+
37+
fs.rmdirSync(path.join('.', 'testdir'), { recursive: true });
38+
});
39+
40+
test('finds deeply nested node_modules directories', () => {
41+
const module1Path = path.join('.', 'testdir', 'subdir', 'node_modules', 'module1');
42+
fs.mkdirSync(module1Path, { recursive: true });
43+
44+
const result = searchAllModuleFolders('.', ['module1']);
45+
expect(result).toEqual({ module1: [module1Path] });
46+
47+
fs.rmdirSync(path.join('.', 'testdir'), { recursive: true });
48+
});
49+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
describe('checkDirectoryExists', () => {
2+
3+
test('should return true when directory exists', async () => {
4+
const { checkDirectoryExists } = require('../../../../../src/utils/common.js');
5+
const fs = require('fs');
6+
7+
fs.mkdirSync('test-dir');
8+
const exists = await checkDirectoryExists('test-dir');
9+
fs.rmdirSync('test-dir');
10+
11+
expect(exists).toBe(true);
12+
});
13+
14+
test('should return false when directory does not exist', async () => {
15+
const { checkDirectoryExists } = require('../../../../../src/utils/common.js');
16+
17+
const exists = await checkDirectoryExists('nonexistent-dir');
18+
19+
expect(exists).toBe(false);
20+
});
21+
22+
test('should return false when provided path is to a file', async () => {
23+
const { checkDirectoryExists } = require('../../../../../src/utils/common.js');
24+
const fs = require('fs');
25+
26+
fs.writeFileSync('test-file.txt', 'test contents');
27+
const exists = await checkDirectoryExists('test-file.txt');
28+
fs.unlinkSync('test-file.txt');
29+
30+
expect(exists).toBe(false);
31+
});
32+
33+
test('should handle null input', async () => {
34+
const { checkDirectoryExists } = require('../../../../../src/utils/common.js');
35+
36+
await expect(checkDirectoryExists(null)).rejects.toThrow();
37+
});
38+
39+
test('should handle undefined input', async () => {
40+
const { checkDirectoryExists } = require('../../../../../src/utils/common.js');
41+
42+
await expect(checkDirectoryExists(undefined)).rejects.toThrow();
43+
});
44+
45+
});
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
describe('compareJson tests', () => {
2+
const { compareJson } = require('../../../../../src/utils/common.js');
3+
4+
test('Should return true for identical objects', () => {
5+
const obj1 = { key: 'value' };
6+
const obj2 = { key: 'value' };
7+
expect(compareJson(obj1, obj2)).toBe(true);
8+
});
9+
10+
test('Should return false for different objects', () => {
11+
const obj1 = { key: 'value' };
12+
const obj2 = { key: 'differentValue' };
13+
expect(compareJson(obj1, obj2)).toBe(false);
14+
});
15+
16+
test('Should return true for identical arrays', () => {
17+
const arr1 = [1, 2, 3];
18+
const arr2 = [1, 2, 3];
19+
expect(compareJson(arr1, arr2)).toBe(true);
20+
});
21+
22+
test('Should return false for different arrays', () => {
23+
const arr1 = [1, 2, 3];
24+
const arr2 = [1, 2, 4];
25+
expect(compareJson(arr1, arr2)).toBe(false);
26+
});
27+
28+
test('Should return true for identical nested objects', () => {
29+
const obj1 = { key: { nestedKey: 'value' } };
30+
const obj2 = { key: { nestedKey: 'value' } };
31+
expect(compareJson(obj1, obj2)).toBe(true);
32+
});
33+
34+
test('Should return false for different nested objects', () => {
35+
const obj1 = { key: { nestedKey: 'value' } };
36+
const obj2 = { key: { nestedKey: 'differentValue' } };
37+
expect(compareJson(obj1, obj2)).toBe(false);
38+
});
39+
40+
test('Should return true for identical objects with Date', () => {
41+
const date = new Date();
42+
const obj1 = { key: date };
43+
const obj2 = { key: date };
44+
expect(compareJson(obj1, obj2)).toBe(true);
45+
});
46+
47+
test('Should return false for different objects with Date', () => {
48+
const date1 = new Date();
49+
const date2 = new Date(date1.getTime() + 1000);
50+
const obj1 = { key: date1 };
51+
const obj2 = { key: date2 };
52+
expect(compareJson(obj1, obj2)).toBe(true);
53+
});
54+
55+
test('Should return true for identical JSON strings', () => {
56+
const jsonString1 = JSON.stringify({ key: 'value' });
57+
const jsonString2 = JSON.stringify({ key: 'value' });
58+
expect(compareJson(jsonString1, jsonString2)).toBe(true);
59+
});
60+
61+
test('Should return false for different JSON strings', () => {
62+
const jsonString1 = JSON.stringify({ key: 'value' });
63+
const jsonString2 = JSON.stringify({ key: 'differentValue' });
64+
expect(compareJson(jsonString1, jsonString2)).toBe(false);
65+
});
66+
67+
test('Should return true for mixed identical data types', () =>{
68+
const date = new Date();
69+
const data1 = { key1: 'value', key2: date, key3: [1, 2, 3] };
70+
const data2 = { key1: 'value', key2: date, key3: [1, 2, 3] };
71+
expect(compareJson(data1, data2)).toBe(true);
72+
});
73+
74+
test('Should return true for strict mode with identical ObjectId strings', () => {
75+
const idString1 = 'ObjectId("123456789012345678901234")';
76+
const idString2 = 'ObjectId("123456789012345678901234")';
77+
expect(compareJson(idString1, idString2, true)).toBe(true);
78+
});
79+
80+
test('Should return false for strict mode with different ObjectId strings', () => {
81+
const idString1 = 'ObjectId("123456789012345678901234")';
82+
const idString2 = 'ObjectId("123456789012345678901235")';
83+
expect(compareJson(idString1, idString2, true)).toBe(false);
84+
});
85+
86+
test('Should return true for non-strict mode with different ObjectId strings', () => {
87+
const idString1 = 'ObjectId("123456789012345678901234")';
88+
const idString2 = 'ObjectId("123456789012345678901235")';
89+
expect(compareJson(idString1, idString2, false)).toBe(true);
90+
});
91+
92+
test('Should return false for comparing an object and an array', () => {
93+
const obj = { key: 'value' };
94+
const arr = ['value'];
95+
expect(compareJson(obj, arr)).toBe(false);
96+
});
97+
98+
test('Should return false for comparing an object and a string', () => {
99+
const obj = { key: 'value' };
100+
const str = 'value';
101+
expect(compareJson(obj, str)).toBe(false);
102+
});
103+
104+
test('Should return false for comparing an array and a string', () => {
105+
const arr = ['value'];
106+
const str = 'value';
107+
expect(compareJson(arr, str)).toBe(false);
108+
});
109+
});

0 commit comments

Comments
 (0)