fix: Fix telemetry causing console error (#10828)

This commit is contained in:
Mutasem Aldmour 2024-09-16 12:11:50 +02:00 committed by GitHub
parent a7ed8ee909
commit 3be31e27ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 76 additions and 1 deletions

View file

@ -16,12 +16,23 @@ import { DEFAULT_POSTHOG_SETTINGS } from './posthog.test';
import { AI_ASSISTANT_EXPERIMENT } from '@/constants'; import { AI_ASSISTANT_EXPERIMENT } from '@/constants';
import { reactive } from 'vue'; import { reactive } from 'vue';
import * as chatAPI from '@/api/assistant'; import * as chatAPI from '@/api/assistant';
import * as telemetryModule from '@/composables/useTelemetry';
import type { Telemetry } from '@/plugins/telemetry';
let settingsStore: ReturnType<typeof useSettingsStore>; let settingsStore: ReturnType<typeof useSettingsStore>;
let posthogStore: ReturnType<typeof usePostHog>; let posthogStore: ReturnType<typeof usePostHog>;
const apiSpy = vi.spyOn(chatAPI, 'chatWithAssistant'); 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) => { const setAssistantEnabled = (enabled: boolean) => {
settingsStore.setSettings( settingsStore.setSettings(
merge({}, defaultSettings, { merge({}, defaultSettings, {
@ -63,6 +74,7 @@ describe('AI Assistant store', () => {
}; };
posthogStore = usePostHog(); posthogStore = usePostHog();
posthogStore.init(); posthogStore.init();
track.mockReset();
}); });
it('initializes with default values', () => { it('initializes with default values', () => {
@ -316,4 +328,67 @@ describe('AI Assistant store', () => {
await assistantStore.initErrorHelper(context); await assistantStore.initErrorHelper(context);
expect(apiSpy).toHaveBeenCalled(); 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__',
});
});
}); });

View file

@ -580,7 +580,7 @@ export const useAssistantStore = defineStore(STORES.ASSISTANT, () => {
workflow_id: workflowsStore.workflowId, workflow_id: workflowsStore.workflowId,
node_type: chatSessionError.value?.node?.type, node_type: chatSessionError.value?.node?.type,
error: chatSessionError.value?.error, error: chatSessionError.value?.error,
chat_session_id: currentSessionId, chat_session_id: currentSessionId.value,
}); });
} }