node-onesky-utils
Version:
Node.js utils for working with OneSky translation service. Original package from @brainly/onesky-utils
336 lines (281 loc) • 10.7 kB
JavaScript
'use strict';
const postScreenshot = rootRequire('lib/postScreenshot.js');
const privateFunctions = rootRequire('lib/privateFunctions.js');
const FormData = require('form-data');
describe('postScreenshot', function() {
let sandbox;
beforeEach(function() {
sandbox = sinon.createSandbox();
});
afterEach(function() {
sandbox.restore();
});
describe('postScreenshot 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('success');
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key',
name: 'homepage',
image: 'data:image/png;base64,iVBORw0KGgoAAAANS...',
tags: []
};
await postScreenshot(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('success');
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key',
name: 'homepage',
image: 'data:image/png;base64,iVBORw0KGgoAAAANS...',
tags: [
{
key: 'welcome_message',
x: 10,
y: 20,
width: 100,
height: 30,
file: 'messages.json'
}
]
};
await postScreenshot(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('POST');
expect(requestOptions.url).to.include('/1/projects/123/screenshots?');
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.data).to.be.instanceOf(FormData);
expect(requestOptions.headers).to.be.an('object');
expect(errorMessage).to.equal('Unable to upload document');
});
it('should structure screenshot data correctly', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success');
const options = {
projectId: 456,
secret: 'my-secret',
apiKey: 'my-api-key',
name: 'login-page',
image: 'data:image/png;base64,abc123',
tags: [
{
key: 'username_label',
x: 50,
y: 100,
width: 80,
height: 20,
file: 'login.json'
},
{
key: 'password_label',
x: 50,
y: 130,
width: 80,
height: 20,
file: 'login.json'
}
]
};
await postScreenshot(options);
const requestCall = makeRequestStub.getCall(0);
const requestOptions = requestCall.args[0];
expect(requestOptions.data).to.be.instanceOf(FormData);
expect(requestOptions.method).to.equal('POST');
expect(requestOptions.url).to.include('/1/projects/456/screenshots?');
});
it('should handle empty tags array', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success');
const options = {
projectId: 123,
secret: 'secret',
apiKey: 'api-key',
name: 'empty-tags-page',
image: 'data:image/png;base64,empty',
tags: []
};
await postScreenshot(options);
const requestCall = makeRequestStub.getCall(0);
const requestOptions = requestCall.args[0];
expect(requestOptions.data).to.be.instanceOf(FormData);
});
it('should return the result from makeRequest', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
const expectedResult = { success: true, screenshot_id: 12345 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
sandbox.stub(privateFunctions, 'makeRequest').resolves(expectedResult);
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key',
name: 'homepage',
image: 'data:image/png;base64,iVBORw0KGgoAAAANS...',
tags: []
};
const result = await postScreenshot(options);
expect(result).to.deep.equal(expectedResult);
});
it('should construct the correct upload URL', async function() {
const mockHash = { devHash: 'hash123', timestamp: 9876543210 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success');
const options = {
projectId: 789,
secret: 'upload-secret',
apiKey: 'upload-api-key',
name: 'dashboard',
image: 'data:image/jpeg;base64,dashboard-image',
tags: []
};
await postScreenshot(options);
const requestUrl = makeRequestStub.getCall(0).args[0].url;
expect(requestUrl).to.include('https://platform.api.onesky.io/1/projects/789/screenshots?');
expect(requestUrl).to.include('api_key=upload-api-key');
expect(requestUrl).to.include('timestamp=9876543210');
expect(requestUrl).to.include('dev_hash=hash123');
});
it('should propagate errors from makeRequest', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
const expectedError = new Error('Screenshot upload failed');
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
sandbox.stub(privateFunctions, 'makeRequest').rejects(expectedError);
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key',
name: 'homepage',
image: 'data:image/png;base64,iVBORw0KGgoAAAANS...',
tags: []
};
try {
await postScreenshot(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('success');
const options = {
projectId: 123,
secret: 'test-secret',
apiKey: 'test-api-key',
name: 'homepage',
image: 'data:image/png;base64,iVBORw0KGgoAAAANS...',
tags: []
};
await postScreenshot(options);
expect(options.hash).to.deep.equal(mockHash);
});
it('should handle complex tag structures', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success');
const complexTags = [
{
key: 'header.title',
x: 0,
y: 0,
width: 200,
height: 50,
file: 'header.json'
},
{
key: 'navigation.home',
x: 10,
y: 60,
width: 60,
height: 25,
file: 'navigation.json'
},
{
key: 'content.welcome_message',
x: 20,
y: 100,
width: 300,
height: 40,
file: 'content.json'
}
];
const options = {
projectId: 123,
secret: 'secret',
apiKey: 'api-key',
name: 'complex-page',
image: 'data:image/png;base64,complex',
tags: complexTags
};
await postScreenshot(options);
const requestCall = makeRequestStub.getCall(0);
const requestOptions = requestCall.args[0];
expect(requestOptions.data).to.be.instanceOf(FormData);
});
it('should handle different image formats', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success');
const imageFormats = [
'data:image/png;base64,pngdata',
'data:image/jpeg;base64,jpegdata',
'data:image/gif;base64,gifdata',
'data:image/webp;base64,webpdata'
];
for (const imageData of imageFormats) {
makeRequestStub.resetHistory();
const options = {
projectId: 123,
secret: 'secret',
apiKey: 'api-key',
name: 'test-image',
image: imageData,
tags: []
};
await postScreenshot(options);
expect(makeRequestStub).to.have.been.calledOnce;
}
});
it('should validate tag structure in screenshot data', async function() {
const mockHash = { devHash: 'test-hash', timestamp: 1234567890 };
sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash);
const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success');
const options = {
projectId: 123,
secret: 'secret',
apiKey: 'api-key',
name: 'validation-test',
image: 'data:image/png;base64,test',
tags: [
{
key: 'test_key',
x: 100,
y: 200,
width: 150,
height: 50,
file: 'test.json'
}
]
};
await postScreenshot(options);
const requestCall = makeRequestStub.getCall(0);
const requestOptions = requestCall.args[0];
expect(requestOptions.data).to.be.instanceOf(FormData);
});
});
});