-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathschema-validation.test.js
More file actions
289 lines (249 loc) · 9.71 KB
/
Copy pathschema-validation.test.js
File metadata and controls
289 lines (249 loc) · 9.71 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { describe, it, expect, beforeAll } from "vitest";
import fs from "fs";
import path from "path";
import Ajv from "ajv";
import addFormats from "ajv-formats";
import { glob } from "glob";
const ROOT = path.resolve(import.meta.dirname, "..");
const SCHEMA_PATH = path.join(ROOT, "schemas", "server-definition.schema.json");
const SERVERS_DIR = path.join(ROOT, "servers");
const BUNDLE_PATH = path.join(ROOT, "bundle", "bundle.json");
const CATEGORIES_PATH = path.join(ROOT, "categories.json");
function loadJson(filePath) {
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
}
// ---------------------------------------------------------------------------
// Schema validation
// ---------------------------------------------------------------------------
describe("Server Definition Schema", () => {
let validate;
let serverFiles;
beforeAll(async () => {
const schema = loadJson(SCHEMA_PATH);
delete schema.$schema; // ajv v8 compat
const ajv = new Ajv({ allErrors: true, strict: false });
addFormats(ajv);
validate = ajv.compile(schema);
const pattern = path.join(SERVERS_DIR, "*.json").replace(/\\/g, "/");
serverFiles = await glob(pattern);
});
it("schema file exists and is valid JSON", () => {
expect(fs.existsSync(SCHEMA_PATH)).toBe(true);
const schema = loadJson(SCHEMA_PATH);
expect(schema.type).toBe("object");
expect(schema.required).toContain("id");
expect(schema.required).toContain("name");
expect(schema.required).toContain("transport");
});
it("has server definition files", () => {
expect(serverFiles.length).toBeGreaterThan(0);
});
it.each([
{ id: "com.example-test", name: "Test", transport: { type: "stdio", command: "echo" } },
{ id: "com.example-remote", name: "Remote", transport: { type: "http", url: "https://example.com/mcp" } },
{
id: "com.example-defaults", name: "Defaults Test",
transport: {
type: "stdio", command: "node", args: ["server.js"],
env: { "LOG_LEVEL": "${input:LOG_LEVEL}" },
metadata: {
inputs: [
{ id: "LOG_LEVEL", label: "Log Level", type: "text", required: false, default: "info" },
{ id: "API_KEY", label: "API Key", type: "password", required: true, secret: true },
],
},
},
},
])("validates valid server definition: $id", (data) => {
expect(validate(data)).toBe(true);
});
it.each([
[{ name: "NoId", transport: { type: "stdio", command: "x" } }, "missing id"],
[{ id: "x", transport: { type: "stdio", command: "x" } }, "missing name"],
[{ id: "x", name: "X" }, "missing transport"],
[{ id: "INVALID-CAPS", name: "X", transport: { type: "stdio", command: "x" } }, "invalid id pattern (caps)"],
[{ id: "com.foo.bar", name: "X", transport: { type: "stdio", command: "x" } }, "invalid id pattern (multi-dot)"],
[{ id: "noDot", name: "X", transport: { type: "stdio", command: "x" } }, "invalid id pattern (no dot)"],
[{ id: "x", name: "", transport: { type: "stdio", command: "x" } }, "empty name"],
])("rejects invalid definition: %s", (data) => {
expect(validate(data)).toBe(false);
});
it("validates all server files against schema", async () => {
const errors = [];
for (const filePath of serverFiles) {
const data = loadJson(filePath);
// Strip platform-managed fields like the validator does
delete data.badges;
delete data.stats;
delete data.sponsored;
delete data.featured;
const valid = validate(data);
if (!valid) {
errors.push({
file: path.basename(filePath),
errors: validate.errors,
});
}
}
if (errors.length > 0) {
const details = errors
.map((e) => `${e.file}: ${e.errors.map((err) => `${err.instancePath} ${err.message}`).join(", ")}`)
.join("\n");
expect.fail(`Schema validation failed:\n${details}`);
}
});
});
// ---------------------------------------------------------------------------
// Required fields and consistency
// ---------------------------------------------------------------------------
describe("Server definitions consistency", () => {
let serverFiles;
let servers;
beforeAll(async () => {
const pattern = path.join(SERVERS_DIR, "*.json").replace(/\\/g, "/");
serverFiles = await glob(pattern);
servers = serverFiles.map((f) => ({
file: path.basename(f),
data: loadJson(f),
}));
});
it("every server has a unique ID", () => {
const ids = servers.map((s) => s.data.id);
const unique = new Set(ids);
expect(unique.size).toBe(ids.length);
});
it("every server has a unique alias (when present)", () => {
const aliases = servers
.filter((s) => s.data.alias)
.map((s) => s.data.alias);
const unique = new Set(aliases);
expect(unique.size).toBe(aliases.length);
});
it("no alias collides with any ID", () => {
const ids = new Set(servers.map((s) => s.data.id));
const aliasCollisions = servers.filter(
(s) => s.data.alias && ids.has(s.data.alias)
);
expect(aliasCollisions).toHaveLength(0);
});
it("every server ID uses {tld}.{publisher}-{name} format", () => {
const pattern = /^[a-z0-9]+\.[a-z0-9][a-z0-9-]*$/;
for (const { data, file } of servers) {
expect(data.id, `${file} ID should match {tld}.{publisher}-{name} format`).toMatch(pattern);
}
});
it("every server has a description", () => {
for (const { data, file } of servers) {
expect(data.description, `${file} missing description`).toBeTruthy();
}
});
it("every server has at least one category", () => {
for (const { data, file } of servers) {
expect(
data.categories?.length,
`${file} should have at least one category`
).toBeGreaterThan(0);
}
});
it("stdio servers have a command", () => {
for (const { data, file } of servers) {
if (data.transport.type === "stdio") {
expect(data.transport.command, `${file} stdio missing command`).toBeTruthy();
}
}
});
it("http servers have a url", () => {
for (const { data, file } of servers) {
if (data.transport.type === "http") {
expect(data.transport.url, `${file} http missing url`).toBeTruthy();
}
}
});
it("filename is a valid JSON file derived from the server ID", () => {
for (const { data, file } of servers) {
// Filename should end with .json
expect(file, `${file} should end with .json`).toMatch(/\.json$/);
// Filename should be lowercase kebab or dot notation
expect(file, `${file} should be lowercase`).toMatch(/^[a-z0-9.-]+\.json$/);
}
});
});
// ---------------------------------------------------------------------------
// Categories
// ---------------------------------------------------------------------------
describe("Categories", () => {
it("categories.json exists and is valid", () => {
expect(fs.existsSync(CATEGORIES_PATH)).toBe(true);
const categories = loadJson(CATEGORIES_PATH);
expect(categories).toBeInstanceOf(Array);
expect(categories.length).toBeGreaterThan(0);
});
it("every category has id and name", () => {
const categories = loadJson(CATEGORIES_PATH);
for (const cat of categories) {
expect(cat.id).toBeTruthy();
expect(cat.name).toBeTruthy();
}
});
it("category IDs are unique", () => {
const categories = loadJson(CATEGORIES_PATH);
const ids = categories.map((c) => c.id);
expect(new Set(ids).size).toBe(ids.length);
});
it("all server categories reference valid category IDs", async () => {
const categories = loadJson(CATEGORIES_PATH);
const validIds = new Set(categories.map((c) => c.id));
const pattern = path.join(SERVERS_DIR, "*.json").replace(/\\/g, "/");
const files = await glob(pattern);
const invalid = [];
for (const f of files) {
const data = loadJson(f);
for (const cat of data.categories || []) {
if (!validIds.has(cat)) {
invalid.push(`${path.basename(f)}: unknown category "${cat}"`);
}
}
}
if (invalid.length > 0) {
expect.fail(`Invalid category references:\n${invalid.join("\n")}`);
}
});
});
// ---------------------------------------------------------------------------
// Bundle
// ---------------------------------------------------------------------------
describe("Bundle", () => {
it("bundle can be built successfully", async () => {
// Run the build script
const { execSync } = await import("child_process");
execSync("node scripts/build-bundle.js", { cwd: ROOT, timeout: 30000 });
expect(fs.existsSync(BUNDLE_PATH)).toBe(true);
});
it("bundle has correct structure", () => {
const bundle = loadJson(BUNDLE_PATH);
expect(bundle.version).toBeTruthy();
expect(bundle.updated_at).toBeTruthy();
expect(bundle.servers).toBeInstanceOf(Array);
expect(bundle.categories).toBeInstanceOf(Array);
expect(bundle.servers.length).toBeGreaterThan(0);
});
it("bundle servers match filesystem servers", async () => {
const bundle = loadJson(BUNDLE_PATH);
const pattern = path.join(SERVERS_DIR, "*.json").replace(/\\/g, "/");
const files = await glob(pattern);
expect(bundle.servers.length).toBe(files.length);
});
it("bundle includes UI config", () => {
const bundle = loadJson(BUNDLE_PATH);
expect(bundle.ui).toBeDefined();
expect(bundle.ui.filters).toBeInstanceOf(Array);
expect(bundle.ui.sort_options).toBeInstanceOf(Array);
expect(bundle.ui.default_sort).toBe("recommended");
expect(bundle.ui.items_per_page).toBe(24);
});
it("bundle includes home section with featured servers", () => {
const bundle = loadJson(BUNDLE_PATH);
expect(bundle.home).toBeDefined();
expect(bundle.home.featured_server_ids).toBeInstanceOf(Array);
});
});