|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +vi.mock('@/blocks/registry-maps', () => ({ |
| 7 | + BLOCK_REGISTRY: { |
| 8 | + slack: { |
| 9 | + type: 'slack', |
| 10 | + tools: { |
| 11 | + access: ['slack_message_v1', 'slack_canvas_v1'], |
| 12 | + config: { |
| 13 | + tool: ({ operation }: { operation?: string }) => |
| 14 | + operation === 'canvas' ? 'slack_canvas_v1' : 'slack_message_v1', |
| 15 | + }, |
| 16 | + }, |
| 17 | + subBlocks: [ |
| 18 | + { |
| 19 | + id: 'operation', |
| 20 | + type: 'dropdown', |
| 21 | + options: [ |
| 22 | + { label: 'Send Message', id: 'send' }, |
| 23 | + { label: 'Create Canvas', id: 'canvas' }, |
| 24 | + ], |
| 25 | + }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + gmail: { |
| 29 | + type: 'gmail', |
| 30 | + tools: { access: ['gmail_send_v1'] }, |
| 31 | + subBlocks: [], |
| 32 | + }, |
| 33 | + /** |
| 34 | + * Multi-tool block with no operation selector: its operation ids ARE its |
| 35 | + * tool ids, so there are no dropdown options to filter. |
| 36 | + */ |
| 37 | + sqs: { |
| 38 | + type: 'sqs', |
| 39 | + tools: { access: ['sqs_send_v1', 'sqs_receive_v1'] }, |
| 40 | + subBlocks: [], |
| 41 | + }, |
| 42 | + }, |
| 43 | +})) |
| 44 | + |
| 45 | +vi.mock('@/tools/registry', () => ({ |
| 46 | + tools: { |
| 47 | + slack_message_v1: { name: 'Send Message' }, |
| 48 | + slack_canvas_v1: { name: 'Create Canvas' }, |
| 49 | + gmail_send_v1: { name: 'Send Email' }, |
| 50 | + sqs_send_v1: { name: 'Send' }, |
| 51 | + sqs_receive_v1: { name: 'Receive' }, |
| 52 | + }, |
| 53 | +})) |
| 54 | + |
| 55 | +vi.mock('@/lib/core/config/env-flags', () => ({ |
| 56 | + getAllowedIntegrationsFromEnv: () => null, |
| 57 | +})) |
| 58 | + |
| 59 | +vi.mock('@/lib/integrations/availability.server', () => ({ |
| 60 | + isIntegrationDeploymentAvailableForVisibility: () => true, |
| 61 | +})) |
| 62 | + |
| 63 | +import { |
| 64 | + projectIntegrationToolsForViewer, |
| 65 | + resolveDeniedBlockOperations, |
| 66 | +} from '@/lib/copilot/integration-tool-projection' |
| 67 | +import { resetExposedIntegrationToolsCache } from '@/lib/copilot/integration-tools' |
| 68 | + |
| 69 | +function toolIds(config: Parameters<typeof projectIntegrationToolsForViewer>[1]): string[] { |
| 70 | + return projectIntegrationToolsForViewer(null, config) |
| 71 | + .tools.map((tool) => tool.toolId) |
| 72 | + .sort() |
| 73 | +} |
| 74 | + |
| 75 | +describe('projectIntegrationToolsForViewer', () => { |
| 76 | + beforeEach(() => { |
| 77 | + resetExposedIntegrationToolsCache() |
| 78 | + }) |
| 79 | + |
| 80 | + it('exposes everything to a viewer with no permission group', () => { |
| 81 | + expect(toolIds(null)).toEqual([ |
| 82 | + 'gmail_send_v1', |
| 83 | + 'slack_canvas_v1', |
| 84 | + 'slack_message_v1', |
| 85 | + 'sqs_receive_v1', |
| 86 | + 'sqs_send_v1', |
| 87 | + ]) |
| 88 | + }) |
| 89 | + |
| 90 | + it('withholds a tool the group denies while keeping its siblings', () => { |
| 91 | + expect(toolIds({ allowedIntegrations: null, deniedTools: ['slack_canvas_v1'] })).toEqual([ |
| 92 | + 'gmail_send_v1', |
| 93 | + 'slack_message_v1', |
| 94 | + 'sqs_receive_v1', |
| 95 | + 'sqs_send_v1', |
| 96 | + ]) |
| 97 | + }) |
| 98 | + |
| 99 | + it('applies the block allowlist and the tool denylist together', () => { |
| 100 | + expect(toolIds({ allowedIntegrations: ['slack'], deniedTools: ['slack_canvas_v1'] })).toEqual([ |
| 101 | + 'slack_message_v1', |
| 102 | + ]) |
| 103 | + }) |
| 104 | + |
| 105 | + it('reports the allowed block types and the tool gate it applied', () => { |
| 106 | + const projection = projectIntegrationToolsForViewer(null, { |
| 107 | + allowedIntegrations: ['Slack'], |
| 108 | + deniedTools: ['slack_canvas_v1'], |
| 109 | + }) |
| 110 | + |
| 111 | + expect(projection.allowedBlockTypes).toEqual(new Set(['slack'])) |
| 112 | + expect(projection.isToolAllowed('slack_canvas_v1')).toBe(false) |
| 113 | + expect(projection.isToolAllowed('slack_message_v1')).toBe(true) |
| 114 | + }) |
| 115 | + |
| 116 | + it('leaves the gate unrestricted when the group denies nothing', () => { |
| 117 | + const projection = projectIntegrationToolsForViewer(null, { |
| 118 | + allowedIntegrations: null, |
| 119 | + deniedTools: [], |
| 120 | + }) |
| 121 | + |
| 122 | + expect(projection.allowedBlockTypes).toBeNull() |
| 123 | + expect(projection.isToolAllowed('slack_canvas_v1')).toBe(true) |
| 124 | + }) |
| 125 | +}) |
| 126 | + |
| 127 | +describe('resolveDeniedBlockOperations', () => { |
| 128 | + const allow = (denied: string[]) => (toolId: string) => !denied.includes(toolId) |
| 129 | + |
| 130 | + it('does no work when the group denies nothing', () => { |
| 131 | + const resolved = resolveDeniedBlockOperations([], allow([])) |
| 132 | + |
| 133 | + expect(resolved.needsProjection.size).toBe(0) |
| 134 | + expect(resolved.fullyDenied.size).toBe(0) |
| 135 | + }) |
| 136 | + |
| 137 | + it('reports the operation ids to withhold from a partly denied block', () => { |
| 138 | + const denied = ['slack_canvas_v1'] |
| 139 | + const resolved = resolveDeniedBlockOperations(denied, allow(denied)) |
| 140 | + |
| 141 | + expect(resolved.needsProjection.get('slack')).toEqual(new Set(['canvas'])) |
| 142 | + expect(resolved.fullyDenied.has('slack')).toBe(false) |
| 143 | + }) |
| 144 | + |
| 145 | + it('withholds a block whose every operation is denied', () => { |
| 146 | + const denied = ['slack_message_v1', 'slack_canvas_v1'] |
| 147 | + const resolved = resolveDeniedBlockOperations(denied, allow(denied)) |
| 148 | + |
| 149 | + expect(resolved.fullyDenied.has('slack')).toBe(true) |
| 150 | + expect(resolved.needsProjection.has('slack')).toBe(false) |
| 151 | + }) |
| 152 | + |
| 153 | + it('withholds a single-tool block whose only tool is denied', () => { |
| 154 | + const denied = ['gmail_send_v1'] |
| 155 | + const resolved = resolveDeniedBlockOperations(denied, allow(denied)) |
| 156 | + |
| 157 | + expect(resolved.fullyDenied.has('gmail')).toBe(true) |
| 158 | + }) |
| 159 | + |
| 160 | + it('reprojects a selector-less block so its tool list drops the denied id', () => { |
| 161 | + const denied = ['sqs_receive_v1'] |
| 162 | + const resolved = resolveDeniedBlockOperations(denied, allow(denied)) |
| 163 | + |
| 164 | + expect(resolved.needsProjection.get('sqs')).toEqual(new Set()) |
| 165 | + expect(resolved.fullyDenied.has('sqs')).toBe(false) |
| 166 | + }) |
| 167 | + |
| 168 | + it('ignores blocks that own no denied tool', () => { |
| 169 | + const denied = ['slack_canvas_v1'] |
| 170 | + const resolved = resolveDeniedBlockOperations(denied, allow(denied)) |
| 171 | + |
| 172 | + expect(resolved.needsProjection.has('gmail')).toBe(false) |
| 173 | + expect(resolved.needsProjection.has('sqs')).toBe(false) |
| 174 | + }) |
| 175 | +}) |
0 commit comments