|
| 1 | +/** @vitest-environment node */ |
| 2 | +import { renderToStaticMarkup } from 'react-dom/server' |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +const mocks = vi.hoisted(() => { |
| 6 | + const values: Record<string, unknown> = {} |
| 7 | + const modes: Record<string, 'basic' | 'advanced'> = {} |
| 8 | + const workflowState = { blocks: { mcp: { type: 'mcp', data: { canonicalModes: modes } } } } |
| 9 | + const registryState = { activeWorkflowId: 'workflow' } |
| 10 | + const subBlockState = { |
| 11 | + workflowValues: { workflow: { mcp: values } }, |
| 12 | + getValue: (_blockId: string, field: string) => values[field], |
| 13 | + } |
| 14 | + return { |
| 15 | + values, |
| 16 | + modes, |
| 17 | + workflowState, |
| 18 | + registryState, |
| 19 | + subBlockState, |
| 20 | + setValue: vi.fn(), |
| 21 | + setMode: vi.fn(), |
| 22 | + } |
| 23 | +}) |
| 24 | + |
| 25 | +vi.mock('@/hooks/use-collaborative-workflow', () => ({ |
| 26 | + useCollaborativeWorkflow: () => ({ |
| 27 | + collaborativeSetSubblockValue: mocks.setValue, |
| 28 | + collaborativeSetBlockCanonicalMode: mocks.setMode, |
| 29 | + }), |
| 30 | +})) |
| 31 | +vi.mock('@/providers/utils', () => ({ getProviderFromModel: vi.fn() })) |
| 32 | +vi.mock('@/stores/workflows/workflow/store', () => ({ |
| 33 | + useWorkflowStore: Object.assign( |
| 34 | + (selector: (state: typeof mocks.workflowState) => unknown) => selector(mocks.workflowState), |
| 35 | + { getState: () => mocks.workflowState } |
| 36 | + ), |
| 37 | +})) |
| 38 | +vi.mock('@/stores/workflows/registry/store', () => ({ |
| 39 | + useWorkflowRegistry: Object.assign( |
| 40 | + (selector: (state: typeof mocks.registryState) => unknown) => selector(mocks.registryState), |
| 41 | + { getState: () => mocks.registryState } |
| 42 | + ), |
| 43 | +})) |
| 44 | +vi.mock('@/stores/workflows/subblock/store', () => ({ |
| 45 | + useSubBlockStore: { getState: () => mocks.subBlockState }, |
| 46 | +})) |
| 47 | +vi.mock('zustand/traditional', () => ({ |
| 48 | + useStoreWithEqualityFn: ( |
| 49 | + store: { getState: () => typeof mocks.subBlockState }, |
| 50 | + selector: (state: typeof mocks.subBlockState) => unknown |
| 51 | + ) => selector(store.getState()), |
| 52 | +})) |
| 53 | +vi.mock('@/stores/workflow-diff/store', () => ({ |
| 54 | + useWorkflowDiffStore: () => ({ hasActiveDiff: false, isShowingDiff: false }), |
| 55 | +})) |
| 56 | + |
| 57 | +import { buildSelectorRawContext } from '@/lib/selectors/context' |
| 58 | +import { getSubBlocksDependingOnChange } from '@/lib/workflows/subblocks/dependencies' |
| 59 | +import { useSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-value' |
| 60 | +import { McpBlock } from '@/blocks/blocks/mcp' |
| 61 | +import { getBlock } from '@/blocks/registry' |
| 62 | + |
| 63 | +describe('MCP server mode switching', () => { |
| 64 | + beforeEach(() => { |
| 65 | + vi.clearAllMocks() |
| 66 | + for (const field of Object.keys(mocks.values)) delete mocks.values[field] |
| 67 | + mocks.values.toolSelector = 'read' |
| 68 | + mocks.modes.server = 'advanced' |
| 69 | + mocks.modes.tool = 'basic' |
| 70 | + mocks.setMode.mockImplementation((_blockId, field, value) => { |
| 71 | + mocks.modes[field] = value |
| 72 | + }) |
| 73 | + mocks.setValue.mockImplementation((blockId: string, field: string, value: unknown) => { |
| 74 | + mocks.values[field] = value |
| 75 | + for (const dependent of getSubBlocksDependingOnChange(McpBlock.subBlocks, field)) { |
| 76 | + if (mocks.values[dependent.id]) mocks.setValue(blockId, dependent.id, '') |
| 77 | + } |
| 78 | + }) |
| 79 | + vi.mocked(getBlock).mockReturnValue(McpBlock) |
| 80 | + }) |
| 81 | + |
| 82 | + it('preserves the exact selected operation before server dependency clearing', () => { |
| 83 | + let changeServer: ((value: string) => void) | undefined |
| 84 | + function Harness() { |
| 85 | + const [, setValue] = useSubBlockValue<string>('mcp', 'serverReference') |
| 86 | + changeServer = setValue |
| 87 | + return null |
| 88 | + } |
| 89 | + renderToStaticMarkup(<Harness />) |
| 90 | + changeServer?.('<connection.id>') |
| 91 | + expect(mocks.values.serverReference).toBe('<connection.id>') |
| 92 | + expect(mocks.values.toolSelector).toBe('') |
| 93 | + expect(mocks.values.toolReference).toBe('read') |
| 94 | + expect(mocks.modes.tool).toBe('advanced') |
| 95 | + }) |
| 96 | + |
| 97 | + it('projects only the active canonical server into operation discovery', () => { |
| 98 | + const subBlocks = { |
| 99 | + serverSelector: { value: 'inactive' }, |
| 100 | + serverReference: { value: 'mcp-cg-abcdefghijklmnopqrstu' }, |
| 101 | + } |
| 102 | + const input = { |
| 103 | + selectorKey: 'mcp.tools' as const, |
| 104 | + blockType: 'mcp', |
| 105 | + subBlocks, |
| 106 | + canonicalModes: mocks.modes, |
| 107 | + dependsOn: ['server'], |
| 108 | + } |
| 109 | + expect(buildSelectorRawContext(input)).toEqual({ mcpServerId: 'mcp-cg-abcdefghijklmnopqrstu' }) |
| 110 | + subBlocks.serverReference.value = '<connection.id>' |
| 111 | + expect(buildSelectorRawContext(input)).toEqual({}) |
| 112 | + }) |
| 113 | +}) |
0 commit comments