Skip to content

Commit 756c8fe

Browse files
committed
Increase coverage for new module
1 parent 4e625cb commit 756c8fe

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

packages/neaps/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function stationsNear(position: GeolibInputCoordinates, limit = 10) {
4444
.map((station) => ({ station, distance: getDistance(position, station) }))
4545
.sort((a, b) => a.distance - b.distance)
4646
.slice(0, limit)
47-
.map(({ station, distance }) => createStation(station, distance))
47+
.map(({ station, distance }) => useStation(station, distance))
4848
}
4949

5050
/**
@@ -65,10 +65,10 @@ export function findStation(query: string) {
6565

6666
if (!found) throw new Error(`Station not found: ${query}`)
6767

68-
return createStation(found)
68+
return useStation(found)
6969
}
7070

71-
function createStation(metadata: Station, distance?: number) {
71+
export function useStation(metadata: Station, distance?: number) {
7272
// Use MLLW as the default datum if available
7373
const defaultDatum = 'MLLW' in metadata.datums ? 'MLLW' : undefined
7474

@@ -82,7 +82,7 @@ function createStation(metadata: Station, distance?: number) {
8282
const datumOffset = metadata.datums?.[datum]
8383
const mslOffset = metadata.datums?.['MSL']
8484

85-
if (!datum) {
85+
if (!datumOffset) {
8686
throw new Error(
8787
`Station ${metadata.id} missing ${datum} datum. Available datums: ${Object.keys(metadata.datums || {}).join(', ')}`
8888
)

packages/neaps/test/index.test.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import stations from '@neaps/tide-stations'
12
import {
23
getExtremesPrediction,
34
nearestStation,
4-
findStation
5+
findStation,
6+
useStation
57
} from '../src/index.js'
68
import { describe, test, expect } from 'vitest'
79

@@ -96,3 +98,63 @@ describe('findStation', () => {
9698
expect(station.getExtremesPrediction).toBeDefined()
9799
})
98100
})
101+
102+
describe('datum', () => {
103+
test('defaults to MLLW datum', () => {
104+
const station = findStation('us-fl-ankona-indian-river')
105+
const extremes = station.getExtremesPrediction({
106+
start: new Date('2025-12-17T00:00:00Z'),
107+
end: new Date('2025-12-18T00:00:00Z')
108+
})
109+
expect(extremes.datum).toBe('MLLW')
110+
})
111+
112+
test('accepts datum option', () => {
113+
const station = findStation('us-fl-ankona-indian-river')
114+
const extremes = station.getExtremesPrediction({
115+
start: new Date('2025-12-17T00:00:00Z'),
116+
end: new Date('2025-12-18T00:00:00Z'),
117+
datum: 'NAVD88'
118+
})
119+
expect(extremes.datum).toBe('NAVD88')
120+
})
121+
122+
test('throws error for unavailable datum', () => {
123+
const station = findStation('us-ma-boston')
124+
expect(() => {
125+
station.getExtremesPrediction({
126+
start: new Date('2025-12-17T00:00:00Z'),
127+
end: new Date('2025-12-18T00:00:00Z'),
128+
datum: 'UNKNOWN_DATUM'
129+
})
130+
}).toThrow(/missing UNKNOWN_DATUM/)
131+
})
132+
133+
test('throws error when missing MSL datum', () => {
134+
// Find station without MSL but with other datums
135+
const station = stations.find(
136+
(s) => !('MSL' in s.datums) && Object.keys(s.datums).length > 0
137+
)
138+
if (!station) expect.fail('No station without MSL datum found')
139+
expect(() => {
140+
useStation(station).getExtremesPrediction({
141+
start: new Date('2025-12-17T00:00:00Z'),
142+
end: new Date('2025-12-18T00:00:00Z'),
143+
datum: 'NAVD88'
144+
})
145+
}).toThrow(/missing MSL/)
146+
})
147+
148+
test('does not apply datums when non available', () => {
149+
// Find a station with no datums
150+
const station = stations.find((s) => Object.entries(s.datums).length === 0)
151+
if (!station) expect.fail('No station without datums found')
152+
const extremes = useStation(station).getExtremesPrediction({
153+
start: new Date('2025-12-17T00:00:00Z'),
154+
end: new Date('2025-12-18T00:00:00Z')
155+
})
156+
157+
expect(extremes.datum).toBeUndefined()
158+
expect(extremes.predictions.length).toBeGreaterThan(0)
159+
})
160+
})

0 commit comments

Comments
 (0)