Skip to content

Commit

Permalink
test: add basic log tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzo committed Sep 14, 2023
1 parent 0fccc20 commit 36b3e02
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { describe, it } from 'node:test'

import Saturn from '#src/index.js'

const TEST_CID = 'QmXjYBY478Cno4jzdCcPy4NcJYFrwHZ51xaCP8vUwN9MGm'

describe('Saturn client', () => {
describe('constructor', () => {
it('should work w/o custom client ID', () => {
Expand Down Expand Up @@ -37,7 +39,7 @@ describe('Saturn client', () => {
const client = new Saturn()

it('should fetch test CID', async () => {
const { res } = await client.fetchCID('QmXjYBY478Cno4jzdCcPy4NcJYFrwHZ51xaCP8vUwN9MGm')
const { res } = await client.fetchCID(TEST_CID)
assert(res instanceof Response)
})

Expand All @@ -46,11 +48,35 @@ describe('Saturn client', () => {
})

it('should fail when exceeding connection timeout', async () => {
await assert.rejects(client.fetchCID('QmXjYBY478Cno4jzdCcPy4NcJYFrwHZ51xaCP8vUwN9MGm', { connectTimeout: 1 }))
await assert.rejects(client.fetchCID(TEST_CID, { connectTimeout: 1 }))
})

it.skip('should fail when exceeding download timeout', async () => {
await assert.rejects(client.fetchCID('QmXjYBY478Cno4jzdCcPy4NcJYFrwHZ51xaCP8vUwN9MGm/blah', { downloadTimeout: 1 }))
await assert.rejects(client.fetchCID(`${TEST_CID}/blah`, { downloadTimeout: 1 }))
})
})

describe('Logging', () => {
const client = new Saturn()
client.reportingLogs = true

it('should create a log on fetch success', async () => {
for await (const _ of client.fetchContent(TEST_CID)) {}

const log = client.logs.pop()

assert(Number.isFinite(log.ttfbMs) && log.ttfbMs > 0)
assert.strictEqual(log.httpStatusCode, 200)
assert(Number.isFinite(log.numBytesSent) && log.numBytesSent > 0)
assert(Number.isFinite(log.requestDurationSec) && log.requestDurationSec > 0)
assert(!log.ifNetworkError)
})

it('should create a log on fetch network error', async () => {
await assert.rejects(client.fetchContentBuffer(TEST_CID, { connectTimeout: 1 }))

const log = client.logs.pop()
assert.strictEqual(log.ifNetworkError, 'This operation was aborted')
})
})
})

0 comments on commit 36b3e02

Please sign in to comment.