|
| 1 | +import { Configuration } from "../../src/configuration/Configuration.js"; |
| 2 | +import { ClientSettings } from "../../src/configuration/SettingsManager.js"; |
| 3 | +import { IEvent } from "../../src/models/IEvent.js"; |
| 4 | +import { IUserDescription } from "../../src/models/IUserDescription.js"; |
| 5 | +import { Response } from "../../src/submission/Response.js"; |
| 6 | +import { TestSubmissionClient } from "./TestSubmissionClient.js" |
| 7 | + |
| 8 | +describe('TestSubmissionClient', () => { |
| 9 | + const config: Configuration = new Configuration({ |
| 10 | + apiKey: 'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', |
| 11 | + serverUrl: 'http://server.localhost:5000', |
| 12 | + configServerUrl: 'http://config.localhost:5000', |
| 13 | + heartbeatServerUrl: 'http://heartbeat.localhost:5000', |
| 14 | + }); |
| 15 | + |
| 16 | + test('should submit events', async () => { |
| 17 | + const fetchMock = TestSubmissionClient.prototype.fetch = jest.fn() |
| 18 | + .mockReturnValueOnce(new Response<void>(202, '', -1, undefined)); |
| 19 | + |
| 20 | + const events = [{ type: 'log', message: 'From js client', reference_id: '123454321' }]; |
| 21 | + const client = new TestSubmissionClient(config); |
| 22 | + await client.submitEvents(events); |
| 23 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 24 | + expect(fetchMock.mock.calls[0][0]).toBe(`${config.serverUrl}/api/v2/events`); |
| 25 | + expect(fetchMock.mock.calls[0][1]).toEqual({ |
| 26 | + method: 'POST', |
| 27 | + body: JSON.stringify(events) |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + test('should submit invalid object data', async () => { |
| 32 | + const fetchMock = TestSubmissionClient.prototype.fetch = jest.fn() |
| 33 | + .mockReturnValueOnce(new Response<void>(202, '', -1, undefined)); |
| 34 | + |
| 35 | + const events: IEvent[] = [{ |
| 36 | + type: 'log', message: 'From js client', reference_id: '123454321', data: { |
| 37 | + name: 'blake', |
| 38 | + age: () => { throw new Error('Test'); } |
| 39 | + } |
| 40 | + }]; |
| 41 | + |
| 42 | + const client = new TestSubmissionClient(config); |
| 43 | + await client.submitEvents(events); |
| 44 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 45 | + expect(fetchMock.mock.calls[0][0]).toBe(`${config.serverUrl}/api/v2/events`); |
| 46 | + expect(fetchMock.mock.calls[0][1]).toEqual({ |
| 47 | + method: 'POST', |
| 48 | + body: JSON.stringify(events) |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + test('should submit user description', async () => { |
| 53 | + const fetchMock = TestSubmissionClient.prototype.fetch = jest.fn() |
| 54 | + .mockReturnValueOnce(new Response<void>(202, '', -1, undefined)); |
| 55 | + |
| 56 | + const description: IUserDescription = { |
| 57 | + email_address: '[email protected]', |
| 58 | + description: 'unit test' |
| 59 | + }; |
| 60 | + |
| 61 | + const client = new TestSubmissionClient(config); |
| 62 | + await client.submitUserDescription("123454321", description); |
| 63 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 64 | + expect(fetchMock.mock.calls[0][0]).toBe(`${config.serverUrl}/api/v2/events/by-ref/123454321/user-description`); |
| 65 | + expect(fetchMock.mock.calls[0][1]).toEqual({ |
| 66 | + method: 'POST', |
| 67 | + body: JSON.stringify(description) |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + test('should submit heartbeat', async () => { |
| 72 | + const fetchMock = TestSubmissionClient.prototype.fetch = jest.fn() |
| 73 | + .mockReturnValueOnce(new Response<void>(200, '', 1, undefined)) |
| 74 | + .mockReturnValueOnce(new Response<ClientSettings>(200, '', 1, new ClientSettings({}, 1))); |
| 75 | + |
| 76 | + const client = config.submissionClient = new TestSubmissionClient(config); |
| 77 | + await client.submitHeartbeat('sessionId', true); |
| 78 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 79 | + expect(fetchMock.mock.calls[0][0]).toBe(`${config.heartbeatServerUrl}/api/v2/events/session/heartbeat?id=sessionId&close=true`); |
| 80 | + expect(fetchMock.mock.calls[0][1]).toEqual({ method: 'GET' }); |
| 81 | + expect(fetchMock.mock.calls[1][0]).toBe(`${config.serverUrl}/api/v2/projects/config?v=0`); |
| 82 | + expect(fetchMock.mock.calls[1][1]).toEqual({ method: 'GET' }); |
| 83 | + }); |
| 84 | + |
| 85 | + test('should get project settings', async () => { |
| 86 | + const fetchMock = TestSubmissionClient.prototype.fetch = jest.fn() |
| 87 | + .mockReturnValueOnce(new Response<ClientSettings>(200, '', undefined, new ClientSettings({}, 1))); |
| 88 | + |
| 89 | + const client = new TestSubmissionClient(config); |
| 90 | + await client.getSettings(0); |
| 91 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 92 | + expect(fetchMock.mock.calls[0][0]).toBe(`${config.serverUrl}/api/v2/projects/config?v=0`); |
| 93 | + expect(fetchMock.mock.calls[0][1]).toEqual({ method: 'GET' }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments