|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as os from 'os'; |
| 3 | + |
| 4 | +import { makeClientMetadata } from '../../../../src/utils'; |
| 5 | + |
| 6 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 7 | +const NODE_DRIVER_VERSION = require('../../../../package.json').version; |
| 8 | + |
| 9 | +describe('makeClientMetadata()', () => { |
| 10 | + context('when driverInfo.platform is provided', () => { |
| 11 | + it('appends driverInfo.platform to the platform field', () => { |
| 12 | + const options = { |
| 13 | + driverInfo: { platform: 'myPlatform' } |
| 14 | + }; |
| 15 | + const metadata = makeClientMetadata(options); |
| 16 | + expect(metadata).to.deep.equal({ |
| 17 | + driver: { |
| 18 | + name: 'nodejs', |
| 19 | + version: NODE_DRIVER_VERSION |
| 20 | + }, |
| 21 | + os: { |
| 22 | + type: os.type(), |
| 23 | + name: process.platform, |
| 24 | + architecture: process.arch, |
| 25 | + version: os.release() |
| 26 | + }, |
| 27 | + platform: `Node.js ${process.version}, ${os.endianness()}|myPlatform` |
| 28 | + }); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + context('when driverInfo.name is provided', () => { |
| 33 | + it('appends driverInfo.name to the driver.name field', () => { |
| 34 | + const options = { |
| 35 | + driverInfo: { name: 'myName' } |
| 36 | + }; |
| 37 | + const metadata = makeClientMetadata(options); |
| 38 | + expect(metadata).to.deep.equal({ |
| 39 | + driver: { |
| 40 | + name: 'nodejs|myName', |
| 41 | + version: NODE_DRIVER_VERSION |
| 42 | + }, |
| 43 | + os: { |
| 44 | + type: os.type(), |
| 45 | + name: process.platform, |
| 46 | + architecture: process.arch, |
| 47 | + version: os.release() |
| 48 | + }, |
| 49 | + platform: `Node.js ${process.version}, ${os.endianness()}` |
| 50 | + }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + context('when driverInfo.version is provided', () => { |
| 55 | + it('appends driverInfo.version to the version field', () => { |
| 56 | + const options = { |
| 57 | + driverInfo: { version: 'myVersion' } |
| 58 | + }; |
| 59 | + const metadata = makeClientMetadata(options); |
| 60 | + expect(metadata).to.deep.equal({ |
| 61 | + driver: { |
| 62 | + name: 'nodejs', |
| 63 | + version: `${NODE_DRIVER_VERSION}|myVersion` |
| 64 | + }, |
| 65 | + os: { |
| 66 | + type: os.type(), |
| 67 | + name: process.platform, |
| 68 | + architecture: process.arch, |
| 69 | + version: os.release() |
| 70 | + }, |
| 71 | + platform: `Node.js ${process.version}, ${os.endianness()}` |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + context('when no custom driverInfo is provided', () => { |
| 77 | + const metadata = makeClientMetadata({ driverInfo: {} }); |
| 78 | + |
| 79 | + it('does not append the driver info to the metadata', () => { |
| 80 | + expect(metadata).to.deep.equal({ |
| 81 | + driver: { |
| 82 | + name: 'nodejs', |
| 83 | + version: NODE_DRIVER_VERSION |
| 84 | + }, |
| 85 | + os: { |
| 86 | + type: os.type(), |
| 87 | + name: process.platform, |
| 88 | + architecture: process.arch, |
| 89 | + version: os.release() |
| 90 | + }, |
| 91 | + platform: `Node.js ${process.version}, ${os.endianness()}` |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('does not set the application field', () => { |
| 96 | + expect(metadata).not.to.have.property('application'); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + context('when app name is provided', () => { |
| 101 | + context('when the app name is over 128 bytes', () => { |
| 102 | + const longString = 'a'.repeat(300); |
| 103 | + const options = { |
| 104 | + appName: longString, |
| 105 | + driverInfo: {} |
| 106 | + }; |
| 107 | + const metadata = makeClientMetadata(options); |
| 108 | + |
| 109 | + it('truncates the application name to <=128 bytes', () => { |
| 110 | + expect(metadata.application?.name).to.be.a('string'); |
| 111 | + // the above assertion fails if `metadata.application?.name` is undefined, so |
| 112 | + // we can safely assert that it exists |
| 113 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 114 | + expect(Buffer.byteLength(metadata.application!.name, 'utf8')).to.equal(128); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + context( |
| 119 | + 'TODO(NODE-5150): fix appName truncation when multi-byte unicode charaters straddle byte 128', |
| 120 | + () => { |
| 121 | + const longString = '€'.repeat(300); |
| 122 | + const options = { |
| 123 | + appName: longString, |
| 124 | + driverInfo: {} |
| 125 | + }; |
| 126 | + const metadata = makeClientMetadata(options); |
| 127 | + |
| 128 | + it('truncates the application name to 129 bytes', () => { |
| 129 | + expect(metadata.application?.name).to.be.a('string'); |
| 130 | + // the above assertion fails if `metadata.application?.name` is undefined, so |
| 131 | + // we can safely assert that it exists |
| 132 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 133 | + expect(Buffer.byteLength(metadata.application!.name, 'utf8')).to.equal(129); |
| 134 | + }); |
| 135 | + } |
| 136 | + ); |
| 137 | + |
| 138 | + context('when the app name is under 128 bytes', () => { |
| 139 | + const options = { |
| 140 | + appName: 'myApplication', |
| 141 | + driverInfo: {} |
| 142 | + }; |
| 143 | + const metadata = makeClientMetadata(options); |
| 144 | + |
| 145 | + it('sets the application name to the value', () => { |
| 146 | + expect(metadata.application?.name).to.equal('myApplication'); |
| 147 | + }); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments