2023-02-12 07:40:49 +00:00
|
|
|
import "dotenv/config";
|
|
|
|
|
|
|
|
import { getJSON } from "../modules/api.js";
|
|
|
|
import { services } from "../modules/config.js";
|
|
|
|
import loadJSON from "../modules/sub/loadJSON.js";
|
|
|
|
import { checkJSONPost } from "../modules/sub/utils.js";
|
|
|
|
|
2023-02-13 13:44:58 +00:00
|
|
|
let tests = loadJSON('./src/test/tests.json');
|
2023-02-12 07:40:49 +00:00
|
|
|
|
|
|
|
let noTest = [];
|
|
|
|
let failed = [];
|
|
|
|
let success = 0;
|
|
|
|
|
2023-02-13 13:44:58 +00:00
|
|
|
function addToFail(service, testName, url, status, response) {
|
2023-02-12 07:40:49 +00:00
|
|
|
failed.push({
|
|
|
|
service: service,
|
|
|
|
name: testName,
|
|
|
|
url: url,
|
2023-02-13 13:44:58 +00:00
|
|
|
status: status,
|
2023-02-12 07:40:49 +00:00
|
|
|
response: response
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for (let i in services) {
|
|
|
|
if (tests[i]) {
|
|
|
|
console.log(`\nRunning tests for ${i}...\n`)
|
|
|
|
for (let k = 0; k < tests[i].length; k++) {
|
|
|
|
let test = tests[i][k];
|
|
|
|
|
|
|
|
console.log(`Running test ${k+1}: ${test.name}`);
|
|
|
|
console.log('params:');
|
|
|
|
let params = {...{url: test.url}, ...test.params};
|
|
|
|
console.log(params);
|
|
|
|
|
|
|
|
let chck = checkJSONPost(params);
|
|
|
|
if (chck) {
|
|
|
|
chck["ip"] = "d21ec524bc2ade41bef569c0361ac57728c69e2764b5cb3cb310fe36568ca53f"; // random sha256
|
|
|
|
let j = await getJSON(chck["url"], "en", chck);
|
|
|
|
console.log('\nReceived:');
|
|
|
|
console.log(j)
|
|
|
|
if (j.status === test.expected.code && j.body.status === test.expected.status) {
|
|
|
|
console.log("\n✅ Success.\n");
|
|
|
|
success++
|
|
|
|
} else {
|
|
|
|
console.log(`\n❌ Fail. Expected: ${test.expected.code} & ${test.expected.status}, received: ${j.status} & ${j.body.status}\n`);
|
2023-02-13 13:44:58 +00:00
|
|
|
addToFail(i, test.name, test.url, j.body.status, j)
|
2023-02-12 07:40:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("\n❌ couldn't validate the request JSON.\n");
|
2023-02-13 13:44:58 +00:00
|
|
|
addToFail(i, test.name, test.url, "unknown", {})
|
2023-02-12 07:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log("\n\n")
|
|
|
|
} else {
|
|
|
|
console.warn(`No tests found for ${i}.`);
|
|
|
|
noTest.push(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:44:58 +00:00
|
|
|
console.log(`✅ ${success} tests succeeded.`);
|
2023-02-12 07:40:49 +00:00
|
|
|
console.log(`❌ ${failed.length} tests failed.`);
|
|
|
|
console.log(`❔ ${noTest.length} services weren't tested.`);
|
|
|
|
|
2023-02-13 13:44:58 +00:00
|
|
|
if (failed.length > 0) {
|
|
|
|
console.log(`\nFailed tests:`);
|
|
|
|
console.log(failed)
|
|
|
|
}
|
2023-02-12 07:40:49 +00:00
|
|
|
|
2023-02-13 13:44:58 +00:00
|
|
|
if (noTest.length > 0) {
|
|
|
|
console.log(`\nMissing tests:`);
|
|
|
|
console.log(noTest)
|
|
|
|
}
|