Skip to content

Commit

Permalink
fix(editor): Record sessionStarted telemetry event in Setting Store (#…
Browse files Browse the repository at this point in the history
…11334)

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <[email protected]>
  • Loading branch information
2 people authored and r00gm committed Oct 24, 2024
1 parent 6b272b0 commit 8b393aa
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
102 changes: 102 additions & 0 deletions packages/editor-ui/src/stores/settings.store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import type { FrontendSettings } from '@n8n/api-types';
import { createPinia, setActivePinia } from 'pinia';
import { mock } from 'vitest-mock-extended';
import { useSettingsStore } from './settings.store';

const { getSettings } = vi.hoisted(() => ({
getSettings: vi.fn(),
}));

const { sessionStarted } = vi.hoisted(() => ({
sessionStarted: vi.fn(),
}));

vi.mock('@/api/settings', () => ({
getSettings,
}));

vi.mock('@/api/events', () => ({
sessionStarted,
}));

vi.mock('@/stores/root.store', () => ({
useRootStore: vi.fn(() => ({
restApiContext: {},
setVersionCli: vi.fn(),
})),
}));

vi.mock('@/stores/root.store', () => ({
useRootStore: vi.fn(() => ({
setUrlBaseWebhook: vi.fn(),
setUrlBaseEditor: vi.fn(),
setEndpointForm: vi.fn(),
setEndpointFormTest: vi.fn(),
setEndpointFormWaiting: vi.fn(),
setEndpointWebhook: vi.fn(),
setEndpointWebhookTest: vi.fn(),
setEndpointWebhookWaiting: vi.fn(),
setTimezone: vi.fn(),
setExecutionTimeout: vi.fn(),
setMaxExecutionTimeout: vi.fn(),
setInstanceId: vi.fn(),
setOauthCallbackUrls: vi.fn(),
setN8nMetadata: vi.fn(),
setDefaultLocale: vi.fn(),
setBinaryDataMode: vi.fn(),
setVersionCli: vi.fn(),
})),
}));

vi.mock('@/stores/versions.store', () => ({
useVersionsStore: vi.fn(() => ({
setVersionNotificationSettings: vi.fn(),
})),
}));

const mockSettings = mock<FrontendSettings>({
authCookie: { secure: true },
});

describe('settings.store', () => {
beforeEach(() => {
vi.restoreAllMocks();
setActivePinia(createPinia());
});

describe('getSettings', () => {
it('should fetch settings and call sessionStarted if telemetry is enabled', async () => {
const settingsStore = useSettingsStore();

getSettings.mockResolvedValueOnce({
...mockSettings,
telemetry: {
enabled: true,
config: {
url: 'https://telemetry.example.com',
key: 'telemetry-key',
},
},
});

await settingsStore.getSettings();
expect(getSettings).toHaveBeenCalled();
expect(sessionStarted).toHaveBeenCalled();
});

it('should fetch settings and skip calling sessionStarted if telemetry is disabled', async () => {
const settingsStore = useSettingsStore();

getSettings.mockResolvedValueOnce({
...mockSettings,
telemetry: {
enabled: false,
},
});

await settingsStore.getSettings();
expect(getSettings).toHaveBeenCalled();
expect(sessionStarted).not.toHaveBeenCalled();
});
});
});
5 changes: 5 additions & 0 deletions packages/editor-ui/src/stores/settings.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Bowser from 'bowser';
import type { IUserManagementSettings, FrontendSettings } from '@n8n/api-types';

import * as publicApiApi from '@/api/api-keys';
import * as eventsApi from '@/api/events';
import * as ldapApi from '@/api/ldap';
import * as settingsApi from '@/api/settings';
import { testHealthEndpoint } from '@/api/templates';
Expand Down Expand Up @@ -247,6 +248,10 @@ export const useSettingsStore = defineStore(STORES.SETTINGS, () => {
rootStore.setDefaultLocale(fetchedSettings.defaultLocale);
rootStore.setBinaryDataMode(fetchedSettings.binaryDataMode);
useVersionsStore().setVersionNotificationSettings(fetchedSettings.versionNotifications);

if (fetchedSettings.telemetry.enabled) {
void eventsApi.sessionStarted(rootStore.restApiContext);
}
};

const initialize = async () => {
Expand Down

0 comments on commit 8b393aa

Please sign in to comment.