UNPKG

precious-metal-mcp

Version:

An MCP tool to get precious metal prices.

70 lines (69 loc) 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const index_1 = require("./index"); // Mock the finnhub client vitest_1.vi.mock("./index", async (importOriginal) => { const original = await importOriginal(); return { ...original, finnhubClient: { get: vitest_1.vi.fn(), }, }; }); // We need to import the mocked client *after* the mock is set up const { finnhubClient } = await import("./index"); (0, vitest_1.describe)("getPreciousMetalPriceLogic", () => { (0, vitest_1.beforeEach)(() => { // Reset mocks before each test vitest_1.vi.resetAllMocks(); }); (0, vitest_1.it)("should calculate the price for gold in New York correctly", async () => { // Arrange: Set up the mock responses from the API finnhubClient.get.mockImplementation((endpoint, { params }) => { if (params.symbol === "OANDA:XAU_USD") { return Promise.resolve({ data: { c: 2000 } }); // Gold price: $2000/oz } if (params.symbol === "OANDA:USD_CNY") { return Promise.resolve({ data: { c: 7.2 } }); // Exchange rate: 7.2 CNY/USD } return Promise.reject(new Error("Unexpected API call")); }); const args = { metal: "gold", type: "spot", market: "new_york", }; // Act: Call the function const result = await (0, index_1.getPreciousMetalPriceLogic)(args); // Assert: Check if the result is correct const expectedPrice = (2000 * 7.2) / index_1.TROY_OUNCE_TO_GRAMS; (0, vitest_1.expect)(result.converted_price.value).toBeCloseTo(expectedPrice, 2); (0, vitest_1.expect)(result.original_price.value).toBe(2000); (0, vitest_1.expect)(result.exchange_rate.rate).toBe(7.2); (0, vitest_1.expect)(result.converted_price.unit).toBe("CNY/gram"); }); (0, vitest_1.it)("should handle API failure gracefully", async () => { // Arrange: Mock a failed API call finnhubClient.get.mockRejectedValue(new Error("API is down")); const args = { metal: "silver", type: "spot", market: "london", }; // Act & Assert: Expect the function to throw an error await (0, vitest_1.expect)((0, index_1.getPreciousMetalPriceLogic)(args)).rejects.toThrow("API is down"); }); (0, vitest_1.it)("should handle invalid price from API", async () => { // Arrange: Mock a response with a zero price finnhubClient.get.mockResolvedValue({ data: { c: 0 } }); const args = { metal: "gold", type: "spot", market: "new_york", }; // Act & Assert await (0, vitest_1.expect)((0, index_1.getPreciousMetalPriceLogic)(args)).rejects.toThrow("Could not fetch a valid price for symbol OANDA:XAU_USD"); }); });