Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
5.7 KB
Raw Blame
// @vitest-environment jsdom
/**
 * §4.7.2 reply-notification rule: a reply is notified ONCE per (taskId) on its
 * first materialization — whether it lands via the coordinator's terminal or the
 * reconciler's durable branch. A re-materialization of the same task (a durable
 * recovery after a terminal already appended, or vice-versa) is swallowed so the
 * in-app channel (which has no per-tag dedupe of its own) can't double-toast.
 * Hydration replaces never route through `pushAssistantReplyNotification`, so they
 * never notify — this only guards the double-materialization path.
 */
import { type MutableRefObject } from 'react'
import { describe, expect, it, vi } from 'vitest'
import { renderHook } from '@testing-library/react'
import type { AgentChatMessage } from '../../../uiTypes'
import { type ActiveChatVisibility, useChatNotifications } from '../useChatNotifications'

function makeParams(overrides?: Partial<Parameters<typeof useChatNotifications>[0]>) {
  const visibility: ActiveChatVisibility = {
    activeChatId: null,
    currentTeamId: 'team-1',
    navItem: 'chat',
    selectedAgent: null,
  }
  const activeChatVisibilityRef = {
    current: visibility,
  } as MutableRefObject<ActiveChatVisibility>
  const pushNotification = vi.fn()
  const showDesktopNotification = vi.fn(async () => 'granted' as const)
  return {
    activeChatVisibilityRef,
    currentTeamId: 'team-1',
    currentTeamName: 'Team One',
    // Display-name resolver: catalog display (spec.host) with identifier
    // fallback — mirrors the real resolver from useAgentsDataController.
    agentDisplayName: (agentName: string) => (agentName === 'agent-x' ? 'Agent X' : agentName),
    pushNotification,
    // Deliver to both channels (chat not visible): the dedupe, not the gating, is
    // what suppresses the second call.
    canDeliverChatResponseNotification: vi.fn(() => true),
    showDesktopNotification,
    openAgentConversationFromNotification: vi.fn(async () => undefined),
    decideApprovalFromNotification: vi.fn(async () => undefined),
    ...overrides,
  }
}

function reply(id: string, taskId?: string): AgentChatMessage {
  return {
    id,
    role: 'assistant',
    content: 'here is your report',
    timestamp: Date.now(),
    ...(taskId ? { task_id: taskId } : {}),
  }
}

describe('useChatNotifications — reply first-materialization dedupe (§4.7.2)', () => {
  it('notifies once per taskId even when the reply materializes twice (different message ids)', () => {
    const params = makeParams()
    const { result } = renderHook(() => useChatNotifications(params))

    // First materialization (e.g. the terminal append).
    result.current.pushAssistantReplyNotification('agent-x', reply('msg-A', 'task-1'), 'chat-1')
    // Second materialization of the SAME task (e.g. a durable recovery) — fresh id.
    result.current.pushAssistantReplyNotification('agent-x', reply('msg-B', 'task-1'), 'chat-1')

    expect(params.pushNotification).toHaveBeenCalledTimes(1)
    expect(params.showDesktopNotification).toHaveBeenCalledTimes(1)
    expect(params.pushNotification).toHaveBeenCalledWith(
      expect.objectContaining({ kind: 'assistant_reply', agentName: 'agent-x' })
    )
  })

  it('notifies each distinct task once', () => {
    const params = makeParams()
    const { result } = renderHook(() => useChatNotifications(params))

    result.current.pushAssistantReplyNotification('agent-x', reply('msg-A', 'task-1'), 'chat-1')
    result.current.pushAssistantReplyNotification('agent-x', reply('msg-B', 'task-2'), 'chat-1')

    expect(params.pushNotification).toHaveBeenCalledTimes(2)
  })

  it('does not dedupe a reply with no task_id (sync response / error path)', () => {
    const params = makeParams()
    const { result } = renderHook(() => useChatNotifications(params))

    result.current.pushAssistantReplyNotification('agent-x', reply('msg-A'), 'chat-1')
    result.current.pushAssistantReplyNotification('agent-x', reply('msg-B'), 'chat-1')

    expect(params.pushNotification).toHaveBeenCalledTimes(2)
  })

  // Rename propagation: the desktop toast TITLE uses the agent's display name
  // (spec.host), while the in-app notification payload stays keyed by the
  // immutable identifier (matching + dedupe survive a rename).
  it('titles desktop toasts with the display name but keys in-app notifications by the identifier', () => {
    const params = makeParams()
    const { result } = renderHook(() => useChatNotifications(params))

    result.current.pushAssistantReplyNotification('agent-x', reply('msg-A', 'task-1'), 'chat-1')

    expect(params.showDesktopNotification).toHaveBeenCalledWith(
      expect.objectContaining({ title: 'Agent X replied' })
    )
    expect(params.pushNotification).toHaveBeenCalledWith(
      expect.objectContaining({ kind: 'assistant_reply', agentName: 'agent-x' })
    )
  })

  // Review P1: the approval toast's BODY fallback must also resolve the
  // display name — an empty approval text used to degrade to the raw slug
  // while the title right above it showed the friendly name.
  it('uses the display name in the approval toast body fallback', () => {
    const params = makeParams()
    const { result } = renderHook(() => useChatNotifications(params))

    result.current.pushApprovalNotification({
      agentName: 'agent-x',
      chatId: 'chat-1',
      taskId: 'task-1',
      requestId: 'request-1',
      text: '',
    })

    expect(params.showDesktopNotification).toHaveBeenCalledWith(
      expect.objectContaining({
        title: 'Agent X needs authorization',
        body: 'Approval required for Agent X.',
      })
    )
    expect(params.pushNotification).toHaveBeenCalledWith(
      expect.objectContaining({ kind: 'approval_required', agentName: 'agent-x' })
    )
  })
})