Skip to content

Commit

Permalink
TMP: figuring out tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoblersTune committed Jul 25, 2024
1 parent 63f5462 commit 17f5214
Showing 1 changed file with 199 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import assert from 'assert'
import assert from 'assert'
import { Readable } from 'stream'
import {
serializeIlpPrepare,
Expand All @@ -11,22 +11,20 @@ import { createContext, MockIncomingMessageOptions } from '../../utils'
import {
createIlpPacketMiddleware,
IlpResponse,
ZeroCopyIlpPrepare
// OutgoingAccount
ZeroCopyIlpPrepare,
OutgoingAccount
} from '../../'
import {
IlpPrepareFactory,
IlpFulfillFactory,
IlpRejectFactory,
// IncomingAccountFactory,
IncomingAccountFactory,
RafikiServicesFactory
} from '../../factories'
import { HttpContext } from '../../rafiki'

describe('ILP Packet Middleware', () => {
test('sets up request and response', async () => {
// const incomingAccount = IncomingAccountFactory.build({ id: 'alice' })
// assert.ok(incomingAccount.id)
const services = RafikiServicesFactory.build({})

const options: MockIncomingMessageOptions = {
Expand All @@ -36,20 +34,6 @@ describe('ILP Packet Middleware', () => {
const ctx = createContext<unknown, HttpContext>({
req: options,
services: { ...services, telemetry: undefined }
// accounts: {
// incoming: incomingAccount,
// outgoing: { asset: { code: 'USD', scale: 2 } } as OutgoingAccount
// },
// state: {
// unfulfillable: false,
// incomingAccount: {
// quote: 'exists',
// asset: {
// code: 'USD',
// scale: 2
// }
// }
// }
})

const prepare = IlpPrepareFactory.build()
Expand Down Expand Up @@ -202,3 +186,198 @@ describe('IlpResponse', () => {
expect(response.rawFulfill).toBeUndefined()
})
})

describe('Telemetry Middleware', function () {
const incomingAccount = IncomingAccountFactory.build({ id: 'alice' })
assert.ok(incomingAccount.id)
let services: ReturnType<typeof RafikiServicesFactory.build>

beforeAll(() => {
services = RafikiServicesFactory.build({})
})

it('should not gather telemetry if telemetry is not enabled (service is undefined)', async () => {
const options: MockIncomingMessageOptions = {
headers: { 'content-type': 'application/octet-stream' }
}
const ctx = createContext<unknown, HttpContext>({
req: options,
services: { ...services, telemetry: undefined },
accounts: {
incoming: incomingAccount,
outgoing: { asset: { code: 'USD', scale: 2 } } as OutgoingAccount
},
state: {
unfulfillable: false,
incomingAccount: {
asset: {
code: 'USD',
scale: 2
}
}
}
})
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()
// add in details like amounts and names maybe
})

it('should not gather telemetry if response.fulfill undefined', async () => {
const options: MockIncomingMessageOptions = {
headers: { 'content-type': 'application/octet-stream' }
}
const 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
}
}
}
})
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(ctx.response.rawFulfill).toBeUndefined()
expect(incrementCounterWithTransactionAmountSpy).not.toHaveBeenCalled()
expect(incrementCounterSpy).toHaveBeenCalledTimes(2)
// add in details like amounts and names maybe
// maybe add assert about fulfill being undefined?
})

// it('should not gather telemetry if request.prepare.amount is 0', async () => { // How do you change prepare amount to 0?
// const options: MockIncomingMessageOptions = {
// headers: { 'content-type': 'application/octet-stream' }
// }
// const 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
// }
// }
// }
// })
// 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)
// // add in details like amounts and names maybe
// // maybe add assert about fulfill being undefined?
// })

it('should gather telemetry when enabled and response.fulfill', async () => {
const options: MockIncomingMessageOptions = {
headers: { 'content-type': 'application/octet-stream' }
}
const 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
}
}
}
})
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(response.rawFulfill).toBeDefined()
expect(incrementCounterWithTransactionAmountSpy).toHaveBeenCalledTimes(1)
expect(incrementCounterSpy).toHaveBeenCalledTimes(2)
// add in details like amounts and names maybe
})
// test reject vs fulfill
})

0 comments on commit 17f5214

Please sign in to comment.