import { describe, it, expect, vi, beforeEach } from 'vitest'
import { McpService } from '../../src/services/McpService'

// Mock global chrome
global.chrome = {
  storage: {
    local: {
      get: vi.fn().mockResolvedValue({}),
      set: vi.fn().mockResolvedValue({}),
    }
  },
  runtime: {
    sendMessage: vi.fn(),
    onMessage: {
      addListener: vi.fn(),
    }
  }
} as any

describe('McpService', () => {
  let mcpService: McpService

  beforeEach(() => {
    mcpService = new McpService('http://localhost:3006')
    vi.clearAllMocks()
    
    // Mock fetch
    global.fetch = vi.fn()
  })

  it('should call n8n_create_workflow tool', async () => {
    // Mock tools/call response
    ;(global.fetch as any).mockResolvedValue({
      ok: true,
      json: () => Promise.resolve({
        jsonrpc: '2.0',
        id: 1,
        result: {
          success: true,
          workflow: { id: 'wf-123', name: 'Test Workflow' }
        }
      })
    })

    const result = await mcpService.callTool('n8n_create_workflow', { name: 'Test Workflow' })
    
    expect(result.status).toBe('success')
    expect(result.result.workflow.id).toBe('wf-123')
  }, 10000)
})
