Skip to main content
Glama
api.test.ts2.42 kB
import { beforeEach, describe, expect, mock, test } from "bun:test"; import { getApiKey, makeApiRequest } from "./api.js"; // Mock fetch globally global.fetch = mock(() => {}); describe("API utilities", () => { beforeEach(() => { mock.restore(); // Clear environment variables process.env.CAPACITIES_API_KEY = undefined; }); describe("getApiKey", () => { test("should return API key from environment variable", () => { process.env.CAPACITIES_API_KEY = "test-api-key"; expect(getApiKey()).toBe("test-api-key"); }); test("should throw error when API key is not set", () => { expect(() => getApiKey()).toThrow( "CAPACITIES_API_KEY environment variable is required", ); }); }); describe("makeApiRequest", () => { test("should make successful API request with proper headers", async () => { process.env.CAPACITIES_API_KEY = "test-api-key"; const mockResponse = { ok: true, json: async () => ({ success: true }), }; global.fetch = mock(() => Promise.resolve(mockResponse)); const response = await makeApiRequest("/test-endpoint"); expect(global.fetch).toHaveBeenCalledWith( "https://api.capacities.io/test-endpoint", { headers: { Authorization: "Bearer test-api-key", "Content-Type": "application/json", }, }, ); expect(response).toBe(mockResponse); }); test("should pass through additional options", async () => { process.env.CAPACITIES_API_KEY = "test-api-key"; const mockResponse = { ok: true }; global.fetch = mock(() => Promise.resolve(mockResponse)); await makeApiRequest("/test", { method: "POST", body: JSON.stringify({ test: true }), }); expect(global.fetch).toHaveBeenCalledWith( "https://api.capacities.io/test", { method: "POST", body: JSON.stringify({ test: true }), headers: { Authorization: "Bearer test-api-key", "Content-Type": "application/json", }, }, ); }); test("should throw error for non-ok responses", async () => { process.env.CAPACITIES_API_KEY = "test-api-key"; const mockResponse = { ok: false, status: 400, statusText: "Bad Request", text: async () => "Invalid request", }; global.fetch = mock(() => Promise.resolve(mockResponse)); await expect(makeApiRequest("/test")).rejects.toThrow( "Capacities API error: 400 Bad Request - Invalid request", ); }); }); });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Im-Hal-9K/Capacities-MCP-Plus'

If you have feedback or need assistance with the MCP directory API, please join our Discord server