|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +vi.mock('@sim/logger', () => ({ |
| 7 | + createLogger: vi.fn(() => ({ |
| 8 | + info: vi.fn(), |
| 9 | + warn: vi.fn(), |
| 10 | + error: vi.fn(), |
| 11 | + })), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/billing/core/subscription', () => ({ |
| 15 | + getUserSubscriptionState: vi.fn(), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@/lib/copilot/chat-context', () => ({ |
| 19 | + processFileAttachments: vi.fn(), |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock('@/lib/core/config/feature-flags', () => ({ |
| 23 | + isHosted: false, |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('@/lib/mcp/utils', () => ({ |
| 27 | + createMcpToolId: vi.fn(), |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock('@/lib/workflows/utils', () => ({ |
| 31 | + getWorkflowById: vi.fn(), |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock('@/tools/registry', () => ({ |
| 35 | + tools: { |
| 36 | + gmail_send: { |
| 37 | + id: 'gmail_send', |
| 38 | + name: 'Gmail Send', |
| 39 | + description: 'Send emails using Gmail', |
| 40 | + }, |
| 41 | + brandfetch_search: { |
| 42 | + id: 'brandfetch_search', |
| 43 | + name: 'Brandfetch Search', |
| 44 | + description: 'Search for brands by company name', |
| 45 | + }, |
| 46 | + }, |
| 47 | +})) |
| 48 | + |
| 49 | +vi.mock('@/tools/utils', () => ({ |
| 50 | + getLatestVersionTools: vi.fn((input) => input), |
| 51 | + stripVersionSuffix: vi.fn((toolId: string) => toolId), |
| 52 | +})) |
| 53 | + |
| 54 | +vi.mock('@/tools/params', () => ({ |
| 55 | + createUserToolSchema: vi.fn(() => ({ type: 'object', properties: {} })), |
| 56 | +})) |
| 57 | + |
| 58 | +import { getUserSubscriptionState } from '@/lib/billing/core/subscription' |
| 59 | +import { buildIntegrationToolSchemas } from '@/lib/copilot/chat-payload' |
| 60 | + |
| 61 | +const mockedGetUserSubscriptionState = getUserSubscriptionState as unknown as { |
| 62 | + mockResolvedValue: (value: unknown) => void |
| 63 | + mockRejectedValue: (value: unknown) => void |
| 64 | + mockClear: () => void |
| 65 | +} |
| 66 | + |
| 67 | +describe('buildIntegrationToolSchemas', () => { |
| 68 | + beforeEach(() => { |
| 69 | + vi.clearAllMocks() |
| 70 | + }) |
| 71 | + |
| 72 | + it('appends the email footer prompt for free users', async () => { |
| 73 | + mockedGetUserSubscriptionState.mockResolvedValue({ isFree: true }) |
| 74 | + |
| 75 | + const toolSchemas = await buildIntegrationToolSchemas('user-free') |
| 76 | + const gmailTool = toolSchemas.find((tool) => tool.name === 'gmail_send') |
| 77 | + |
| 78 | + expect(getUserSubscriptionState).toHaveBeenCalledWith('user-free') |
| 79 | + expect(gmailTool?.description).toContain('sent with sim ai') |
| 80 | + }) |
| 81 | + |
| 82 | + it('does not append the email footer prompt for paid users', async () => { |
| 83 | + mockedGetUserSubscriptionState.mockResolvedValue({ isFree: false }) |
| 84 | + |
| 85 | + const toolSchemas = await buildIntegrationToolSchemas('user-paid') |
| 86 | + const gmailTool = toolSchemas.find((tool) => tool.name === 'gmail_send') |
| 87 | + |
| 88 | + expect(getUserSubscriptionState).toHaveBeenCalledWith('user-paid') |
| 89 | + expect(gmailTool?.description).toBe('Send emails using Gmail') |
| 90 | + }) |
| 91 | + |
| 92 | + it('still builds integration tools when subscription lookup fails', async () => { |
| 93 | + mockedGetUserSubscriptionState.mockRejectedValue(new Error('db unavailable')) |
| 94 | + |
| 95 | + const toolSchemas = await buildIntegrationToolSchemas('user-error') |
| 96 | + const gmailTool = toolSchemas.find((tool) => tool.name === 'gmail_send') |
| 97 | + const brandfetchTool = toolSchemas.find((tool) => tool.name === 'brandfetch_search') |
| 98 | + |
| 99 | + expect(getUserSubscriptionState).toHaveBeenCalledWith('user-error') |
| 100 | + expect(gmailTool?.description).toBe('Send emails using Gmail') |
| 101 | + expect(brandfetchTool?.description).toBe('Search for brands by company name') |
| 102 | + }) |
| 103 | +}) |
0 commit comments