|
| 1 | +const proxyquire = require('proxyquire').noCallThru(); |
| 2 | +const chai = require('chai'); |
| 3 | +const sinon = require('sinon'); |
| 4 | + |
| 5 | +const expect = chai.expect; |
| 6 | +const stubFsPromises = { |
| 7 | + writeFile: sinon.stub(), |
| 8 | + readFile: sinon.stub(), |
| 9 | +}; |
| 10 | +const stubLogger = { |
| 11 | + debug: sinon.stub(), |
| 12 | + log: sinon.stub(), |
| 13 | + error: sinon.stub(), |
| 14 | +}; |
| 15 | + |
| 16 | +describe('helpers', () => { |
| 17 | + afterEach(() => { |
| 18 | + for (const stub in stubFsPromises) { |
| 19 | + stubFsPromises[stub].resetHistory(); |
| 20 | + } |
| 21 | + for (const stub in stubLogger) { |
| 22 | + stubLogger[stub].resetHistory(); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + describe('saveServerAddress() and getServerAddress()', () => { |
| 27 | + describe('saveServerAddress()', () => { |
| 28 | + it('should write the server address to the file', async () => { |
| 29 | + const SERVER_ADDRESS_PATH = 'little/bobby/tables/we/call/him'; |
| 30 | + const serverAddress = 'foo'; |
| 31 | + const { saveServerAddress } = proxyquire('../lib/helpers.js', { |
| 32 | + 'node:fs/promises': stubFsPromises, |
| 33 | + 'cf-logs': { Logger: () => stubLogger }, |
| 34 | + './const': { SERVER_ADDRESS_PATH }, |
| 35 | + }); |
| 36 | + await saveServerAddress(serverAddress); |
| 37 | + expect(stubFsPromises.writeFile).to.have.been.calledOnceWithExactly(SERVER_ADDRESS_PATH, serverAddress, { encoding: 'utf8' }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should fail if the file cannot be written', async () => { |
| 41 | + const { saveServerAddress } = proxyquire('../lib/helpers.js', { |
| 42 | + 'node:fs/promises': stubFsPromises, |
| 43 | + 'cf-logs': { Logger: () => stubLogger }, |
| 44 | + }); |
| 45 | + const expectedError = new Error('oh no'); |
| 46 | + stubFsPromises.writeFile.rejects(expectedError); |
| 47 | + try { |
| 48 | + await saveServerAddress('foo'); |
| 49 | + expect.fail('should have thrown an error'); |
| 50 | + } catch (error) { |
| 51 | + expect(error).to.be.equal(expectedError); |
| 52 | + expect(stubLogger.error).to.have.been.calledOnceWithExactly(`Failed to save server address: ${expectedError}`); |
| 53 | + } |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('getServerAddress()', () => { |
| 58 | + it('should read the server address from the file', async () => { |
| 59 | + const SERVER_ADDRESS_PATH = 'little/bobby/tables/we/call/him'; |
| 60 | + const serverAddress = 'foo'; |
| 61 | + const { getServerAddress } = proxyquire('../lib/helpers.js', { |
| 62 | + 'node:fs/promises': stubFsPromises, |
| 63 | + 'cf-logs': { Logger: () => stubLogger }, |
| 64 | + './const': { SERVER_ADDRESS_PATH }, |
| 65 | + }); |
| 66 | + stubFsPromises.readFile.resolves(serverAddress); |
| 67 | + const result = await getServerAddress(); |
| 68 | + expect(result).to.be.equal(serverAddress); |
| 69 | + expect(stubFsPromises.readFile).to.have.been.calledOnceWithExactly(SERVER_ADDRESS_PATH, { encoding: 'utf8' }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should fail if the file cannot be read', async () => { |
| 73 | + const { getServerAddress } = proxyquire('../lib/helpers.js', { |
| 74 | + 'node:fs/promises': stubFsPromises, |
| 75 | + 'cf-logs': { Logger: () => stubLogger }, |
| 76 | + }); |
| 77 | + const expectedError = new Error('oh no'); |
| 78 | + stubFsPromises.readFile.rejects(expectedError); |
| 79 | + try { |
| 80 | + await getServerAddress(); |
| 81 | + expect.fail('should have thrown an error'); |
| 82 | + } catch (error) { |
| 83 | + expect(error).to.be.equal(expectedError); |
| 84 | + expect(stubLogger.error).to.have.been.calledOnceWithExactly(`Failed to read server address: ${expectedError}`); |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should write/read to/from the same location', async () => { |
| 90 | + const { saveServerAddress, getServerAddress } = require('../lib/helpers.js'); |
| 91 | + const serverAddress = 'http://g.codefresh.io'; |
| 92 | + await saveServerAddress(serverAddress); |
| 93 | + const result = await getServerAddress(); |
| 94 | + expect(result).to.be.equal(serverAddress); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments