|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { evaluateSubBlockCondition } from '@/lib/workflows/subblocks/visibility' |
| 6 | +import { ManagedAgentBlock } from '@/blocks/blocks/managed_agent' |
| 7 | + |
| 8 | +const subBlockById = (id: string) => { |
| 9 | + const config = ManagedAgentBlock.subBlocks.find((sub) => sub.id === id) |
| 10 | + if (!config) throw new Error(`No sub-block '${id}' on the Managed Agent block`) |
| 11 | + return config |
| 12 | +} |
| 13 | + |
| 14 | +/** Mirrors how the editor and serializer evaluate visibility: raw stored values. */ |
| 15 | +const isVisible = (id: string, values: Record<string, unknown>) => |
| 16 | + evaluateSubBlockCondition(subBlockById(id).condition, values) |
| 17 | + |
| 18 | +const selectTool = (params: Record<string, unknown>) => |
| 19 | + ManagedAgentBlock.tools.config?.tool?.(params as never) |
| 20 | + |
| 21 | +/** |
| 22 | + * The block gained an operation selector after it shipped. Blocks saved before |
| 23 | + * that have NO stored `operation` value, so these tests pin the promise that |
| 24 | + * they keep behaving — and looking — exactly as they did. |
| 25 | + */ |
| 26 | +describe('Managed Agent block — legacy blocks without a stored operation', () => { |
| 27 | + const legacyValues = { |
| 28 | + credential: 'cred-1', |
| 29 | + agent: 'agent_1', |
| 30 | + environment: 'env_1', |
| 31 | + environmentType: 'cloud', |
| 32 | + userMessage: 'do the thing', |
| 33 | + } |
| 34 | + |
| 35 | + it('resolves to the run-session tool', () => { |
| 36 | + expect(selectTool(legacyValues)).toBe('managed_agent_run_session') |
| 37 | + }) |
| 38 | + |
| 39 | + it('resolves to the run-session tool when operation is explicitly absent', () => { |
| 40 | + expect(selectTool({})).toBe('managed_agent_run_session') |
| 41 | + expect(selectTool({ operation: undefined })).toBe('managed_agent_run_session') |
| 42 | + expect(selectTool({ operation: null })).toBe('managed_agent_run_session') |
| 43 | + }) |
| 44 | + |
| 45 | + it.each(['agent', 'environment', 'environmentType', 'userMessage'])( |
| 46 | + 'keeps the %s field visible', |
| 47 | + (id) => { |
| 48 | + expect(isVisible(id, legacyValues)).toBe(true) |
| 49 | + } |
| 50 | + ) |
| 51 | + |
| 52 | + it('keeps cloud-only fields visible', () => { |
| 53 | + expect(isVisible('memoryStoreId', legacyValues)).toBe(true) |
| 54 | + expect(isVisible('files', legacyValues)).toBe(true) |
| 55 | + expect(isVisible('sessionParameters', legacyValues)).toBe(true) |
| 56 | + }) |
| 57 | + |
| 58 | + it('still hides cloud-only fields on a self-hosted legacy block', () => { |
| 59 | + const selfHosted = { ...legacyValues, environmentType: 'self_hosted' } |
| 60 | + expect(isVisible('memoryStoreId', selfHosted)).toBe(false) |
| 61 | + expect(isVisible('files', selfHosted)).toBe(false) |
| 62 | + // Metadata applies to both environment models. |
| 63 | + expect(isVisible('sessionParameters', selfHosted)).toBe(true) |
| 64 | + }) |
| 65 | + |
| 66 | + it('does not show session-targeting fields', () => { |
| 67 | + expect(isVisible('sessionId', legacyValues)).toBe(false) |
| 68 | + expect(isVisible('toolUseIds', legacyValues)).toBe(false) |
| 69 | + }) |
| 70 | + |
| 71 | + it('keeps the legacy run-session output shape as the fallback', () => { |
| 72 | + expect(Object.keys(ManagedAgentBlock.outputs)).toEqual( |
| 73 | + expect.arrayContaining(['content', 'sessionId', 'inputTokens', 'outputTokens']) |
| 74 | + ) |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +describe('Managed Agent block — operation routing', () => { |
| 79 | + it.each([ |
| 80 | + ['run_session', 'managed_agent_run_session'], |
| 81 | + ['create_session', 'managed_agent_create_session'], |
| 82 | + ['send_message', 'managed_agent_send_message'], |
| 83 | + ['get_session', 'managed_agent_get_session'], |
| 84 | + ['list_events', 'managed_agent_list_events'], |
| 85 | + ['update_session', 'managed_agent_update_session'], |
| 86 | + ['interrupt_session', 'managed_agent_interrupt_session'], |
| 87 | + ['respond_tool_confirmation', 'managed_agent_respond_tool_confirmation'], |
| 88 | + ['archive_session', 'managed_agent_archive_session'], |
| 89 | + ['delete_session', 'managed_agent_delete_session'], |
| 90 | + ])('maps %s to %s', (operation, toolId) => { |
| 91 | + expect(selectTool({ operation })).toBe(toolId) |
| 92 | + }) |
| 93 | + |
| 94 | + it('falls back to run session for an unknown operation', () => { |
| 95 | + expect(selectTool({ operation: 'not_a_real_operation' })).toBe('managed_agent_run_session') |
| 96 | + }) |
| 97 | + |
| 98 | + it('declares every mapped tool in tools.access', () => { |
| 99 | + const operations = subBlockById('operation').options as Array<{ id: string }> |
| 100 | + for (const { id } of operations) { |
| 101 | + expect(ManagedAgentBlock.tools.access).toContain(selectTool({ operation: id })) |
| 102 | + } |
| 103 | + }) |
| 104 | +}) |
| 105 | + |
| 106 | +describe('Managed Agent block — per-operation field visibility', () => { |
| 107 | + it('shows the session id for operations that target an existing session', () => { |
| 108 | + for (const operation of [ |
| 109 | + 'send_message', |
| 110 | + 'get_session', |
| 111 | + 'list_events', |
| 112 | + 'update_session', |
| 113 | + 'interrupt_session', |
| 114 | + 'respond_tool_confirmation', |
| 115 | + 'archive_session', |
| 116 | + 'delete_session', |
| 117 | + ]) { |
| 118 | + expect(isVisible('sessionId', { operation })).toBe(true) |
| 119 | + } |
| 120 | + }) |
| 121 | + |
| 122 | + it('hides the agent and environment pickers once a session exists', () => { |
| 123 | + expect(isVisible('agent', { operation: 'send_message' })).toBe(false) |
| 124 | + expect(isVisible('environment', { operation: 'get_session' })).toBe(false) |
| 125 | + expect(isVisible('environmentType', { operation: 'archive_session' })).toBe(false) |
| 126 | + }) |
| 127 | + |
| 128 | + it('shows the user message for the three operations that send one', () => { |
| 129 | + expect(isVisible('userMessage', { operation: 'run_session' })).toBe(true) |
| 130 | + expect(isVisible('userMessage', { operation: 'create_session' })).toBe(true) |
| 131 | + expect(isVisible('userMessage', { operation: 'send_message' })).toBe(true) |
| 132 | + expect(isVisible('userMessage', { operation: 'get_session' })).toBe(false) |
| 133 | + }) |
| 134 | + |
| 135 | + it('makes the user message optional only for create session', () => { |
| 136 | + const required = subBlockById('userMessage').required |
| 137 | + expect(evaluateSubBlockCondition(required as never, { operation: 'create_session' })).toBe( |
| 138 | + false |
| 139 | + ) |
| 140 | + expect(evaluateSubBlockCondition(required as never, { operation: 'send_message' })).toBe(true) |
| 141 | + expect(evaluateSubBlockCondition(required as never, { operation: 'run_session' })).toBe(true) |
| 142 | + // A legacy block has no stored operation and must stay required. |
| 143 | + expect(evaluateSubBlockCondition(required as never, {})).toBe(true) |
| 144 | + }) |
| 145 | + |
| 146 | + it('reveals the deny reason only when denying a tool confirmation', () => { |
| 147 | + expect( |
| 148 | + isVisible('denyMessage', { operation: 'respond_tool_confirmation', decision: 'deny' }) |
| 149 | + ).toBe(true) |
| 150 | + expect( |
| 151 | + isVisible('denyMessage', { operation: 'respond_tool_confirmation', decision: 'allow' }) |
| 152 | + ).toBe(false) |
| 153 | + expect(isVisible('denyMessage', { operation: 'send_message', decision: 'deny' })).toBe(false) |
| 154 | + }) |
| 155 | + |
| 156 | + it('shows metadata for update session but not the agent config fields', () => { |
| 157 | + expect(isVisible('sessionParameters', { operation: 'update_session' })).toBe(true) |
| 158 | + expect(isVisible('title', { operation: 'update_session' })).toBe(true) |
| 159 | + expect(isVisible('vaults', { operation: 'update_session' })).toBe(false) |
| 160 | + }) |
| 161 | +}) |
0 commit comments