|
| 1 | +import { requestUrl } from "obsidian"; |
| 2 | +import { UniRateClient, UniRateClientError } from "../src/client"; |
| 3 | + |
| 4 | +const mockedRequestUrl = requestUrl as unknown as jest.Mock; |
| 5 | + |
| 6 | +function makeResponse(body: unknown, status = 200) { |
| 7 | + return { |
| 8 | + status, |
| 9 | + headers: {}, |
| 10 | + arrayBuffer: new ArrayBuffer(0), |
| 11 | + text: JSON.stringify(body), |
| 12 | + json: body, |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +describe("UniRateClient", () => { |
| 17 | + beforeEach(() => { |
| 18 | + mockedRequestUrl.mockReset(); |
| 19 | + }); |
| 20 | + |
| 21 | + test("getRate hits /api/rates and returns the target rate", async () => { |
| 22 | + mockedRequestUrl.mockResolvedValueOnce(makeResponse({ rates: { EUR: 0.92 } })); |
| 23 | + const c = new UniRateClient("k"); |
| 24 | + expect(await c.getRate("USD", "EUR")).toBe(0.92); |
| 25 | + const call = mockedRequestUrl.mock.calls[0][0]; |
| 26 | + expect(call.url).toContain("/api/rates"); |
| 27 | + expect(call.url).toContain("from=USD"); |
| 28 | + expect(call.url).toContain("to=EUR"); |
| 29 | + expect(call.url).toContain("api_key=k"); |
| 30 | + }); |
| 31 | + |
| 32 | + test("getRate routes to historical endpoint when date is given", async () => { |
| 33 | + mockedRequestUrl.mockResolvedValueOnce(makeResponse({ rates: { EUR: 0.91 } })); |
| 34 | + const c = new UniRateClient("k"); |
| 35 | + expect(await c.getRate("USD", "EUR", "2024-01-15")).toBe(0.91); |
| 36 | + expect(mockedRequestUrl.mock.calls[0][0].url).toContain("/api/historical/rates"); |
| 37 | + expect(mockedRequestUrl.mock.calls[0][0].url).toContain("date=2024-01-15"); |
| 38 | + }); |
| 39 | + |
| 40 | + test("convert multiplies amount by rate", async () => { |
| 41 | + mockedRequestUrl.mockResolvedValueOnce(makeResponse({ rates: { EUR: 0.5 } })); |
| 42 | + const c = new UniRateClient("k"); |
| 43 | + const r = await c.convert("USD", "EUR", 100); |
| 44 | + expect(r.rate).toBe(0.5); |
| 45 | + expect(r.result).toBe(50); |
| 46 | + }); |
| 47 | + |
| 48 | + test("throws AuthenticationError-style on 401", async () => { |
| 49 | + mockedRequestUrl.mockResolvedValueOnce(makeResponse({}, 401)); |
| 50 | + const c = new UniRateClient("bad"); |
| 51 | + await expect(c.getRate("USD", "EUR")).rejects.toMatchObject({ |
| 52 | + name: "UniRateClientError", |
| 53 | + status: 401, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + test("throws on 403 with Pro-plan message", async () => { |
| 58 | + mockedRequestUrl.mockResolvedValueOnce(makeResponse({}, 403)); |
| 59 | + const c = new UniRateClient("k"); |
| 60 | + await expect(c.getRate("USD", "XAU", "2024-01-15")).rejects.toMatchObject({ |
| 61 | + status: 403, |
| 62 | + message: expect.stringContaining("Pro"), |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + test("throws on 429", async () => { |
| 67 | + mockedRequestUrl.mockResolvedValueOnce(makeResponse({}, 429)); |
| 68 | + const c = new UniRateClient("k"); |
| 69 | + await expect(c.getRate("USD", "EUR")).rejects.toMatchObject({ status: 429 }); |
| 70 | + }); |
| 71 | + |
| 72 | + test("throws on 404", async () => { |
| 73 | + mockedRequestUrl.mockResolvedValueOnce(makeResponse({}, 404)); |
| 74 | + const c = new UniRateClient("k"); |
| 75 | + await expect(c.getRate("USD", "ZZZ")).rejects.toMatchObject({ status: 404 }); |
| 76 | + }); |
| 77 | + |
| 78 | + test("throws when API key is missing", async () => { |
| 79 | + const c = new UniRateClient(""); |
| 80 | + await expect(c.getRate("USD", "EUR")).rejects.toBeInstanceOf(UniRateClientError); |
| 81 | + expect(mockedRequestUrl).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + test("listCurrencies sorts and returns the list", async () => { |
| 85 | + mockedRequestUrl.mockResolvedValueOnce( |
| 86 | + makeResponse({ currencies: ["EUR", "USD", "GBP"] }), |
| 87 | + ); |
| 88 | + const c = new UniRateClient("k"); |
| 89 | + expect(await c.listCurrencies()).toEqual(["EUR", "GBP", "USD"]); |
| 90 | + }); |
| 91 | + |
| 92 | + test("throws when /api/rates response is missing target", async () => { |
| 93 | + mockedRequestUrl.mockResolvedValueOnce(makeResponse({ rates: { JPY: 150 } })); |
| 94 | + const c = new UniRateClient("k"); |
| 95 | + await expect(c.getRate("USD", "EUR")).rejects.toBeInstanceOf(UniRateClientError); |
| 96 | + }); |
| 97 | + |
| 98 | + test("sends Accept: application/json header (workaround for /api/currencies HTML 404)", async () => { |
| 99 | + mockedRequestUrl.mockResolvedValueOnce(makeResponse({ currencies: ["USD"] })); |
| 100 | + const c = new UniRateClient("k"); |
| 101 | + await c.listCurrencies(); |
| 102 | + const call = mockedRequestUrl.mock.calls[0][0]; |
| 103 | + expect(call.headers).toMatchObject({ Accept: "application/json" }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments