feat: implement tests and coverage reports for KpiCards and scrapeLakes functionality

This commit is contained in:
David Fencl
2026-06-05 23:08:44 +02:00
parent 0030dca448
commit 8193ce818a
37 changed files with 4309 additions and 31 deletions
+25
View File
@@ -0,0 +1,25 @@
import { describe, it, expect } from 'vitest';
import { parseDateString } from '../scrapeLakes';
describe('scrapeLakes - parseDateString', () => {
it('should parse valid date strings correctly', () => {
// Note: JS Date parsing uses local timezone, so the output ISO string depends on where the test runs.
// To make it deterministic, we just check if it returns a string and is not null.
const result = parseDateString('05.06.2026 22:30');
expect(result).not.toBeNull();
// Assuming standard parsing, it should contain 2026
expect(result).toContain('2026-06-05');
});
it('should return null for invalid formats', () => {
expect(parseDateString('')).toBeNull();
expect(parseDateString('invalid date string')).toBeNull();
expect(parseDateString('05.06.2026')).toBeNull(); // Missing time
expect(parseDateString('22:30')).toBeNull(); // Missing date
});
it('should return null for malformed parts', () => {
expect(parseDateString('99.99.9999 99:99')).toBeNull(); // JS Date might parse this as valid overflow, but let's check
expect(parseDateString('abc def ghi')).toBeNull();
});
});
+1 -1
View File
@@ -26,7 +26,7 @@ const lakes = lakesConfig.map(lake => {
if (data.length > 0) {
// Find latest valid record or just the last record
const lastValidLevelData = [...data].reverse().find(d => d.level !== null && !isNaN(d.level));
const lastValidFlowData = [...data].reverse().find(d => d.flow !== null && !isNaN(d.flow) && d.flow > 0);
const lastValidFlowData = [...data].reverse().find(d => d.flow !== null && !isNaN(d.flow) && d.flow >= 0);
currentLevel = lastValidLevelData ? lastValidLevelData.level : 0;
currentFlow = lastValidFlowData ? lastValidFlowData.flow : 0;
+6 -2
View File
@@ -16,7 +16,7 @@ interface DataRecord {
}
// Parse date from DD.MM.YYYY HH:MM to ISO
function parseDateString(dateStr: string): string | null {
export function parseDateString(dateStr: string): string | null {
try {
if (!dateStr || !dateStr.includes(' ')) return null;
const [datePart, timePart] = dateStr.trim().split(' ');
@@ -25,8 +25,12 @@ function parseDateString(dateStr: string): string | null {
if (!year || !hours) return null;
const d = new Date(parseInt(year), parseInt(month) - 1, parseInt(day), parseInt(hours), parseInt(minutes));
const y = parseInt(year);
const m = parseInt(month) - 1;
const dDay = parseInt(day);
const d = new Date(y, m, dDay, parseInt(hours), parseInt(minutes));
if (isNaN(d.getTime())) return null;
if (d.getFullYear() !== y || d.getMonth() !== m || d.getDate() !== dDay) return null;
return d.toISOString();
} catch (e) {
return null;