Files
insights/products/alerts/frontend/logic/alertNotificationLogic.test.ts
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

239 lines
10 KiB
TypeScript

import { expectLogic } from 'kea-test-utils'
import api from 'lib/api'
import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { initKeaTests } from '~/test/init'
import { InsightsFunctionType, IntegrationType } from '~/types'
import { alertsTestDeliveryCreate } from 'products/alerts/frontend/generated/api'
import {
ALERT_NOTIFICATION_TYPE_DISCORD,
ALERT_NOTIFICATION_TYPE_MICROSOFT_TEAMS,
ALERT_NOTIFICATION_TYPE_SLACK,
ALERT_NOTIFICATION_TYPE_WEBHOOK,
} from 'products/alerts/frontend/logic/alertNotifications'
import { alertNotificationLogic } from './alertNotificationLogic'
jest.mock('products/alerts/frontend/generated/api', () => ({
alertsTestDeliveryCreate: jest.fn(),
}))
describe('alertNotificationLogic', () => {
let logic: ReturnType<typeof alertNotificationLogic.build>
let createSpy: jest.SpyInstance
let listSpy: jest.SpyInstance
beforeEach(() => {
initKeaTests()
listSpy = jest.spyOn(api.insightsFunctions, 'list').mockResolvedValue({ results: [], count: 0 })
createSpy = jest
.spyOn(api.insightsFunctions, 'create')
.mockImplementation(async (data) => ({ id: 'hf-1', ...data }) as InsightsFunctionType)
})
afterEach(() => {
logic?.unmount()
createSpy.mockRestore()
listSpy.mockRestore()
})
const makeSlackIntegration = (id: number): IntegrationType => ({
id,
kind: 'slack',
display_name: `Workspace ${id}`,
icon_url: '',
config: {},
created_at: '2026-01-01T00:00:00Z',
})
it('clears staged inputs when the destination type changes', async () => {
logic = alertNotificationLogic({ alertId: 'alert-123' })
logic.mount()
// A webhook URL entered for one destination must not carry over to another.
logic.actions.setWebhookUrl('https://discord.com/api/webhooks/123/abc')
await expectLogic(logic).toMatchValues({ webhookUrl: 'https://discord.com/api/webhooks/123/abc' })
logic.actions.setSelectedType(ALERT_NOTIFICATION_TYPE_MICROSOFT_TEAMS)
await expectLogic(logic).toMatchValues({ webhookUrl: '' })
// A staged Slack channel is cleared the same way.
logic.actions.setSlackChannelValue('C123|#general')
await expectLogic(logic).toMatchValues({ slackChannelValue: 'C123|#general' })
logic.actions.setSelectedSlackIntegrationId(2)
await expectLogic(logic).toMatchValues({ slackChannelValue: null })
logic.actions.setSlackChannelValue('C456|#alerts')
logic.actions.setSelectedType(ALERT_NOTIFICATION_TYPE_WEBHOOK)
await expectLogic(logic).toMatchValues({ slackChannelValue: null })
})
it('selects a Slack workspace that loaded before the alert logic mounted', async () => {
const workspace = makeSlackIntegration(1)
const integrationsListSpy = jest.spyOn(api.integrations, 'list').mockReturnValue(new Promise(() => {}))
const unmountIntegrations = integrationsLogic.mount()
integrationsLogic.actions.loadIntegrationsSuccess([workspace])
await expectLogic(integrationsLogic).toMatchValues({ slackIntegrations: [workspace] })
logic = alertNotificationLogic({ alertId: 'alert-123' })
logic.mount()
await expectLogic(logic).toMatchValues({ selectedSlackIntegration: workspace })
unmountIntegrations()
integrationsListSpy.mockRestore()
})
it('tracks integration loading failures until a retry succeeds', async () => {
logic = alertNotificationLogic({ alertId: 'alert-123' })
logic.mount()
logic.actions.loadIntegrationsFailure('Network error')
await expectLogic(logic).toMatchValues({ integrationsFailed: true })
logic.actions.loadIntegrations()
await expectLogic(logic).toMatchValues({ integrationsFailed: false })
logic.actions.loadIntegrationsSuccess([])
await expectLogic(logic).toMatchValues({ integrationsFailed: false })
})
it('sends a test delivery for the saved alert', async () => {
jest.mocked(alertsTestDeliveryCreate).mockResolvedValue({
destination_count: 2,
email_recipient_count: 1,
failed_delivery_channels: [],
})
logic = alertNotificationLogic({ alertId: 'alert-123' })
logic.mount()
logic.actions.sendTestDelivery()
await expectLogic(logic).toFinishAllListeners()
expect(alertsTestDeliveryCreate).toHaveBeenCalledWith('997', 'alert-123')
await expectLogic(logic).toMatchValues({
testDeliveryResult: { destination_count: 2, email_recipient_count: 1, failed_delivery_channels: [] },
})
})
it('clears the channel when an integrations refresh removes the selected workspace', async () => {
logic = alertNotificationLogic({ alertId: 'alert-123' })
logic.mount()
const firstWorkspace = makeSlackIntegration(1)
const secondWorkspace = makeSlackIntegration(2)
logic.actions.loadIntegrationsSuccess([firstWorkspace, secondWorkspace])
await expectLogic(logic).toMatchValues({ selectedSlackIntegrationId: 1 })
logic.actions.setSelectedSlackIntegrationId(2)
logic.actions.setSlackChannelValue('C123|#general')
logic.actions.loadIntegrationsSuccess([firstWorkspace])
await expectLogic(logic).toMatchValues({
selectedSlackIntegrationId: 1,
selectedSlackIntegration: firstWorkspace,
slackChannelValue: null,
})
})
it('drops staged Slack notifications for a workspace removed by an integrations refresh', async () => {
logic = alertNotificationLogic({ alertId: 'alert-123' })
logic.mount()
const firstWorkspace = makeSlackIntegration(1)
const secondWorkspace = makeSlackIntegration(2)
logic.actions.loadIntegrationsSuccess([firstWorkspace, secondWorkspace])
logic.actions.addPendingNotification({
type: ALERT_NOTIFICATION_TYPE_SLACK,
slackWorkspaceId: 2,
slackChannelId: 'C1',
slackChannelName: 'general',
})
logic.actions.addPendingNotification({
type: ALERT_NOTIFICATION_TYPE_WEBHOOK,
webhookUrl: 'https://example.com',
})
await expectLogic(logic).toMatchValues({ pendingNotifications: [expect.anything(), expect.anything()] })
logic.actions.loadIntegrationsSuccess([firstWorkspace])
// The staged Slack destination pointed at the now-removed workspace 2 is dropped;
// saving it would create a InsightsFunction referencing a dead integration. The webhook
// notification doesn't reference a workspace, so it's untouched.
await expectLogic(logic).toMatchValues({
pendingNotifications: [{ type: ALERT_NOTIFICATION_TYPE_WEBHOOK, webhookUrl: 'https://example.com' }],
})
})
it('creates a Discord destination InsightsFunction end-to-end', async () => {
logic = alertNotificationLogic({ alertId: 'alert-123' })
logic.mount()
// Drive the inline editor: pick Discord, enter a webhook URL, stage it
logic.actions.setSelectedType(ALERT_NOTIFICATION_TYPE_DISCORD)
logic.actions.setWebhookUrl('https://discord.com/api/webhooks/123/abc')
logic.actions.addPendingNotification({
type: ALERT_NOTIFICATION_TYPE_DISCORD,
webhookUrl: 'https://discord.com/api/webhooks/123/abc',
})
await expectLogic(logic).toMatchValues({
pendingNotifications: [
{ type: ALERT_NOTIFICATION_TYPE_DISCORD, webhookUrl: 'https://discord.com/api/webhooks/123/abc' },
],
})
// Saving the alert flushes pending notifications through the real payload builder + API
logic.actions.createPendingInsightsFunctions('alert-123', 'Daily revenue check')
await expectLogic(logic).toFinishAllListeners()
expect(createSpy).toHaveBeenCalledTimes(1)
const payload = createSpy.mock.calls[0][0]
expect(payload.template_id).toBe('template-discord')
expect(payload.name).toBe('Daily revenue check: Discord')
expect(payload.enabled).toBe(true)
expect(payload.inputs?.webhookUrl).toEqual({ value: 'https://discord.com/api/webhooks/123/abc' })
expect(payload.inputs?.content?.value).toContain('{event.properties.alert_name}')
await expectLogic(logic).toMatchValues({ pendingNotifications: [] })
})
it('creates a Microsoft Teams destination InsightsFunction end-to-end', async () => {
logic = alertNotificationLogic({ alertId: 'alert-123' })
logic.mount()
// Drive the inline editor: pick Microsoft Teams, enter a webhook URL, stage it
logic.actions.setSelectedType(ALERT_NOTIFICATION_TYPE_MICROSOFT_TEAMS)
logic.actions.setWebhookUrl('https://example.webhook.office.com/webhookb2/abc')
logic.actions.addPendingNotification({
type: ALERT_NOTIFICATION_TYPE_MICROSOFT_TEAMS,
webhookUrl: 'https://example.webhook.office.com/webhookb2/abc',
})
await expectLogic(logic).toMatchValues({
pendingNotifications: [
{
type: ALERT_NOTIFICATION_TYPE_MICROSOFT_TEAMS,
webhookUrl: 'https://example.webhook.office.com/webhookb2/abc',
},
],
})
// Saving the alert flushes pending notifications through the real payload builder + API
logic.actions.createPendingInsightsFunctions('alert-123', 'Daily revenue check')
await expectLogic(logic).toFinishAllListeners()
expect(createSpy).toHaveBeenCalledTimes(1)
const payload = createSpy.mock.calls[0][0]
expect(payload.template_id).toBe('template-microsoft-teams')
expect(payload.name).toBe('Daily revenue check: Microsoft Teams')
expect(payload.enabled).toBe(true)
expect(payload.inputs?.webhookUrl).toEqual({ value: 'https://example.webhook.office.com/webhookb2/abc' })
expect(payload.inputs?.text?.value).toContain('{event.properties.alert_name}')
await expectLogic(logic).toMatchValues({ pendingNotifications: [] })
})
})