Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 52 additions & 27 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -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);
}
Loading