UNPKG

node-onesky-utils

Version:

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

204 lines (158 loc) 6.99 kB
'use strict'; const getLanguages = rootRequire('lib/getLanguages.js'); const privateFunctions = rootRequire('lib/privateFunctions.js'); describe('getLanguages', function() { let sandbox; beforeEach(function() { sandbox = sinon.createSandbox(); }); afterEach(function() { sandbox.restore(); }); describe('getLanguages 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([]); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key' }; await getLanguages(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([]); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key' }; await getLanguages(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/languages?'); 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(errorMessage).to.equal('Unable to fetch project languages'); }); it('should return the result from makeRequest', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; const expectedLanguages = [ { code: 'en', english_name: 'English', local_name: 'English' }, { code: 'fr', english_name: 'French', local_name: 'Français' }, { code: 'es', english_name: 'Spanish', local_name: 'Español' } ]; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); sandbox.stub(privateFunctions, 'makeRequest').resolves(expectedLanguages); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key' }; const result = await getLanguages(options); expect(result).to.deep.equal(expectedLanguages); }); 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([]); const options = { projectId: 456, secret: 'my-secret', apiKey: 'my-api-key' }; await getLanguages(options); const requestUrl = makeRequestStub.getCall(0).args[0].url; expect(requestUrl).to.include('https://platform.api.onesky.io/1/projects/456/languages?'); expect(requestUrl).to.include('api_key=my-api-key'); expect(requestUrl).to.include('timestamp=9876543210'); expect(requestUrl).to.include('dev_hash=abc123'); }); it('should not include locale or source_file_name parameters', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves([]); const options = { projectId: 123, secret: 'secret', apiKey: 'api-key' }; await getLanguages(options); const requestUrl = makeRequestStub.getCall(0).args[0].url; expect(requestUrl).to.not.include('locale='); expect(requestUrl).to.not.include('source_file_name='); expect(requestUrl).to.not.include('file_format='); }); it('should propagate errors from makeRequest', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; const expectedError = new Error('Unauthorized access'); sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); sandbox.stub(privateFunctions, 'makeRequest').rejects(expectedError); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key' }; try { await getLanguages(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([]); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key' }; await getLanguages(options); expect(options.hash).to.deep.equal(mockHash); }); it('should handle empty language list', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); sandbox.stub(privateFunctions, 'makeRequest').resolves([]); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key' }; const result = await getLanguages(options); expect(result).to.be.an('array'); expect(result).to.have.length(0); }); it('should work with different project IDs', async function() { const mockHash = { devHash: 'hash123', timestamp: 1111111111 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves([]); const testCases = [ { projectId: 1, expected: '/1/projects/1/languages?' }, { projectId: 999, expected: '/1/projects/999/languages?' }, { projectId: 1234567, expected: '/1/projects/1234567/languages?' } ]; for (const testCase of testCases) { makeRequestStub.resetHistory(); const options = { projectId: testCase.projectId, secret: 'secret', apiKey: 'api-key' }; await getLanguages(options); const requestUrl = makeRequestStub.getCall(0).args[0].url; expect(requestUrl).to.include(testCase.expected); } }); }); });