diff --git a/tests/json.test.js b/tests/json.test.js index 80d83c9ab..16dd360a2 100644 --- a/tests/json.test.js +++ b/tests/json.test.js @@ -35,7 +35,7 @@ const reservedDomains = require("../util/reserved-domains.json"); const domainsPath = path.resolve("domains"); const files = fs.readdirSync(domainsPath); -const expandReservedDomains = (reserved) => { +function expandReservedDomains(reserved) { const expandedList = [...reserved]; reserved.forEach((item) => { @@ -58,10 +58,30 @@ const expandReservedDomains = (reserved) => { }); return expandedList; -}; +} const expandedReservedDomains = expandReservedDomains(reservedDomains); +function findDuplicateKeys(jsonString) { + const keyPattern = /"([^"]+)"(?=\s*:)/g; + const keys = []; + let match; + + // Find all keys in the JSON string + while ((match = keyPattern.exec(jsonString)) !== null) { + keys.push(match[1]); + } + + // Count occurrences of each key + const keyCount = {}; + keys.forEach((key) => { + keyCount[key] = (keyCount[key] || 0) + 1; + }); + + // Return keys that occur more than once + return Object.keys(keyCount).filter((key) => keyCount[key] > 1); +} + function validateFields(t, obj, fields, file, prefix = "") { Object.keys(fields).forEach((key) => { const fieldPath = prefix ? `${prefix}.${key}` : key; @@ -109,6 +129,16 @@ t("All files should be valid JSON", (t) => { }); }); +t("All files should not have duplicate keys", (t) => { + files.forEach((file) => { + // Parse JSON as a string because JS automatically gets the last key if there are duplicates + const rawData = fs.readFileSync(`${domainsPath}/${file}`, "utf8"); + const duplicateKeys = findDuplicateKeys(rawData); + + t.true(!duplicateKeys.length, `${file}: Duplicate keys found: ${duplicateKeys.join(", ")}`); + }); +}); + t("All files should have valid file names", (t) => { files.forEach((file) => { validateFileName(t, file); diff --git a/tests/records.test.js b/tests/records.test.js index a13530cf5..8a1bbabbc 100644 --- a/tests/records.test.js +++ b/tests/records.test.js @@ -275,13 +275,3 @@ t("All files should have valid record types", (t) => { t.pass(); }); - -t("All files should not have duplicate record keys", (t) => { - files.forEach((file) => { - const data = getDomainData(file); - const recordKeys = Object.keys(data.record); - const uniqueRecordKeys = new Set(recordKeys); - - t.is(recordKeys.length, uniqueRecordKeys.size, `${file}: Duplicate record keys found`); - }); -});