Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

369 lines
14 KiB
TypeScript

import { expectLogic } from 'kea-test-utils'
import api from 'lib/api'
import { insightDataLogic } from 'scenes/insights/insightDataLogic'
import { createEmptyInsight, insightLogic } from 'scenes/insights/insightLogic'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { NodeKind } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
import { ChartDisplayType, InsightsFunctionType, InsightLogicProps, InsightShortId } from '~/types'
import type { AlertType } from '../types'
import {
alertsUnsupportedReason,
areAlertsSupportedForInsight,
areAnomalyAlertsSupportedForInsight,
insightAlertsLogic,
} from './insightAlertsLogic'
const Insight42 = '42' as InsightShortId
const API_QUERY = {
kind: NodeKind.InsightVizNode,
source: {
kind: NodeKind.TrendsQuery,
series: [{ kind: NodeKind.EventsNode, event: '$pageview', math: 'total' }],
trendsFilter: { display: 'ActionsLineGraph' },
},
}
const FUNNEL_QUERY = {
kind: NodeKind.InsightVizNode,
source: {
kind: NodeKind.FunnelsQuery,
series: [{ kind: NodeKind.EventsNode, event: '$pageview' }],
},
}
// Metrics insights persist their query as a bare MetricsQuery node — no InsightVizNode wrapper.
const METRICS_QUERY = {
kind: NodeKind.MetricsQuery,
clauses: [{ name: 'a', metricName: 'queue.depth', aggregation: 'avg' }],
}
describe('insightAlertsLogic', () => {
let listSpy: jest.SpyInstance
let insightsFunctionsListSpy: jest.SpyInstance
beforeEach(() => {
initKeaTests()
listSpy = jest.spyOn(api.alerts, 'list').mockResolvedValue({ results: [], count: 0 })
insightsFunctionsListSpy = jest.spyOn(api.insightsFunctions, 'list').mockResolvedValue({ results: [], count: 0 })
})
afterEach(() => {
listSpy.mockRestore()
insightsFunctionsListSpy.mockRestore()
})
function mountInsightStack(insightLogicProps: InsightLogicProps): void {
insightLogic(insightLogicProps).mount()
insightDataLogic(insightLogicProps).mount()
insightVizDataLogic(insightLogicProps).mount()
}
it('does not hydrate from empty insight.alerts or fetch on mount when deferInitialAlertsLoad is true', async () => {
const insightLogicProps: InsightLogicProps = {
dashboardItemId: Insight42,
dashboardId: 1,
cachedInsight: {
...createEmptyInsight(Insight42),
id: 42,
query: API_QUERY,
alerts: [],
},
}
mountInsightStack(insightLogicProps)
const alertsLogic = insightAlertsLogic({
insightId: 42,
insightLogicProps,
deferInitialAlertsLoad: true,
})
alertsLogic.mount()
await expectLogic(alertsLogic).toMatchValues({ alerts: [] })
await expectLogic(alertsLogic).toNotHaveDispatchedActions(['loadAlerts', 'loadAlertsSuccess'])
expect(listSpy).not.toHaveBeenCalled()
})
it('calls the alerts API when loadAlerts runs after a deferred mount', async () => {
const insightLogicProps: InsightLogicProps = {
dashboardItemId: Insight42,
dashboardId: 1,
cachedInsight: {
...createEmptyInsight(Insight42),
id: 42,
query: API_QUERY,
alerts: [],
},
}
mountInsightStack(insightLogicProps)
const alertsLogic = insightAlertsLogic({
insightId: 42,
insightLogicProps,
deferInitialAlertsLoad: true,
})
alertsLogic.mount()
await expectLogic(alertsLogic, () => {
alertsLogic.actions.loadAlerts()
}).toFinishAllListeners()
expect(listSpy).toHaveBeenCalledWith(42)
})
it('dispatches loadAlerts on mount when not deferred and cached insight has no alerts field', async () => {
const insightLogicProps: InsightLogicProps = {
dashboardItemId: Insight42,
dashboardId: 1,
cachedInsight: {
...createEmptyInsight(Insight42),
id: 42,
query: API_QUERY,
},
}
mountInsightStack(insightLogicProps)
const alertsLogic = insightAlertsLogic({
insightId: 42,
insightLogicProps,
})
alertsLogic.mount()
await expectLogic(alertsLogic).toFinishAllListeners()
expect(listSpy).toHaveBeenCalledWith(42)
})
it('uses loadAlertsSuccess from insight.alerts on mount when not deferred and does not call the API for an empty array', async () => {
const insightLogicProps: InsightLogicProps = {
dashboardItemId: Insight42,
dashboardId: 1,
cachedInsight: {
...createEmptyInsight(Insight42),
id: 42,
query: API_QUERY,
alerts: [],
},
}
mountInsightStack(insightLogicProps)
const alertsLogic = insightAlertsLogic({
insightId: 42,
insightLogicProps,
})
alertsLogic.mount()
await expectLogic(alertsLogic).toDispatchActions(['loadAlertsSuccess'])
await expectLogic(alertsLogic).toMatchValues({ alerts: [] })
expect(listSpy).not.toHaveBeenCalled()
})
it('upsertAlert appends then replaces by id', async () => {
const insightLogicProps: InsightLogicProps = {
dashboardItemId: Insight42,
dashboardId: 1,
cachedInsight: {
...createEmptyInsight(Insight42),
id: 42,
query: API_QUERY,
alerts: [],
},
}
mountInsightStack(insightLogicProps)
const alertsLogic = insightAlertsLogic({
insightId: 42,
insightLogicProps,
deferInitialAlertsLoad: true,
})
alertsLogic.mount()
const first = { id: 'alert-a', name: 'First' } as AlertType
const updated = { id: 'alert-a', name: 'Updated' } as AlertType
await expectLogic(alertsLogic, () => {
alertsLogic.actions.upsertAlert(first)
alertsLogic.actions.upsertAlert({ id: 'alert-b', name: 'Other' } as AlertType)
alertsLogic.actions.upsertAlert(updated)
}).toMatchValues({
alerts: [updated, expect.objectContaining({ id: 'alert-b', name: 'Other' })],
})
})
it('removeAlert drops the matching alert id', async () => {
const insightLogicProps: InsightLogicProps = {
dashboardItemId: Insight42,
dashboardId: 1,
cachedInsight: {
...createEmptyInsight(Insight42),
id: 42,
query: API_QUERY,
alerts: [],
},
}
mountInsightStack(insightLogicProps)
const alertsLogic = insightAlertsLogic({
insightId: 42,
insightLogicProps,
deferInitialAlertsLoad: true,
})
alertsLogic.mount()
await expectLogic(alertsLogic, () => {
alertsLogic.actions.upsertAlert({ id: 'keep', name: 'K' } as AlertType)
alertsLogic.actions.upsertAlert({ id: 'drop', name: 'D' } as AlertType)
alertsLogic.actions.removeAlert('drop')
}).toMatchValues({
alerts: [expect.objectContaining({ id: 'keep' })],
})
})
it('loads destination counts for all alerts in one request', async () => {
const insightLogicProps: InsightLogicProps = {
dashboardItemId: Insight42,
dashboardId: 1,
cachedInsight: { ...createEmptyInsight(Insight42), id: 42, query: API_QUERY, alerts: [] },
}
mountInsightStack(insightLogicProps)
insightsFunctionsListSpy.mockResolvedValue({
count: 3,
results: [
{ filters: { properties: [{ key: 'alert_id', value: 'alert-a' }] } },
{ filters: { properties: [{ key: 'alert_id', value: 'alert-a' }] } },
{ filters: { properties: [{ key: 'alert_id', value: 'alert-b' }] } },
] as InsightsFunctionType[],
})
const alertsLogic = insightAlertsLogic({ insightId: 42, insightLogicProps, deferInitialAlertsLoad: true })
alertsLogic.mount()
const alerts = [{ id: 'alert-a' }, { id: 'alert-b' }] as AlertType[]
await expectLogic(alertsLogic, () => alertsLogic.actions.loadAlertsSuccess(alerts))
.toFinishAllListeners()
.toMatchValues({ alertDestinationCounts: { 'alert-a': 2, 'alert-b': 1 } })
expect(insightsFunctionsListSpy).toHaveBeenCalledTimes(1)
})
it('refreshes destination counts after an alert is saved', async () => {
const insightLogicProps: InsightLogicProps = {
dashboardItemId: Insight42,
dashboardId: 1,
cachedInsight: { ...createEmptyInsight(Insight42), id: 42, query: API_QUERY, alerts: [] },
}
mountInsightStack(insightLogicProps)
insightsFunctionsListSpy.mockResolvedValue({
count: 1,
results: [{ filters: { properties: [{ key: 'alert_id', value: 'alert-a' }] } }] as InsightsFunctionType[],
})
const alertsLogic = insightAlertsLogic({ insightId: 42, insightLogicProps, deferInitialAlertsLoad: true })
alertsLogic.mount()
await expectLogic(alertsLogic, () => alertsLogic.actions.upsertAlert({ id: 'alert-a' } as AlertType))
.toFinishAllListeners()
.toMatchValues({ alertDestinationCounts: { 'alert-a': 1 } })
expect(insightsFunctionsListSpy).toHaveBeenCalledTimes(1)
})
})
describe('areAlertsSupportedForInsight', () => {
it('returns false when query is null or undefined', () => {
expect(areAlertsSupportedForInsight(null)).toBe(false)
expect(areAlertsSupportedForInsight(undefined)).toBe(false)
})
it.each([
['with trendsFilter', API_QUERY],
[
'without trendsFilter',
{
...API_QUERY,
source: {
...API_QUERY.source,
trendsFilter: undefined,
},
},
],
[
'with null trendsFilter',
{
...API_QUERY,
source: {
...API_QUERY.source,
trendsFilter: null,
},
},
],
])('returns true for trends insight viz %s', (_name, query) => {
expect(areAlertsSupportedForInsight(query)).toBe(true)
})
it('supports funnel insight viz by default', () => {
expect(areAlertsSupportedForInsight(FUNNEL_QUERY)).toBe(true)
})
it('supports bare metrics query nodes only when metricsAlertsEnabled', () => {
expect(areAlertsSupportedForInsight(METRICS_QUERY, { metricsAlertsEnabled: true })).toBe(true)
expect(areAlertsSupportedForInsight(METRICS_QUERY)).toBe(false)
})
it('supports steps and trends funnels but not time-to-convert or flow', () => {
const withViz = (funnelVizType?: string): Record<string, any> => ({
...FUNNEL_QUERY,
source: { ...FUNNEL_QUERY.source, funnelsFilter: { funnelVizType } },
})
expect(areAlertsSupportedForInsight(withViz('steps'))).toBe(true)
expect(areAlertsSupportedForInsight(withViz('trends'))).toBe(true)
expect(areAlertsSupportedForInsight(withViz('time_to_convert'))).toBe(false)
expect(areAlertsSupportedForInsight(withViz('flow'))).toBe(false)
})
it.each<[string, ChartDisplayType | undefined, boolean]>([
['line graph', ChartDisplayType.ActionsLineGraph, true],
['default display', undefined, true],
['bold number', ChartDisplayType.BoldNumber, false],
['bar value', ChartDisplayType.ActionsBarValue, false],
])('reports anomaly support for %s trends', (_name, display, expected) => {
const query = {
...API_QUERY,
source: {
...API_QUERY.source,
trendsFilter: display ? { display } : null,
},
}
expect(areAnomalyAlertsSupportedForInsight(query)).toBe(expected)
})
})
describe('alertsUnsupportedReason', () => {
it.each([
['metrics gated off', {}, ['trends', 'SQL', 'funnel'], ['metrics']],
['metrics enabled', { metricsAlertsEnabled: true }, ['trends', 'SQL', 'funnel', 'metrics'], []],
])('always lists trends/SQL/funnel and gates only metrics: %s', (_name, options, included, excluded) => {
const reason = alertsUnsupportedReason(options)
expect(included.every((type) => reason.includes(type))).toBe(true)
expect(excluded.every((type) => !reason.includes(type))).toBe(true)
})
// A time-to-convert/flow funnel is itself a funnel, so the generic "funnel insights are supported"
// copy reads as a contradiction; the reason should name the real cause instead.
it('gives a funnel-viz-specific reason for time-to-convert / flow funnels', () => {
const funnelWithViz = (funnelVizType: string): Record<string, any> => ({
...FUNNEL_QUERY,
source: { ...FUNNEL_QUERY.source, funnelsFilter: { funnelVizType } },
})
for (const viz of ['time_to_convert', 'flow']) {
const reason = alertsUnsupportedReason({}, funnelWithViz(viz))
expect(reason).toContain('conversion rate')
expect(reason).toContain('steps or trends')
expect(reason).not.toContain('only available for')
}
// No query → backward-compatible generic copy.
expect(alertsUnsupportedReason({})).toContain('only available for')
})
})