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.

1.5 KB
Raw Blame
import { CompatMessage } from '../../../../types'
import { ExecutionBudget } from '../../runtime/budget'
import { Scope } from '../../scope'
import { LiteralExpr } from '../expr'
import { Pattern } from '../predicate'
import { DelegateRule } from './delegate'
import { DispatchEngine, NO_MATCH } from './dispatch'

const scope = Scope.forNode({ role: 'assistant' }, 'user')
const budget = new ExecutionBudget()

describe('DelegateRule', () => {
    it('re-dispatches the derived value and returns its messages', () => {
        const inner: CompatMessage[] = [{ role: 'assistant', content: 'delegated' }]
        const dispatch = jest.fn().mockReturnValue(inner)
        const engine: DispatchEngine = { dispatch, coercer: { buildMessage: jest.fn(), stamp: jest.fn() }, budget }

        const rule = new DelegateRule(new Pattern({}), [], new LiteralExpr({ unwrapped: true }))
        expect(rule.produce(scope, engine, false, 3)).toBe(inner)
        // delegation re-dispatches the derived value at the next depth, carrying the resolved role
        expect(dispatch).toHaveBeenCalledWith({ unwrapped: true }, 'assistant', 4)
    })

    it('returns nothing when the delegated value matches no recipe', () => {
        const engine: DispatchEngine = {
            dispatch: jest.fn().mockReturnValue(NO_MATCH),
            coercer: { buildMessage: jest.fn(), stamp: jest.fn() },
            budget,
        }
        const rule = new DelegateRule(new Pattern({}), [], new LiteralExpr('anything'))
        expect(rule.produce(scope, engine, false, 0)).toEqual([])
    })
})