|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { authorizeChat, listIntegrations } = vi.hoisted(() => ({ |
| 7 | + authorizeChat: vi.fn(), |
| 8 | + listIntegrations: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('@/lib/copilot/chat/organization-chats', () => ({ |
| 12 | + authorizeOrganizationChatDelegation: { execute: authorizeChat }, |
| 13 | +})) |
| 14 | +vi.mock('@/lib/knowledge/application/personal-search-integrations', () => ({ |
| 15 | + listPersonalSearchIntegrations: { execute: listIntegrations }, |
| 16 | +})) |
| 17 | + |
| 18 | +import { loadCopilotSearchIntegrations } from '@/lib/copilot/application/load-search-integrations' |
| 19 | +import type { listPersonalSearchIntegrations } from '@/lib/knowledge/application/personal-search-integrations' |
| 20 | + |
| 21 | +type InventoryPage = Awaited<ReturnType<typeof listPersonalSearchIntegrations.execute>> |
| 22 | + |
| 23 | +const context = { |
| 24 | + userId: 'person-1', |
| 25 | + organizationId: 'org-1', |
| 26 | + chatId: 'private-chat-1', |
| 27 | + messageId: 'message-1', |
| 28 | +} |
| 29 | +const emptyPage: InventoryPage = { |
| 30 | + connections: [], |
| 31 | + available: [], |
| 32 | + completedCredentialId: null, |
| 33 | + nextCursor: null, |
| 34 | +} |
| 35 | + |
| 36 | +describe('loadCopilotSearchIntegrations', () => { |
| 37 | + beforeEach(() => { |
| 38 | + vi.clearAllMocks() |
| 39 | + authorizeChat.mockResolvedValue(undefined) |
| 40 | + listIntegrations.mockResolvedValue(emptyPage) |
| 41 | + }) |
| 42 | + |
| 43 | + it('authorizes the private chat and reads only for the authenticated person and organization', async () => { |
| 44 | + expect(await loadCopilotSearchIntegrations(context)).toBe('{"connections":[],"available":[]}') |
| 45 | + const principal = authorizeChat.mock.calls[0][0].principal |
| 46 | + expect(principal).toMatchObject({ |
| 47 | + kind: 'organization_delegated', |
| 48 | + serviceId: 'copilot', |
| 49 | + subjectUserId: 'person-1', |
| 50 | + organizationId: 'org-1', |
| 51 | + delegationId: 'message-1', |
| 52 | + audience: 'sim:knowledge', |
| 53 | + resourceScope: { chatId: 'private-chat-1' }, |
| 54 | + }) |
| 55 | + expect(listIntegrations).toHaveBeenCalledExactlyOnceWith({ |
| 56 | + principal, |
| 57 | + input: { organizationId: 'org-1' }, |
| 58 | + }) |
| 59 | + expect(authorizeChat.mock.invocationCallOrder[0]).toBeLessThan( |
| 60 | + listIntegrations.mock.invocationCallOrder[0] |
| 61 | + ) |
| 62 | + }) |
| 63 | + |
| 64 | + it('loads every page and preserves account status and exact connection controls', async () => { |
| 65 | + const available: InventoryPage['available'][number] = { |
| 66 | + name: 'Gmail', |
| 67 | + description: 'Personal mail', |
| 68 | + target: { type: 'link', provider: 'google-email', connectorType: 'gmail' }, |
| 69 | + } |
| 70 | + const connection: InventoryPage['connections'][number] = { |
| 71 | + name: 'Gmail', |
| 72 | + providerId: 'google-email', |
| 73 | + connectorType: 'gmail', |
| 74 | + connectorId: 'source-1', |
| 75 | + knowledgeBaseId: 'kb-1', |
| 76 | + description: 'Personal mail', |
| 77 | + accounts: [ |
| 78 | + { |
| 79 | + credentialId: 'account-1', |
| 80 | + displayName: 'me@example.com', |
| 81 | + status: 'reconnect_needed', |
| 82 | + action: { ...available.target, connectorId: 'source-1', credentialId: 'account-1' }, |
| 83 | + }, |
| 84 | + ], |
| 85 | + connectionStatus: 'reconnect_needed', |
| 86 | + indexingStatus: 'indexed', |
| 87 | + searchableDocuments: 7, |
| 88 | + action: null, |
| 89 | + } |
| 90 | + listIntegrations |
| 91 | + .mockResolvedValueOnce({ ...emptyPage, available: [available], nextCursor: 'page-2' }) |
| 92 | + .mockResolvedValueOnce({ ...emptyPage, connections: [connection], available: [available] }) |
| 93 | + |
| 94 | + expect(JSON.parse(await loadCopilotSearchIntegrations(context))).toEqual({ |
| 95 | + connections: [connection], |
| 96 | + available: [available], |
| 97 | + }) |
| 98 | + expect(listIntegrations.mock.calls[1][0]).toEqual({ |
| 99 | + principal: authorizeChat.mock.calls[0][0].principal, |
| 100 | + input: { organizationId: 'org-1', cursor: 'page-2' }, |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + it('does not read inventory when private-chat authorization fails', async () => { |
| 105 | + authorizeChat.mockRejectedValueOnce(new Error('Chat belongs to another person')) |
| 106 | + await expect(loadCopilotSearchIntegrations(context)).rejects.toThrow( |
| 107 | + 'Chat belongs to another person' |
| 108 | + ) |
| 109 | + expect(listIntegrations).not.toHaveBeenCalled() |
| 110 | + }) |
| 111 | + |
| 112 | + it('fails the turn if a later page cannot be read', async () => { |
| 113 | + listIntegrations |
| 114 | + .mockResolvedValueOnce({ ...emptyPage, nextCursor: 'page-2' }) |
| 115 | + .mockRejectedValueOnce(new Error('Inventory unavailable')) |
| 116 | + await expect(loadCopilotSearchIntegrations(context)).rejects.toThrow('Inventory unavailable') |
| 117 | + }) |
| 118 | + |
| 119 | + it('rejects a repeated cursor instead of looping or returning partial inventory', async () => { |
| 120 | + listIntegrations.mockResolvedValue({ ...emptyPage, nextCursor: 'page-2' }) |
| 121 | + await expect(loadCopilotSearchIntegrations(context)).rejects.toThrow( |
| 122 | + 'pagination did not advance' |
| 123 | + ) |
| 124 | + expect(listIntegrations).toHaveBeenCalledTimes(2) |
| 125 | + }) |
| 126 | + |
| 127 | + it('bounds page loading when the inventory never ends', async () => { |
| 128 | + listIntegrations.mockImplementation(async () => ({ |
| 129 | + ...emptyPage, |
| 130 | + nextCursor: `page-${listIntegrations.mock.calls.length + 1}`, |
| 131 | + })) |
| 132 | + await expect(loadCopilotSearchIntegrations(context)).rejects.toThrow('pagination limit') |
| 133 | + expect(listIntegrations).toHaveBeenCalledTimes(100) |
| 134 | + }) |
| 135 | + |
| 136 | + it('rejects oversized prompt content instead of silently truncating it', async () => { |
| 137 | + listIntegrations.mockResolvedValueOnce({ |
| 138 | + ...emptyPage, |
| 139 | + available: [ |
| 140 | + { |
| 141 | + name: 'Gmail', |
| 142 | + description: 'x'.repeat(256 * 1024), |
| 143 | + target: { type: 'link', provider: 'google-email', connectorType: 'gmail' }, |
| 144 | + }, |
| 145 | + ], |
| 146 | + }) |
| 147 | + await expect(loadCopilotSearchIntegrations(context)).rejects.toThrow('prompt size limit') |
| 148 | + }) |
| 149 | + |
| 150 | + it('stops loading when the turn is cancelled between pages', async () => { |
| 151 | + const controller = new AbortController() |
| 152 | + listIntegrations.mockImplementationOnce(async () => { |
| 153 | + controller.abort(new Error('Turn cancelled')) |
| 154 | + return { ...emptyPage, nextCursor: 'page-2' } |
| 155 | + }) |
| 156 | + await expect( |
| 157 | + loadCopilotSearchIntegrations({ ...context, signal: controller.signal }) |
| 158 | + ).rejects.toThrow('Turn cancelled') |
| 159 | + expect(listIntegrations).toHaveBeenCalledTimes(1) |
| 160 | + }) |
| 161 | +}) |
0 commit comments