forked from OurHike/OurHike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublishedConditions.test.ts
More file actions
275 lines (218 loc) · 10.2 KB
/
Copy pathpublishedConditions.test.ts
File metadata and controls
275 lines (218 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
// The published conditions baselines (#435 closures, #436 reports).
// Everything here turns on the same distinction lib/dataManifest.test.ts
// records: `null` means "no usable baseline", never "no conditions" - the two
// are opposite answers and the whole point of this path is that a hiker can
// tell them apart.
//
// config.ts reads VITE_DATA_BASE_URL once at module load and it is unset under
// test, so each case imports the module fresh against a stubbed env - which is
// also the only way to cover "no bucket configured at all".
const BASE = 'https://cdn.example.org'
async function loadWithBase(base: string | undefined) {
vi.resetModules()
vi.stubEnv('VITE_DATA_BASE_URL', base ?? '')
return await import('./publishedConditions')
}
function mockResponse(body: unknown, { status = 200 } = {}) {
return vi
.spyOn(globalThis, 'fetch')
.mockImplementation(
async () =>
new Response(typeof body === 'string' ? body : JSON.stringify(body), { status }),
)
}
const A_CLOSURES_DOCUMENT = {
generated_at: '2026-08-08T06:00:00Z',
closures: [{ id: 'c1', start_mile_marker: 10, end_mile_marker: 11 }],
}
const A_REPORTS_DOCUMENT = {
generated_at: '2026-08-08T06:00:00Z',
reports: [{ id: 'r1', type: 'blowdown', severity: 'serious', lat: 41.2, lon: -74.1 }],
}
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
vi.unstubAllEnvs()
vi.restoreAllMocks()
})
// The two baselines share one loader, so the failure handling is proven once,
// against whichever artifact the case reads most naturally with - and each
// artifact still gets its own happy path, because the key and the payload
// field are the two things that genuinely differ.
describe('fetchPublishedClosures', () => {
it('reads the artifact from the configured bucket', async () => {
const fetchSpy = mockResponse(A_CLOSURES_DOCUMENT)
const { fetchPublishedClosures, PUBLISHED_CLOSURES_KEY } = await loadWithBase(BASE)
const published = await fetchPublishedClosures()
expect(fetchSpy).toHaveBeenCalledWith(
`${BASE}/${PUBLISHED_CLOSURES_KEY}`,
expect.anything(),
)
expect(published?.items).toHaveLength(1)
expect(published?.generatedAt.toISOString()).toBe('2026-08-08T06:00:00.000Z')
})
it('asks for nothing when no bucket was configured at build time', async () => {
const fetchSpy = mockResponse(A_CLOSURES_DOCUMENT)
const { fetchPublishedClosures } = await loadWithBase(undefined)
expect(await fetchPublishedClosures()).toBeNull()
expect(fetchSpy).not.toHaveBeenCalled()
})
it('returns null rather than throwing when the bucket is unreachable', async () => {
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new TypeError('offline'))
const { fetchPublishedClosures } = await loadWithBase(BASE)
await expect(fetchPublishedClosures()).resolves.toBeNull()
})
it('returns null on a 404, which is what a release built before this artifact looks like', async () => {
mockResponse('', { status: 404 })
const { fetchPublishedClosures } = await loadWithBase(BASE)
expect(await fetchPublishedClosures()).toBeNull()
})
it('returns null on a document that is not JSON', async () => {
mockResponse('<!doctype html>')
const { fetchPublishedClosures } = await loadWithBase(BASE)
expect(await fetchPublishedClosures()).toBeNull()
})
it('refuses a document with no generated_at, rather than defaulting one', async () => {
// The strict case, and the only one. That timestamp is what becomes
// "as of <date>" - without it the app would show day-old closures with no
// sign of their age, which is the failure this path exists to remove.
mockResponse({ closures: [] })
const { fetchPublishedClosures } = await loadWithBase(BASE)
expect(await fetchPublishedClosures()).toBeNull()
})
it('refuses a document whose generated_at is not a date', async () => {
mockResponse({ generated_at: 'whenever', closures: [] })
const { fetchPublishedClosures } = await loadWithBase(BASE)
expect(await fetchPublishedClosures()).toBeNull()
})
it('refuses a document whose closures are not a list', async () => {
mockResponse({ generated_at: '2026-08-08T06:00:00Z', closures: 'none' })
const { fetchPublishedClosures } = await loadWithBase(BASE)
expect(await fetchPublishedClosures()).toBeNull()
})
it('accepts an empty list, which is a real answer and not a failure', async () => {
// The state of the bucket the day this shipped: the bake ran, read zero
// verified closures, and published exactly that. Treating it as a failure
// would fall back to "unavailable" and tell a hiker we could not ask,
// when in fact we asked and the trail is open.
mockResponse({ generated_at: '2026-08-08T06:00:00Z', closures: [] })
const { fetchPublishedClosures } = await loadWithBase(BASE)
const published = await fetchPublishedClosures()
expect(published).not.toBeNull()
expect(published?.items).toEqual([])
})
})
describe('fetchPublishedReports', () => {
it('reads its own artifact, under its own key', async () => {
const fetchSpy = mockResponse(A_REPORTS_DOCUMENT)
const { fetchPublishedReports, PUBLISHED_REPORTS_KEY } = await loadWithBase(BASE)
const published = await fetchPublishedReports()
expect(fetchSpy).toHaveBeenCalledWith(
`${BASE}/${PUBLISHED_REPORTS_KEY}`,
expect.anything(),
)
expect(published?.items).toHaveLength(1)
expect(published?.generatedAt.toISOString()).toBe('2026-08-08T06:00:00.000Z')
})
it('refuses a document whose payload is under the wrong name', async () => {
// A closures document served where reports were expected - a bucket
// misconfiguration, or a copy-paste in the bake. Validating the field by
// name turns it into "no usable baseline" rather than an empty trail.
mockResponse(A_CLOSURES_DOCUMENT)
const { fetchPublishedReports } = await loadWithBase(BASE)
expect(await fetchPublishedReports()).toBeNull()
})
it('accepts an empty list, which is a real answer and not a failure', async () => {
mockResponse({ generated_at: '2026-08-08T06:00:00Z', reports: [] })
const { fetchPublishedReports } = await loadWithBase(BASE)
const published = await fetchPublishedReports()
expect(published).not.toBeNull()
expect(published?.items).toEqual([])
})
})
// The third artifact (features/ATC_TRAIL_UPDATES.md, #461). Its own key and
// its own payload name like the other two, plus the one field neither of them
// has: `reviewed_at`, which is when a PERSON last checked ATC's page rather
// than when the bake ran. Both are real, they differ, and a daily bake
// stamping a daily `generated_at` on a three-month-old review would claim a
// freshness nobody has.
const AN_ATC_UPDATES_DOCUMENT = {
generated_at: '2026-08-12T08:40:00Z',
reviewed_at: '2026-08-12',
atc_updates: [
{
atc_id: 'va-creeper-trail-closure-detour',
title: 'SW Virginia: VA Creeper Trail Closure/Detour',
category: 'Closure',
states: ['VA'],
start_mile_marker: 476.6,
end_mile_marker: 485.8,
obstructs_trail: true,
updated_at: '2026-07-17T00:00:00Z',
source_url: 'https://appalachiantrail.org/trail-updates/va-creeper/',
},
],
}
describe('fetchPublishedAtcUpdates', () => {
it('reads its own artifact, under its own key', async () => {
const fetchSpy = mockResponse(AN_ATC_UPDATES_DOCUMENT)
const { fetchPublishedAtcUpdates, PUBLISHED_ATC_UPDATES_KEY } =
await loadWithBase(BASE)
const published = await fetchPublishedAtcUpdates()
expect(fetchSpy).toHaveBeenCalledWith(
`${BASE}/${PUBLISHED_ATC_UPDATES_KEY}`,
expect.anything(),
)
expect(published?.items).toHaveLength(1)
})
it('carries the review date alongside the bake date', async () => {
mockResponse(AN_ATC_UPDATES_DOCUMENT)
const { fetchPublishedAtcUpdates } = await loadWithBase(BASE)
const published = await fetchPublishedAtcUpdates()
expect(published?.generatedAt.toISOString()).toBe('2026-08-12T08:40:00.000Z')
expect(published?.reviewedAt?.toISOString()).toBe('2026-08-12T00:00:00.000Z')
})
it('still reads a document that has no review date', async () => {
// Lenient where `generated_at` is strict, because the two carry different
// weight: a missing review date costs a line of provenance on a sheet,
// not the age of the data.
mockResponse({ generated_at: '2026-08-12T08:40:00Z', atc_updates: [] })
const { fetchPublishedAtcUpdates } = await loadWithBase(BASE)
const published = await fetchPublishedAtcUpdates()
expect(published).not.toBeNull()
expect(published?.reviewedAt).toBeUndefined()
})
it('drops an unparseable review date rather than carrying an Invalid Date', async () => {
mockResponse({ ...AN_ATC_UPDATES_DOCUMENT, reviewed_at: 'sometime in May' })
const { fetchPublishedAtcUpdates } = await loadWithBase(BASE)
expect((await fetchPublishedAtcUpdates())?.reviewedAt).toBeUndefined()
})
it('answers null for the 404 served while nobody has reviewed the file', async () => {
// The pipeline publishes nothing at all in that state, deliberately: "we
// have not looked" and "ATC reports nothing" are different claims, and
// only one of them is safe to draw as an empty map.
mockResponse('', { status: 404 })
const { fetchPublishedAtcUpdates } = await loadWithBase(BASE)
expect(await fetchPublishedAtcUpdates()).toBeNull()
})
it('refuses a document whose payload is under the wrong name', async () => {
mockResponse(A_CLOSURES_DOCUMENT)
const { fetchPublishedAtcUpdates } = await loadWithBase(BASE)
expect(await fetchPublishedAtcUpdates()).toBeNull()
})
it('accepts an empty list, which a reviewer can honestly produce', async () => {
// "We looked, and ATC has nothing placeable" - the reviewed-but-empty
// case, which is a real answer and distinct from the 404 above.
mockResponse({
generated_at: '2026-08-12T08:40:00Z',
reviewed_at: '2026-08-12',
atc_updates: [],
})
const { fetchPublishedAtcUpdates } = await loadWithBase(BASE)
const published = await fetchPublishedAtcUpdates()
expect(published).not.toBeNull()
expect(published?.items).toEqual([])
})
})