Elffuss BraidHub

kikocis/continue public

169e4d970312 · 5.1 KiBRawPermalink
1import fs from "node:fs";
2import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
3import { GlobalContext } from "../../util/GlobalContext";
4import { getGlobalContextFilePath } from "../../util/paths";
5import { getOauthToken, performAuth, removeMCPAuth } from "./MCPOauth";
6
7// Mock the MCP SDK auth module
8vi.mock("@modelcontextprotocol/sdk/client/auth.js", () => ({
9 auth: vi.fn(),
10}));
11
12// Mock the MCPManagerSingleton to avoid circular imports
13vi.mock("./MCPManagerSingleton", () => ({
14 MCPManagerSingleton: {
15 getInstance: vi.fn(() => ({
16 refreshConnection: vi.fn(),
17 })),
18 },
19}));
20
21describe("MCPOauth", () => {
22 let globalContextFilePath: string;
23 let mockIde: any;
24 let mockMcpServer: any;
25
26 beforeEach(() => {
27 // file is present in the core/test directory
28 globalContextFilePath = getGlobalContextFilePath();
29 if (fs.existsSync(globalContextFilePath)) {
30 fs.unlinkSync(globalContextFilePath);
31 }
32
33 mockIde = {
34 openUrl: vi.fn(),
35 showToast: vi.fn(),
36 };
37
38 mockMcpServer = {
39 id: "test-server",
40 transport: {
41 type: "sse",
42 url: "https://test-server.com",
43 },
44 };
45
46 vi.clearAllMocks();
47 });
48
49 afterEach(() => {
50 if (fs.existsSync(globalContextFilePath)) {
51 fs.unlinkSync(globalContextFilePath);
52 }
53 });
54
55 describe("getOauthToken", () => {
56 test("should return undefined when no tokens are stored", async () => {
57 const result = await getOauthToken("https://test-server.com", mockIde);
58 expect(result).toBeUndefined();
59 });
60
61 test("should return access token when tokens are stored", async () => {
62 const globalContext = new GlobalContext();
63 globalContext.update("mcpOauthStorage", {
64 "https://test-server.com": {
65 tokens: {
66 access_token: "test-access-token",
67 token_type: "Bearer",
68 },
69 },
70 });
71
72 const result = await getOauthToken("https://test-server.com", mockIde);
73 expect(result).toBe("test-access-token");
74 });
75
76 test("should handle different server URLs independently", async () => {
77 const globalContext = new GlobalContext();
78 globalContext.update("mcpOauthStorage", {
79 "https://server1.com": {
80 tokens: {
81 access_token: "token1",
82 token_type: "Bearer",
83 },
84 },
85 "https://server2.com": {
86 tokens: {
87 access_token: "token2",
88 token_type: "Bearer",
89 },
90 },
91 });
92
93 const result1 = await getOauthToken("https://server1.com", mockIde);
94 const result2 = await getOauthToken("https://server2.com", mockIde);
95 const result3 = await getOauthToken("https://server3.com", mockIde);
96
97 expect(result1).toBe("token1");
98 expect(result2).toBe("token2");
99 expect(result3).toBeUndefined();
100 });
101 });
102
103 describe("performAuth", () => {
104 test("should call auth with correct parameters and return auth result", async () => {
105 const { auth } = await import("@modelcontextprotocol/sdk/client/auth.js");
106 const mockAuth = vi.mocked(auth);
107 mockAuth.mockResolvedValue("AUTHORIZED");
108
109 const result = await performAuth(mockMcpServer, mockIde);
110
111 expect(mockAuth).toHaveBeenCalledWith(
112 expect.any(Object), // MCPConnectionOauthProvider instance
113 {
114 serverUrl: "https://test-server.com",
115 },
116 );
117 expect(result).toBe("AUTHORIZED");
118 });
119 });
120
121 describe("removeMCPAuth", () => {
122 test("should clear oauth storage for the server", () => {
123 const globalContext = new GlobalContext();
124 globalContext.update("mcpOauthStorage", {
125 "https://test-server.com": {
126 tokens: {
127 access_token: "test-access-token",
128 token_type: "Bearer",
129 },
130 clientInformation: {
131 client_id: "test-client-id",
132 redirect_uris: ["http://localhost:3000"],
133 },
134 codeVerifier: "test-code-verifier",
135 },
136 "https://other-server.com": {
137 tokens: {
138 access_token: "other-access-token",
139 token_type: "Bearer",
140 },
141 },
142 });
143
144 removeMCPAuth(mockMcpServer, mockIde);
145
146 const updatedStorage = globalContext.get("mcpOauthStorage");
147 expect(updatedStorage).toEqual({
148 "https://other-server.com": {
149 tokens: {
150 access_token: "other-access-token",
151 token_type: "Bearer",
152 },
153 },
154 });
155 });
156
157 test("should handle non-existent server gracefully", () => {
158 const globalContext = new GlobalContext();
159 globalContext.update("mcpOauthStorage", {
160 "https://other-server.com": {
161 tokens: {
162 access_token: "other-access-token",
163 token_type: "Bearer",
164 },
165 },
166 });
167
168 removeMCPAuth(mockMcpServer, mockIde);
169
170 const updatedStorage = globalContext.get("mcpOauthStorage");
171 expect(updatedStorage).toEqual({
172 "https://other-server.com": {
173 tokens: {
174 access_token: "other-access-token",
175 token_type: "Bearer",
176 },
177 },
178 });
179 });
180 });
181});