diff --git a/frontend/src/__tests__/public/validate-json.test.ts b/frontend/src/__tests__/public/validate-json.test.ts index 562ebe9..96e0bfa 100644 --- a/frontend/src/__tests__/public/validate-json.test.ts +++ b/frontend/src/__tests__/public/validate-json.test.ts @@ -8,46 +8,59 @@ const jsonDir = "public/json"; const metadataFileName = "metadata.json"; const encoding = "utf-8"; -const fileNames = (await fs.readdir(jsonDir)) - .filter((fileName) => fileName !== metadataFileName) +let fileNames: string[] = []; -describe.each(fileNames)("%s", async (fileName) => { - let script: Script; +try { + // 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 () => { - const filePath = path.resolve(jsonDir, fileName); - const fileContent = await fs.readFile(filePath, encoding) - script = JSON.parse(fileContent); - }) +if (fileNames.length > 0) { + describe.each(fileNames)("%s", async (fileName) => { + let script: Script; - it("should have valid json according to script schema", () => { - ScriptSchema.parse(script); - }); + beforeAll(async () => { + const filePath = path.resolve(jsonDir, fileName); + const fileContent = await fs.readFile(filePath, encoding); + script = JSON.parse(fileContent); + }); - it("should have a corresponding script file", () => { - script.install_methods.forEach((method) => { - const scriptPath = path.resolve("..", method.script) - assert(fs.stat(scriptPath), `Script file not found: ${scriptPath}`) - }) - }); -}) + it("should have valid json according to script schema", () => { + ScriptSchema.parse(script); + }); -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", () => { - // 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) + 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 () => { + 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."); +}