Files
tolaria/src/hooks/useTelemetry.test.ts
Test d9254ffaf5 refactor: remove Anthropic API integration, CLI agent only (ADR-0028)
Remove AIChatPanel, useAIChat hook, Rust ai_chat command, and
anthropic_key from settings. AI is now exclusively via Claude CLI
subprocess (AiPanel). Simplifies codebase and eliminates API key
management for users. ADR-0027 superseded.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 15:05:56 +02:00

76 lines
2.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook } from '@testing-library/react'
import { useTelemetry } from './useTelemetry'
import type { Settings } from '../types'
const mockInitSentry = vi.fn()
const mockTeardownSentry = vi.fn()
const mockInitPostHog = vi.fn()
const mockTeardownPostHog = vi.fn()
vi.mock('../lib/telemetry', () => ({
initSentry: (...args: unknown[]) => mockInitSentry(...args),
teardownSentry: () => mockTeardownSentry(),
initPostHog: (...args: unknown[]) => mockInitPostHog(...args),
teardownPostHog: () => mockTeardownPostHog(),
}))
const baseSettings: Settings = {
openai_key: null, google_key: null,
github_token: null, github_username: null, auto_pull_interval_minutes: null,
telemetry_consent: null, crash_reporting_enabled: null,
analytics_enabled: null, anonymous_id: null, update_channel: null,
}
describe('useTelemetry', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('does nothing when settings are not loaded', () => {
renderHook(() => useTelemetry(baseSettings, false))
expect(mockInitSentry).not.toHaveBeenCalled()
expect(mockInitPostHog).not.toHaveBeenCalled()
})
it('does nothing when consent is not granted', () => {
renderHook(() => useTelemetry({ ...baseSettings, telemetry_consent: false }, true))
expect(mockInitSentry).not.toHaveBeenCalled()
expect(mockInitPostHog).not.toHaveBeenCalled()
})
it('initializes Sentry when crash reporting is enabled', () => {
renderHook(() =>
useTelemetry({ ...baseSettings, crash_reporting_enabled: true, anonymous_id: 'test-uuid' }, true)
)
expect(mockInitSentry).toHaveBeenCalledWith('test-uuid')
})
it('initializes PostHog when analytics is enabled', () => {
renderHook(() =>
useTelemetry({ ...baseSettings, analytics_enabled: true, anonymous_id: 'test-uuid' }, true)
)
expect(mockInitPostHog).toHaveBeenCalledWith('test-uuid')
})
it('tears down Sentry when crash reporting is disabled after being enabled', () => {
const settings1 = { ...baseSettings, crash_reporting_enabled: true, anonymous_id: 'test-uuid' }
const { rerender } = renderHook(
({ settings, loaded }) => useTelemetry(settings, loaded),
{ initialProps: { settings: settings1, loaded: true } }
)
expect(mockInitSentry).toHaveBeenCalledOnce()
const settings2 = { ...baseSettings, crash_reporting_enabled: false, anonymous_id: 'test-uuid' }
rerender({ settings: settings2, loaded: true })
expect(mockTeardownSentry).toHaveBeenCalledOnce()
})
it('does not initialize without anonymous_id', () => {
renderHook(() =>
useTelemetry({ ...baseSettings, crash_reporting_enabled: true, anonymous_id: null }, true)
)
expect(mockInitSentry).not.toHaveBeenCalled()
})
})