ftmocks-utils 1.3.1 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/compare-utils.js +151 -0
- package/src/index.js +26 -316
- package/src/log-utils.js +103 -0
- package/src/mock-utils.js +101 -0
- package/src/rank-compare-utils.js +62 -0
- package/src/recorder.js +289 -271
- package/src/test-utils.js +49 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { getMockDir, nameToFolder } = require("./common-utils");
|
|
4
|
+
|
|
5
|
+
const getDefaultMockDataFromConfig = (testConfig) => {
|
|
6
|
+
const defaultPath = path.join(
|
|
7
|
+
getMockDir(testConfig),
|
|
8
|
+
"defaultMocks",
|
|
9
|
+
"_mock_list.json"
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const defaultData = fs.readFileSync(defaultPath, "utf8");
|
|
14
|
+
let parsedData = JSON.parse(defaultData);
|
|
15
|
+
|
|
16
|
+
// Read and attach mock data for each entry in parsedData
|
|
17
|
+
parsedData.forEach((entry) => {
|
|
18
|
+
const mockFilePath = path.join(
|
|
19
|
+
getMockDir(testConfig),
|
|
20
|
+
"defaultMocks",
|
|
21
|
+
`mock_${entry.id}.json`
|
|
22
|
+
);
|
|
23
|
+
try {
|
|
24
|
+
const mockData = fs.readFileSync(mockFilePath, "utf8");
|
|
25
|
+
entry.fileContent = JSON.parse(mockData);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.error(`Error reading mock data for ${entry.id}:`, error);
|
|
28
|
+
return entry; // Return the original entry if there's an error
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return parsedData;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error(`Error reading or parsing default mocks:`, error);
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const loadMockDataFromConfig = (testConfig, _testName) => {
|
|
39
|
+
try {
|
|
40
|
+
let testName = _testName;
|
|
41
|
+
if (!testName) {
|
|
42
|
+
// Read the test ID from mockServer.config.json
|
|
43
|
+
const configPath = path.join(
|
|
44
|
+
getMockDir(testConfig),
|
|
45
|
+
"mockServer.config.json"
|
|
46
|
+
);
|
|
47
|
+
const configData = fs.readFileSync(configPath, "utf8");
|
|
48
|
+
const config = JSON.parse(configData);
|
|
49
|
+
testName = config.testName;
|
|
50
|
+
}
|
|
51
|
+
// Read the tests from testConfig
|
|
52
|
+
const mocksPath = path.join(
|
|
53
|
+
getMockDir(testConfig),
|
|
54
|
+
`test_${nameToFolder(testName)}`,
|
|
55
|
+
"_mock_list.json"
|
|
56
|
+
);
|
|
57
|
+
const mocksData = fs.readFileSync(mocksPath, "utf8");
|
|
58
|
+
const mocks = JSON.parse(mocksData);
|
|
59
|
+
|
|
60
|
+
mocks.forEach((mock) => {
|
|
61
|
+
const fileContent = JSON.parse(
|
|
62
|
+
fs.readFileSync(
|
|
63
|
+
path.join(
|
|
64
|
+
getMockDir(testConfig),
|
|
65
|
+
`test_${nameToFolder(testName)}`,
|
|
66
|
+
`mock_${mock.id}.json`
|
|
67
|
+
),
|
|
68
|
+
"utf8"
|
|
69
|
+
)
|
|
70
|
+
);
|
|
71
|
+
mock.fileContent = fileContent;
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
return mocks;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error("Error loading test data:", error.message);
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
async function resetAllMockStats({ testMockData, testConfig, testName }) {
|
|
82
|
+
for (let i = 0; i < testMockData.length; i++) {
|
|
83
|
+
const tmd = testMockData[i];
|
|
84
|
+
const mockFilePath = path.join(
|
|
85
|
+
getMockDir(testConfig),
|
|
86
|
+
`test_${nameToFolder(testName)}`,
|
|
87
|
+
`mock_${tmd.id}.json`
|
|
88
|
+
);
|
|
89
|
+
tmd.fileContent.served = false;
|
|
90
|
+
await fs.writeFileSync(
|
|
91
|
+
mockFilePath,
|
|
92
|
+
JSON.stringify(tmd.fileContent, null, 2)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
module.exports = {
|
|
98
|
+
getDefaultMockDataFromConfig,
|
|
99
|
+
loadMockDataFromConfig,
|
|
100
|
+
resetAllMockStats,
|
|
101
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const { clearNulls, charDifference, processURL } = require("./common-utils");
|
|
2
|
+
const { FtJSON } = require("./json-utils");
|
|
3
|
+
const { isUrlAndMethodSame } = require("./compare-utils");
|
|
4
|
+
|
|
5
|
+
const getSameRequestRank = (req1, req2) => {
|
|
6
|
+
let rank = 1;
|
|
7
|
+
clearNulls(req1.postData);
|
|
8
|
+
clearNulls(req2.postData);
|
|
9
|
+
// Compare query strings
|
|
10
|
+
const queryDiff = charDifference(
|
|
11
|
+
req1.url.split("?")[1] || "",
|
|
12
|
+
req2.url.split("?")[1] || ""
|
|
13
|
+
);
|
|
14
|
+
rank = rank + queryDiff;
|
|
15
|
+
// Compare post data
|
|
16
|
+
const postDataDiff = charDifference(
|
|
17
|
+
FtJSON.stringify(req1.postData || {}),
|
|
18
|
+
FtJSON.stringify(req2.postData || {})
|
|
19
|
+
);
|
|
20
|
+
rank = rank + postDataDiff;
|
|
21
|
+
return rank;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function getCompareRankMockToFetchRequest(mock, fetchReq) {
|
|
25
|
+
try {
|
|
26
|
+
const mockURL = processURL(
|
|
27
|
+
mock.fileContent.url,
|
|
28
|
+
mock.fileContent.ignoreParams
|
|
29
|
+
);
|
|
30
|
+
const reqURL = processURL(fetchReq.url, mock.fileContent.ignoreParams);
|
|
31
|
+
if (
|
|
32
|
+
!isUrlAndMethodSame(
|
|
33
|
+
{ url: mockURL, method: mock.fileContent.method },
|
|
34
|
+
{ url: reqURL, method: fetchReq.options.method || "GET" }
|
|
35
|
+
)
|
|
36
|
+
) {
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
const postData = mock.fileContent.request?.postData?.text
|
|
40
|
+
? FtJSON.parse(mock.fileContent.request?.postData?.text)
|
|
41
|
+
: mock.fileContent.request?.postData;
|
|
42
|
+
return getSameRequestRank(
|
|
43
|
+
{ url: mockURL, method: mock.fileContent.method, postData },
|
|
44
|
+
{
|
|
45
|
+
method: fetchReq.options.method || "GET",
|
|
46
|
+
postData: fetchReq.options.body?.length
|
|
47
|
+
? FtJSON.parse(fetchReq.options.body)
|
|
48
|
+
: fetchReq.options.body,
|
|
49
|
+
url: reqURL,
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
} catch (e) {
|
|
53
|
+
console.error("error at getCompareRankMockToFetchRequest", mock, fetchReq);
|
|
54
|
+
console.error(e);
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = {
|
|
60
|
+
getSameRequestRank,
|
|
61
|
+
getCompareRankMockToFetchRequest,
|
|
62
|
+
};
|