// {{ display_name }} MCP server — generated by mcpify. Do not hand-edit.
//
// Only runs when RUN_INTEGRATION_TESTS=true (see vitest.config.ts).

import { afterAll, beforeAll, describe, expect, it } from 'vitest';

import { AuthManager } from '../../src/auth/auth-manager.js';
import { DEFAULT_API_VERSION, type Config } from '../../src/core/config-schema.js';
import { closeStore, openStore, type Store } from '../../src/data/store-repository.js';
import { ApiClient } from '../../src/services/api-client.js';
import { getOperation } from '../../src/tools/get-tool.js';
import { searchOperations } from '../../src/tools/search-tool.js';
import { buildDummyCredentials } from '../helpers/dummy-credentials.js';
import { startMockApiServer, type MockApiServer } from '../helpers/mock-api-server.js';

let store: Store;
let mockApi: MockApiServer;
let config: Config;

beforeAll(async () => {
  store = openStore(DEFAULT_API_VERSION);
  mockApi = await startMockApiServer();
  config = {
    url: mockApi.url,
    auth_method: '{{ auth_method_keys.0 }}',
    api_version: DEFAULT_API_VERSION,
    log_level: 'info',
    rate_limit: 100,
    timeout_ms: 5000,
    cache_size: 500,
    retry_attempts: 1,
    transport: 'stdio',
    host: '127.0.0.1',
    port: 3000,
  };
});

afterAll(async () => {
  closeStore(store);
  await mockApi.close();
});

describe('search -> get -> call pipeline', () => {
{%- if operations | length > 0 %}
  it('finds, describes, and invokes a real operation end to end', async () => {
    const query = '{{ operations.0.summary | default(value=operations.0.operation_id) }}';
    const results = await searchOperations(store, query, 5);
    expect(results.length).toBeGreaterThan(0);

    const endpoint = await getOperation(store, results[0].operationId);
    expect(endpoint.operationId).toBe(results[0].operationId);

    const authManager = new AuthManager(config.auth_method);
    authManager.setCredentials(buildDummyCredentials());
    const client = new ApiClient(config, authManager);
    await client.execute(endpoint, {});

    expect(mockApi.requests.length).toBeGreaterThan(0);
  });
{%- endif %}
});
