|
| 1 | +import { expect } from 'chai' |
| 2 | +import { afterEach, beforeEach, describe, it } from 'mocha' |
| 3 | +import sinon, { SinonStub } from 'sinon' |
| 4 | +import * as riverModel from '../../src/models/river' |
| 5 | +import * as riverLevelModel from '../../src/models/river-level' |
| 6 | +import * as riverLinkModel from '../../src/models/river-link' |
| 7 | +import { River } from '../../src/models/river' |
| 8 | +import { RiverLink } from '../../src/models/river-link' |
| 9 | +import { runFloodCalibration } from '../../src/tasks/flood-calibration' |
| 10 | + |
| 11 | +const T0 = new Date('2026-01-01T00:00:00.000Z').getTime() |
| 12 | +const MIN = 60_000 |
| 13 | +const DAY = 24 * 60 * MIN |
| 14 | + |
| 15 | +const iso = (ms: number) => new Date(ms).toISOString() |
| 16 | + |
| 17 | +const river = (id: number, soglia1: number | null): River => ({ |
| 18 | + id, |
| 19 | + station_id: String(id), |
| 20 | + river_name: 'Idice', |
| 21 | + station_name: `st-${id}`, |
| 22 | + soglia1, |
| 23 | + soglia2: null, |
| 24 | + soglia3: null, |
| 25 | + created_on: iso(T0), |
| 26 | + updated_on: iso(T0), |
| 27 | +}) |
| 28 | + |
| 29 | +const link = (overrides: Partial<RiverLink> = {}): RiverLink => ({ |
| 30 | + id: 1, |
| 31 | + upstream_river_id: 10, |
| 32 | + downstream_river_id: 20, |
| 33 | + target_threshold: 1, |
| 34 | + lead_time_minutes: null, |
| 35 | + precursor_level: null, |
| 36 | + sample_size: 0, |
| 37 | + model_json: null, |
| 38 | + last_calibrated_on: null, |
| 39 | + created_on: iso(T0), |
| 40 | + updated_on: iso(T0), |
| 41 | + ...overrides, |
| 42 | +}) |
| 43 | + |
| 44 | +// Three downstream exceedances (>10) 3 days apart, each preceded by an upstream peak. |
| 45 | +const buildPair = (dayOffset: number, leadMin: number, upstreamPeak: number) => { |
| 46 | + const onset = T0 + dayOffset * DAY |
| 47 | + return { |
| 48 | + downstream: [ |
| 49 | + { value: 9, measured_at: iso(onset - 30 * MIN) }, |
| 50 | + { value: 11, measured_at: iso(onset) }, |
| 51 | + { value: 12, measured_at: iso(onset + 30 * MIN) }, |
| 52 | + { value: 8, measured_at: iso(onset + 60 * MIN) }, |
| 53 | + ], |
| 54 | + upstream: [ |
| 55 | + { value: upstreamPeak - 2, measured_at: iso(onset - (leadMin + 60) * MIN) }, |
| 56 | + { value: upstreamPeak, measured_at: iso(onset - leadMin * MIN) }, |
| 57 | + { value: upstreamPeak - 1, measured_at: iso(onset - (leadMin - 30) * MIN) }, |
| 58 | + ], |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +const e = [buildPair(2, 120, 5), buildPair(5, 150, 6), buildPair(8, 135, 5.5)] |
| 63 | +const downstreamRows = e.flatMap((p) => p.downstream) |
| 64 | +const upstreamRows = e.flatMap((p) => p.upstream) |
| 65 | + |
| 66 | +describe('tests/tasks/flood-calibration', () => { |
| 67 | + let getLinks: SinonStub |
| 68 | + let getRiver: SinonStub |
| 69 | + let getLevels: SinonStub |
| 70 | + let update: SinonStub |
| 71 | + |
| 72 | + beforeEach(() => { |
| 73 | + getLinks = sinon.stub(riverLinkModel, 'getRiverLinks') |
| 74 | + getRiver = sinon.stub(riverModel, 'getRiverById') |
| 75 | + getLevels = sinon.stub(riverLevelModel, 'getRiverReadingsSince') |
| 76 | + update = sinon.stub(riverLinkModel, 'updateRiverLinkModel').resolves({} as never) |
| 77 | + }) |
| 78 | + |
| 79 | + afterEach(() => sinon.restore()) |
| 80 | + |
| 81 | + it('learns and persists an active model from historical events', async () => { |
| 82 | + getLinks.resolves([link()]) |
| 83 | + getRiver.withArgs(20).resolves(river(20, 10)) |
| 84 | + getRiver.withArgs(10).resolves(river(10, 5)) |
| 85 | + getLevels.withArgs(20).resolves(downstreamRows) |
| 86 | + getLevels.withArgs(10).resolves(upstreamRows) |
| 87 | + |
| 88 | + const summary = await runFloodCalibration() |
| 89 | + |
| 90 | + expect(update.calledOnce).to.equal(true) |
| 91 | + const patch = update.firstCall.args[1] |
| 92 | + expect(patch.sample_size).to.equal(3) |
| 93 | + expect(patch.lead_time_minutes).to.equal(135) |
| 94 | + expect(patch.precursor_level).to.be.closeTo(5.25, 1e-9) |
| 95 | + expect(summary).to.deep.equal({ calibrated: 1, active: 1, skipped: 0 }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('skips a link whose downstream soglia is not set', async () => { |
| 99 | + getLinks.resolves([link()]) |
| 100 | + getRiver.withArgs(20).resolves(river(20, null)) |
| 101 | + getRiver.withArgs(10).resolves(river(10, 5)) |
| 102 | + |
| 103 | + const summary = await runFloodCalibration() |
| 104 | + |
| 105 | + expect(update.called).to.equal(false) |
| 106 | + expect(summary).to.deep.equal({ calibrated: 0, active: 0, skipped: 1 }) |
| 107 | + }) |
| 108 | + |
| 109 | + it('skips a link with a missing river', async () => { |
| 110 | + getLinks.resolves([link()]) |
| 111 | + getRiver.withArgs(20).resolves(undefined) |
| 112 | + getRiver.withArgs(10).resolves(river(10, 5)) |
| 113 | + |
| 114 | + const summary = await runFloodCalibration() |
| 115 | + |
| 116 | + expect(summary.skipped).to.equal(1) |
| 117 | + expect(update.called).to.equal(false) |
| 118 | + }) |
| 119 | + |
| 120 | + it('isolates a per-link failure and continues', async () => { |
| 121 | + getLinks.resolves([link({ id: 1 }), link({ id: 2 })]) |
| 122 | + getRiver.withArgs(20).resolves(river(20, 10)) |
| 123 | + getRiver.withArgs(10).resolves(river(10, 5)) |
| 124 | + getLevels.withArgs(20).rejects(new Error('db down')) |
| 125 | + |
| 126 | + const summary = await runFloodCalibration() |
| 127 | + |
| 128 | + expect(summary.skipped).to.equal(2) |
| 129 | + expect(summary.calibrated).to.equal(0) |
| 130 | + }) |
| 131 | + |
| 132 | + it('persists an inactive model when there are too few events', async () => { |
| 133 | + getLinks.resolves([link()]) |
| 134 | + getRiver.withArgs(20).resolves(river(20, 10)) |
| 135 | + getRiver.withArgs(10).resolves(river(10, 5)) |
| 136 | + getLevels.withArgs(20).resolves(e[0].downstream) // single event only |
| 137 | + getLevels.withArgs(10).resolves(e[0].upstream) |
| 138 | + |
| 139 | + const summary = await runFloodCalibration() |
| 140 | + |
| 141 | + const patch = update.firstCall.args[1] |
| 142 | + expect(patch.sample_size).to.equal(1) |
| 143 | + expect(summary).to.deep.equal({ calibrated: 1, active: 0, skipped: 0 }) |
| 144 | + }) |
| 145 | +}) |
0 commit comments