diff --git a/src/platform/settings/settingStore.ts b/src/platform/settings/settingStore.ts index a2087c108..64e2acb8d 100644 --- a/src/platform/settings/settingStore.ts +++ b/src/platform/settings/settingStore.ts @@ -80,18 +80,20 @@ export const useSettingStore = defineStore('setting', () => { const dialogStore = useDialogStore() if (dialogStore.isDialogOpen('global-settings')) { const telemetry = useTelemetry() - const param = settingsById.value[key] + const settingParameter = settingsById.value[key] const { category, subCategory } = getSettingInfo( - param ?? + settingParameter ?? ({ id: String(key) } as unknown as SettingParams) ) const inputType = (() => { - const type = param?.type - if (!type) return undefined - return typeof type === 'function' ? 'custom' : String(type) + const settingType = settingParameter?.type + if (!settingType) return undefined + return typeof settingType === 'function' + ? 'custom' + : String(settingType) })() telemetry?.trackSettingChanged({ diff --git a/tests-ui/platform/settings/settingStore.test.ts b/tests-ui/platform/settings/settingStore.test.ts new file mode 100644 index 000000000..2aac02ba9 --- /dev/null +++ b/tests-ui/platform/settings/settingStore.test.ts @@ -0,0 +1,98 @@ +import { describe, it, expect, beforeEach, vi } from 'vitest' +import { setActivePinia, createPinia } from 'pinia' + +const hoisted = vi.hoisted(() => ({ + trackSettingChanged: vi.fn(), + storeSetting: vi.fn().mockResolvedValue(undefined) +})) + +let isSettingsDialogOpen = false + +vi.mock('@/platform/telemetry', () => { + return { + useTelemetry: () => ({ + trackSettingChanged: hoisted.trackSettingChanged + }) + } +}) + +vi.mock('@/stores/dialogStore', () => { + return { + useDialogStore: () => ({ + isDialogOpen: (key: string) => + isSettingsDialogOpen && key === 'global-settings' + }) + } +}) + +vi.mock('@/scripts/api', () => { + return { + api: { + storeSetting: hoisted.storeSetting, + getSettings: vi.fn().mockResolvedValue({}) + } + } +}) + +vi.mock('@/scripts/app', () => { + return { + app: { + ui: { + settings: { + dispatchChange: vi.fn() + } + } + } + } +}) + +import { useSettingStore } from '@/platform/settings/settingStore' + +describe('useSettingStore telemetry', () => { + beforeEach(() => { + setActivePinia(createPinia()) + hoisted.trackSettingChanged.mockReset() + hoisted.storeSetting.mockReset().mockResolvedValue(undefined) + isSettingsDialogOpen = false + }) + + it('tracks telemetry when settings dialog is open', async () => { + isSettingsDialogOpen = true + + const store = useSettingStore() + store.addSetting({ + id: 'main.sub.setting.name', + name: 'Test Setting', + type: 'text', + defaultValue: 'old' + }) + + await store.set('main.sub.setting.name', 'new') + + expect(hoisted.trackSettingChanged).toHaveBeenCalledTimes(1) + expect(hoisted.trackSettingChanged).toHaveBeenCalledWith({ + setting_id: 'main.sub.setting.name', + input_type: 'text', + category: 'main', + sub_category: 'sub', + previous_value: 'old', + new_value: 'new' + }) + }) + + it('does not track telemetry when settings dialog is closed', async () => { + isSettingsDialogOpen = false + + const store = useSettingStore() + store.addSetting({ + id: 'single.setting', + name: 'Another Setting', + type: 'text', + defaultValue: 'x' + }) + + await store.set('single.setting', 'y') + + expect(hoisted.trackSettingChanged).not.toHaveBeenCalled() + }) +})