2023-03-24 07:57:22 -07:00
|
|
|
import { createPinia, setActivePinia } from 'pinia';
|
2023-05-05 01:41:54 -07:00
|
|
|
import { usePostHog } from '@/stores/posthog.store';
|
|
|
|
import { useUsersStore } from '@/stores/users.store';
|
|
|
|
import { useSettingsStore } from '@/stores/settings.store';
|
2024-06-18 10:15:12 -07:00
|
|
|
import { useRootStore } from '@/stores/root.store';
|
2024-09-17 08:21:11 -07:00
|
|
|
import type { FrontendSettings } from '@n8n/api-types';
|
2023-11-07 01:06:08 -08:00
|
|
|
import { LOCAL_STORAGE_EXPERIMENT_OVERRIDES } from '@/constants';
|
|
|
|
import { nextTick } from 'vue';
|
2024-11-01 09:01:13 -07:00
|
|
|
import { defaultSettings } from '../__tests__/defaults';
|
2024-09-23 05:13:06 -07:00
|
|
|
import { useTelemetry } from '@/composables/useTelemetry';
|
2023-03-24 07:57:22 -07:00
|
|
|
|
2024-09-17 08:21:11 -07:00
|
|
|
export const DEFAULT_POSTHOG_SETTINGS: FrontendSettings['posthog'] = {
|
2023-03-24 07:57:22 -07:00
|
|
|
enabled: true,
|
|
|
|
apiHost: 'host',
|
|
|
|
apiKey: 'key',
|
|
|
|
autocapture: false,
|
|
|
|
disableSessionRecording: true,
|
|
|
|
debug: false,
|
|
|
|
};
|
|
|
|
const CURRENT_USER_ID = '1';
|
|
|
|
const CURRENT_INSTANCE_ID = '456';
|
|
|
|
|
2024-09-17 08:21:11 -07:00
|
|
|
function setSettings(overrides?: Partial<FrontendSettings>) {
|
2023-03-24 07:57:22 -07:00
|
|
|
useSettingsStore().setSettings({
|
2024-03-18 10:34:41 -07:00
|
|
|
...defaultSettings,
|
2023-03-24 07:57:22 -07:00
|
|
|
posthog: DEFAULT_POSTHOG_SETTINGS,
|
|
|
|
instanceId: CURRENT_INSTANCE_ID,
|
|
|
|
...overrides,
|
2024-09-17 08:21:11 -07:00
|
|
|
} as FrontendSettings);
|
2023-03-24 07:57:22 -07:00
|
|
|
|
|
|
|
useRootStore().setInstanceId(CURRENT_INSTANCE_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setCurrentUser() {
|
|
|
|
useUsersStore().addUsers([
|
|
|
|
{
|
|
|
|
id: CURRENT_USER_ID,
|
|
|
|
isPending: false,
|
|
|
|
createdAt: '2023-03-17T14:01:36.432Z',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
useUsersStore().currentUserId = CURRENT_USER_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetStores() {
|
2024-07-19 05:35:36 -07:00
|
|
|
useSettingsStore().reset();
|
2024-07-08 07:21:03 -07:00
|
|
|
useUsersStore().reset();
|
2023-03-24 07:57:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function setup() {
|
|
|
|
setActivePinia(createPinia());
|
|
|
|
window.posthog = {
|
|
|
|
init: () => {},
|
|
|
|
identify: () => {},
|
|
|
|
};
|
|
|
|
|
2024-09-23 05:13:06 -07:00
|
|
|
const telemetry = useTelemetry();
|
2023-03-24 07:57:22 -07:00
|
|
|
|
|
|
|
vi.spyOn(window.posthog, 'init');
|
|
|
|
vi.spyOn(window.posthog, 'identify');
|
2024-09-23 05:13:06 -07:00
|
|
|
vi.spyOn(telemetry, 'track');
|
2023-03-24 07:57:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('Posthog store', () => {
|
|
|
|
describe('should not init', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setup();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not init if posthog is not enabled', () => {
|
|
|
|
setSettings({ posthog: { ...DEFAULT_POSTHOG_SETTINGS, enabled: false } });
|
|
|
|
setCurrentUser();
|
|
|
|
const posthog = usePostHog();
|
|
|
|
posthog.init();
|
|
|
|
|
|
|
|
expect(window.posthog?.init).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not init if user is not logged in', () => {
|
|
|
|
setSettings();
|
|
|
|
const posthog = usePostHog();
|
|
|
|
posthog.init();
|
|
|
|
|
|
|
|
expect(window.posthog?.init).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
resetStores();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('should init posthog', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setup();
|
|
|
|
setSettings();
|
|
|
|
setCurrentUser();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should init store with serverside flags', () => {
|
|
|
|
const TEST = 'test';
|
|
|
|
const flags = {
|
|
|
|
[TEST]: 'variant',
|
|
|
|
};
|
|
|
|
const posthog = usePostHog();
|
|
|
|
posthog.init(flags);
|
|
|
|
|
|
|
|
expect(posthog.getVariant('test')).toEqual(flags[TEST]);
|
|
|
|
expect(window.posthog?.init).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should identify user', () => {
|
|
|
|
const posthog = usePostHog();
|
|
|
|
posthog.init();
|
|
|
|
|
|
|
|
const userId = `${CURRENT_INSTANCE_ID}#${CURRENT_USER_ID}`;
|
|
|
|
expect(window.posthog?.identify).toHaveBeenCalledWith(userId, {
|
|
|
|
created_at_timestamp: 1679061696432,
|
|
|
|
instance_id: CURRENT_INSTANCE_ID,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-11-07 01:06:08 -08:00
|
|
|
it('sets override feature flags', async () => {
|
2023-03-24 07:57:22 -07:00
|
|
|
const TEST = 'test';
|
|
|
|
const flags = {
|
|
|
|
[TEST]: 'variant',
|
|
|
|
};
|
|
|
|
const posthog = usePostHog();
|
|
|
|
posthog.init(flags);
|
|
|
|
|
|
|
|
window.featureFlags?.override(TEST, 'override');
|
2023-11-07 01:06:08 -08:00
|
|
|
await nextTick();
|
2023-03-24 07:57:22 -07:00
|
|
|
|
|
|
|
expect(posthog.getVariant('test')).toEqual('override');
|
|
|
|
expect(window.posthog?.init).toHaveBeenCalled();
|
2023-11-07 01:06:08 -08:00
|
|
|
expect(window.localStorage.getItem(LOCAL_STORAGE_EXPERIMENT_OVERRIDES)).toEqual(
|
2023-03-24 07:57:22 -07:00
|
|
|
JSON.stringify({ test: 'override' }),
|
|
|
|
);
|
|
|
|
|
|
|
|
window.featureFlags?.override('other_test', 'override');
|
2023-11-07 01:06:08 -08:00
|
|
|
await nextTick();
|
|
|
|
expect(window.localStorage.getItem(LOCAL_STORAGE_EXPERIMENT_OVERRIDES)).toEqual(
|
2023-03-24 07:57:22 -07:00
|
|
|
JSON.stringify({ test: 'override', other_test: 'override' }),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
resetStores();
|
|
|
|
window.localStorage.clear();
|
|
|
|
window.featureFlags = undefined;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|