fix(editor): Record sessionStarted telemetry event in Setting Store (#11334)

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
Federico Meini 2024-10-22 17:48:08 +02:00 committed by GitHub
parent fb3518fcb8
commit 1b734dd9f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 107 additions and 0 deletions

View file

@ -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();
});
});
});

View file

@ -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';
@ -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 () => {