1 | const t = require('tap')
|
---|
2 | const MPS = require('../')
|
---|
3 |
|
---|
4 | t.test('ok if size checks out', t => {
|
---|
5 | const mps = new MPS({ size: 4 })
|
---|
6 |
|
---|
7 | mps.write(Buffer.from('a').toString('hex'), 'hex')
|
---|
8 | mps.write(Buffer.from('sd'))
|
---|
9 | mps.end('f')
|
---|
10 | return mps.concat().then(data => t.equal(data.toString(), 'asdf'))
|
---|
11 | })
|
---|
12 |
|
---|
13 | t.test('error if size exceeded', t => {
|
---|
14 | const mps = new MPS({ size: 1 })
|
---|
15 | mps.on('error', er => {
|
---|
16 | t.match(er, {
|
---|
17 | message: 'Bad data size: expected 1 bytes, but got 4',
|
---|
18 | found: 4,
|
---|
19 | expect: 1,
|
---|
20 | code: 'EBADSIZE',
|
---|
21 | name: 'SizeError',
|
---|
22 | })
|
---|
23 | t.end()
|
---|
24 | })
|
---|
25 | mps.write('asdf')
|
---|
26 | })
|
---|
27 |
|
---|
28 | t.test('error if size is not met', t => {
|
---|
29 | const mps = new MPS({ size: 999 })
|
---|
30 | t.throws(() => mps.end(), {
|
---|
31 | message: 'Bad data size: expected 999 bytes, but got 0',
|
---|
32 | found: 0,
|
---|
33 | name: 'SizeError',
|
---|
34 | expect: 999,
|
---|
35 | code: 'EBADSIZE',
|
---|
36 | })
|
---|
37 | t.end()
|
---|
38 | })
|
---|
39 |
|
---|
40 | t.test('error if non-string/buffer is written', t => {
|
---|
41 | const mps = new MPS({size:1})
|
---|
42 | mps.on('error', er => {
|
---|
43 | t.match(er, {
|
---|
44 | message: 'MinipassSized streams only work with string and buffer data'
|
---|
45 | })
|
---|
46 | t.end()
|
---|
47 | })
|
---|
48 | mps.write({some:'object'})
|
---|
49 | })
|
---|
50 |
|
---|
51 | t.test('projectiles', t => {
|
---|
52 | t.throws(() => new MPS(), {
|
---|
53 | message: 'invalid expected size: undefined'
|
---|
54 | }, 'size is required')
|
---|
55 | t.throws(() => new MPS({size: true}), {
|
---|
56 | message: 'invalid expected size: true'
|
---|
57 | }, 'size must be number')
|
---|
58 | t.throws(() => new MPS({size: NaN}), {
|
---|
59 | message: 'invalid expected size: NaN'
|
---|
60 | }, 'size must not be NaN')
|
---|
61 | t.throws(() => new MPS({size:1.2}), {
|
---|
62 | message: 'invalid expected size: 1.2'
|
---|
63 | }, 'size must be integer')
|
---|
64 | t.throws(() => new MPS({size: Infinity}), {
|
---|
65 | message: 'invalid expected size: Infinity'
|
---|
66 | }, 'size must be finite')
|
---|
67 | t.throws(() => new MPS({size: -1}), {
|
---|
68 | message: 'invalid expected size: -1'
|
---|
69 | }, 'size must be positive')
|
---|
70 | t.throws(() => new MPS({objectMode: true}), {
|
---|
71 | message: 'MinipassSized streams only work with string and buffer data'
|
---|
72 | }, 'no objectMode')
|
---|
73 | t.throws(() => new MPS({size: Number.MAX_SAFE_INTEGER + 1000000}), {
|
---|
74 | message: 'invalid expected size: 9007199255740992'
|
---|
75 | })
|
---|
76 | t.end()
|
---|
77 | })
|
---|
78 |
|
---|
79 | t.test('exports SizeError class', t => {
|
---|
80 | t.isa(MPS.SizeError, 'function')
|
---|
81 | t.isa(MPS.SizeError.prototype, Error)
|
---|
82 | t.end()
|
---|
83 | })
|
---|