|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { MAX_ID_LENGTH } from '@/lib/api/contracts/primitives' |
| 6 | +import { |
| 7 | + MAX_AGENT_TOOLS_PER_BLOCK, |
| 8 | + v2AgentToolInputSchema, |
| 9 | + v2ApplyWorkflowOperationsBodySchema, |
| 10 | +} from '@/lib/api/contracts/v2/workflows' |
| 11 | +import { MAX_MCP_TOOL_NAME_BYTES } from '@/lib/mcp/constants' |
| 12 | + |
| 13 | +describe('v2AgentToolInputSchema', () => { |
| 14 | + it('accepts catalog integration, custom-tool reference, and MCP tool shapes', () => { |
| 15 | + const tools = [ |
| 16 | + { |
| 17 | + type: 'cloudwatch', |
| 18 | + operation: 'describe_alarm_history', |
| 19 | + usageControl: 'auto', |
| 20 | + params: { region: 'us-east-1' }, |
| 21 | + }, |
| 22 | + { |
| 23 | + type: 'custom-tool', |
| 24 | + customToolId: 'cst_123', |
| 25 | + usageControl: 'force', |
| 26 | + }, |
| 27 | + { |
| 28 | + type: 'mcp', |
| 29 | + params: { serverId: 'mcp_123', toolName: 'search_docs', collection: 'incidents' }, |
| 30 | + usageControl: 'none', |
| 31 | + }, |
| 32 | + ] |
| 33 | + |
| 34 | + expect(v2AgentToolInputSchema.parse(tools)).toEqual(tools) |
| 35 | + }) |
| 36 | + |
| 37 | + it('keeps the legacy inline custom-tool shape available for workflow round trips', () => { |
| 38 | + const tools = [ |
| 39 | + { |
| 40 | + type: 'custom-tool', |
| 41 | + schema: { |
| 42 | + type: 'function', |
| 43 | + function: { |
| 44 | + name: 'lookup_incident', |
| 45 | + description: 'Look up an incident.', |
| 46 | + parameters: { type: 'object', properties: { id: { type: 'string' } } }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + code: 'return params.id', |
| 50 | + }, |
| 51 | + ] |
| 52 | + |
| 53 | + expect(v2AgentToolInputSchema.parse(tools)).toEqual(tools) |
| 54 | + }) |
| 55 | + |
| 56 | + it.each([ |
| 57 | + [{ type: 'custom-tool', usageControl: 'auto' }], |
| 58 | + [{ type: 'mcp', params: { serverId: 'mcp_123' }, usageControl: 'auto' }], |
| 59 | + [{ type: 'slack', operation: 'send', usageControl: 'sometimes' }], |
| 60 | + ])('rejects a malformed reserved tool shape', (tools) => { |
| 61 | + expect(v2AgentToolInputSchema.safeParse(tools).success).toBe(false) |
| 62 | + }) |
| 63 | + |
| 64 | + it('rejects a tool list above the workflow-operation ceiling', () => { |
| 65 | + const tools = Array.from({ length: MAX_AGENT_TOOLS_PER_BLOCK + 1 }, (_, index) => ({ |
| 66 | + type: `integration-${index}`, |
| 67 | + })) |
| 68 | + |
| 69 | + expect(v2AgentToolInputSchema.safeParse(tools).success).toBe(false) |
| 70 | + }) |
| 71 | + |
| 72 | + it.each([ |
| 73 | + [ |
| 74 | + 'inline function name', |
| 75 | + { |
| 76 | + type: 'custom-tool', |
| 77 | + schema: { |
| 78 | + type: 'function', |
| 79 | + function: { name: 'a'.repeat(65), parameters: { type: 'object' } }, |
| 80 | + }, |
| 81 | + code: 'return null', |
| 82 | + }, |
| 83 | + ], |
| 84 | + [ |
| 85 | + 'MCP server id', |
| 86 | + { |
| 87 | + type: 'mcp', |
| 88 | + params: { serverId: 'a'.repeat(MAX_ID_LENGTH + 1), toolName: 'search_docs' }, |
| 89 | + }, |
| 90 | + ], |
| 91 | + [ |
| 92 | + 'MCP tool name', |
| 93 | + { |
| 94 | + type: 'mcp', |
| 95 | + params: { |
| 96 | + serverId: 'mcp_123', |
| 97 | + toolName: 'a'.repeat(MAX_MCP_TOOL_NAME_BYTES + 1), |
| 98 | + }, |
| 99 | + }, |
| 100 | + ], |
| 101 | + [ |
| 102 | + 'MCP multibyte tool name', |
| 103 | + { |
| 104 | + type: 'mcp', |
| 105 | + params: { |
| 106 | + serverId: 'mcp_123', |
| 107 | + toolName: '💡'.repeat(Math.floor(MAX_MCP_TOOL_NAME_BYTES / 4) + 1), |
| 108 | + }, |
| 109 | + }, |
| 110 | + ], |
| 111 | + ])('rejects an overlong %s', (_label, tool) => { |
| 112 | + expect(v2AgentToolInputSchema.safeParse([tool]).success).toBe(false) |
| 113 | + }) |
| 114 | +}) |
| 115 | + |
| 116 | +describe('workflow operation Agent tools contract', () => { |
| 117 | + it('publishes and validates tools under params.inputs without closing other catalog inputs', () => { |
| 118 | + const body = { |
| 119 | + operations: [ |
| 120 | + { |
| 121 | + operation_type: 'add', |
| 122 | + block_id: 'triage', |
| 123 | + params: { |
| 124 | + type: 'agent', |
| 125 | + name: 'Triage', |
| 126 | + inputs: { |
| 127 | + model: 'gpt-5', |
| 128 | + tools: [ |
| 129 | + { |
| 130 | + type: 'cloudwatch', |
| 131 | + operation: 'describe_alarms', |
| 132 | + params: { region: 'us-west-2' }, |
| 133 | + usageControl: 'auto', |
| 134 | + futureMetadata: { preserved: true }, |
| 135 | + }, |
| 136 | + ], |
| 137 | + }, |
| 138 | + futureOperationSetting: true, |
| 139 | + }, |
| 140 | + }, |
| 141 | + ], |
| 142 | + } |
| 143 | + |
| 144 | + expect(v2ApplyWorkflowOperationsBodySchema.parse(body)).toEqual({ |
| 145 | + ...body, |
| 146 | + atomic: false, |
| 147 | + layout: 'targeted', |
| 148 | + }) |
| 149 | + }) |
| 150 | + |
| 151 | + it('rejects malformed Agent tools before the edit engine runs', () => { |
| 152 | + const parsed = v2ApplyWorkflowOperationsBodySchema.safeParse({ |
| 153 | + operations: [ |
| 154 | + { |
| 155 | + operation_type: 'add', |
| 156 | + block_id: 'triage', |
| 157 | + params: { |
| 158 | + type: 'agent', |
| 159 | + name: 'Triage', |
| 160 | + inputs: { tools: [{ type: 'mcp', params: { serverId: 'mcp_123' } }] }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + ], |
| 164 | + }) |
| 165 | + |
| 166 | + expect(parsed.success).toBe(false) |
| 167 | + }) |
| 168 | +}) |
0 commit comments