Skip to content

Commit

Permalink
chore: removing missed middleware tests from the connector core
Browse files Browse the repository at this point in the history
  • Loading branch information
JoblersTune committed Aug 7, 2024
1 parent a8a9b32 commit c3c8c53
Showing 1 changed file with 1 addition and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert from 'assert'
import { Readable } from 'stream'
import {
serializeIlpPrepare,
Expand All @@ -11,14 +10,12 @@ import { createContext, MockIncomingMessageOptions } from '../../utils'
import {
createIlpPacketMiddleware,
IlpResponse,
ZeroCopyIlpPrepare,
OutgoingAccount
ZeroCopyIlpPrepare
} from '../../'
import {
IlpPrepareFactory,
IlpFulfillFactory,
IlpRejectFactory,
IncomingAccountFactory,
RafikiServicesFactory
} from '../../factories'
import { HttpContext } from '../../rafiki'
Expand Down Expand Up @@ -186,133 +183,3 @@ describe('IlpResponse', () => {
expect(response.rawFulfill).toBeUndefined()
})
})

describe('Telemetry Collection In ILP Packet Middleware', function () {
let ctx: HttpContext
let services: ReturnType<typeof RafikiServicesFactory.build>
let incomingAccount: ReturnType<typeof IncomingAccountFactory.build>

beforeEach(() => {
services = RafikiServicesFactory.build({})
incomingAccount = IncomingAccountFactory.build({ id: 'alice' })
assert.ok(incomingAccount.id)
const options: MockIncomingMessageOptions = {
headers: { 'content-type': 'application/octet-stream' }
}
ctx = createContext<unknown, HttpContext>({
req: options,
services,
accounts: {
incoming: incomingAccount,
outgoing: { asset: { code: 'USD', scale: 2 } } as OutgoingAccount
},
state: {
unfulfillable: false,
incomingAccount: {
asset: {
code: 'USD',
scale: 2
}
}
}
})
})

it('should not gather telemetry if telemetry is not enabled (service is undefined)', async () => {
ctx.services = { ...services, telemetry: undefined }
const incrementCounterWithTransactionAmountSpy = jest
.spyOn(services.telemetry, 'incrementCounterWithTransactionAmount')
.mockImplementation(() => Promise.resolve())

const incrementCounterSpy = jest
.spyOn(services.telemetry, 'incrementCounter')
.mockImplementation(() => Promise.resolve())

const prepare = IlpPrepareFactory.build()
const getRawBody = async (_req: Readable) => serializeIlpPrepare(prepare)
const rawReply = serializeIlpFulfill(IlpFulfillFactory.build())
const ilpHandler = jest.fn().mockImplementation((ctx, next) => {
ctx.response.rawReply = rawReply
next()
})
const next = jest.fn()
const middleware = createIlpPacketMiddleware(ilpHandler, { getRawBody })

await expect(middleware(ctx, next)).resolves.toBeUndefined()
expect(incrementCounterWithTransactionAmountSpy).not.toHaveBeenCalled()
expect(incrementCounterSpy).not.toHaveBeenCalled()
})

it('should not gather telemetry if request.prepare.amount is 0', async () => {
const incrementCounterWithTransactionAmountSpy = jest
.spyOn(services.telemetry, 'incrementCounterWithTransactionAmount')
.mockImplementation(() => Promise.resolve())

const incrementCounterSpy = jest
.spyOn(services.telemetry, 'incrementCounter')
.mockImplementation(() => Promise.resolve())

const prepare = IlpPrepareFactory.build()
prepare.amount = '0'
const getRawBody = async (_req: Readable) => serializeIlpPrepare(prepare)
const rawReply = serializeIlpReject(IlpRejectFactory.build())
const ilpHandler = jest.fn().mockImplementation((ctx, next) => {
ctx.response.rawReply = rawReply
next()
})
const next = jest.fn()
const middleware = createIlpPacketMiddleware(ilpHandler, { getRawBody })

await expect(middleware(ctx, next)).resolves.toBeUndefined()
expect(incrementCounterWithTransactionAmountSpy).not.toHaveBeenCalled()
expect(incrementCounterSpy).not.toHaveBeenCalled()
})

it('should gather amount and packet count telemetry if response.fulfill is defined', async () => {
const incrementCounterWithTransactionAmountSpy = jest
.spyOn(services.telemetry, 'incrementCounterWithTransactionAmount')
.mockImplementation(() => Promise.resolve())

const incrementCounterSpy = jest
.spyOn(services.telemetry, 'incrementCounter')
.mockImplementation(() => Promise.resolve())

const prepare = IlpPrepareFactory.build()
const getRawBody = async (_req: Readable) => serializeIlpPrepare(prepare)
const rawReply = serializeIlpFulfill(IlpFulfillFactory.build())
const ilpHandler = jest.fn().mockImplementation((ctx, next) => {
ctx.response.rawReply = rawReply
next()
})
const next = jest.fn()
const middleware = createIlpPacketMiddleware(ilpHandler, { getRawBody })

await expect(middleware(ctx, next)).resolves.toBeUndefined()
expect(incrementCounterWithTransactionAmountSpy).toHaveBeenCalledTimes(1)
expect(incrementCounterSpy).toHaveBeenCalledTimes(2)
})

it('should only gather packet count telemetry if response.fulfill undefined', async () => {
const incrementCounterWithTransactionAmountSpy = jest
.spyOn(services.telemetry, 'incrementCounterWithTransactionAmount')
.mockImplementation(() => Promise.resolve())

const incrementCounterSpy = jest
.spyOn(services.telemetry, 'incrementCounter')
.mockImplementation(() => Promise.resolve())

const prepare = IlpPrepareFactory.build()
const getRawBody = async (_req: Readable) => serializeIlpPrepare(prepare)
const rawReply = serializeIlpReject(IlpRejectFactory.build())
const ilpHandler = jest.fn().mockImplementation((ctx, next) => {
ctx.response.rawReply = rawReply
next()
})
const next = jest.fn()
const middleware = createIlpPacketMiddleware(ilpHandler, { getRawBody })

await expect(middleware(ctx, next)).resolves.toBeUndefined()
expect(incrementCounterWithTransactionAmountSpy).not.toHaveBeenCalled()
expect(incrementCounterSpy).toHaveBeenCalledTimes(2)
})
})

0 comments on commit c3c8c53

Please sign in to comment.