File size: 1,829 Bytes
1e92f2d |
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 |
import assert from 'assert'
import createInterface from './snowflake.js'
import { mock, test } from 'node:test'
test('sending measurements to the batch endpoint', async () => {
const requests: Array<[string, RequestInit]> = []
const fetchMock = mock.method(
global,
'fetch',
(req: string, options: RequestInit) => {
requests.push([req, options])
return Promise.resolve(Response.json({}))
}
)
const iface = createInterface({
gatewayUri: 'http://127.0.0.1/v1/batch',
topicName: 'my-topic',
schemaId: 123,
})
assert(iface.measurement != null)
await iface.measurement('scenario', { myprop: 'foo' }, 'one', 42, 'ms')
await iface.measurement('scenario', { myprop: 'foo' }, 'two', 45, 'ms')
await iface.measurement('scenario', { myprop: 'foo' }, 'three', 38, 'ms')
assert(iface.end != null)
await iface.end('scenario', { myprop: 'foo' })
assert.equal(requests.length, 1)
const [url, options] = requests[0]
assert.equal(url, 'http://127.0.0.1/v1/batch')
assert(typeof options.body === 'string')
const body = JSON.parse(options.body)
assert.equal(body.schema_id, 123)
assert.equal(body.topic, 'my-topic')
assert(body.records.length === 3)
assert.equal(body.records[0].metric, 'one')
assert.equal(body.records[0].value, 42)
assert.equal(body.records[0].props_json, '{"myprop":"foo"}')
assert.equal(body.records[0].unit, 'ms')
assert.equal(body.records[1].metric, 'two')
assert.equal(body.records[1].value, 45)
assert.equal(body.records[1].props_json, '{"myprop":"foo"}')
assert.equal(body.records[1].unit, 'ms')
assert.equal(body.records[2].metric, 'three')
assert.equal(body.records[2].value, 38)
assert.equal(body.records[2].props_json, '{"myprop":"foo"}')
assert.equal(body.records[2].unit, 'ms')
fetchMock.mock.restore()
})
|