Skip to content

Commit

Permalink
Mock range req (#58)
Browse files Browse the repository at this point in the history
* test: check range request output has expected bytes

* tests: fix msw not being able to stall connections
  • Loading branch information
guanzo committed Jan 29, 2024
1 parent 1504fb0 commit 18f5429
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 36 deletions.
113 changes: 78 additions & 35 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import assert from 'node:assert/strict'
import { randomUUID } from 'node:crypto'
import { describe, it, before, after } from 'node:test'
import { getMockServer, mockJWT, MSW_SERVER_OPTS } from './test-utils.js'
import {
getMockServer,
mockJWT,
mockOriginHandler,
MSW_SERVER_OPTS
} from './test-utils.js'
import { Saturn } from '#src/index.js'

const TEST_CID = 'QmXjYBY478Cno4jzdCcPy4NcJYFrwHZ51xaCP8vUwN9MGm'
const HELLO_CID = 'bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4'
const TEST_AUTH = 'https://fz3dyeyxmebszwhuiky7vggmsu0rlkoy.lambda-url.us-west-2.on.aws/'
const TEST_ORIGIN_DOMAIN = 'l1s.saturn.test'
const clientKey = 'abc123'

describe('Saturn client', () => {
Expand Down Expand Up @@ -38,10 +45,17 @@ describe('Saturn client', () => {
})

describe('Fetch a CID', () => {
const client = new Saturn({ clientKey, authURL: TEST_AUTH })
const client = new Saturn({
clientKey,
cdnURL: TEST_ORIGIN_DOMAIN,
authURL: TEST_AUTH
})

const handlers = [
mockJWT(TEST_AUTH)
mockJWT(TEST_AUTH),
mockOriginHandler(TEST_ORIGIN_DOMAIN, 0, false)
]

const server = getMockServer(handlers)

before(() => {
Expand All @@ -50,11 +64,66 @@ describe('Saturn client', () => {
after(() => {
server.close()
})

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

it('should fetch test CID with range', async () => {
const opts = {
range: {
rangeStart: 3,
rangeEnd: 7
}
}
const uintArray = await client.fetchContentBuffer(HELLO_CID, opts)
const actualContent = Buffer.from(uintArray).toString()

// CAR content is: "hello world"
// To get this value:
// $ car gb hello.car bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4 | cut -c 4-8
const expectedContent = 'lo wo'

assert.strictEqual(actualContent.length, 5)
assert.strictEqual(actualContent, expectedContent)
})

it('should create a log on fetch success', async () => {
client.reportingLogs = true
for await (const _ of client.fetchContent(HELLO_CID)) {} // eslint-disable-line

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)
})
})

describe('Fetch CID error cases', () => {
const client = new Saturn({
clientKey,
authURL: TEST_AUTH
})

// Doesn't use L1 origin mock, not sure how to force msw to stall a connection
// to test connection timeouts.
const handlers = [
mockJWT(TEST_AUTH)
]

const server = getMockServer(handlers)

before(() => {
server.listen(MSW_SERVER_OPTS)
})
after(() => {
server.close()
})

it('should fail to fetch non CID', async () => {
await assert.rejects(client.fetchCID('a'))
})
Expand All @@ -76,6 +145,12 @@ describe('Saturn client', () => {
)
})

it('should create a log on fetch network error', async () => {
await assert.rejects(client.fetchContentBuffer(HELLO_CID, { connectTimeout: 1 }))
const log = client.logs.pop()
assert.strictEqual(log.error, 'This operation was aborted')
})

it.skip('should fail when exceeding download timeout', async () => {
await assert.rejects(client.fetchCID(`${TEST_CID}/blah`, { downloadTimeout: 1 }))
})
Expand All @@ -92,36 +167,4 @@ describe('Saturn client', () => {
assert.strictEqual(client.createRequestURL('bafy...', { range: { rangeEnd: 20 } }).searchParams.get('entity-bytes'), '0:20')
})
})

describe('Logging', () => {
const handlers = [
mockJWT(TEST_AUTH)
]
const server = getMockServer(handlers)
const client = new Saturn({ clientKey, clientId: 'tesd', authURL: TEST_AUTH })
before(() => {
server.listen(MSW_SERVER_OPTS)
})
after(() => {
server.close()
})
it('should create a log on fetch success', async () => {
client.reportingLogs = true
for await (const _ of client.fetchContent(TEST_CID)) {} // eslint-disable-line

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.error, 'This operation was aborted')
})
})
})
2 changes: 1 addition & 1 deletion test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function generateNodes (count, originDomain) {
}

/**
* Generates a mock handler to mimick Saturn's orchestrator /nodes endpoint.
* Generates a mock handler to mimick an L1 node.
*
* @param {string} originUrl - originUrl
* @param {number} delay - request delay in ms
Expand Down

0 comments on commit 18f5429

Please sign in to comment.