|
| 1 | +import { expect } from 'chai' |
| 2 | +import { describe, it } from 'mocha' |
| 3 | +import { parseMeteoAlert } from '../../src/utilites/meteo-alerts' |
| 4 | +import { alertZones, MeteoAlertType } from '../../src/services/meteo-alerts' |
| 5 | + |
| 6 | +describe('parseMeteoAlert', () => { |
| 7 | + it('throws if the requested zone is not present in the alert', () => { |
| 8 | + const zone = 'non_existent_zone' |
| 9 | + const alert: any = { |
| 10 | + link: '/alerts/allerta_123_45.pdf', |
| 11 | + title: 'Test alert', |
| 12 | + // no zone property |
| 13 | + } |
| 14 | + |
| 15 | + expect(() => parseMeteoAlert(alert, zone as any)).to.throw(`Zone ${zone} not found in alert data`) |
| 16 | + }) |
| 17 | + |
| 18 | + it('throws if alert.link does not contain a filename', () => { |
| 19 | + const zone = alertZones[0] |
| 20 | + const alert: any = { |
| 21 | + link: '/path/with/trailing/slash/', |
| 22 | + title: 'Test alert', |
| 23 | + [zone]: { |
| 24 | + ghiaccio_pioggia_gela: null, |
| 25 | + }, |
| 26 | + } |
| 27 | + |
| 28 | + expect(() => parseMeteoAlert(alert, zone)).to.throw(`Invalid link format: ${alert.link}`) |
| 29 | + }) |
| 30 | + |
| 31 | + it('parses id, updates link, computes isCritic and criticZoneData and omits zone keys from root', () => { |
| 32 | + const zone = alertZones[0] |
| 33 | + // Construct zone data using the known keys from the utility's correctColors mapping. |
| 34 | + const zoneData = { |
| 35 | + ghiaccio_pioggia_gela: null, // allowed / removed from criticZoneData |
| 36 | + idraulica: MeteoAlertType.yellow, // allowed by correctColors but is not in colorsToRemove -> should appear in criticZoneData |
| 37 | + idrogeologica: null, |
| 38 | + mareggiate: null, |
| 39 | + neve: MeteoAlertType.yellow, // NOT allowed by correctColors.neve -> makes isCritic true and included in criticZoneData |
| 40 | + stato_mare: null, |
| 41 | + temperature_estreme: MeteoAlertType.green, |
| 42 | + temporali: MeteoAlertType.green, |
| 43 | + vento: MeteoAlertType.red, // NOT allowed -> included in criticZoneData |
| 44 | + } as any |
| 45 | + |
| 46 | + const originalLink = '/alerts/allerta_123_45.pdf' |
| 47 | + const alert: any = { |
| 48 | + link: originalLink, |
| 49 | + title: 'Sample alert title', |
| 50 | + otherProp: 42, |
| 51 | + [zone]: zoneData, |
| 52 | + } |
| 53 | + |
| 54 | + const parsed = parseMeteoAlert(alert, zone) |
| 55 | + |
| 56 | + // id: 'allerta_123_45' -> remove .pdf -> 'allerta_123_45' -> replace first '_' -> 'allerta/123_45' -> remove 'allerta' -> '/123_45' |
| 57 | + expect(parsed.id).to.equal('/123_45') |
| 58 | + |
| 59 | + // link must be prefixed with the base URL |
| 60 | + expect(parsed.link).to.equal(`https://allertameteo.regione.emilia-romagna.it${originalLink}`) |
| 61 | + |
| 62 | + // zoneData should be preserved on the returned object |
| 63 | + expect(parsed.zoneData).to.deep.equal(zoneData) |
| 64 | + |
| 65 | + // isCritic should be true because 'neve' and 'vento' use types not allowed by correctColors |
| 66 | + expect(parsed.isCritic).to.equal(true) |
| 67 | + |
| 68 | + // criticZoneData should include only keys whose value is not in colorsToRemove ([null, green]) |
| 69 | + // From our zoneData that means idraulica (yellow), neve (yellow), vento (red) |
| 70 | + expect(parsed.criticZoneData).to.deep.equal({ |
| 71 | + idraulica: MeteoAlertType.yellow, |
| 72 | + neve: MeteoAlertType.yellow, |
| 73 | + vento: MeteoAlertType.red, |
| 74 | + }) |
| 75 | + |
| 76 | + // The top-level returned object should have omitted the original zone key (it is provided separately as zoneData) |
| 77 | + expect((parsed as any)[zone]).to.equal(undefined) |
| 78 | + }) |
| 79 | +}) |
0 commit comments