|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { decideHealthBanner, LAGGING_BANNER_THRESHOLD } from './node-health-banner' |
| 3 | + |
| 4 | +/** |
| 5 | + * The banner is the user-facing half of /api/health: it exists so a visitor is |
| 6 | + * never shown a frozen chain that looks current. It must be loud when we are |
| 7 | + * demonstrably behind and silent otherwise — a banner that cries wolf during |
| 8 | + * ordinary block propagation gets ignored, which is the same failure as not |
| 9 | + * having one. |
| 10 | + */ |
| 11 | + |
| 12 | +describe('decideHealthBanner', () => { |
| 13 | + it('says nothing when the node is on the chain tip', () => { |
| 14 | + expect(decideHealthBanner({ status: 'ok', lagBlocks: 0 })).toBeNull() |
| 15 | + }) |
| 16 | + |
| 17 | + it('says nothing while the health of the node is still unknown to the page', () => { |
| 18 | + expect(decideHealthBanner(undefined)).toBeNull() |
| 19 | + }) |
| 20 | + |
| 21 | + it('stays silent for a one-block lag, which is ordinary propagation', () => { |
| 22 | + expect(decideHealthBanner({ status: 'lagging', lagBlocks: 1 })).toBeNull() |
| 23 | + }) |
| 24 | + |
| 25 | + it('warns once the lag grows past the threshold', () => { |
| 26 | + const decision = decideHealthBanner({ status: 'lagging', lagBlocks: LAGGING_BANNER_THRESHOLD }) |
| 27 | + expect(decision).not.toBeNull() |
| 28 | + expect(decision?.tone).toBe('warning') |
| 29 | + expect(decision?.messageKey).toBe('lagging') |
| 30 | + }) |
| 31 | + |
| 32 | + it('raises a danger banner when the node has stopped following the chain', () => { |
| 33 | + const decision = decideHealthBanner({ status: 'stalled', lagBlocks: 69 }) |
| 34 | + expect(decision?.tone).toBe('danger') |
| 35 | + expect(decision?.messageKey).toBe('stalled') |
| 36 | + expect(decision?.lagBlocks).toBe(69) |
| 37 | + }) |
| 38 | + |
| 39 | + it('warns when health cannot be established rather than staying silent', () => { |
| 40 | + const decision = decideHealthBanner({ status: 'unknown', lagBlocks: 0 }) |
| 41 | + expect(decision?.tone).toBe('warning') |
| 42 | + expect(decision?.messageKey).toBe('unknown') |
| 43 | + }) |
| 44 | + |
| 45 | + it('warns when the health endpoint itself cannot be reached', () => { |
| 46 | + const decision = decideHealthBanner(null) |
| 47 | + expect(decision?.tone).toBe('warning') |
| 48 | + expect(decision?.messageKey).toBe('unreachable') |
| 49 | + }) |
| 50 | +}) |
0 commit comments