Skip to content

Commit f87ff73

Browse files
authored
Merge pull request #36 from Pythagora-io/unit_tests
Unit tests
2 parents 43f78e2 + aadbcd0 commit f87ff73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2738
-430
lines changed

README.md

Lines changed: 110 additions & 337 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pythagora",
3-
"version": "0.0.59",
3+
"version": "0.0.60",
44
"author": {
55
"name": "Zvonimir Sabljic",
66
"email": "[email protected]"
@@ -15,7 +15,8 @@
1515
"main": "src/Pythagora.js",
1616
"scripts": {
1717
"publish-pythagora": "src/bin/publish.bash",
18-
"prepublishOnly": "node src/bin/prepublish.js"
18+
"prepublishOnly": "node src/bin/prepublish.js",
19+
"test": "npx jest ./pythagora_tests/ --coverage"
1920
},
2021
"bin": {
2122
"pythagora": "src/bin/run.js"
@@ -24,15 +25,23 @@
2425
"node": ">=14.x"
2526
},
2627
"dependencies": {
28+
"@babel/generator": "^7.22.3",
29+
"@babel/parser": "^7.22.3",
30+
"@babel/traverse": "^7.22.1",
2731
"@mswjs/interceptors": "^0.19.2",
2832
"axios": "^1.2.2",
33+
"blessed": "^0.1.81",
2934
"body-parser": "^1.20.1",
35+
"handlebars": "^4.7.7",
3036
"jest": "^29.5.0",
3137
"lodash": "^4.17.21",
3238
"nyc": "^15.1.0",
3339
"tryrequire": "^3.0.0",
3440
"uuid": "^9.0.0"
3541
},
42+
"devDependencies": {
43+
"mongodb": "^5.6.0"
44+
},
3645
"contributors": [
3746
{
3847
"name": "Zvonimir Sabljic",
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+
const {ObjectId} = require("mongodb");
2+
const {mongoObjToJson} = require("../../../../../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 = new RegExp(/^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+
});

0 commit comments

Comments
 (0)