UNPKG

node-onesky-utils

Version:

Node.js utils for working with OneSky translation service. Original package from @brainly/onesky-utils

171 lines (136 loc) 5.83 kB
'use strict'; const getFile = rootRequire('lib/getFile.js'); const privateFunctions = rootRequire('lib/privateFunctions.js'); describe('getFile', function() { let sandbox; beforeEach(function() { sandbox = sinon.createSandbox(); }); afterEach(function() { sandbox.restore(); }); describe('getFile function', function() { it('should call getDevHash with the secret', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; const getDevHashStub = sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('file content'); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', language: 'en', fileName: 'test.json' }; await getFile(options); expect(getDevHashStub).to.have.been.calledOnceWith('test-secret'); }); it('should call makeRequest with correct parameters', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('file content'); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', language: 'en', fileName: 'test.json' }; await getFile(options); expect(makeRequestStub).to.have.been.calledOnce; const requestCall = makeRequestStub.getCall(0); const requestOptions = requestCall.args[0]; const errorMessage = requestCall.args[1]; expect(requestOptions.method).to.equal('GET'); expect(requestOptions.url).to.include('/1/projects/123/translations?'); expect(requestOptions.url).to.include('locale=en'); expect(requestOptions.url).to.include('api_key=test-api-key'); expect(requestOptions.url).to.include('timestamp=1234567890'); expect(requestOptions.url).to.include('dev_hash=test-hash'); expect(requestOptions.url).to.include('source_file_name=test.json'); expect(errorMessage).to.equal('Unable to fetch document'); }); it('should return the result from makeRequest', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; const expectedContent = '{"key": "translated value"}'; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); sandbox.stub(privateFunctions, 'makeRequest').resolves(expectedContent); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', language: 'en', fileName: 'test.json' }; const result = await getFile(options); expect(result).to.equal(expectedContent); }); it('should construct the correct API URL', async function() { const mockHash = { devHash: 'abc123', timestamp: 9876543210 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('content'); const options = { projectId: 456, secret: 'my-secret', apiKey: 'my-api-key', language: 'fr', fileName: 'messages.po' }; await getFile(options); const requestUrl = makeRequestStub.getCall(0).args[0].url; expect(requestUrl).to.include('https://platform.api.onesky.io/1/projects/456/translations?'); expect(requestUrl).to.include('locale=fr'); expect(requestUrl).to.include('api_key=my-api-key'); expect(requestUrl).to.include('timestamp=9876543210'); expect(requestUrl).to.include('dev_hash=abc123'); expect(requestUrl).to.include('source_file_name=messages.po'); }); it('should handle URL encoding of file names', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('content'); const options = { projectId: 123, secret: 'secret', apiKey: 'api-key', language: 'en', fileName: 'file with spaces.json' }; await getFile(options); const requestUrl = makeRequestStub.getCall(0).args[0].url; expect(requestUrl).to.include('source_file_name=file%20with%20spaces.json'); }); it('should propagate errors from makeRequest', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; const expectedError = new Error('Network error'); sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); sandbox.stub(privateFunctions, 'makeRequest').rejects(expectedError); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', language: 'en', fileName: 'test.json' }; try { await getFile(options); expect.fail('Should have thrown an error'); } catch (error) { expect(error).to.equal(expectedError); } }); it('should modify the options object by adding hash', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); sandbox.stub(privateFunctions, 'makeRequest').resolves('content'); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', language: 'en', fileName: 'test.json' }; await getFile(options); expect(options.hash).to.deep.equal(mockHash); }); }); });