mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -08:00
fix: Fix telemetry causing console error (#10828)
This commit is contained in:
parent
a7ed8ee909
commit
3be31e27ed
|
@ -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__',
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -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,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue