Files
supabase/apps/studio/lib/posthog.test.ts
Pamela Chia 0208c0b242 feat(studio): send the platform marker on flag tracking calls (#53165)
I changed Studio's feature-flag tracking call to send `platform:
'studio'`, so `$feature_flag_called` events captured by the management
API carry the same app marker as Studio's other events (#53078). I held
this step back until the API's strict flag-track body schema accepted
the field in production, which it has since 2026-11-09.

**Note:** I regenerated `packages/api-types` with a full `codegen:prod`
run, so it also includes other recent production API additions. The
`verify-production-api-types` check diffs every type file against
production, so a single-type update would fail it.

## To test

Accept the cookie banner first: flag tracking only fires after consent.

Tested on the PR preview:
- [x] Sign in to the studio-staging preview, open any project page, and
find the `POST /platform/telemetry/feature-flags/track` request in
DevTools: expect the body to include `"platform":"studio"` and a 2xx
response (a 400 means the field was rejected). Observed
`{"feature_flag_name":"proBenefitWording","feature_flag_value":"variant-a","platform":"studio"}`
with a 201, and every `/platform/telemetry/feature-flags` GET returned
200.

## Linear
- fixes GROWTH-1461
2026-11-10 01:13:50 +09:00

60 lines
1.9 KiB
TypeScript

import * as common from 'common'
import { describe, expect, it, vi } from 'vitest'
import * as constants from './constants'
import { trackFeatureFlag } from './posthog'
import * as fetchers from '@/data/fetchers'
vi.mock('@/data/fetchers', () => ({
post: vi.fn(),
handleError: vi.fn(),
}))
vi.mock('common', () => ({
hasConsented: vi.fn(),
LOCAL_STORAGE_KEYS: {},
}))
vi.mock('./constants', () => ({
IS_PLATFORM: true,
}))
describe('trackFeatureFlag', () => {
it('returns undefined if user has not consented', async () => {
vi.spyOn(common, 'hasConsented').mockReturnValue(false)
const result = await trackFeatureFlag({ some: 'value' } as any)
expect(result).toBeUndefined()
})
it('returns undefined if not on platform', async () => {
vi.spyOn(common, 'hasConsented').mockReturnValue(true)
vi.spyOn(constants, 'IS_PLATFORM', 'get').mockReturnValue(false)
const result = await trackFeatureFlag({ some: 'value' } as any)
expect(result).toBeUndefined()
})
it('calls post with correct body if consented and on platform', async () => {
vi.spyOn(common, 'hasConsented').mockReturnValue(true)
vi.spyOn(constants, 'IS_PLATFORM', 'get').mockReturnValue(true)
vi.spyOn(fetchers, 'post').mockResolvedValue({ data: 'success' })
const result = await trackFeatureFlag({ foo: 'bar' } as any)
expect(fetchers.post).toHaveBeenCalledWith('/platform/telemetry/feature-flags/track', {
body: { foo: 'bar', platform: 'studio' },
})
expect(result).toBe('success')
})
it('calls handleError if post returns error', async () => {
vi.spyOn(common, 'hasConsented').mockReturnValue(true)
vi.spyOn(constants, 'IS_PLATFORM', 'get').mockReturnValue(true)
vi.spyOn(fetchers, 'post').mockResolvedValue({ error: { message: 'fail' } })
await trackFeatureFlag({ foo: 'bar' } as any)
expect(fetchers.handleError).toHaveBeenCalledWith(
{ message: 'fail' },
{ reportImmediately: true }
)
})
})