Update validate-json.test.ts

This commit is contained in:
CanbiZ 2025-03-12 12:59:10 +01:00 committed by GitHub
parent 80f4243cda
commit 5af70aad99
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,62 +8,46 @@ const jsonDir = "public/json";
const metadataFileName = "metadata.json"; const metadataFileName = "metadata.json";
const encoding = "utf-8"; const encoding = "utf-8";
let fileNames: string[] = []; const fileNames = (await fs.readdir(jsonDir))
.filter((fileName) => fileName !== metadataFileName)
try { describe.each(fileNames)("%s", async (fileName) => {
fileNames = (await fs.readdir(jsonDir)).filter((fileName) => fileName !== metadataFileName); let script: Script;
} catch (error) {
console.warn(`Skipping JSON validation tests: ${error.message}`);
}
if (fileNames.length === 0) { beforeAll(async () => {
describe("Dummy Test Suite", () => { const filePath = path.resolve(jsonDir, fileName);
it("Skipping JSON tests because no files were found", () => { const fileContent = await fs.readFile(filePath, encoding)
assert(true); script = JSON.parse(fileContent);
}); })
});
} else {
describe.each(fileNames)("%s", async (fileName) => {
let script: Script;
beforeAll(async () => { it("should have valid json according to script schema", () => {
const filePath = path.resolve(jsonDir, fileName); ScriptSchema.parse(script);
const fileContent = await fs.readFile(filePath, encoding);
script = JSON.parse(fileContent);
});
it("should have valid json according to script schema", () => {
ScriptSchema.parse(script);
});
it("should have a corresponding script file", async () => {
for (const method of script.install_methods) {
const scriptPath = path.resolve("..", method.script);
try {
await fs.stat(scriptPath);
} catch {
assert.fail(`Script file not found: ${scriptPath}`);
}
}
});
}); });
describe(`${metadataFileName}`, async () => { it("should have a corresponding script file", () => {
let metadata: Metadata; script.install_methods.forEach((method) => {
const scriptPath = path.resolve("..", method.script)
assert(fs.stat(scriptPath), `Script file not found: ${scriptPath}`)
})
});
})
beforeAll(async () => { describe(`${metadataFileName}`, async () => {
const filePath = path.resolve(jsonDir, metadataFileName); let metadata: Metadata;
const fileContent = await fs.readFile(filePath, encoding);
metadata = JSON.parse(fileContent);
});
it("should have valid json according to metadata schema", () => { beforeAll(async () => {
assert(metadata.categories.length > 0); const filePath = path.resolve(jsonDir, metadataFileName);
metadata.categories.forEach((category) => { const fileContent = await fs.readFile(filePath, encoding)
assert.isString(category.name); metadata = JSON.parse(fileContent);
assert.isNumber(category.id); })
assert.isNumber(category.sort_order);
}); 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)
}); });
}); });
} })