: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { expectLogic } from 'kea-test-utils'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { preflightLogic } from 'lib/logic/preflightLogic'
import { initKeaTests } from '~/test/init'
import { AppContext } from '~/types'
import { aiShellLogic } from './aiShellLogic'
describe('aiShellLogic', () => {
let logic: ReturnType<typeof aiShellLogic.build>
beforeEach(() => {
localStorage.clear()
initKeaTests()
logic = aiShellLogic()
logic.mount()
})
afterEach(() => {
logic.unmount()
localStorage.clear()
})
describe('isMaxAvailable selector', () => {
it.each([
{ realm: 'a not-yet-loaded preflight', preflight: null, expected: true },
{ realm: 'PostHog Cloud', preflight: { cloud: true }, expected: true },
{
realm: 'self-hosted with an Anthropic key',
preflight: { cloud: false, is_debug: false, anthropic_available: true },
expected: true,
},
{
realm: 'self-hosted without a key',
preflight: { cloud: false, is_debug: false, anthropic_available: false },
expected: false,
},
])('is $expected on $realm', async ({ preflight, expected }) => {
preflightLogic.actions.loadPreflightSuccess(preflight as any)
await expectLogic(logic).toMatchValues({ isMaxAvailable: expected })
})
})
// The flag is the only thing that may expose the new posthog_ai surface — without the gate a stored
// preference (or a missing default) would leak it to every user. Lock in: flag off collapses to legacy
// regardless of the stored mode; flag on passes the stored mode through. When the server sends its
// access decision, that decision replaces the flag: a denied user gets classic PostHog AI.
describe('effectivePhaiView selector', () => {
it.each([
{ flagOn: false, gate: undefined, mode: 'new', expected: 'legacy' },
{ flagOn: false, gate: undefined, mode: 'legacy', expected: 'legacy' },
{ flagOn: true, gate: undefined, mode: 'new', expected: 'new' },
{ flagOn: true, gate: undefined, mode: 'legacy', expected: 'legacy' },
{ flagOn: true, gate: false, mode: 'new', expected: 'legacy' },
{ flagOn: false, gate: true, mode: 'new', expected: 'new' },
] as const)(
'is $expected when sandbox flag is $flagOn, access gate is $gate and stored mode is $mode',
async ({ flagOn, gate, mode, expected }) => {
const originalAppContext = window.POSTHOG_APP_CONTEXT
window.POSTHOG_APP_CONTEXT = { ...originalAppContext, posthog_ai_access: gate } as AppContext
try {
featureFlagLogic.actions.setFeatureFlags(
flagOn ? [FEATURE_FLAGS.PHAI_SANDBOX_MODE] : [],
flagOn ? { [FEATURE_FLAGS.PHAI_SANDBOX_MODE]: true } : {}
)
logic.actions.setPhaiViewMode(mode)
await expectLogic(logic).toMatchValues({ effectivePhaiView: expected })
} finally {
window.POSTHOG_APP_CONTEXT = originalAppContext
}
}
)
})
// Users already chose a view under this key. A renamed key, or a second persisted reducer that wins
// the race, would silently reset that choice.
it.each(['new', 'legacy'] as const)('restores the view mode %s saved under the existing key', (mode) => {
logic.unmount()
initKeaTests()
localStorage.setItem('posthog_ai_view_mode', JSON.stringify(mode))
logic = aiShellLogic()
logic.mount()
expect(logic.values.phaiViewMode).toBe(mode)
})
})