// {{ display_name }} MCP server — generated by mcpify. Do not hand-edit.

import { afterAll, afterEach, 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, getEndpoint, openStore } from '../../../src/data/store-repository.js';
import { ApiClient } from '../../../src/services/api-client.js';
import { callOperation } from '../../../src/tools/call-tool.js';
import { buildDummyCredentials } from '../../helpers/dummy-credentials.js';
import { startMockApiServer, type MockApiServer } from '../../helpers/mock-api-server.js';

const store = openStore(DEFAULT_API_VERSION);
let mockApi: MockApiServer;
let config: Config;

beforeAll(async () => {
  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: 0,
    transport: 'stdio',
    host: '127.0.0.1',
    port: 3000,
  };
});

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

afterEach(() => {
  mockApi.requests.length = 0;
});

describe('callOperation', () => {
  it('throws for an unknown operationId without making an HTTP request', async () => {
    const authManager = new AuthManager(config.auth_method);
    await expect(callOperation(store, config, authManager, 'does-not-exist', {})).rejects.toThrow();
    expect(mockApi.requests).toHaveLength(0);
  });

{%- if operations | length > 0 %}
  it('dispatches a live request for a known operationId', async () => {
    // Exercises ApiClient directly rather than callOperation's full
    // pipeline: the mock server's generic echo body won't conform to this
    // operation's real declared output schema, and validateOutput would
    // reject it — this test is only about the request actually reaching
    // the target with the right method/path/auth, not response shape.
    const authManager = new AuthManager(config.auth_method);
    authManager.setCredentials(buildDummyCredentials());
    const endpoint = getEndpoint(store, '{{ operations.0.operation_id }}')!;
    const client = new ApiClient(config, authManager);
    await client.execute(endpoint, {});
    expect(mockApi.requests).toHaveLength(1);
  });
{%- endif %}
});
