Open History to see the last commit
1// @vitest-environment jsdom
2import { act, renderHook } from '@testing-library/react'
3import { describe, expect, it, vi } from 'vitest'
4import { useMcpAppBridge } from '../src/use-mcp-app-bridge'
5import type { UseMcpAppBridgeOptions } from '../src/use-mcp-app-bridge'
6
7type SendMessage = UseMcpAppBridgeOptions['chat']['sendMessage']
8
9function options(
10 overrides?: Partial<UseMcpAppBridgeOptions>,
11): UseMcpAppBridgeOptions {
12 return {
13 threadId: 't1',
14 callEndpoint: '/api/mcp-apps-call',
15 chat: { sendMessage: vi.fn<SendMessage>(async () => {}) },
16 ...overrides,
17 }
18}
19
20describe('useMcpAppBridge', () => {
21 it('returns a bridge exposing callTool, sendPrompt and openLink', () => {
22 const { result } = renderHook(() => useMcpAppBridge(options()))
23 expect(typeof result.current.callTool).toBe('function')
24 expect(typeof result.current.sendPrompt).toBe('function')
25 expect(typeof result.current.openLink).toBe('function')
26 })
27
28 it('returns a STABLE bridge across rerenders with a fresh options object', () => {
29 const { result, rerender } = renderHook(
30 (opts: UseMcpAppBridgeOptions) => useMcpAppBridge(opts),
31 { initialProps: options() },
32 )
33 const first = result.current
34 // New options object + new inline sendMessage each render must NOT churn it.
35 rerender(options())
36 expect(result.current).toBe(first)
37 })
38
39 it('recreates the bridge when threadId changes', () => {
40 const { result, rerender } = renderHook(
41 (opts: UseMcpAppBridgeOptions) => useMcpAppBridge(opts),
42 { initialProps: options({ threadId: 'a' }) },
43 )
44 const first = result.current
45 rerender(options({ threadId: 'b' }))
46 expect(result.current).not.toBe(first)
47 })
48
49 it('invokes the LATEST chat.sendMessage even though the bridge is stable', async () => {
50 const first = vi.fn<SendMessage>(async () => {})
51 const second = vi.fn<SendMessage>(async () => {})
52 const { result, rerender } = renderHook(
53 (opts: UseMcpAppBridgeOptions) => useMcpAppBridge(opts),
54 { initialProps: options({ chat: { sendMessage: first } }) },
55 )
56 const bridge = result.current
57 rerender(options({ chat: { sendMessage: second } }))
58 expect(result.current).toBe(bridge) // identity unchanged
59
60 await act(async () => {
61 await bridge.sendPrompt('hello')
62 })
63 expect(first).not.toHaveBeenCalled()
64 expect(second).toHaveBeenCalledTimes(1)
65 expect(second.mock.calls[0]?.[0]).toBe('hello')
66 })
67
68 it('openLink forwards to the LATEST onLink handler', () => {
69 const first = vi.fn<(url: string) => void>()
70 const second = vi.fn<(url: string) => void>()
71 const { result, rerender } = renderHook(
72 (opts: UseMcpAppBridgeOptions) => useMcpAppBridge(opts),
73 { initialProps: options({ onLink: first }) },
74 )
75 rerender(options({ onLink: second }))
76
77 expect(result.current.openLink('https://example.com')).toEqual({
78 isError: false,
79 })
80 expect(first).not.toHaveBeenCalled()
81 expect(second).toHaveBeenCalledWith('https://example.com')
82 })
83
84 it('openLink returns { isError: true } when no onLink is provided (display-only)', () => {
85 const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
86 const { result } = renderHook(() => useMcpAppBridge(options()))
87 expect(result.current.openLink('https://example.com')).toEqual({
88 isError: true,
89 })
90 warn.mockRestore()
91 })
92})