Skip to content

Commit 8ecc4e5

Browse files
committed
Add tests for submitting measurements
1 parent b21fa56 commit 8ecc4e5

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed

lib/submit-measurement.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
*
3+
* @param {boolean} measurement
4+
* @param {typeof globalThis.fetch} fetch
5+
*/
6+
export const submit = async (measurement, fetch = globalThis.fetch) => {
7+
const res = await fetch('https://api.checker.network/arweave/measurement', {
8+
method: 'POST',
9+
headers: {
10+
'Content-Type': 'application/json'
11+
},
12+
body: JSON.stringify({ retrievalSucceeded: measurement })
13+
})
14+
if (!res.ok) {
15+
throw new Error(`Failed to submit measurement (status=${res.status})`, {
16+
cause: new Error(await res.text().catch(() => null))
17+
})
18+
}
19+
}

main.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/* global Arweave */
2+
/* global Zinnia */
23

34
import './vendor/arweave.js'
45
import pTimeout from './vendor/p-timeout.js'
56
import { getNodes } from './lib/nodes.js'
7+
import { submit } from './lib/submit-measurement.js'
68

79
const ONE_MINUTE = 60_000
810
const MEASUREMENT_DELAY = ONE_MINUTE
@@ -31,21 +33,6 @@ const measure = async node => {
3133
return true
3234
}
3335

34-
const submit = async measurement => {
35-
const res = await fetch('https://api.checker.network/arweave/measurement', {
36-
method: 'POST',
37-
headers: {
38-
'Content-Type': 'application/json'
39-
},
40-
body: JSON.stringify(measurement)
41-
})
42-
if (!res.ok) {
43-
throw new Error(`Failed to submit measurement (status=${res.status})`, {
44-
cause: new Error(await res.text().catch(() => null))
45-
})
46-
}
47-
}
48-
4936
let nodes = await getNodes()
5037

5138
;(async () => {
@@ -71,6 +58,7 @@ while (true) {
7158
console.error('Error submitting measurement')
7259
console.error(err)
7360
}
61+
Zinnia.jobComplete()
7462
console.log(`Waiting ${MEASUREMENT_DELAY / 1_000} seconds...`)
7563
await new Promise(resolve => setTimeout(resolve, MEASUREMENT_DELAY))
7664
}

tests/submit-measurement.test.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { test } from 'zinnia:test'
2+
import { assertEquals, assertRejects } from 'zinnia:assert'
3+
import { submit } from '../lib/submit-measurement.js'
4+
5+
test('submit measurement succeeds', async () => {
6+
const requests = []
7+
const fetch = async (url, allOpts) => {
8+
const { signal, ...opts } = allOpts
9+
requests.push({ url, opts })
10+
11+
return {
12+
status: 200,
13+
ok: true
14+
15+
}
16+
}
17+
18+
const measurement = true
19+
await submit(measurement, fetch)
20+
assertEquals(requests.length, 1)
21+
assertEquals(requests, [
22+
{
23+
url: 'https://api.checker.network/arweave/measurement',
24+
opts: {
25+
method: 'POST',
26+
headers: { 'Content-Type': 'application/json' },
27+
body: JSON.stringify({ retrievalSucceeded: measurement })
28+
}
29+
}
30+
])
31+
})
32+
33+
test('submit measurements fails', async () => {
34+
const requests = []
35+
const fetch = async (url, allOpts) => {
36+
const { signal, ...opts } = allOpts
37+
requests.push({ url, opts })
38+
39+
return {
40+
status: 500,
41+
ok: false,
42+
text: async () => 'Internal Server Error'
43+
}
44+
}
45+
46+
const measurement = true
47+
await assertRejects(async () => await submit(measurement, fetch), 'Failed to submit measurement (status=500)')
48+
assertEquals(requests.length, 1)
49+
assertEquals(requests, [
50+
{
51+
url: 'https://api.checker.network/arweave/measurement',
52+
opts: {
53+
method: 'POST',
54+
headers: { 'Content-Type': 'application/json' },
55+
body: JSON.stringify({ retrievalSucceeded: measurement })
56+
}
57+
}
58+
])
59+
})

0 commit comments

Comments
 (0)