: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 { resumeKeaLoadersErrors, silenceKeaLoadersErrors } from '~/initKea'
import { initKeaTests } from '~/test/init'
import { FilterLogicalOperator, PropertyFilterType, PropertyOperator, UniversalFiltersGroup } from '~/types'
import { logsViewerConfigLogic } from 'products/logs/frontend/components/LogsViewer/config/logsViewerConfigLogic'
import {
SERVICE_NAME_FILTER,
SEVERITY_LEVEL_FILTER,
facetSelection,
} from 'products/logs/frontend/components/LogsViewer/FacetRail/facetFilters'
import { logsViewerFiltersLogic } from 'products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic'
import { logsPatternsCreate, logsPatternsDiffCreate } from 'products/logs/frontend/generated/api'
import type {
_LogsPatternsDiffResponseApi,
_LogsPatternsResponseApi,
} from 'products/logs/frontend/generated/api.schemas'
import { logsPatternsLogic } from './logsPatternsLogic'
jest.mock('products/logs/frontend/generated/api', () => ({
__esModule: true,
logsPatternsCreate: jest.fn(),
logsPatternsDiffCreate: jest.fn(),
}))
const mockCreate = logsPatternsCreate as jest.MockedFunction<typeof logsPatternsCreate>
const mockDiffCreate = logsPatternsDiffCreate as jest.MockedFunction<typeof logsPatternsDiffCreate>
const ID = 'test-viewer'
const RESPONSE: _LogsPatternsResponseApi = {
patterns: [
{
pattern: 'User <*> not found',
count: 3,
estimated_count: 3,
volume_share_pct: 75,
error_count: 3,
estimated_error_count: 3,
first_seen: '2026-06-23T12:00:00+00:00',
last_seen: '2026-06-23T12:05:00+00:00',
examples: [],
services: ['auth', 'api'],
sparkline: [1, 2],
severity_counts: { error: 2, warn: 1 },
match_regex: '^\\s*User\\s+\\S+\\s+not\\s+found\\s*$',
// The miner's `extract_match_literal` returns the longest run between `<*>` holes —
// for 'User <*> not found' that's 'not found', not 'User'. Keep the fixture faithful.
match_literal: 'not found',
},
],
scanned_count: 3,
total_count: 3,
sampled: false,
sample_coverage_pct: 100,
sparkline_buckets: [
{ start: '2026-06-23T12:00:00+00:00', end: '2026-06-23T12:30:00+00:00' },
{ start: '2026-06-23T12:30:00+00:00', end: '2026-06-23T13:00:00+00:00' },
],
}
const DIFF_RESPONSE: _LogsPatternsDiffResponseApi = {
entries: [
{
classification: 'new',
rate_ratio: null,
pattern: RESPONSE.patterns[0],
baseline_estimated_count: null,
baseline_volume_share_pct: null,
masked_example: 'GET <*> returned <N>',
services: [],
first_seen_ever: null,
},
],
current: {
scanned_count: 3,
total_count: 3,
sampled: false,
sample_coverage_pct: 100,
date_from: '2026-06-23T12:00:00+00:00',
date_to: '2026-06-23T13:00:00+00:00',
},
baseline: {
scanned_count: 0,
total_count: 0,
sampled: false,
sample_coverage_pct: 100,
date_from: '2026-06-16T12:00:00+00:00',
date_to: '2026-06-16T13:00:00+00:00',
},
window: {
current: { from: '2026-06-23T12:00:00Z', to: '2026-06-23T13:00:00Z' },
baseline: { from: '2026-06-16T12:00:00Z', to: '2026-06-16T13:00:00Z' },
timezone: 'UTC',
},
source: 'body_mining',
pattern_version: null,
sampled: false,
truncated: false,
notes: [],
next: [],
}
describe('logsPatternsLogic', () => {
afterEach(resumeKeaLoadersErrors)
let logic: ReturnType<typeof logsPatternsLogic.build>
let filtersLogic: ReturnType<typeof logsViewerFiltersLogic.build>
const selectedLevels = (): string[] =>
facetSelection(filtersLogic.values.filters.filterGroup, SEVERITY_LEVEL_FILTER).included
const selectedServices = (): string[] =>
facetSelection(filtersLogic.values.filters.filterGroup, SERVICE_NAME_FILTER).included
beforeEach(() => {
initKeaTests()
jest.clearAllMocks()
mockCreate.mockResolvedValue(RESPONSE)
filtersLogic = logsViewerFiltersLogic({ id: ID })
filtersLogic.mount()
logic = logsPatternsLogic({ id: ID })
})
it('loads patterns on mount from the shared viewer filters', async () => {
logic.mount()
await expectLogic(logic)
.toDispatchActions(['loadPatterns', 'loadPatternsSuccess'])
.toMatchValues({ patterns: RESPONSE.patterns })
expect(mockCreate).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({ query: expect.objectContaining({ filterGroup: expect.anything() }) })
)
})
it('viewMatchingLogs writes a visible message filter and switches to the Logs view', async () => {
// The pivot's contract: the predicate must land in the shared, user-visible filterGroup
// (removable like any filter, never hidden state) and the viewer must leave Patterns mode.
const configLogic = logsViewerConfigLogic({ id: ID })
configLogic.mount()
configLogic.actions.setViewMode('patterns')
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatternsSuccess'])
logic.actions.viewMatchingLogs(RESPONSE.patterns[0])
const inner = filtersLogic.values.filters.filterGroup.values[0] as UniversalFiltersGroup
expect(inner.values).toContainEqual(
expect.objectContaining({
key: 'message',
operator: PropertyOperator.Regex,
type: PropertyFilterType.Log,
value: RESPONSE.patterns[0].match_regex,
})
)
// The pivot also scopes by everything the sample saw — service_name and severity
// prune the scan the body regex can't, and multi-value IN filters narrow just as well.
expect(selectedServices()).toEqual(['auth', 'api'])
expect(selectedLevels()).toEqual(['error', 'warn'])
expect(configLogic.values.viewMode).toBe('logs')
// A filter that could silently exclude matching lines must be withheld: a services
// list at the miner's cap may be truncated, and a non-canonical severity ("notice")
// can't be expressed as a severity filter. Both leave the previous values untouched.
logic.actions.viewMatchingLogs({
...RESPONSE.patterns[0],
services: ['auth', 'api', 'db', 'kafka'],
severity_counts: { error: 2, notice: 1 },
})
expect(selectedServices()).toEqual(['auth', 'api'])
expect(selectedLevels()).toEqual(['error', 'warn'])
})
it('viewMatchingLogs falls back to an icontains literal filter when the regex was withheld', async () => {
// Validation can withhold the regex (match_regex: null); the pivot must still scope the
// Logs view using the template's longest literal run with icontains, not a regex match.
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatternsSuccess'])
logic.actions.viewMatchingLogs({ ...RESPONSE.patterns[0], match_regex: null })
const inner = filtersLogic.values.filters.filterGroup.values[0] as UniversalFiltersGroup
expect(inner.values).toContainEqual(
expect.objectContaining({
key: 'message',
operator: PropertyOperator.IContains,
type: PropertyFilterType.Log,
value: RESPONSE.patterns[0].match_literal,
})
)
})
it('viewMatchingLogs matches canonical members and version without narrowing by example facets', async () => {
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatternsSuccess'])
logic.actions.viewMatchingLogs({
...RESPONSE.patterns[0],
match_patterns: ['User <N> not found', 'User <UUID> not found'],
pattern_version: 3,
match_regex: null,
match_literal: null,
})
const inner = filtersLogic.values.filters.filterGroup.values[0] as UniversalFiltersGroup
expect(inner.values).toEqual(
expect.arrayContaining([
{
key: 'pattern',
value: ['User <N> not found', 'User <UUID> not found'],
operator: PropertyOperator.Exact,
type: PropertyFilterType.Log,
},
{ key: 'pattern_version', value: 3, operator: PropertyOperator.Exact, type: PropertyFilterType.Log },
])
)
expect(selectedServices()).toEqual([])
expect(selectedLevels()).toEqual([])
})
it('compare mode diffs with the same query body and switches loaders for filter reloads', async () => {
// The diff must scope its windows with exactly the filters a plain mine uses — if the
// two request bodies drift apart, compare mode silently answers a different question
// than the table next to it. And once compare is on, filter changes must reload the
// diff, not the plain mine.
mockDiffCreate.mockResolvedValue(DIFF_RESPONSE)
const configLogic = logsViewerConfigLogic({ id: ID })
configLogic.mount()
configLogic.actions.setViewMode('patterns')
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatternsSuccess'])
const minedQuery = mockCreate.mock.calls[0][1].query
await expectLogic(logic, () => {
logic.actions.setCompareEnabled(true)
}).toDispatchActions(['loadDiff', 'loadDiffSuccess'])
expect(mockDiffCreate).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ query: minedQuery }))
expect(logic.values.diffResponse).toEqual(DIFF_RESPONSE)
mockDiffCreate.mockClear()
await expectLogic(logic, () => {
filtersLogic.actions.setFilters({ serviceNames: ['auth'] })
}).toDispatchActions(['loadDiffSuccess'])
expect(mockDiffCreate).toHaveBeenCalledTimes(1)
expect(mockCreate).toHaveBeenCalledTimes(1) // only the original mount load
})
it('defaults to the preceding baseline: the same-length window ending where the current one starts', async () => {
// Locks the window arithmetic: a drift here (wrong length, overlap, or gap) makes the
// "vs. preceding period" comparison silently wrong while still returning results.
mockDiffCreate.mockResolvedValue(DIFF_RESPONSE)
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatternsSuccess'])
filtersLogic.actions.setDateRange({
date_from: '2026-06-23T12:00:00.000Z',
date_to: '2026-06-23T13:00:00.000Z',
})
logic.actions.setCompareEnabled(true)
await expectLogic(logic).toDispatchActions(['loadDiffSuccess'])
expect(mockDiffCreate).toHaveBeenLastCalledWith(
expect.any(String),
expect.objectContaining({
baselineDateRange: {
date_from: '2026-06-23T11:00:00.000Z',
date_to: '2026-06-23T12:00:00.000Z',
},
})
)
logic.actions.setBaselineMode('lastWeek')
await expectLogic(logic).toDispatchActions(['loadDiffSuccess'])
expect(mockDiffCreate).toHaveBeenLastCalledWith(
expect.any(String),
expect.objectContaining({ baselineDateRange: undefined }) // the backend applies the -7d default
)
})
it('compare armed before mount makes mounting load the diff, not the plain mine', async () => {
// Compare state lives on the config logic and survives Logs↔Patterns lens switches,
// while this logic unmounts when leaving the lens. Re-entering Patterns with compare
// still on must resume the diff — an afterMount that always ran the plain mine would
// silently show the wrong table.
mockDiffCreate.mockResolvedValue(DIFF_RESPONSE)
const configLogic = logsViewerConfigLogic({ id: ID })
configLogic.mount()
configLogic.actions.setCompareEnabled(true)
configLogic.actions.setBaselineMode('lastWeek')
configLogic.actions.setViewMode('patterns')
logic.mount()
await expectLogic(logic).toDispatchActions(['loadDiff', 'loadDiffSuccess'])
expect(mockCreate).not.toHaveBeenCalled()
expect(logic.values.baselineMode).toBe('lastWeek')
})
it('surfaces a load failure as patternsError and clears it on the next success', async () => {
silenceKeaLoadersErrors()
// A failed mine (e.g. sampling query over budget) must not render as "no patterns".
mockCreate.mockRejectedValueOnce(new Error('estimated query execution time is too long'))
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatterns', 'loadPatternsFailure'])
expect(logic.values.patternsError).toBeTruthy()
await expectLogic(logic, () => {
logic.actions.loadPatterns()
}).toDispatchActions(['loadPatternsSuccess'])
expect(logic.values.patternsError).toBeNull()
})
it('a diff failure does not surface as an error once compare is toggled off', async () => {
silenceKeaLoadersErrors()
// The two loaders run independently: toggling compare off fires a plain mine without
// cancelling an in-flight diff. If that stale diff fails afterwards, its error must not
// clobber the successful plain-mine table's "no patterns" empty state.
mockDiffCreate.mockRejectedValueOnce(new Error('estimated query execution time is too long'))
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatternsSuccess'])
logic.actions.setCompareEnabled(true)
await expectLogic(logic).toDispatchActions(['loadDiff', 'loadDiffFailure'])
expect(logic.values.patternsError).toBeTruthy()
logic.actions.setCompareEnabled(false)
await expectLogic(logic).toDispatchActions(['loadPatterns', 'loadPatternsSuccess'])
expect(logic.values.patternsError).toBeNull()
})
it('reloads when a shared filter changes', async () => {
// Mining only re-runs while Patterns is the active view (the logic is mounted only then).
const configLogic = logsViewerConfigLogic({ id: ID })
configLogic.mount()
configLogic.actions.setViewMode('patterns')
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatternsSuccess'])
mockCreate.mockClear()
await expectLogic(logic, () => {
filtersLogic.actions.setFilters({ severityLevels: ['error'] })
}).toDispatchActions(['setFilters', 'loadPatterns', 'loadPatternsSuccess'])
expect(mockCreate).toHaveBeenLastCalledWith(
expect.any(String),
expect.objectContaining({
query: expect.objectContaining({
filterGroup: expect.objectContaining({
values: [
expect.objectContaining({
values: [expect.objectContaining({ key: 'severity_level', value: ['error'] })],
}),
],
}),
}),
})
)
})
it('re-mines when the refresh button runs the query', async () => {
// The refresh button dispatches `bumpFacetRefresh` on the shared filters logic. Without
// wiring, Patterns kept the earlier mine while the logs table below it re-ran.
const configLogic = logsViewerConfigLogic({ id: ID })
configLogic.mount()
configLogic.actions.setViewMode('patterns')
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatternsSuccess'])
mockCreate.mockClear()
await expectLogic(logic, () => {
filtersLogic.actions.bumpFacetRefresh()
}).toDispatchActions(['loadPatterns', 'loadPatternsSuccess'])
expect(mockCreate).toHaveBeenCalledTimes(1)
})
it('ignores the refresh button while Patterns is not the active lens', async () => {
// The logic can stay mounted after a lens switch; a refresh from the Logs table must not
// fire a stray mine.
const configLogic = logsViewerConfigLogic({ id: ID })
configLogic.mount()
configLogic.actions.setViewMode('patterns')
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatternsSuccess'])
configLogic.actions.setViewMode('logs')
mockCreate.mockClear()
filtersLogic.actions.bumpFacetRefresh()
await expectLogic(logic).toFinishAllListeners()
expect(mockCreate).not.toHaveBeenCalled()
})
// The label format flips on a >= 24h window threshold (time-of-day vs date-prefixed) and
// early-returns on empty buckets — none of the above tests read `sparklineLabels`, so a
// flipped threshold or a broken dayjs format would otherwise go undetected.
const labelCases: [string, { start: string; end: string }[], string[]][] = [
['empty buckets yield no labels', [], []],
[
'a sub-day window uses time-of-day labels',
[
{ start: '2026-06-23T12:00:00+00:00', end: '2026-06-23T12:30:00+00:00' },
{ start: '2026-06-23T12:30:00+00:00', end: '2026-06-23T13:00:00+00:00' },
],
['12:00 – 12:30', '12:30 – 13:00'],
],
[
'a multi-day window prefixes the date',
[
{ start: '2026-06-23T00:00:00+00:00', end: '2026-06-24T00:00:00+00:00' },
{ start: '2026-06-24T00:00:00+00:00', end: '2026-06-25T00:00:00+00:00' },
],
['Jun 23 00:00 – Jun 24 00:00', 'Jun 24 00:00 – Jun 25 00:00'],
],
]
it.each(labelCases)('builds sparkline labels: %s', async (_name, sparkline_buckets, expected) => {
mockCreate.mockResolvedValue({ ...RESPONSE, sparkline_buckets })
logic.mount()
await expectLogic(logic).toDispatchActions(['loadPatternsSuccess'])
expect(logic.values.sparklineLabels).toEqual(expected)
})
it('scopes mining to the embedding viewer pinned filters', async () => {
// A scoped embedded viewer (e.g. person/trace logs) pins a filter so Patterns mode
// can't mine project-wide logs — assert it reaches the query via `queryFilterGroup`.
const pinnedFilters: UniversalFiltersGroup = {
type: FilterLogicalOperator.And,
values: [
{
key: 'distinct_id',
value: ['user-123'],
operator: PropertyOperator.Exact,
type: PropertyFilterType.LogAttribute,
},
],
}
const scopedFiltersLogic = logsViewerFiltersLogic({ id: 'scoped-viewer', pinnedFilters })
scopedFiltersLogic.mount()
const scopedLogic = logsPatternsLogic({ id: 'scoped-viewer' })
scopedLogic.mount()
await expectLogic(scopedLogic).toDispatchActions(['loadPatternsSuccess'])
expect(mockCreate).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
query: expect.objectContaining({
filterGroup: expect.objectContaining({
values: [expect.objectContaining({ values: pinnedFilters.values })],
}),
}),
})
)
})
})