-
-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathcheck-test-ids.js
More file actions
140 lines (113 loc) · 3.58 KB
/
check-test-ids.js
File metadata and controls
140 lines (113 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as fs from "node:fs";
import * as path from "node:path";
import * as crypto from "node:crypto";
import jsonStringify from "json-stringify-deterministic";
import { normalize } from "./normalize.js";
import { loadRemotes } from "./load-remotes.js";
// Helpers
function* jsonFiles(dir) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
yield* jsonFiles(full);
} else if (entry.isFile() && entry.name.endsWith(".json")) {
yield full;
}
}
}
function dialectFromDir(dir) {
const draft = path.basename(dir);
switch (draft) {
case "draft2020-12":
return "https://json-schema.org/draft/2020-12/schema";
case "draft2019-09":
return "https://json-schema.org/draft/2019-09/schema";
case "draft7":
return "http://json-schema.org/draft-07/schema#";
case "draft6":
return "http://json-schema.org/draft-06/schema#";
case "draft4":
return "http://json-schema.org/draft-04/schema#";
default:
throw new Error(`Unknown draft directory: ${draft}`);
}
}
function generateTestId(normalizedSchema, testData, testValid) {
return crypto
.createHash("md5")
.update(
jsonStringify(normalizedSchema) +
jsonStringify(testData) +
testValid
)
.digest("hex");
}
async function checkVersion(dir) {
const missingIdFiles = new Set();
const mismatchedIdFiles = new Set();
const dialectUri = dialectFromDir(dir);
console.log(`Checking tests in ${dir}...`);
console.log(`Using dialect: ${dialectUri}`);
// Load remotes ONCE for this dialect
const remotesPath = "./remotes";
if (fs.existsSync(remotesPath)) {
loadRemotes(dialectUri, remotesPath);
}
for (const file of jsonFiles(dir)) {
const testCases = JSON.parse(fs.readFileSync(file, "utf8"));
for (const testCase of testCases) {
const normalizedSchema = await normalize(testCase.schema, dialectUri);
for (const test of testCase.tests) {
if (!test.id) {
missingIdFiles.add(file);
console.log(
` ✗ Missing ID: ${file} | ${testCase.description} | ${test.description}`
);
continue;
}
const expectedId = generateTestId(
normalizedSchema,
test.data,
test.valid
);
if (test.id !== expectedId) {
mismatchedIdFiles.add(file);
console.log(` ✗ Mismatched ID: ${file}`);
console.log(
` Test: ${testCase.description} | ${test.description}`
);
console.log(` Current ID: ${test.id}`);
console.log(` Expected ID: ${expectedId}`);
}
}
}
}
//Summary
console.log("\n" + "=".repeat(60));
console.log("Summary:");
console.log("=".repeat(60));
console.log("\nFiles with missing IDs:");
missingIdFiles.size === 0
? console.log(" ✓ None")
: [...missingIdFiles].forEach(f => console.log(` - ${f}`));
console.log("\nFiles with mismatched IDs:");
mismatchedIdFiles.size === 0
? console.log(" ✓ None")
: [...mismatchedIdFiles].forEach(f => console.log(` - ${f}`));
const hasErrors =
missingIdFiles.size > 0 || mismatchedIdFiles.size > 0;
console.log("\n" + "=".repeat(60));
if (hasErrors) {
console.log("❌ Check failed - issues found");
process.exit(1);
} else {
console.log("✅ All checks passed!");
}
}
// CLI
const dir = process.argv[2];
if (!dir) {
console.error("Usage: node scripts/check-test-ids.js <tests/draftXXXX>");
process.exit(1);
}
await checkVersion(dir);