All files / test mocks.ts

96.55% Statements 28/29
44.44% Branches 4/9
100% Functions 5/5
96.55% Lines 28/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1202x 2x   2x   2x   2x         2x                                 2x         2x                                 2x             2x             2x             2x             2x                         2x 6x   6x 6x 26x 26x   26x   26x     26x   6x 6x 6x 6x   6x 6x            
import { createServer } from 'http'
import { Bee } from '../src'
 
let i = 11633
 
const responses = new Map<string, string>()
 
responses.set(
  'GET /chainstate',
  JSON.stringify({ chainTip: 38679237, block: 38679230, totalAmount: '183499080648', currentPrice: '26558' }),
)
 
responses.set(
  'GET /stamps/f8b2ad296d64824a8fe51a33ff15fe8668df13a20ad3d4eea4bb97ca600029aa',
  JSON.stringify({
    batchID: 'f8b2ad296d64824a8fe51a33ff15fe8668df13a20ad3d4eea4bb97ca600029aa',
    utilization: 46,
    usable: true,
    label: '',
    depth: 23,
    amount: '85844033282',
    bucketDepth: 16,
    blockNumber: 37496330,
    immutableFlag: true,
    exists: true,
    batchTTL: 2722136,
  }),
)
 
responses.set(
  'POST /stamps/458922241/22',
  JSON.stringify({ batchID: 'b330000000000000000000000000000000000000000000000000000000000000' }),
)
 
responses.set(
  'GET /stamps/b330000000000000000000000000000000000000000000000000000000000000',
  JSON.stringify({
    batchID: 'b330000000000000000000000000000000000000000000000000000000000000',
    utilization: 0,
    usable: true,
    label: '',
    depth: 22,
    amount: '458922240',
    bucketDepth: 16,
    blockNumber: 37496330,
    immutableFlag: true,
    exists: true,
    batchTTL: 86400,
  }),
)
 
responses.set(
  'PATCH /stamps/topup/b330000000000000000000000000000000000000000000000000000000000000/458922241',
  JSON.stringify({
    batchID: 'b330000000000000000000000000000000000000000000000000000000000000',
  }),
)
 
responses.set(
  'PATCH /stamps/topup/b330000000000000000000000000000000000000000000000000000000000000/917844481',
  JSON.stringify({
    batchID: 'b330000000000000000000000000000000000000000000000000000000000000',
  }),
)
 
responses.set(
  'PATCH /stamps/dilute/b330000000000000000000000000000000000000000000000000000000000000/23',
  JSON.stringify({
    batchID: 'b330000000000000000000000000000000000000000000000000000000000000',
  }),
)
 
responses.set(
  'PATCH /stamps/dilute/b330000000000000000000000000000000000000000000000000000000000000/24',
  JSON.stringify({
    batchID: 'b330000000000000000000000000000000000000000000000000000000000000',
  }),
)
 
responses.set(
  'POST /bzz?name=filename.txt',
  JSON.stringify({
    reference: 'f8b2ad296d64824a8fe51a33ff15fe8668df13a20ad3d4eea4bb97ca600029aa',
  }),
)
 
interface MockedCall {
  method: string
  url: string
  headers: Record<string, string | string[] | undefined>
}
 
export async function mocked(runnable: (bee: Bee) => Promise<void>): Promise<MockedCall[]> {
  const calls: MockedCall[] = []
 
  return new Promise(resolve => {
    const server = createServer((req, res) => {
      const identifier = (req.method || 'GET') + ' ' + (req.url || '/')
      calls.push({ method: req.method || 'GET', url: req.url || '/', headers: req.headers })
 
      const response = responses.get(identifier)
 
      Iif (!response) {
        res.end('Not found - ' + identifier)
      }
      res.end(response)
    })
    const port = i++
    server.listen(port, async () => {
      try {
        await runnable(new Bee(`http://localhost:${port}`))
      } finally {
        server.close(() => {
          resolve(calls)
        })
      }
    })
  })
}