|
1 | 1 | /* eslint-disable no-console */ |
2 | 2 | import { getMachineId } from '.'; |
3 | 3 | import { machineIdSync as otherMachineId } from 'node-machine-id'; |
4 | | -import { expect } from 'chai'; |
| 4 | +import chai, { expect } from 'chai'; |
5 | 5 | import { createHash } from 'crypto'; |
| 6 | +import sinonChai from 'sinon-chai'; |
| 7 | +import sinon from 'sinon'; |
| 8 | +import bindings from 'bindings'; |
| 9 | + |
| 10 | +chai.use(sinonChai); |
6 | 11 |
|
7 | 12 | describe('machine-id', function () { |
8 | 13 | this.timeout(5_000); |
@@ -53,6 +58,43 @@ describe('machine-id', function () { |
53 | 58 | const hashRegex = /^[0-9a-f]{64}$/i; |
54 | 59 |
|
55 | 60 | expect(hashRegex.test(id)); |
| 61 | + |
| 62 | + expect(id).equals( |
| 63 | + createHash('sha256') |
| 64 | + .update(getMachineId({ raw: true }) || '') |
| 65 | + .digest('hex'), |
| 66 | + ); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('edge cases', function () { |
| 71 | + afterEach(function () { |
| 72 | + sinon.restore(); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('returns undefined', function () { |
| 76 | + it('if something goes wrong with the binding function', function () { |
| 77 | + sinon |
| 78 | + .stub(bindings('machine_id'), 'getMachineId') |
| 79 | + .throws(new Error('Binding error')); |
| 80 | + |
| 81 | + expect(getMachineId({ raw: true })).to.be.undefined; |
| 82 | + expect(getMachineId()).to.be.undefined; |
| 83 | + }); |
| 84 | + |
| 85 | + it('if the binding function returns an empty string', function () { |
| 86 | + sinon.stub(bindings('machine_id'), 'getMachineId').returns(''); |
| 87 | + |
| 88 | + expect(getMachineId({ raw: true })).to.be.undefined; |
| 89 | + expect(getMachineId()).to.be.undefined; |
| 90 | + }); |
| 91 | + |
| 92 | + it('if the binding function returns undefined', function () { |
| 93 | + sinon.stub(bindings('machine_id'), 'getMachineId').returns(undefined); |
| 94 | + |
| 95 | + expect(getMachineId({ raw: true })).to.be.undefined; |
| 96 | + expect(getMachineId()).to.be.undefined; |
| 97 | + }); |
56 | 98 | }); |
57 | 99 | }); |
58 | 100 | }); |
0 commit comments