Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.4 KB
Raw Blame
import { Page } from '@playwright/test'

export function decideResponse(featureFlags: Record<string, any>): Record<string, any> {
    return {
        config: {
            enable_collect_everything: true,
        },
        toolbarParams: {
            toolbarVersion: 'toolbar',
        },
        isAuthenticated: true,
        supportedCompression: ['gzip', 'gzip-js', 'lz64'],
        hasFeatureFlags: Object.keys(featureFlags).length > 0,
        featureFlags,
        sessionRecording: {
            endpoint: '/s/',
        },
    }
}

export const mockFeatureFlags = async (page: Page, overrides: Record<string, any>): Promise<void> => {
    // Tricky - the new RemoteConfig endpoint is optimised to not load decide if there are no feature flags in the DB.
    // We need to intercept both the RemoteConfig and the decide endpoint to ensure that the feature flags are always loaded.

    await page.route('**/array/*/config', async (route) => {
        await route.fulfill({
            status: 200,
            body: JSON.stringify({
                ...decideResponse(overrides),
            }),
        })
    })

    await page.route('**/array/*/config.js', async (route) => {
        await route.continue()
    })

    await page.route('**/flags/*', async (route) => {
        await route.fulfill({
            status: 200,
            body: JSON.stringify({
                ...decideResponse(overrides),
            }),
        })
    })
}