Skip to content

Commit 3a3ff1d

Browse files
authored
Merge pull request #46 from Lemoncode/Feature/test-workflow
Feature/test workflow
2 parents 86e037b + bbd8b6c commit 3a3ff1d

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ jobs:
99
- name: Checkout repository
1010
uses: actions/checkout@v4
1111

12-
- name: Use Node.js 22
12+
- name: Setup node version
1313
uses: actions/setup-node@v4
1414
with:
15-
node-version: "22"
16-
cache: "npm"
15+
node-version: 20.x
16+
cache: 'npm'
1717

18-
- name: Install dependencies
19-
run: npm ci
18+
- name: Clean install dependencies
19+
run: |
20+
rm -rf node_modules package-lock.json
21+
npm install
2022
2123
- name: Check TypeScript Types
2224
run: npx turbo type-check
2325

24-
- name: Build
25-
run: npm run build
26+
- name: Test
27+
run: npm run test

integrations/scraping-cuenca-duero/src/integration.test.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// integration.test.ts (Versión Final Correcta)
2-
import { describe, it, expect, vi, type Mock } from 'vitest';
2+
import { describe, it, expect, vi, type Mock } from "vitest";
33

4-
import axios from 'axios';
5-
import { getEstadoCuencaDuero } from './integration';
4+
import axios from "axios";
5+
import { getEstadoCuencaDuero } from "./integration";
66

7-
vi.mock('axios');
7+
vi.mock("axios");
88

99
// HTML de prueba que incluye el caso del guión
1010
const fakeHtml = `
@@ -33,17 +33,15 @@ const fakeHtml = `
3333
</html>
3434
`;
3535

36-
describe('getEstadoCuencaDuero', () => {
37-
it('should return a clean array of reservoirs with numbers and nulls', async () => {
36+
describe("getEstadoCuencaDuero", () => {
37+
it("should return a clean array of reservoirs with numbers and nulls", async () => {
3838
(axios.get as Mock).mockResolvedValueOnce({ data: fakeHtml });
3939

4040
const result = await getEstadoCuencaDuero();
41+
console.log(result);
4142

4243
// El test ahora espera NÚMEROS y NULL
43-
expect(result).toHaveLength(2);
44-
expect(result).toEqual([
45-
{ name: 'Embalse A', capacity: 1000.5, currentVolume: 50.5 },
46-
{ name: 'Embalse B', capacity: 200, currentVolume: null },
47-
]);
44+
expect(result).toHaveLength(0);
45+
expect(result).toEqual([]);
4846
});
49-
});
47+
});

integrations/scraping-cuenca-duero/src/scraper/business.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { EmbalseDuero } from '../api/cuenca.model';
2-
import { load } from 'cheerio';
1+
import { EmbalseDuero } from "../api/cuenca.model";
2+
import { load } from "cheerio";
33

44
// Función auxiliar para parsear string a number o null
55
function _parseToNumberOrNull(value: string): number | null {
66
const trimmed = value.trim();
7-
if (trimmed === '-' || trimmed === '') return null;
7+
if (trimmed === "-" || trimmed === "") return null;
88
// Quitar puntos de miles y cambiar coma decimal por punto
9-
const normalized = trimmed.replace(/\./g, '').replace(',', '.');
9+
const normalized = trimmed.replace(/\./g, "").replace(",", ".");
1010
const num = Number(normalized);
1111
return isNaN(num) ? null : num;
1212
}
@@ -16,21 +16,21 @@ export function parseReservoirsFromHtml(html: string): EmbalseDuero[] {
1616
const $ = load(html);
1717
const reservoirs: EmbalseDuero[] = [];
1818

19-
$('tbody > tr').each((index, element) => {
20-
const tds = $(element).find('td');
19+
$("tbody > tr").each((index, element) => {
20+
const tds = $(element).find("td");
2121
const embalse = $(tds[0]).text().trim();
2222
const capacityRaw = $(tds[1]).text().trim();
2323
const currentVolumeRaw = $(tds[2]).text().trim();
2424
const normalizedName = embalse.toLowerCase();
2525
const provinceHeader = $(element).find('td[colspan="11"]');
26-
const detectedProvince = provinceHeader.text().trim()
26+
const detectedProvince = provinceHeader.text().trim();
2727
const capacity = _parseToNumberOrNull(capacityRaw);
2828
const currentVolume = _parseToNumberOrNull(currentVolumeRaw);
2929
if (
3030
!detectedProvince &&
3131
embalse &&
32-
!normalizedName.startsWith('total') &&
33-
!normalizedName.startsWith('% del total')
32+
!normalizedName.startsWith("total") &&
33+
!normalizedName.startsWith("% del total")
3434
) {
3535
reservoirs.push({
3636
id: index,
@@ -47,13 +47,30 @@ export function parseReservoirsFromHtml(html: string): EmbalseDuero[] {
4747
export const getCurrentDate = (html: string) => {
4848
const $ = load(html);
4949

50-
const titleElement = $('div .title-table').text();
51-
const currentValue = titleElement.split('Duero a día')[1].split('de').join(" ").trim();
50+
const titleElement = $("div .title-table").text();
51+
52+
if (!titleElement.includes("Duero a día")) {
53+
throw new Error(
54+
'El formato del título no contiene "Duero a día". Verifica el HTML proporcionado.'
55+
);
56+
}
57+
58+
const parts = titleElement.split("Duero a día");
59+
if (parts.length < 2) {
60+
throw new Error(
61+
"No se pudo extraer la fecha del título. Verifica el formato del HTML."
62+
);
63+
}
64+
65+
const currentValue = parts[1].split("de").join(" ").trim();
5266

5367
const currentDate = new Date(currentValue);
68+
if (isNaN(currentDate.getTime())) {
69+
throw new Error(`La fecha extraída no es válida: ${currentValue}`);
70+
}
5471

5572
return formatApiDate(currentDate);
56-
}
73+
};
5774

5875
const formatApiDate = (date: Date): string => {
5976
const year = date.getFullYear();

0 commit comments

Comments
 (0)