prettier + updates to tests

This commit is contained in:
William Harrison committed 2025-02-13 18:01:15 +08:00
1 parent 0bab0f1756
commit bc55135cef
50 files changed
+372 -203

No files matched your search

+79 -23
View File
@@ -6,29 +6,30 @@ const ignoredRootJSONFiles = ["package-lock.json", "package.json"];
const requiredFields = {
owner: "object",
record: "object"
record: "object",
};
const optionalFields = {
proxied: "boolean",
redirect_config: "object"
redirect_config: "object",
};
const requiredOwnerFields = {
username: "string"
username: "string",
};
const optionalOwnerFields = {
email: "string"
email: "string",
};
const optionalRedirectConfigFields = {
custom_paths: "object",
redirect_paths: "boolean"
redirect_paths: "boolean",
};
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
const hostnameRegex = /^(?=.{1,253}$)(?:(?:[_a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+[a-zA-Z]{2,63}$/;
const hostnameRegex =
/^(?=.{1,253}$)(?:(?:[_a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+[a-zA-Z]{2,63}$/;
const exceptedDomains = require("../util/excepted.json");
const reservedDomains = require("../util/reserved.json");
@@ -52,7 +53,9 @@ function expandReservedDomains(reserved) {
}
expandedList.splice(expandedList.indexOf(item), 1);
} else {
throw new Error(`[util/reserved.json] Invalid range [${start}-${end}] in "${item}"`);
throw new Error(
`[util/reserved.json] Invalid range [${start}-${end}] in "${item}"`,
);
}
}
});
@@ -87,7 +90,11 @@ function validateFields(t, obj, fields, file, prefix = "") {
const fieldPath = prefix ? `${prefix}.${key}` : key;
if (obj.hasOwnProperty(key)) {
t.is(typeof obj[key], fields[key], `${file}: Field ${fieldPath} should be of type ${fields[key]}`);
t.is(
typeof obj[key],
fields[key],
`${file}: Field ${fieldPath} should be of type ${fields[key]}`,
);
} else if (fields === requiredFields) {
t.true(false, `${file}: Missing required field: ${fieldPath}`);
}
@@ -95,9 +102,18 @@ function validateFields(t, obj, fields, file, prefix = "") {
}
function validateFileName(t, file) {
t.true(file.endsWith(".json"), `${file}: File does not have .json extension`);
t.false(file.includes(".is-a.dev"), `${file}: File name should not contain .is-a.dev`);
t.true(file === file.toLowerCase(), `${file}: File name should be all lowercase`);
t.true(
file.endsWith(".json"),
`${file}: File does not have .json extension`,
);
t.false(
file.includes(".is-a.dev"),
`${file}: File name should not contain .is-a.dev`,
);
t.true(
file === file.toLowerCase(),
`${file}: File name should be all lowercase`,
);
// Ignore root domain
if (file !== "@.json") {
@@ -106,12 +122,27 @@ function validateFileName(t, file) {
t.regex(
subdomain + ".is-a.dev",
hostnameRegex,
`${file}: FQDN must be 1-253 characters, use letters, numbers, dots, or hyphens, and not start or end with a hyphen.`
`${file}: FQDN must be 1-253 characters, use letters, numbers, dots, or hyphens, and not start or end with a hyphen.`,
);
t.false(
expandedReservedDomains.includes(subdomain),
`${file}: Subdomain name is reserved`,
);
// Disallow nested subdomains above reserved domains
t.true(
!expandedReservedDomains.some((reserved) =>
subdomain.endsWith(`.${reserved}`),
),
`${file}: Subdomain name is reserved`,
);
t.true(!expandedReservedDomains.includes(subdomain), `${file}: Subdomain name is reserved`);
if (subdomain.split(".").length === 1 && !exceptedDomains.includes(subdomain)) {
t.false(subdomain.startsWith("_"), `${file}: Root subdomains should not start with an underscore`);
const rootSubdomain = subdomain.split(".").pop();
if (!exceptedDomains.includes(rootSubdomain)) {
t.false(
rootSubdomain.startsWith("_"),
`${file}: Root subdomains should not start with an underscore`,
);
}
}
}
@@ -119,13 +150,19 @@ function validateFileName(t, file) {
t("JSON files should not be in the root directory", (t) => {
const rootFiles = fs
.readdirSync(path.resolve())
.filter((file) => file.endsWith(".json") && !ignoredRootJSONFiles.includes(file));
.filter(
(file) =>
file.endsWith(".json") && !ignoredRootJSONFiles.includes(file),
);
t.is(rootFiles.length, 0, "JSON files should not be in the root directory");
});
t("All files should be valid JSON", (t) => {
files.forEach((file) => {
t.notThrows(() => fs.readJsonSync(path.join(domainsPath, file)), `${file}: Invalid JSON file`);
t.notThrows(
() => fs.readJsonSync(path.join(domainsPath, file)),
`${file}: Invalid JSON file`,
);
});
});
@@ -135,7 +172,10 @@ t("All files should not have duplicate keys", (t) => {
const rawData = fs.readFileSync(`${domainsPath}/${file}`, "utf8");
const duplicateKeys = findDuplicateKeys(rawData);
t.true(!duplicateKeys.length, `${file}: Duplicate keys found: ${duplicateKeys.join(", ")}`);
t.true(
!duplicateKeys.length,
`${file}: Duplicate keys found: ${duplicateKeys.join(", ")}`,
);
});
});
@@ -159,17 +199,33 @@ t("All files should have valid required and optional fields", (t) => {
// Validate optional fields for top-level and redirect config
validateFields(t, data, optionalFields, file);
if (data.redirect_config) {
validateFields(t, data.redirect_config, optionalRedirectConfigFields, file, "redirect_config");
validateFields(
t,
data.redirect_config,
optionalRedirectConfigFields,
file,
"redirect_config",
);
}
// Validate email format
if (data.owner.email) {
t.regex(data.owner.email, emailRegex, `${file}: Owner email should be a valid email address`);
t.true(!data.owner.email.endsWith("@users.noreply.github.com"), `${file}: Owner email should not be a GitHub no-reply email`);
t.regex(
data.owner.email,
emailRegex,
`${file}: Owner email should be a valid email address`,
);
t.false(
data.owner.email.endsWith("@users.noreply.github.com"),
`${file}: Owner email should not be a GitHub no-reply email`,
);
}
// Ensure 'record' field is not empty
t.true(Object.keys(data.record).length > 0, `${file}: Missing DNS records`);
t.true(
Object.keys(data.record).length > 0,
`${file}: Missing DNS records`,
);
});
});
@@ -180,7 +236,7 @@ t("Reserved domains file should be valid", (t) => {
t.regex(
item,
subdomainRegex,
`[util/reserved-domains.json] Invalid subdomain name "${item}" at index ${index}`
`[util/reserved-domains.json] Invalid subdomain name "${item}" at index ${index}`,
);
});