Update validate-json.test.ts

This commit is contained in:
CanbiZ 2025-03-12 12:55:25 +01:00 committed by GitHub
parent 03f2284903
commit 388d6e4f3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,46 +8,59 @@ const jsonDir = "public/json";
const metadataFileName = "metadata.json"; const metadataFileName = "metadata.json";
const encoding = "utf-8"; const encoding = "utf-8";
const fileNames = (await fs.readdir(jsonDir)) let fileNames: string[] = [];
.filter((fileName) => fileName !== metadataFileName)
describe.each(fileNames)("%s", async (fileName) => { try {
let script: Script; // Prüfen, ob das Verzeichnis existiert, falls nicht, Tests überspringen
fileNames = (await fs.readdir(jsonDir)).filter((fileName) => fileName !== metadataFileName);
} catch (error) {
console.warn(`Skipping JSON validation tests: ${error.message}`);
}
beforeAll(async () => { if (fileNames.length > 0) {
const filePath = path.resolve(jsonDir, fileName); describe.each(fileNames)("%s", async (fileName) => {
const fileContent = await fs.readFile(filePath, encoding) let script: Script;
script = JSON.parse(fileContent);
})
it("should have valid json according to script schema", () => { beforeAll(async () => {
ScriptSchema.parse(script); const filePath = path.resolve(jsonDir, fileName);
}); const fileContent = await fs.readFile(filePath, encoding);
script = JSON.parse(fileContent);
});
it("should have a corresponding script file", () => { it("should have valid json according to script schema", () => {
script.install_methods.forEach((method) => { ScriptSchema.parse(script);
const scriptPath = path.resolve("..", method.script) });
assert(fs.stat(scriptPath), `Script file not found: ${scriptPath}`)
})
});
})
describe(`${metadataFileName}`, async () => { it("should have a corresponding script file", async () => {
let metadata: Metadata; for (const method of script.install_methods) {
const scriptPath = path.resolve("..", method.script);
beforeAll(async () => { try {
const filePath = path.resolve(jsonDir, metadataFileName); await fs.stat(scriptPath);
const fileContent = await fs.readFile(filePath, encoding) } catch {
metadata = JSON.parse(fileContent); assert.fail(`Script file not found: ${scriptPath}`);
}) }
}
it("should have valid json according to metadata schema", () => {
// TODO: create zod schema for metadata. Move zod schemas to /lib/types.ts
assert(metadata.categories.length > 0);
metadata.categories.forEach((category) => {
assert.isString(category.name)
assert.isNumber(category.id)
assert.isNumber(category.sort_order)
}); });
}); });
})
describe(`${metadataFileName}`, async () => {
let metadata: Metadata;
beforeAll(async () => {
const filePath = path.resolve(jsonDir, metadataFileName);
const fileContent = await fs.readFile(filePath, encoding);
metadata = JSON.parse(fileContent);
});
it("should have valid json according to metadata schema", () => {
assert(metadata.categories.length > 0);
metadata.categories.forEach((category) => {
assert.isString(category.name);
assert.isNumber(category.id);
assert.isNumber(category.sort_order);
});
});
});
} else {
console.warn("Skipping tests because no JSON files were found.");
}