node-onesky-utils
Version:
Node.js utils for working with OneSky translation service. Original package from @brainly/onesky-utils
283 lines (226 loc) • 9.79 kB
JavaScript
'use strict';
const getMultilingualFile = rootRequire('lib/getMultilingualFile.js');
const privateFunctions = rootRequire('lib/privateFunctions.js');
describe('getMultilingualFile', function() {
let sandbox;
beforeEach(function() {
sandbox = sinon.createSandbox();
});
afterEach(function() {
sandbox.restore();
});
describe('getMultilingualFile 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',
language: 'en',
fileName: 'test.json'
};
await getMultilingualFile(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',
language: 'en',
fileName: 'test.json'
};
await getMultilingualFile(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/multilingual?');
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 use default file format when format is not provided', 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',
language: 'en',
fileName: 'test.json'
};
await getMultilingualFile(options);
const requestUrl = makeRequestStub.getCall(0).args[0].url;
expect(requestUrl).to.include('file_format=I18NEXT_MULTILINGUAL_JSON');
});
it('should use provided file format when specified', 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',
language: 'en',
fileName: 'test.json',
format: 'XLIFF'
};
await getMultilingualFile(options);
const requestUrl = makeRequestStub.getCall(0).args[0].url;
expect(requestUrl).to.include('file_format=XLIFF');
expect(requestUrl).to.not.include('file_format=I18NEXT_MULTILINGUAL_JSON');
});
it('should return the result from makeRequest', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
const expectedContent = '{"en": {"key": "value"}, "fr": {"key": "valeur"}}';
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 getMultilingualFile(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('{}');
const options = {
projectId: 456,
secret: 'my-secret',
apiKey: 'my-api-key',
language: 'fr',
fileName: 'messages.po',
format: 'GETTEXT_PO'
};
await getMultilingualFile(options);
const requestUrl = makeRequestStub.getCall(0).args[0].url;
expect(requestUrl).to.include('https://platform.api.onesky.io/1/projects/456/translations/multilingual?');
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');
expect(requestUrl).to.include('file_format=GETTEXT_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('{}');
const options = {
projectId: 123,
secret: 'secret',
apiKey: 'api-key',
language: 'en',
fileName: 'file with spaces & special chars.json'
};
await getMultilingualFile(options);
const requestUrl = makeRequestStub.getCall(0).args[0].url;
expect(requestUrl).to.include('source_file_name=file%20with%20spaces%20%26%20special%20chars.json');
});
it('should handle various file formats', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('{}');
const formats = [
'I18NEXT_MULTILINGUAL_JSON',
'XLIFF',
'GETTEXT_PO',
'JSON',
'XML'
];
for (const format of formats) {
makeRequestStub.resetHistory();
const options = {
projectId: 123,
secret: 'secret',
apiKey: 'api-key',
language: 'en',
fileName: 'test.json',
format: format
};
await getMultilingualFile(options);
const requestUrl = makeRequestStub.getCall(0).args[0].url;
expect(requestUrl).to.include(`file_format=${format}`);
}
});
it('should propagate errors from makeRequest', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
const expectedError = new Error('File not found');
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 getMultilingualFile(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',
language: 'en',
fileName: 'test.json'
};
await getMultilingualFile(options);
expect(options.hash).to.deep.equal(mockHash);
});
it('should not include language parameter in URL (different from getFile)', 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',
language: 'en',
fileName: 'test.json'
};
await getMultilingualFile(options);
const requestUrl = makeRequestStub.getCall(0).args[0].url;
expect(requestUrl).to.not.include('locale=en');
expect(requestUrl).to.not.include('language=en');
});
it('should handle empty format string', 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',
language: 'en',
fileName: 'test.json',
format: ''
};
await getMultilingualFile(options);
const requestUrl = makeRequestStub.getCall(0).args[0].url;
expect(requestUrl).to.include('file_format=I18NEXT_MULTILINGUAL_JSON');
});
});
});