From c36548718a5bf2b443a7b5c0a9e1f85bd1271797 Mon Sep 17 00:00:00 2001 From: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com> Date: Mon, 16 Sep 2024 12:11:50 +0200 Subject: [PATCH] fix: Fix telemetry causing console error (#10828) --- .../stores/__tests__/assistant.store.test.ts | 75 +++++++++++++++++++ .../editor-ui/src/stores/assistant.store.ts | 2 +- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/packages/editor-ui/src/stores/__tests__/assistant.store.test.ts b/packages/editor-ui/src/stores/__tests__/assistant.store.test.ts index fad5bae0c74b09..4ca4337ab39ada 100644 --- a/packages/editor-ui/src/stores/__tests__/assistant.store.test.ts +++ b/packages/editor-ui/src/stores/__tests__/assistant.store.test.ts @@ -16,12 +16,23 @@ import { DEFAULT_POSTHOG_SETTINGS } from './posthog.test'; import { AI_ASSISTANT_EXPERIMENT } from '@/constants'; import { reactive } from 'vue'; import * as chatAPI from '@/api/assistant'; +import * as telemetryModule from '@/composables/useTelemetry'; +import type { Telemetry } from '@/plugins/telemetry'; let settingsStore: ReturnType; let posthogStore: ReturnType; const apiSpy = vi.spyOn(chatAPI, 'chatWithAssistant'); +const track = vi.fn(); +const spy = vi.spyOn(telemetryModule, 'useTelemetry'); +spy.mockImplementation( + () => + ({ + track, + }) as unknown as Telemetry, +); + const setAssistantEnabled = (enabled: boolean) => { settingsStore.setSettings( merge({}, defaultSettings, { @@ -63,6 +74,7 @@ describe('AI Assistant store', () => { }; posthogStore = usePostHog(); posthogStore.init(); + track.mockReset(); }); it('initializes with default values', () => { @@ -316,4 +328,67 @@ describe('AI Assistant store', () => { await assistantStore.initErrorHelper(context); expect(apiSpy).toHaveBeenCalled(); }); + + it('should call telemetry for opening assistant with error', async () => { + const context: ChatRequest.ErrorContext = { + error: { + description: '', + message: 'Hey', + name: 'NodeOperationError', + }, + node: { + id: '1', + type: 'n8n-nodes-base.stopAndError', + typeVersion: 1, + name: 'Stop and Error', + position: [250, 250], + parameters: {}, + }, + }; + const mockSessionId = 'test'; + + const assistantStore = useAssistantStore(); + apiSpy.mockImplementation((_ctx, _payload, onMessage) => { + onMessage({ + messages: [], + sessionId: mockSessionId, + }); + }); + + await assistantStore.initErrorHelper(context); + expect(apiSpy).toHaveBeenCalled(); + expect(assistantStore.currentSessionId).toEqual(mockSessionId); + + assistantStore.trackUserOpenedAssistant({ + task: 'error', + source: 'error', + has_existing_session: true, + }); + expect(track).toHaveBeenCalledWith( + 'Assistant session started', + { + chat_session_id: 'test', + node_type: 'n8n-nodes-base.stopAndError', + task: 'error', + credential_type: undefined, + }, + { + withPostHog: true, + }, + ); + + expect(track).toHaveBeenCalledWith('User opened assistant', { + chat_session_id: 'test', + error: { + description: '', + message: 'Hey', + name: 'NodeOperationError', + }, + has_existing_session: true, + node_type: 'n8n-nodes-base.stopAndError', + source: 'error', + task: 'error', + workflow_id: '__EMPTY__', + }); + }); }); diff --git a/packages/editor-ui/src/stores/assistant.store.ts b/packages/editor-ui/src/stores/assistant.store.ts index 386df83adfd537..0bd883ad5171c7 100644 --- a/packages/editor-ui/src/stores/assistant.store.ts +++ b/packages/editor-ui/src/stores/assistant.store.ts @@ -580,7 +580,7 @@ export const useAssistantStore = defineStore(STORES.ASSISTANT, () => { workflow_id: workflowsStore.workflowId, node_type: chatSessionError.value?.node?.type, error: chatSessionError.value?.error, - chat_session_id: currentSessionId, + chat_session_id: currentSessionId.value, }); }