: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 { urls } from 'scenes/urls'
import { initKeaTests } from '~/test/init'
import {
addHiddenSelectedConnectionOption,
connectionSelectorLogic,
getConnectionSelectorValue,
LOADING_CONNECTIONS,
POSTHOG_WAREHOUSE,
} from 'products/data_warehouse/frontend/shared/logics/connectionSelectorLogic'
import { externalDataSourcesConnectionsList } from 'products/warehouse_sources/frontend/generated/api'
import type { ExternalDataSourceConnectionOptionApi } from 'products/warehouse_sources/frontend/generated/api.schemas'
jest.mock('products/warehouse_sources/frontend/generated/api', () => ({
externalDataSourcesConnectionsList: jest.fn(),
}))
const mockConnectionsList = externalDataSourcesConnectionsList as jest.Mock
describe('connectionSelectorLogic', () => {
let logic: ReturnType<typeof connectionSelectorLogic.build>
beforeEach(() => {
initKeaTests()
mockConnectionsList.mockReset().mockResolvedValue([
{
id: 'conn-123',
prefix: 'warehouse',
engine: 'postgres',
source_type: 'Postgres',
access_method: 'direct',
supports_hogql: true,
is_builtin_managed_warehouse: false,
},
{
id: 'conn-456',
prefix: 'prod',
engine: null,
source_type: 'MySQL',
access_method: 'warehouse',
supports_hogql: true,
is_builtin_managed_warehouse: false,
},
{
id: 'conn-trino',
description: 'Managed Warehouse Trino',
prefix: 'trino',
engine: 'trino',
source_type: 'Trino',
access_method: 'direct',
supports_hogql: false,
is_builtin_managed_warehouse: false,
},
])
})
afterEach(() => {
logic?.unmount()
})
it('does not fetch on mount — embedded editors mount this logic without the selector', async () => {
logic = connectionSelectorLogic()
logic.mount()
await expectLogic(logic).toFinishAllListeners()
expect(mockConnectionsList).not.toHaveBeenCalled()
})
it('loads connection options when the selector requests them', async () => {
logic = connectionSelectorLogic()
logic.mount()
logic.actions.maybeLoadConnectionOptions()
await expectLogic(logic).toFinishAllListeners()
expect(mockConnectionsList).toHaveBeenCalledTimes(1)
expect(logic.values.connectionSelectOptions[0].options).toEqual(
expect.arrayContaining([
expect.objectContaining({ value: POSTHOG_WAREHOUSE }),
expect.objectContaining({
value: 'conn-123',
label: 'warehouse (Postgres)',
managementUrl: urls.dataWarehouseSource('managed-conn-123'),
}),
// Synced source: no detected engine — label derives from source_type + synced marker
expect.objectContaining({
value: 'conn-456',
label: 'prod (MySQL · synced)',
managementUrl: urls.dataWarehouseSource('managed-conn-456'),
}),
expect.objectContaining({
value: 'conn-trino',
label: 'Managed Warehouse Trino (Trino)',
managementUrl: urls.dataWarehouseSource('managed-conn-trino'),
}),
])
)
expect(logic.values.connectionSelectOptions[1].options).toEqual(
expect.arrayContaining([expect.not.objectContaining({ managementUrl: expect.anything() })])
)
})
it.each([false, true])(
'keeps the managed connection but hides its menu option with Trino enabled=%s',
async (enabled) => {
mockConnectionsList.mockResolvedValue([
{
id: 'conn-duck',
prefix: 'managed_warehouse',
engine: 'duckdb',
source_type: 'Postgres',
access_method: 'direct',
supports_hogql: true,
is_builtin_managed_warehouse: true,
},
])
logic = connectionSelectorLogic()
logic.mount()
featureFlagLogic.actions.setFeatureFlags(enabled ? [FEATURE_FLAGS.MANAGED_TRINO_QUERY] : [], {
[FEATURE_FLAGS.MANAGED_TRINO_QUERY]: enabled,
})
logic.actions.maybeLoadConnectionOptions()
await expectLogic(logic).toFinishAllListeners()
const [clickHouseOption, managedWarehouseOption] = logic.values.connectionSelectOptions[0].options
expect(clickHouseOption).toEqual(
expect.objectContaining({ value: POSTHOG_WAREHOUSE, label: 'PostHog (ClickHouse)' })
)
expect(managedWarehouseOption).toEqual(
expect.objectContaining({ value: 'conn-duck', label: 'PostHog (Managed warehouse)', hidden: enabled })
)
expect(managedWarehouseOption.iconSrc).toEqual(clickHouseOption.iconSrc)
expect(managedWarehouseOption).not.toHaveProperty('managementUrl')
expect(logic.values.connectionOptions).toContainEqual(expect.objectContaining({ id: 'conn-duck' }))
expect(getConnectionSelectorValue(false, 'conn-duck')).toEqual('conn-duck')
featureFlagLogic.actions.setFeatureFlags([], {})
expect(logic.values.connectionSelectOptions[0].options[1].hidden).toBe(false)
}
)
it('keeps the auto-provisioned Duckgres source external when the backend marks it external', async () => {
mockConnectionsList.mockResolvedValue([
{
id: 'conn-duck',
prefix: 'managed_warehouse',
description: 'Managed warehouse (auto-provisioned)',
engine: 'duckdb',
source_type: 'Postgres',
access_method: 'direct',
supports_hogql: true,
is_builtin_managed_warehouse: false,
},
])
logic = connectionSelectorLogic()
logic.mount()
featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.MANAGED_TRINO_QUERY], {
[FEATURE_FLAGS.MANAGED_TRINO_QUERY]: true,
})
logic.actions.maybeLoadConnectionOptions()
await expectLogic(logic).toFinishAllListeners()
const externalOption = logic.values.connectionSelectOptions[0].options[1]
expect(externalOption).toEqual(
expect.objectContaining({
value: 'conn-duck',
label: 'Managed warehouse (auto-provisioned) (DuckDB)',
managementUrl: urls.dataWarehouseSource('managed-conn-duck'),
})
)
expect(externalOption.hidden).toBe(false)
})
it('derives the selected connection value from sql editor state', async () => {
expect(getConnectionSelectorValue(true, undefined)).toEqual(LOADING_CONNECTIONS)
expect(getConnectionSelectorValue(false, 'conn-123')).toEqual('conn-123')
expect(getConnectionSelectorValue(false, 'missing')).toEqual('missing')
})
it.each([
['reader after the flag is disabled', 'reader-connection', 'legacy-connection'],
['legacy connection after the flag is enabled', 'legacy-connection', 'reader-connection'],
])('keeps a hidden selected %s instead of displaying ClickHouse', (_name, selectedId, visibleId) => {
const optionGroups = [
{
options: [
{ value: POSTHOG_WAREHOUSE, label: 'PostHog (ClickHouse)' },
{ value: visibleId, label: 'Visible managed warehouse' },
],
},
]
const connectionOptions = [{ id: visibleId }] as ExternalDataSourceConnectionOptionApi[]
const displayedOptions = addHiddenSelectedConnectionOption(optionGroups, connectionOptions, false, selectedId)
expect(getConnectionSelectorValue(false, selectedId)).toEqual(selectedId)
expect(displayedOptions[0].options).toContainEqual({
value: selectedId,
label: 'Selected connection (hidden)',
hidden: true,
})
})
})