Skip to content

: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.

12.4 KB
Raw Blame
import { closeHub, createHub } from '~/common/utils/db/hub'
import { PostgresUse } from '~/common/utils/db/postgres'
import { UUIDT } from '~/common/utils/utils'
import { createTeam, createTestTeamFixture, getTeam } from '~/tests/helpers/sql'
import { INVALID_TEAM_ID_KEYS } from '~/tests/helpers/team'
import { Hub, Team } from '~/types'

import { RecipientGetArgs, RecipientsManagerService } from './recipients-manager.service'

describe('RecipientsManager', () => {
    let hub: Hub
    let manager: RecipientsManagerService
    let team: Team
    let team2: Team

    beforeEach(async () => {
        hub = await createHub()
        manager = new RecipientsManagerService(hub.postgres)
        const { organizationId, team: fixtureTeam } = await createTestTeamFixture(hub.postgres)
        team = fixtureTeam
        const team2Id = await createTeam(hub.postgres, organizationId)
        team2 = (await getTeam(hub.postgres, team2Id))!
    })

    afterEach(async () => {
        await closeHub(hub)
    })

    const createRecipient = async (teamId: number, identifier: string, preferences: Record<string, string> = {}) => {
        const id = new UUIDT().toString()
        await hub.postgres.query(
            PostgresUse.COMMON_WRITE,
            `INSERT INTO posthog_messagerecipientpreference (id, team_id, identifier, preferences, created_at, updated_at, deleted)
             VALUES ($1, $2, $3, $4, NOW(), NOW(), false)`,
            [id, teamId, identifier, JSON.stringify(preferences)],
            'testInsertRecipient'
        )
        return id
    }

    describe('get', () => {
        it('should return null when recipient does not exist', async () => {
            const args: RecipientGetArgs = { teamId: team.id, identifier: '[email protected]' }
            const result = await manager.get(args)
            expect(result).toBeNull()
        })

        it('should return recipient when it exists', async () => {
            const preferences = { 'category-1': 'OPTED_IN', 'category-2': 'OPTED_OUT' }
            const id = await createRecipient(team.id, '[email protected]', preferences)

            const args: RecipientGetArgs = { teamId: team.id, identifier: '[email protected]' }
            const result = await manager.get(args)

            expect(result).toEqual({
                id: id,
                team_id: team.id,
                identifier: '[email protected]',
                preferences: preferences,
                created_at: expect.any(String),
                updated_at: expect.any(String),
            })
        })

        it('should handle invalid preference statuses by defaulting to NO_PREFERENCE', async () => {
            const preferences = { 'category-1': 'INVALID_STATUS', 'category-2': 'OPTED_IN' }
            await createRecipient(team.id, '[email protected]', preferences)

            const args: RecipientGetArgs = { teamId: team.id, identifier: '[email protected]' }
            const result = await manager.get(args)

            expect(result?.preferences).toEqual({
                'category-1': 'NO_PREFERENCE',
                'category-2': 'OPTED_IN',
            })
        })

        it('should filter out deleted recipients', async () => {
            const id = await createRecipient(team.id, '[email protected]', {})

            await hub.postgres.query(
                PostgresUse.COMMON_WRITE,
                `UPDATE posthog_messagerecipientpreference SET deleted = true WHERE id = $1`,
                [id],
                'testMarkDeleted'
            )

            const args: RecipientGetArgs = { teamId: team.id, identifier: '[email protected]' }
            const result = await manager.get(args)
            expect(result).toBeNull()
        })
    })

    describe('getMany', () => {
        it('should return multiple recipients', async () => {
            const id1 = await createRecipient(team.id, '[email protected]', { 'cat-1': 'OPTED_IN' })
            const id2 = await createRecipient(team.id, '[email protected]', { 'cat-1': 'OPTED_OUT' })
            const id3 = await createRecipient(team2.id, '[email protected]', { 'cat-1': 'NO_PREFERENCE' })

            const args: RecipientGetArgs[] = [
                { teamId: team.id, identifier: '[email protected]' },
                { teamId: team.id, identifier: '[email protected]' },
                { teamId: team2.id, identifier: '[email protected]' },
                { teamId: team.id, identifier: '[email protected]' }, // This should return null
            ]

            const results = await manager.getMany(args)

            expect(results[`${team.id}:[email protected]`]).toEqual({
                id: id1,
                team_id: team.id,
                identifier: '[email protected]',
                preferences: { 'cat-1': 'OPTED_IN' },
                created_at: expect.any(String),
                updated_at: expect.any(String),
            })

            expect(results[`${team.id}:[email protected]`]).toEqual({
                id: id2,
                team_id: team.id,
                identifier: '[email protected]',
                preferences: { 'cat-1': 'OPTED_OUT' },
                created_at: expect.any(String),
                updated_at: expect.any(String),
            })

            expect(results[`${team2.id}:[email protected]`]).toEqual({
                id: id3,
                team_id: team2.id,
                identifier: '[email protected]',
                preferences: { 'cat-1': 'NO_PREFERENCE' },
                created_at: expect.any(String),
                updated_at: expect.any(String),
            })

            expect(results[`${team.id}:[email protected]`]).toBeNull()
        })

        it('should handle empty input', async () => {
            const results = await manager.getMany([])
            expect(results).toEqual({})
        })

        it.each(INVALID_TEAM_ID_KEYS)(
            'returns null for invalid team id %p without failing the valid team in the same load',
            async (key) => {
                await createRecipient(team.id, '[email protected]', { 'cat-1': 'OPTED_IN' })
                const invalidTeamId = key as unknown as number

                const results = await manager.getMany([
                    { teamId: team.id, identifier: '[email protected]' },
                    { teamId: invalidTeamId, identifier: '[email protected]' },
                ])

                expect(results[`${team.id}:[email protected]`]?.preferences).toEqual({ 'cat-1': 'OPTED_IN' })
                expect(results[`${key}:[email protected]`]).toBeNull()
                // A load that holds only invalid ids has no query to run.
                expect(await manager.get({ teamId: invalidTeamId, identifier: '[email protected]' })).toBeNull()
            }
        )
    })

    describe('getPreference', () => {
        it('should return the correct preference status', () => {
            const recipient = {
                id: 'test-id',
                team_id: team.id,
                identifier: '[email protected]',
                preferences: {
                    'category-1': 'OPTED_IN' as const,
                    'category-2': 'OPTED_OUT' as const,
                },
                created_at: '2023-01-01T00:00:00Z',
                updated_at: '2023-01-01T00:00:00Z',
            }

            expect(manager.getPreference(recipient, 'category-1')).toBe('OPTED_IN')
            expect(manager.getPreference(recipient, 'category-2')).toBe('OPTED_OUT')
            expect(manager.getPreference(recipient, 'category-3')).toBe('NO_PREFERENCE')
        })
    })

    describe('optOut', () => {
        it('should create a new recipient with $all OPTED_OUT when none exists', async () => {
            await manager.optOut(team.id, ['[email protected]'])

            const result = await manager.get({ teamId: team.id, identifier: '[email protected]' })
            expect(result).toMatchObject({
                team_id: team.id,
                identifier: '[email protected]',
                preferences: { $all: 'OPTED_OUT' },
            })
        })

        it('should merge $all OPTED_OUT into existing preferences', async () => {
            await createRecipient(team.id, '[email protected]', { 'category-1': 'OPTED_IN' })

            await manager.optOut(team.id, ['[email protected]'])

            const result = await manager.get({ teamId: team.id, identifier: '[email protected]' })
            expect(result?.preferences).toEqual({
                'category-1': 'OPTED_IN',
                $all: 'OPTED_OUT',
            })
        })

        it('should overwrite existing $all preference with OPTED_OUT', async () => {
            await createRecipient(team.id, '[email protected]', { $all: 'OPTED_IN', 'category-1': 'OPTED_IN' })

            await manager.optOut(team.id, ['[email protected]'])

            const result = await manager.get({ teamId: team.id, identifier: '[email protected]' })
            expect(result?.preferences).toEqual({
                'category-1': 'OPTED_IN',
                $all: 'OPTED_OUT',
            })
        })

        it('should opt out multiple recipients in a single call', async () => {
            await createRecipient(team.id, '[email protected]', { 'category-1': 'OPTED_IN' })
            // user2 does not exist yet
            await createRecipient(team.id, '[email protected]', { 'category-1': 'OPTED_IN', 'category-2': 'OPTED_IN' })

            await manager.optOut(team.id, ['[email protected]', '[email protected]', '[email protected]'])

            const result1 = await manager.get({ teamId: team.id, identifier: '[email protected]' })
            expect(result1?.preferences).toEqual({ 'category-1': 'OPTED_IN', $all: 'OPTED_OUT' })

            const result2 = await manager.get({ teamId: team.id, identifier: '[email protected]' })
            expect(result2?.preferences).toEqual({ $all: 'OPTED_OUT' })

            const result3 = await manager.get({ teamId: team.id, identifier: '[email protected]' })
            expect(result3?.preferences).toEqual({
                'category-1': 'OPTED_IN',
                'category-2': 'OPTED_IN',
                $all: 'OPTED_OUT',
            })
        })

        it('should be a no-op for an empty array', async () => {
            await createRecipient(team.id, '[email protected]', { 'category-1': 'OPTED_IN' })

            await manager.optOut(team.id, [])

            const result = await manager.get({ teamId: team.id, identifier: '[email protected]' })
            expect(result?.preferences).toEqual({ 'category-1': 'OPTED_IN' })
        })

        it('should clear the cache after opt-out', async () => {
            await createRecipient(team.id, '[email protected]', { $all: 'OPTED_IN' })

            // Load into cache
            const before = await manager.get({ teamId: team.id, identifier: '[email protected]' })
            expect(before?.preferences.$all).toBe('OPTED_IN')

            // Opt out (should clear cache)
            await manager.optOut(team.id, ['[email protected]'])

            // Should reflect the new value, not the cached one
            const after = await manager.get({ teamId: team.id, identifier: '[email protected]' })
            expect(after?.preferences.$all).toBe('OPTED_OUT')
        })
    })

    describe('caching', () => {
        it('should cache results and not hit database on second call', async () => {
            const id = await createRecipient(team.id, '[email protected]', { 'cat-1': 'OPTED_IN' })
            const args: RecipientGetArgs = { teamId: team.id, identifier: '[email protected]' }

            const result1 = await manager.get(args)
            expect(result1?.id).toBe(id)

            await hub.postgres.query(
                PostgresUse.COMMON_WRITE,
                `DELETE FROM posthog_messagerecipientpreference WHERE id = $1`,
                [id],
                'testDeleteRecipient'
            )

            const result2 = await manager.get(args)
            expect(result2?.id).toBe(id)
        })

        it('should clear cache', async () => {
            const id = await createRecipient(team.id, '[email protected]', { 'cat-1': 'OPTED_IN' })
            const args: RecipientGetArgs = { teamId: team.id, identifier: '[email protected]' }

            await manager.get(args)

            manager.clear()

            await hub.postgres.query(
                PostgresUse.COMMON_WRITE,
                `DELETE FROM posthog_messagerecipientpreference WHERE id = $1`,
                [id],
                'testDeleteRecipient'
            )

            const result = await manager.get(args)
            expect(result).toBeNull()
        })
    })
})