diff --git a/task-1/cocktail.js b/task-1/cocktail.js index 228ab63..298e8f3 100644 --- a/task-1/cocktail.js +++ b/task-1/cocktail.js @@ -1,33 +1,58 @@ -// API documentation: https://www.thecocktaildb.com/api.php - +import fs from 'fs/promises'; import path from 'path'; - -const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1'; - -// Add helper functions as needed here - -export async function main() { - if (process.argv.length < 3) { - console.log('Please provide a cocktail name as a command line argument.'); - return; +import { startVitest } from 'vitest/node'; + +const __dirname = import.meta.dirname; +const reportPath = path.join(__dirname, 'report.json'); +const reportFileName = 'report.json'; + +await startVitest( + 'test', + [], // CLI filters + { + include: ['../**/*.test.js'], + reporters: ['json'], + outputFile: reportPath, + watch: false, + }, // override test config + {}, // override Vite config + {} // custom Vitest options +); + +const reportContent = await fs.readFile(reportFileName, 'utf-8'); +await fs.unlink(reportFileName); + +try { + const { testResults } = JSON.parse(reportContent); + let testCount = 0; + let passedCount = 0; + + for (const result of testResults) { + for (const assertionResult of result.assertionResults) { + const { title, status } = assertionResult; + let icon; + if (status == 'passed') { + icon = '✅'; + passedCount++; + } else { + icon = '❌'; + } + console.log(`${icon} ${title}`); + testCount++; + } } - const cocktailName = process.argv[2]; - const url = `${BASE_URL}/search.php?s=${cocktailName}`; + const PASSING_SCORE = 50; + const totalScore = (passedCount / testCount) * 100; - const __dirname = import.meta.dirname; - const outPath = path.join(__dirname, `../output/${cocktailName}.md`); - - try { - // 1. Fetch data from the API at the given URL - // 2. Generate markdown content to match the examples - // 3. Write the generated content to a markdown file as given by outPath - } catch (error) { - // 4. Handle errors - } -} + const results = { + score: totalScore, + pass: totalScore >= PASSING_SCORE, + passingScore: PASSING_SCORE, + }; -// Do not change the code below -if (!process.env.VITEST) { - main(); + await fs.writeFile('score.json', JSON.stringify(results, null, 2)); +} catch (error) { + console.error('Error parsing report JSON:', error); + process.exit(1); }