Skip to content

Commit 98685f8

Browse files
committed
primera prueba gha
1 parent 4e26013 commit 98685f8

File tree

8 files changed

+207
-0
lines changed

8 files changed

+207
-0
lines changed

.github/workflows/tests.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Node.js CI
2+
3+
on:
4+
push:
5+
branches: [ master, test ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
defaults:
13+
run:
14+
working-directory: varios/21
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Use Node.js 18.x
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: '18.x'
23+
cache: 'npm'
24+
cache-dependency-path: varios/21/package.json
25+
26+
- name: Install dependencies
27+
run: npm ci
28+
29+
- name: Run tests
30+
run: npm test
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Node.js CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Use Node.js 18.x
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: '18.x'
20+
cache: 'npm'
21+
22+
- name: Install dependencies
23+
run: npm ci
24+
25+
- name: Run tests
26+
run: npm test

varios/21/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM node:18-alpine
2+
3+
WORKDIR /app
4+
5+
COPY package*.json ./
6+
7+
RUN npm install
8+
9+
COPY . .
10+
11+
# The default command will start the application
12+
CMD ["npm", "start"]

varios/21/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "github-actions-demo",
3+
"version": "1.0.0",
4+
"description": "Demo application for GitHub Actions with unit tests",
5+
"main": "src/app.js",
6+
"scripts": {
7+
"start": "node src/app.js",
8+
"test": "jest",
9+
"test:watch": "jest --watch"
10+
},
11+
"dependencies": {
12+
"express": "^4.18.2"
13+
},
14+
"devDependencies": {
15+
"jest": "^29.7.0",
16+
"supertest": "^6.3.3"
17+
}
18+
}

varios/21/src/app.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const express = require('express');
2+
const { calculateDiscount } = require('./utils');
3+
4+
const app = express();
5+
app.use(express.json());
6+
7+
app.get('/health', (req, res) => {
8+
res.json({ status: 'ok' });
9+
});
10+
11+
app.post('/calculate-discount', (req, res) => {
12+
try {
13+
const { price, percentage } = req.body;
14+
const finalPrice = calculateDiscount(price, percentage);
15+
res.json({
16+
original: price,
17+
discount: percentage,
18+
final: finalPrice
19+
});
20+
} catch (error) {
21+
res.status(400).json({ error: error.message });
22+
}
23+
});
24+
25+
const PORT = process.env.PORT || 3000;
26+
27+
if (process.env.NODE_ENV !== 'test') {
28+
app.listen(PORT, () => {
29+
console.log(`Server running on port ${PORT}`);
30+
});
31+
}
32+
33+
module.exports = app;

varios/21/src/app.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const request = require('supertest');
2+
const app = require('./app');
3+
4+
describe('API Endpoints', () => {
5+
describe('GET /health', () => {
6+
test('should return health status', async () => {
7+
const response = await request(app)
8+
.get('/health')
9+
.expect(200);
10+
11+
expect(response.body).toEqual({ status: 'ok' });
12+
});
13+
});
14+
15+
describe('POST /calculate-discount', () => {
16+
test('should calculate discount correctly', async () => {
17+
const response = await request(app)
18+
.post('/calculate-discount')
19+
.send({ price: 100, percentage: 20 })
20+
.expect(200);
21+
22+
expect(response.body).toEqual({
23+
original: 100,
24+
discount: 20,
25+
final: 80
26+
});
27+
});
28+
29+
test('should return 400 for invalid input', async () => {
30+
const response = await request(app)
31+
.post('/calculate-discount')
32+
.send({ price: '100', percentage: 20 })
33+
.expect(400);
34+
35+
expect(response.body).toHaveProperty('error');
36+
});
37+
38+
test('should return 400 for invalid percentage', async () => {
39+
const response = await request(app)
40+
.post('/calculate-discount')
41+
.send({ price: 100, percentage: 150 })
42+
.expect(400);
43+
44+
expect(response.body).toHaveProperty('error');
45+
});
46+
});
47+
});

varios/21/src/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function calculateDiscount(price, percentage) {
2+
if (typeof price !== 'number' || typeof percentage !== 'number') {
3+
throw new Error('Both price and percentage must be numbers');
4+
}
5+
6+
if (percentage < 0 || percentage > 100) {
7+
throw new Error('Percentage must be between 0 and 100');
8+
}
9+
10+
return price - (price * (percentage / 100));
11+
}
12+
13+
module.exports = {
14+
calculateDiscount
15+
};

varios/21/src/utils.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { calculateDiscount } = require('./utils');
2+
3+
describe('calculateDiscount', () => {
4+
test('calculates discount correctly', () => {
5+
expect(calculateDiscount(100, 20)).toBe(80);
6+
expect(calculateDiscount(50, 10)).toBe(45);
7+
});
8+
9+
test('handles zero discount', () => {
10+
expect(calculateDiscount(100, 0)).toBe(100);
11+
});
12+
13+
test('handles full discount', () => {
14+
expect(calculateDiscount(100, 100)).toBe(0);
15+
});
16+
17+
test('throws error for invalid number inputs', () => {
18+
expect(() => calculateDiscount('100', 20)).toThrow('Both price and percentage must be numbers');
19+
expect(() => calculateDiscount(100, '20')).toThrow('Both price and percentage must be numbers');
20+
});
21+
22+
test('throws error for invalid percentage range', () => {
23+
expect(() => calculateDiscount(100, -10)).toThrow('Percentage must be between 0 and 100');
24+
expect(() => calculateDiscount(100, 110)).toThrow('Percentage must be between 0 and 100');
25+
});
26+
});

0 commit comments

Comments
 (0)