chore(settings): clarify variable names in telemetry tracking and add unit test for settings telemetry trigger

This commit is contained in:
Benjamin Lu
2025-10-31 23:20:54 -07:00
parent f2952e9ef2
commit 5d7321e23a
2 changed files with 105 additions and 5 deletions

View File

@@ -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({

View File

@@ -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()
})
})