n8n/packages/cli/test/unit/Telemetry.test.ts
Csaba Tuncsik 0da338f9b5
feat(editor): Add usage and plan pages (#4819)
* feat(editor): Usage and plan page (#4793)

feat(editor): usage and plan page

* feat(editor): Update Usage and plan page (#4842)

* feat(editor): usage and plan store

* feat(editor): usage and plan page updates

* feat(editor): usage and plan add buttons and alert

* tes(editor): usage and plan store

* tes(editor): usage remove refresh button and add link to view plans

* tes(editor): usage use info tip

* tes(editor): usage info style

* feat(editor): Get quotas data (#4866)

feat(editor): get quotas data

* feat(editor): In-app experience (#4875)

* feat: Add license quotas endpoint

* feat: Add trigger count to workflow activation process

* refactor: Get quotas from db

* feat: Add license information

*  - finalised GET /license endpoint

* 🔨 - getActiveTriggerCount return 0 instead of null

* 🐛 - ignore manualTrigger when counting active triggers

*  - add activation endpoint

*  - added renew endpoint

* 🔨 - added return type interfaces

* 🔨 - handle license errors where methods are called

* 🔨 - rename function to match name from lib

* feat(editor): usage add plans buttons logic

* 🚨 - testing new License methods

* feat(editor): usage add more business logic

* chore(editor): code formatting

* 🚨 - added license api tests

* fix(editor): usage store

* fix(editor): usage update translations

* feat(editor): usage add license activation modal

* feat(editor): usage change subscription app url

* feat(editor): usage add contact us link

* feat(editor): usage fix modal width

*  - Add renewal tracking metric

*  - add license data to pulse event

* 🔨 - set default triggercount on entity model

*  - add db migrations for mysql and postgres

* fix(editor): Usage api call data processing and error handling

* fix(editor): Usage fix activation query key

* 🚨 - add initDb to telemetry tests

* 🔨 - move getlicensedata to licenseservice

* 🔨 - return 403 instead of 404 to non owners

* 🔨 - move owner checking to middleware

* 🐛 - fixed incorrectly returned error from middleware

* 🐛 - using mock instead of test db for pulse tests

* fix(editor): Usage fix activation and add success messages

* fix(editor): Usage should not renew activation right after activation

* 🚨 - skipping failing pulse tests for now

* fix(editor): Usage add telemetry calls and apply design review outcomes

* feat(editor): Hide usage page according to BE flag

* feat(editor): Usage modify key activation flow

* feat(editor): Usage change subscription app url

* feat(editor): Usage add telemetry for manage plan

* feat(editor): Usage extend link url query params

* feat(editor): Usage add line chart if there is a workflow limit

* feat(editor): Usage remove query after key activation redirection

* fix(editor): Usage handle limit exceeded workflow chart, add focus to input when modal opened

* fix(editor): Usage activation can return router promise when removing query

* fix(editor): Usage and plan design review

* 🐛 - fix renew endpoint hanging issue

* 🐛 - fix license activation bug

* fix(editor): Usage proper translation for plans and/or editions

* fix(editor): Usage apply David's review results

* fix(editor): Usage page set as default and first under Settings

* fix(editor): Usage open subscription app in new tab

* fix(editor): Usage page having key query param a plan links

* test: Fix broken test

* fix(editor): Usage page address review

* 🧪 Flush promises on telemetry tests

*  Extract helper with `setImmediate`

* 🔥 Remove leftovers

*  Use Adi's helper

* refactor: Comment broken tests

* refactor: add Tenant id to settings

* feat: add environment to license endpoints

* refactor: Move license environment to general settings

* fix: fix routing bug

* fix(editor): Usage page some code review changes and formatting

* fix(editor): Usage page remove direct usage of reusable translation keys

* fix(editor): Usage page async await instead of then

* fix(editor): Usage page show some content only if network requests in component mounted were successful

* chore(editor): code formatting

* fix(editor): Usage checking license environment

* feat(editor): Improve license activation error messages (no-changelog) (#4958)

* fix(editor): Usage changing activation error title

* remove unnecessary import

* fix(editor): Usage refactor notification showing

* fix(editor): Usage using notification directly in store actions

Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: freyamade <freya@n8n.io>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: Mutasem <mutdmour@gmail.com>
Co-authored-by: Cornelius Suermann <cornelius@n8n.io>

* fix(editor): Usage change mounted lifecycle logic

* fix(editor): Usage return after successful activation in mounted

* fix: remove console log

* test: fix tests related to settings (#4979)

Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: freyamade <freya@n8n.io>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: Mutasem <mutdmour@gmail.com>
Co-authored-by: Cornelius Suermann <cornelius@n8n.io>
Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com>
2022-12-20 10:52:01 +01:00

434 lines
13 KiB
TypeScript

import { Telemetry } from '@/telemetry';
import config from '@/config';
import { flushPromises } from './Helpers';
jest.mock('@/license/License.service', () => {
return {
LicenseService: {
getActiveTriggerCount: async () => 0,
},
};
});
jest.mock('posthog-node');
jest.spyOn(Telemetry.prototype as any, 'initRudderStack').mockImplementation(() => {
return {
flush: () => {},
identify: () => {},
track: () => {},
};
});
describe('Telemetry', () => {
let startPulseSpy: jest.SpyInstance;
const spyTrack = jest.spyOn(Telemetry.prototype, 'track').mockName('track');
let telemetry: Telemetry;
const n8nVersion = '0.0.0';
const instanceId = 'Telemetry unit test';
const testDateTime = new Date('2022-01-01 00:00:00');
beforeAll(() => {
startPulseSpy = jest
.spyOn(Telemetry.prototype as any, 'startPulse')
.mockImplementation(() => {});
jest.useFakeTimers();
jest.setSystemTime(testDateTime);
config.set('diagnostics.enabled', true);
config.set('deployment.type', 'n8n-testing');
});
afterAll(() => {
jest.clearAllTimers();
jest.useRealTimers();
startPulseSpy.mockRestore();
telemetry.trackN8nStop();
});
beforeEach(() => {
spyTrack.mockClear();
telemetry = new Telemetry(instanceId, n8nVersion);
});
afterEach(() => {
telemetry.trackN8nStop();
});
describe('trackN8nStop', () => {
test('should call track method', () => {
telemetry.trackN8nStop();
expect(spyTrack).toHaveBeenCalledTimes(1);
});
});
describe('trackWorkflowExecution', () => {
beforeEach(() => {
jest.setSystemTime(testDateTime);
});
test('should count executions correctly', async () => {
const payload = {
workflow_id: '1',
is_manual: true,
success: true,
error_node_type: 'custom-nodes-base.node-type',
};
payload.is_manual = true;
payload.success = true;
const execTime1 = fakeJestSystemTime('2022-01-01 12:00:00');
await telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
await telemetry.trackWorkflowExecution(payload);
payload.is_manual = false;
payload.success = true;
const execTime2 = fakeJestSystemTime('2022-01-01 13:00:00');
await telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
await telemetry.trackWorkflowExecution(payload);
payload.is_manual = true;
payload.success = false;
const execTime3 = fakeJestSystemTime('2022-01-01 14:00:00');
await telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
await telemetry.trackWorkflowExecution(payload);
payload.is_manual = false;
payload.success = false;
const execTime4 = fakeJestSystemTime('2022-01-01 15:00:00');
await telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
await telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
const execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_success?.count).toBe(2);
expect(execBuffer['1'].manual_success?.first).toEqual(execTime1);
expect(execBuffer['1'].prod_success?.count).toBe(2);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime2);
expect(execBuffer['1'].manual_error?.count).toBe(2);
expect(execBuffer['1'].manual_error?.first).toEqual(execTime3);
expect(execBuffer['1'].prod_error?.count).toBe(2);
expect(execBuffer['1'].prod_error?.first).toEqual(execTime4);
});
test('should fire "Workflow execution errored" event for failed executions', async () => {
const payload = {
workflow_id: '1',
is_manual: true,
success: false,
error_node_type: 'custom-nodes-base.node-type',
};
const execTime1 = fakeJestSystemTime('2022-01-01 12:00:00');
await telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
await telemetry.trackWorkflowExecution(payload);
let execBuffer = telemetry.getCountsBuffer();
// should not fire event for custom nodes
expect(spyTrack).toHaveBeenCalledTimes(0);
expect(execBuffer['1'].manual_error?.count).toBe(2);
expect(execBuffer['1'].manual_error?.first).toEqual(execTime1);
payload.error_node_type = 'n8n-nodes-base.node-type';
fakeJestSystemTime('2022-01-01 13:00:00');
await telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
await telemetry.trackWorkflowExecution(payload);
execBuffer = telemetry.getCountsBuffer();
// should fire event for custom nodes
expect(spyTrack).toHaveBeenCalledTimes(2);
expect(spyTrack).toHaveBeenCalledWith('Workflow execution errored', payload);
expect(execBuffer['1'].manual_error?.count).toBe(4);
expect(execBuffer['1'].manual_error?.first).toEqual(execTime1);
});
test('should track production executions count correctly', async () => {
const payload = {
workflow_id: '1',
is_manual: false,
success: true,
error_node_type: 'node_type',
};
// successful execution
const execTime1 = fakeJestSystemTime('2022-01-01 12:00:00');
await telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
let execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_error).toBeUndefined();
expect(execBuffer['1'].manual_success).toBeUndefined();
expect(execBuffer['1'].prod_error).toBeUndefined();
expect(execBuffer['1'].prod_success?.count).toBe(1);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime1);
// successful execution n8n node
payload.error_node_type = 'n8n-nodes-base.merge';
payload.workflow_id = '2';
await telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_error).toBeUndefined();
expect(execBuffer['1'].manual_success).toBeUndefined();
expect(execBuffer['1'].prod_error).toBeUndefined();
expect(execBuffer['1'].prod_success?.count).toBe(1);
expect(execBuffer['2'].prod_success?.count).toBe(1);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime1);
expect(execBuffer['2'].prod_success?.first).toEqual(execTime1);
// additional successful execution
payload.error_node_type = 'n8n-nodes-base.merge';
payload.workflow_id = '2';
await telemetry.trackWorkflowExecution(payload);
payload.error_node_type = 'n8n-nodes-base.merge';
payload.workflow_id = '1';
await telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_error).toBeUndefined();
expect(execBuffer['1'].manual_success).toBeUndefined();
expect(execBuffer['1'].prod_error).toBeUndefined();
expect(execBuffer['2'].manual_error).toBeUndefined();
expect(execBuffer['2'].manual_success).toBeUndefined();
expect(execBuffer['2'].prod_error).toBeUndefined();
expect(execBuffer['1'].prod_success?.count).toBe(2);
expect(execBuffer['2'].prod_success?.count).toBe(2);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime1);
expect(execBuffer['2'].prod_success?.first).toEqual(execTime1);
// failed execution
const execTime2 = fakeJestSystemTime('2022-01-01 12:00:00');
payload.error_node_type = 'custom-package.custom-node';
payload.success = false;
await telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_error).toBeUndefined();
expect(execBuffer['1'].manual_success).toBeUndefined();
expect(execBuffer['2'].manual_error).toBeUndefined();
expect(execBuffer['2'].manual_success).toBeUndefined();
expect(execBuffer['2'].prod_error).toBeUndefined();
expect(execBuffer['1'].prod_error?.count).toBe(1);
expect(execBuffer['1'].prod_success?.count).toBe(2);
expect(execBuffer['2'].prod_success?.count).toBe(2);
expect(execBuffer['1'].prod_error?.first).toEqual(execTime2);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime1);
expect(execBuffer['2'].prod_success?.first).toEqual(execTime1);
// failed execution n8n node
payload.success = false;
payload.error_node_type = 'n8n-nodes-base.merge';
await telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(1);
execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_error).toBeUndefined();
expect(execBuffer['1'].manual_success).toBeUndefined();
expect(execBuffer['2'].manual_error).toBeUndefined();
expect(execBuffer['2'].manual_success).toBeUndefined();
expect(execBuffer['2'].prod_error).toBeUndefined();
expect(execBuffer['1'].prod_success?.count).toBe(2);
expect(execBuffer['1'].prod_error?.count).toBe(2);
expect(execBuffer['2'].prod_success?.count).toBe(2);
expect(execBuffer['1'].prod_error?.first).toEqual(execTime2);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime1);
expect(execBuffer['2'].prod_success?.first).toEqual(execTime1);
});
});
describe('pulse', () => {
let pulseSpy: jest.SpyInstance;
beforeAll(() => {
startPulseSpy.mockRestore();
});
beforeEach(() => {
fakeJestSystemTime(testDateTime);
pulseSpy = jest.spyOn(Telemetry.prototype as any, 'pulse').mockName('pulseSpy');
});
afterEach(() => {
pulseSpy.mockClear();
});
xtest('should trigger pulse in intervals', async () => {
expect(pulseSpy).toBeCalledTimes(0);
jest.advanceTimersToNextTimer();
await flushPromises();
expect(pulseSpy).toBeCalledTimes(1);
expect(spyTrack).toHaveBeenCalledTimes(1);
expect(spyTrack).toHaveBeenCalledWith('pulse', {
plan_name_current: 'Community',
quota: -1,
usage: 0,
});
jest.advanceTimersToNextTimer();
await flushPromises();
expect(pulseSpy).toBeCalledTimes(2);
expect(spyTrack).toHaveBeenCalledTimes(2);
expect(spyTrack).toHaveBeenCalledWith('pulse', {
plan_name_current: 'Community',
quota: -1,
usage: 0,
});
});
xtest('should track workflow counts correctly', async () => {
expect(pulseSpy).toBeCalledTimes(0);
let execBuffer = telemetry.getCountsBuffer();
// expect clear counters on start
expect(Object.keys(execBuffer).length).toBe(0);
const payload = {
workflow_id: '1',
is_manual: true,
success: true,
error_node_type: 'custom-nodes-base.node-type',
};
await telemetry.trackWorkflowExecution(payload);
await telemetry.trackWorkflowExecution(payload);
payload.is_manual = false;
payload.success = true;
await telemetry.trackWorkflowExecution(payload);
await telemetry.trackWorkflowExecution(payload);
payload.is_manual = true;
payload.success = false;
await telemetry.trackWorkflowExecution(payload);
await telemetry.trackWorkflowExecution(payload);
payload.is_manual = false;
payload.success = false;
await telemetry.trackWorkflowExecution(payload);
await telemetry.trackWorkflowExecution(payload);
payload.workflow_id = '2';
await telemetry.trackWorkflowExecution(payload);
await telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
expect(pulseSpy).toBeCalledTimes(0);
jest.advanceTimersToNextTimer();
execBuffer = telemetry.getCountsBuffer();
await flushPromises();
expect(pulseSpy).toBeCalledTimes(1);
expect(spyTrack).toHaveBeenCalledTimes(3);
expect(spyTrack).toHaveBeenNthCalledWith(
1,
'Workflow execution count',
{
event_version: '2',
workflow_id: '1',
user_id: undefined,
manual_error: {
count: 2,
first: testDateTime,
},
manual_success: {
count: 2,
first: testDateTime,
},
prod_error: {
count: 2,
first: testDateTime,
},
prod_success: {
count: 2,
first: testDateTime,
},
},
{ withPostHog: true },
);
expect(spyTrack).toHaveBeenNthCalledWith(
2,
'Workflow execution count',
{
event_version: '2',
workflow_id: '2',
user_id: undefined,
prod_error: {
count: 2,
first: testDateTime,
},
},
{ withPostHog: true },
);
expect(spyTrack).toHaveBeenNthCalledWith(3, 'pulse', {
plan_name_current: 'Community',
quota: -1,
usage: 0,
});
expect(Object.keys(execBuffer).length).toBe(0);
// Adding a second step here because we believe PostHog may use timers for sending data
// and adding posthog to the above metric was causing the pulseSpy timer to not be ran
jest.advanceTimersToNextTimer();
execBuffer = telemetry.getCountsBuffer();
expect(Object.keys(execBuffer).length).toBe(0);
// @TODO: Flushing promises here is not working
// expect(pulseSpy).toBeCalledTimes(2);
// expect(spyTrack).toHaveBeenCalledTimes(4);
// expect(spyTrack).toHaveBeenNthCalledWith(4, 'pulse', {
// plan_name_current: 'Community',
// quota: -1,
// usage: 0,
// });
});
});
});
const fakeJestSystemTime = (dateTime: string | Date): Date => {
const dt = new Date(dateTime);
jest.setSystemTime(dt);
return dt;
};