Files
accounted/lib/analytics/__tests__/enabled.test.ts
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

67 lines
2.7 KiB
TypeScript

import { describe, it, expect, vi, afterEach } from 'vitest'
import { isAnalyticsEnabled, warnIfAnalyticsMisconfigured, POSTHOG_TOKEN_VAR } from '../enabled'
describe('analytics gate', () => {
afterEach(() => {
vi.unstubAllEnvs()
vi.restoreAllMocks()
})
describe('isAnalyticsEnabled', () => {
it('returns false when NEXT_PUBLIC_SELF_HOSTED is true, even with a token', () => {
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'true')
vi.stubEnv('NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN', 'phc_test')
expect(isAnalyticsEnabled()).toBe(false)
})
it('returns true when a token is set and not self-hosted', () => {
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'false')
vi.stubEnv('NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN', 'phc_test')
expect(isAnalyticsEnabled()).toBe(true)
})
it('returns false when the token is unset', () => {
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'false')
vi.stubEnv('NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN', '')
expect(isAnalyticsEnabled()).toBe(false)
})
})
describe('warnIfAnalyticsMisconfigured', () => {
it('returns true and stays quiet when configured', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'false')
vi.stubEnv('NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN', 'phc_test')
expect(warnIfAnalyticsMisconfigured()).toBe(true)
expect(warn).not.toHaveBeenCalled()
})
it('warns in development when the token is missing', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'false')
vi.stubEnv('NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN', '')
vi.stubEnv('NODE_ENV', 'development')
expect(warnIfAnalyticsMisconfigured()).toBe(false)
expect(warn).toHaveBeenCalledWith(expect.stringContaining(POSTHOG_TOKEN_VAR))
})
it('stays silent in production when the token is missing', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'false')
vi.stubEnv('NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN', '')
vi.stubEnv('NODE_ENV', 'production')
expect(warnIfAnalyticsMisconfigured()).toBe(false)
expect(warn).not.toHaveBeenCalled()
})
it('stays silent on self-hosted: off is deliberate, not a misconfiguration', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'true')
vi.stubEnv('NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN', '')
vi.stubEnv('NODE_ENV', 'development')
expect(warnIfAnalyticsMisconfigured()).toBe(false)
expect(warn).not.toHaveBeenCalled()
})
})
})