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.

4.8 KB
Raw Blame
import { markdownToHtml } from 'lib/utils/markdown'

import type { TicketMacroApi } from '../../generated/api.schemas'
import {
    isStartOfMacroSlash,
    macroContextFromTicket,
    macroPreview,
    matchMacros,
    renderMacro,
    type MacroContext,
} from './renderMacro'

const ctx: MacroContext = {
    customer: { first_name: 'Jane', name: 'Jane Doe', email: '[email protected]' },
    agent: { first_name: 'Nik', name: 'Nik Smith' },
    ticket: { number: '42' },
}

describe('support macros', () => {
    test.each<[string, MacroContext, string]>([
        ['Hi {{customer.first_name}}, ticket {{ticket.number}}', ctx, 'Hi Jane, ticket 42'],
        ['Cheers,\n{{agent.first_name}}', ctx, 'Cheers,\nNik'],
        ['{{ customer.name }}', ctx, 'Jane Doe'],
        ['From {{agent.name}}', ctx, 'From Nik Smith'],
        [
            'Hi {{customer.first_name}}',
            { ...ctx, customer: { first_name: null, name: null, email: '[email protected]' } },
            'Hi {{customer.first_name}}',
        ],
        ['See {{customer.plan}}', ctx, 'See {{customer.plan}}'],
        ['No vars', ctx, 'No vars'],
        ['Hi \\{\\{customer\\.first\\_name\\}\\}, \\{\\{ticket\\.number\\}\\}', ctx, 'Hi Jane, 42'],
        ["What's up\\! {{customer.first_name}}", ctx, "What's up\\! Jane"],
    ])('renders %s', (body, context, expected) => {
        expect(renderMacro(body, context)).toBe(markdownToHtml(expected))
    })

    test.each([
        ['<b>Jane</b>', '&lt;b&gt;Jane&lt;/b&gt;'],
        ['![x](https://example.com/x.png)', '![x](https://example.com/x.png)'],
        ['**Jane**', '**Jane**'],
    ])('keeps customer name %s as literal text', (name, expectedText) => {
        const html = renderMacro('Hi **{{customer.name}}**', { ...ctx, customer: { ...ctx.customer, name } })
        expect(html).toBe(`<p>Hi <strong>${expectedText}</strong></p>\n`)
    })

    test.each([
        [
            '[Email](mailto:{{customer.email}})',
            { ...ctx, customer: { ...ctx.customer, email: '[email protected]' } },
            '<p><a href="mailto:jane%40example.com">Email</a></p>\n',
        ],
        [
            '[Profile]({{customer.name}})',
            { ...ctx, customer: { ...ctx.customer, name: 'javascript:alert(1)' } },
            '<p><a href="javascript%3Aalert(1)">Profile</a></p>\n',
        ],
        ['Hi \uE0005\uE001 {{customer.first_name}}', ctx, '<p>Hi 5 Jane</p>\n'],
    ])('renders the edge case %s', (body, context, expected) => {
        expect(renderMacro(body, context)).toBe(expected)
    })

    test.each([
        ['refund', ['My refund', 'Full refund', 'Partial refund', 'Refund policy']],
        ['ref', ['Partial refund', 'Full refund', 'My refund', 'Refund policy']],
        ['policy', ['Refund policy']],
    ])('ranks matches for %s', (query, expected) => {
        // Input is most used first, so an exact shortcut must outrank a more used prefix match,
        // and your personal macro must outrank a teammate's shared one on the same shortcut.
        const macros = [
            { id: '1', name: 'Partial refund', shortcut: 'refund-partial', visibility: 'shared' },
            { id: '2', name: 'Refund policy', shortcut: null, visibility: 'shared' },
            { id: '3', name: 'Full refund', shortcut: 'refund', visibility: 'shared' },
            { id: '4', name: 'My refund', shortcut: 'refund', visibility: 'personal' },
        ] as unknown as TicketMacroApi[]
        expect(matchMacros(macros, query).map((macro) => macro.name)).toEqual(expected)
    })

    it('does not treat an email address as a first name', () => {
        const context = macroContextFromTicket(
            {
                ticket_number: 7,
                anonymous_traits: { email: '[email protected]' },
                person: null,
            } as unknown as Parameters<typeof macroContextFromTicket>[0],
            { first_name: 'Nik', email: '[email protected]' } as unknown as Parameters<typeof macroContextFromTicket>[1]
        )
        expect(context.customer.first_name).toBeNull()
        expect(context.customer.name).toBe('[email protected]')
        expect(context.customer.email).toBe('[email protected]')
        expect(context.agent.first_name).toBe('Nik')
        expect(context.ticket.number).toBe('7')
    })

    it('uses the first non-empty line and truncates it', () => {
        const body = `\n\n${'Thanks for writing. '.repeat(10)}`
        expect(macroPreview(body).endsWith('…')).toBe(true)
        expect(macroPreview(body).length).toBe(80)
        expect(macroPreview('Hi \\{\\{customer\\.name\\}\\}\\!')).toBe('Hi {{customer.name}}!')
    })

    test.each([
        ['', true],
        [' ', true],
        ['\n', true],
        ['/', false],
        ['a', false],
    ])('slash after %j is a trigger: %s', (before, expected) => {
        expect(isStartOfMacroSlash(before)).toBe(expected)
    })
})