Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix telemetry causing console error #10828

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions packages/editor-ui/src/stores/__tests__/assistant.store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@
import { AI_ASSISTANT_EXPERIMENT } from '@/constants';
import { reactive } from 'vue';
import * as chatAPI from '@/api/assistant';
import * as telemetryModule from '@/composables/useTelemetry';
import { Telemetry } from '@/plugins/telemetry';

Check failure on line 20 in packages/editor-ui/src/stores/__tests__/assistant.store.test.ts

View workflow job for this annotation

GitHub Actions / Lint / Lint

All imports in the declaration are only used as types. Use `import type`
mutdmour marked this conversation as resolved.
Show resolved Hide resolved

let settingsStore: ReturnType<typeof useSettingsStore>;
let posthogStore: ReturnType<typeof usePostHog>;

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, {
Expand Down Expand Up @@ -63,6 +74,7 @@
};
posthogStore = usePostHog();
posthogStore.init();
track.mockReset();
});

it('initializes with default values', () => {
Expand Down Expand Up @@ -316,4 +328,67 @@
await assistantStore.initErrorHelper(context);
expect(apiSpy).toHaveBeenCalled();
});

it.only('should call telemetry for opening assistant with error', async () => {

Check failure on line 332 in packages/editor-ui/src/stores/__tests__/assistant.store.test.ts

View workflow job for this annotation

GitHub Actions / Lint / Lint

Remove `.only()` call
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__',
});
});
});
2 changes: 1 addition & 1 deletion packages/editor-ui/src/stores/assistant.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand Down
Loading