optimise + improve tests

This commit is contained in:
William Harrison committed 2025-01-13 12:11:37 +08:00
1 parent c84fd1377c
commit bcf071b6f2
4 files changed
+244 -248

No files matched your search

+19 -4
View File
@@ -3,10 +3,25 @@ const fs = require("fs-extra");
const path = require("path");
const requiredRecordsToProxy = new Set(["A", "AAAA", "CNAME"]);
// URL records are not listed here because they are proxied by default, so they don't need the proxied flag
const domainCache = {};
function getDomainData(file) {
if (domainCache[file]) {
return domainCache[file];
}
try {
const data = fs.readJsonSync(path.join(domainsPath, file));
domainCache[file] = data;
return data;
} catch (error) {
throw new Error(`Failed to read JSON for ${file}: ${error.message}`);
}
}
function validateProxiedRecords(t, data, file) {
// Convert the Set to an array for message display
// Convert the Set to an array for message display (moved outside the loop to optimize performance)
const recordTypes = Array.from(requiredRecordsToProxy).join(", ");
if (data.proxied) {
@@ -22,9 +37,9 @@ function validateProxiedRecords(t, data, file) {
const domainsPath = path.resolve("domains");
const files = fs.readdirSync(domainsPath).filter((file) => file.endsWith(".json"));
t("Domains with proxy enabled should have at least one record that can be proxied", (t) => {
t("Domains with proxy enabled must have at least one proxy-able record", (t) => {
files.forEach((file) => {
const domain = fs.readJsonSync(path.join(domainsPath, file));
const domain = getDomainData(file);
validateProxiedRecords(t, domain, file);
});