|
| 1 | +import type { Mock } from "vitest"; |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 3 | +import { TokenManager, TokenError } from "./tokenManager.js"; |
| 4 | +import type { FetchLike } from "@modelcontextprotocol/client"; |
| 5 | + |
| 6 | +const TEST_TOKEN_1 = "token-1"; |
| 7 | +const TEST_TOKEN_2 = "token-2"; |
| 8 | + |
| 9 | +function createManager(fetch: FetchLike): TokenManager { |
| 10 | + return new TokenManager("https://test.com/api/oauth/token", "client_id", "client_secret", 10_000, fetch); |
| 11 | +} |
| 12 | + |
| 13 | +function mockOkFetch(tokens: string[] = [TEST_TOKEN_1]): Mock { |
| 14 | + const mock = vi.fn(); |
| 15 | + for (const token of tokens) { |
| 16 | + mock.mockResolvedValueOnce({ |
| 17 | + ok: true, |
| 18 | + status: 200, |
| 19 | + json: () => ({ access_token: token, expires_in: 3600 }), |
| 20 | + }); |
| 21 | + } |
| 22 | + return mock; |
| 23 | +} |
| 24 | + |
| 25 | +describe("TokenManager", () => { |
| 26 | + beforeEach(() => { |
| 27 | + vi.useFakeTimers(); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + vi.useRealTimers(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("token fetching, caching and refreshing", () => { |
| 35 | + it("fetches a token on the first call", async () => { |
| 36 | + const mockFetch = mockOkFetch(); |
| 37 | + const manager = createManager(mockFetch); |
| 38 | + const token = await manager.getToken(); |
| 39 | + |
| 40 | + expect(token).toBe(TEST_TOKEN_1); |
| 41 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 42 | + }); |
| 43 | + |
| 44 | + it("returns the cached token on subsequent calls", async () => { |
| 45 | + const mockFetch = mockOkFetch(); |
| 46 | + const manager = createManager(mockFetch); |
| 47 | + |
| 48 | + await manager.getToken(); |
| 49 | + const token = await manager.getToken(); |
| 50 | + |
| 51 | + expect(token).toBe(TEST_TOKEN_1); |
| 52 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 53 | + }); |
| 54 | + |
| 55 | + it("refreshes when within 10 minutes to expiration", async () => { |
| 56 | + const mockFetch = mockOkFetch([TEST_TOKEN_1, TEST_TOKEN_2]); |
| 57 | + const manager = createManager(mockFetch); |
| 58 | + |
| 59 | + const token1 = await manager.getToken(); |
| 60 | + vi.setSystemTime(Date.now() + 51 * 60 * 1000); |
| 61 | + const token2 = await manager.getToken(); |
| 62 | + |
| 63 | + expect(token1).toBe(TEST_TOKEN_1); |
| 64 | + expect(token2).toBe(TEST_TOKEN_2); |
| 65 | + expect(mockFetch).toHaveBeenCalledTimes(2); |
| 66 | + }); |
| 67 | + |
| 68 | + it("refreshes when the token is expired", async () => { |
| 69 | + const mockFetch = mockOkFetch([TEST_TOKEN_1, TEST_TOKEN_2]); |
| 70 | + const manager = createManager(mockFetch); |
| 71 | + |
| 72 | + const token1 = await manager.getToken(); |
| 73 | + vi.setSystemTime(Date.now() + 61 * 60 * 1000); |
| 74 | + const token2 = await manager.getToken(); |
| 75 | + |
| 76 | + expect(token1).toBe(TEST_TOKEN_1); |
| 77 | + expect(token2).toBe(TEST_TOKEN_2); |
| 78 | + expect(mockFetch).toHaveBeenCalledTimes(2); |
| 79 | + }); |
| 80 | + |
| 81 | + it("fetches a new token after invalidating", async () => { |
| 82 | + const mockFetch = mockOkFetch([TEST_TOKEN_1, TEST_TOKEN_2]); |
| 83 | + const manager = createManager(mockFetch); |
| 84 | + |
| 85 | + const token1 = await manager.getToken(); |
| 86 | + manager.invalidateToken(); |
| 87 | + const token2 = await manager.getToken(); |
| 88 | + |
| 89 | + expect(token1).toBe(TEST_TOKEN_1); |
| 90 | + expect(token2).toBe(TEST_TOKEN_2); |
| 91 | + expect(mockFetch).toHaveBeenCalledTimes(2); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe("single-flight refresh", () => { |
| 96 | + it("concurrent getToken() calls share a single fetch", async () => { |
| 97 | + const mockFetch = mockOkFetch(); |
| 98 | + const manager = createManager(mockFetch); |
| 99 | + |
| 100 | + const [token1, token2] = await Promise.all([manager.getToken(), manager.getToken()]); |
| 101 | + |
| 102 | + expect(token1).toBe(TEST_TOKEN_1); |
| 103 | + expect(token2).toBe(TEST_TOKEN_1); |
| 104 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("error cases", () => { |
| 109 | + it("throws TokenError with the HTTP status code on a non-OK response", async () => { |
| 110 | + const mockFetch = vi.fn().mockResolvedValue({ |
| 111 | + ok: false, |
| 112 | + status: 401, |
| 113 | + text: () => "Unauthorized", |
| 114 | + }); |
| 115 | + const manager = createManager(mockFetch); |
| 116 | + |
| 117 | + const error = await manager.getToken().catch((e: unknown) => e); |
| 118 | + expect(error).toBeInstanceOf(TokenError); |
| 119 | + expect((error as TokenError).statusCode).toBe(401); |
| 120 | + expect((error as TokenError).message).toContain("Token request failed"); |
| 121 | + }); |
| 122 | + |
| 123 | + it("throws TokenError with a timeout message on TimeoutError", async () => { |
| 124 | + const mockFetch = vi.fn().mockRejectedValue(Object.assign(new Error("timeout"), { name: "TimeoutError" })); |
| 125 | + const manager = createManager(mockFetch); |
| 126 | + |
| 127 | + const error = await manager.getToken().catch((e: unknown) => e); |
| 128 | + expect(error).toBeInstanceOf(TokenError); |
| 129 | + expect((error as TokenError).message).toContain("Token request timed out"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("throws TokenError when access_token is missing from the response", async () => { |
| 133 | + const mockFetch = vi.fn().mockResolvedValue({ |
| 134 | + ok: true, |
| 135 | + status: 200, |
| 136 | + json: () => ({ expires_in: 3600 }), |
| 137 | + }); |
| 138 | + const manager = createManager(mockFetch); |
| 139 | + |
| 140 | + const error = await manager.getToken().catch((e: unknown) => e); |
| 141 | + expect(error).toBeInstanceOf(TokenError); |
| 142 | + expect((error as TokenError).message).toContain("Token response missing access_token"); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments