|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as vm from 'node:vm'; |
| 3 | +import { Binary, BSON } from '../register-bson'; |
| 4 | + |
| 5 | +describe('class Binary', () => { |
| 6 | + context('constructor()', () => { |
| 7 | + it('creates an 256 byte Binary with subtype 0 by default', () => { |
| 8 | + const binary = new Binary(); |
| 9 | + expect(binary).to.have.property('buffer'); |
| 10 | + expect(binary).to.have.property('position', 0); |
| 11 | + expect(binary).to.have.property('sub_type', 0); |
| 12 | + expect(binary).to.have.nested.property('buffer.byteLength', 256); |
| 13 | + const emptyZeroedArray = new Uint8Array(256); |
| 14 | + emptyZeroedArray.fill(0x00); |
| 15 | + expect(binary.buffer).to.deep.equal(emptyZeroedArray); |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + context('createFromHexString()', () => { |
| 20 | + context('when called with a hex sequence', () => { |
| 21 | + it('returns a Binary instance with the decoded bytes', () => { |
| 22 | + const bytes = Buffer.from('abc', 'utf8'); |
| 23 | + const binary = Binary.createFromHexString(bytes.toString('hex')); |
| 24 | + expect(binary).to.have.deep.property('buffer', bytes); |
| 25 | + expect(binary).to.have.property('sub_type', 0); |
| 26 | + }); |
| 27 | + |
| 28 | + it('returns a Binary instance with the decoded bytes and subtype', () => { |
| 29 | + const bytes = Buffer.from('abc', 'utf8'); |
| 30 | + const binary = Binary.createFromHexString(bytes.toString('hex'), 0x23); |
| 31 | + expect(binary).to.have.deep.property('buffer', bytes); |
| 32 | + expect(binary).to.have.property('sub_type', 0x23); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + context('when called with an empty string', () => { |
| 37 | + it('creates an empty binary', () => { |
| 38 | + const binary = Binary.createFromHexString(''); |
| 39 | + expect(binary).to.have.deep.property('buffer', new Uint8Array(0)); |
| 40 | + expect(binary).to.have.property('sub_type', 0); |
| 41 | + }); |
| 42 | + |
| 43 | + it('creates an empty binary with subtype', () => { |
| 44 | + const binary = Binary.createFromHexString('', 0x42); |
| 45 | + expect(binary).to.have.deep.property('buffer', new Uint8Array(0)); |
| 46 | + expect(binary).to.have.property('sub_type', 0x42); |
| 47 | + }); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + context('createFromBase64()', () => { |
| 52 | + context('when called with a base64 sequence', () => { |
| 53 | + it('returns a Binary instance with the decoded bytes', () => { |
| 54 | + const bytes = Buffer.from('abc', 'utf8'); |
| 55 | + const binary = Binary.createFromBase64(bytes.toString('base64')); |
| 56 | + expect(binary).to.have.deep.property('buffer', bytes); |
| 57 | + expect(binary).to.have.property('sub_type', 0); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns a Binary instance with the decoded bytes and subtype', () => { |
| 61 | + const bytes = Buffer.from('abc', 'utf8'); |
| 62 | + const binary = Binary.createFromBase64(bytes.toString('base64'), 0x23); |
| 63 | + expect(binary).to.have.deep.property('buffer', bytes); |
| 64 | + expect(binary).to.have.property('sub_type', 0x23); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + context('when called with an empty string', () => { |
| 69 | + it('creates an empty binary', () => { |
| 70 | + const binary = Binary.createFromBase64(''); |
| 71 | + expect(binary).to.have.deep.property('buffer', new Uint8Array(0)); |
| 72 | + expect(binary).to.have.property('sub_type', 0); |
| 73 | + }); |
| 74 | + |
| 75 | + it('creates an empty binary with subtype', () => { |
| 76 | + const binary = Binary.createFromBase64('', 0x42); |
| 77 | + expect(binary).to.have.deep.property('buffer', new Uint8Array(0)); |
| 78 | + expect(binary).to.have.property('sub_type', 0x42); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + context('inspect()', () => { |
| 84 | + it('when value is default returns "Binary.createFromBase64("", 0)"', () => { |
| 85 | + expect(new Binary().inspect()).to.equal('Binary.createFromBase64("", 0)'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('when value is empty returns "Binary.createFromBase64("", 0)"', () => { |
| 89 | + expect(new Binary(new Uint8Array(0)).inspect()).to.equal('Binary.createFromBase64("", 0)'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('when value is default with a subtype returns "Binary.createFromBase64("", 35)"', () => { |
| 93 | + expect(new Binary(null, 0x23).inspect()).to.equal('Binary.createFromBase64("", 35)'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('when value is empty with a subtype returns "Binary.createFromBase64("", 35)"', () => { |
| 97 | + expect(new Binary(new Uint8Array(0), 0x23).inspect()).to.equal( |
| 98 | + 'Binary.createFromBase64("", 35)' |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('when value has utf8 "abcdef" encoded returns "Binary.createFromBase64("YWJjZGVm", 0)"', () => { |
| 103 | + expect(new Binary(Buffer.from('abcdef', 'utf8')).inspect()).to.equal( |
| 104 | + 'Binary.createFromBase64("YWJjZGVm", 0)' |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + context('when result is executed', () => { |
| 109 | + it('has a position of zero when constructed with default space', () => { |
| 110 | + const bsonValue = new Binary(); |
| 111 | + const ctx = { ...BSON, module: { exports: { result: null } } }; |
| 112 | + vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx); |
| 113 | + expect(ctx.module.exports.result).to.have.property('position', 0); |
| 114 | + expect(ctx.module.exports.result).to.have.property('sub_type', 0); |
| 115 | + |
| 116 | + // While the default Binary has 256 bytes the newly constructed one will have 0 |
| 117 | + // both will have a position of zero so when serialized to BSON they are the equivalent. |
| 118 | + expect(ctx.module.exports.result).to.have.nested.property('buffer.byteLength', 0); |
| 119 | + expect(bsonValue).to.have.nested.property('buffer.byteLength', 256); |
| 120 | + }); |
| 121 | + |
| 122 | + it('is deep equal with a Binary that has no data', () => { |
| 123 | + const bsonValue = new Binary(new Uint8Array(0)); |
| 124 | + const ctx = { ...BSON, module: { exports: { result: null } } }; |
| 125 | + vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx); |
| 126 | + expect(ctx.module.exports.result).to.deep.equal(bsonValue); |
| 127 | + }); |
| 128 | + |
| 129 | + it('is deep equal with a Binary that has a subtype but no data', () => { |
| 130 | + const bsonValue = new Binary(new Uint8Array(0), 0x23); |
| 131 | + const ctx = { ...BSON, module: { exports: { result: null } } }; |
| 132 | + vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx); |
| 133 | + expect(ctx.module.exports.result).to.deep.equal(bsonValue); |
| 134 | + }); |
| 135 | + |
| 136 | + it('is deep equal with a Binary that has data', () => { |
| 137 | + const bsonValue = new Binary(Buffer.from('abc', 'utf8')); |
| 138 | + const ctx = { ...BSON, module: { exports: { result: null } } }; |
| 139 | + vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx); |
| 140 | + expect(ctx.module.exports.result).to.deep.equal(bsonValue); |
| 141 | + }); |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments