Skip to content

Commit

Permalink
fix: tests and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrunic authored and ckousik committed Dec 23, 2022
1 parent 2a791f5 commit 5fc1cc6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/circuit/v2/hop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function reserve (connection: Connection) {
type: HopMessage.Type.RESERVE
}).finish())

let response
let response: HopMessage|undefined
try {
response = HopMessage.decode(await streamHandler.read())
} catch (e: any) {
Expand All @@ -60,7 +60,7 @@ export async function reserve (connection: Connection) {
if (response.status === Status.OK && response.reservation !== null) {
return response.reservation
}
const errMsg = `reservation failed with status ${response.status}`
const errMsg = `reservation failed with status ${response.status ?? 'undefined'}`
log.error(errMsg)
throw new Error(errMsg)
}
Expand Down Expand Up @@ -118,8 +118,8 @@ async function handleConnect (options: HopConnectOptions) {
return
}

// @ts-expect-error
const dstPeer = peerIdFromBytes(request.peer.id)
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const dstPeer = peerIdFromBytes(request.peer!.id)

if (acl?.allowConnect !== undefined) {
const status = await acl.allowConnect(connection.remotePeer, connection.remoteAddr, dstPeer)
Expand Down
5 changes: 3 additions & 2 deletions src/circuit/v2/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ export async function stop ({
}

if (response == null) {
return streamHandler.close()
streamHandler.close()
return undefined
}

if (response.status === Status.OK) {
log('stop request to %s was successful', connection.remotePeer)
return streamHandler.rest()
}

log('stop request failed with code %d', response.status)
streamHandler.close()
return undefined
}
23 changes: 15 additions & 8 deletions test/circuit/v2/hop.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { protocolIDv2Hop } from './../../../src/circuit/multicodec.js'

import { protocolIDv2Stop } from './../../../src/circuit/multicodec.js'
import { mockDuplex, mockConnection, mockMultiaddrConnection, mockStream } from '@libp2p/interface-compliance-tests/mocks'
import { expect } from 'aegir/utils/chai.js'
import { expect } from 'aegir/chai'
import * as peerUtils from '../../utils/creators/peer.js'
import { handleHopProtocol } from '../../../src/circuit/v2/hop.js'
import { StreamHandlerV2 } from '../../../src/circuit/v2/stream-handler.js'
import type { Connection } from '@libp2p/interfaces/connection'
import type { PeerId } from '@libp2p/interfaces/peer-id'
import { Status, HopMessage } from '../../../src/circuit/v2/pb/index.js'
import { Status, StopMessage, HopMessage } from '../../../src/circuit/v2/pb/index.js'
import { ReservationStore } from '../../../src/circuit/v2/reservation-store.js'
import sinon from 'sinon'
import { Circuit } from '../../../src/circuit/transport.js'
Expand Down Expand Up @@ -64,10 +65,10 @@ describe('Circuit v2 - hop protocol', function () {
expect(reserveStub.calledOnceWith(conn.remotePeer, conn.remoteAddr)).to.be.true()
const response = HopMessage.decode(await streamHandler.read())
expect(response.type).to.be.equal(HopMessage.Type.STATUS)
expect(response.limit).to.be.null()
expect(response.limit).to.be.undefined()
expect(response.status).to.be.equal(Status.OK)
expect(response.reservation?.expire).to.be.equal(expire)
expect(response.reservation?.voucher).to.not.be.null()
expect(response.reservation?.voucher).to.not.be.undefined()
expect(response.reservation?.addrs?.length).to.be.greaterThan(0)
})

Expand All @@ -88,7 +89,7 @@ describe('Circuit v2 - hop protocol', function () {
expect(reserveStub.notCalled).to.be.true()
const response = HopMessage.decode(await streamHandler.read())
expect(response.type).to.be.equal(HopMessage.Type.STATUS)
expect(response.limit).to.be.null()
expect(response.limit).to.be.undefined()
expect(response.status).to.be.equal(Status.PERMISSION_DENIED)
})

Expand All @@ -109,7 +110,7 @@ describe('Circuit v2 - hop protocol', function () {
expect(reserveStub.calledOnce).to.be.true()
const response = HopMessage.decode(await streamHandler.read())
expect(response.type).to.be.equal(HopMessage.Type.STATUS)
expect(response.limit).to.be.null()
expect(response.limit).to.be.undefined()
expect(response.status).to.be.equal(Status.RESERVATION_REFUSED)
})

Expand Down Expand Up @@ -161,7 +162,13 @@ describe('Circuit v2 - hop protocol', function () {
mockMultiaddrConnection(pair<Uint8Array>(), dstPeer)
)
const streamStub = sinon.stub(dstConn, 'newStream')
streamStub.resolves({ protocol: protocolIDv2Hop, stream: mockStream(pair<Uint8Array>()) })
const dstStream = { protocol: protocolIDv2Stop, stream: mockStream(pair<Uint8Array>()) }
streamStub.resolves(dstStream)
const dstStreamHandler = new StreamHandlerV2({ stream: dstStream.stream })
dstStreamHandler.write(StopMessage.encode({
type: StopMessage.Type.STATUS,
status: Status.OK
}))
const stub = sinon.stub(circuit, 'getPeerConnection')
stub.returns(dstConn)
await handleHopProtocol({
Expand Down

0 comments on commit 5fc1cc6

Please sign in to comment.