|
5 | 5 | import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
6 | 6 | import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; |
7 | 7 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
8 | | -import { createElectronMcpServer } from "./index"; |
| 8 | +import { createElectronMcpServer, recommendedGuards } from "./index"; |
9 | 9 |
|
10 | 10 | // Ephemeral-port helper — the harness picks a free port via `port: 0` |
11 | 11 | // and the handle exposes the resolved URL. |
@@ -120,3 +120,68 @@ describe("createElectronMcpServer", () => { |
120 | 120 | expect(handle.isRunning).toBe(false); |
121 | 121 | }); |
122 | 122 | }); |
| 123 | + |
| 124 | +describe("recommendedGuards", () => { |
| 125 | + it("throws a packaged-state error when called without options", () => { |
| 126 | + expect(() => recommendedGuards()).toThrow(/isPackaged/i); |
| 127 | + }); |
| 128 | + |
| 129 | + it("allows startup when the app is unpackaged and the opt-in env var is enabled", () => { |
| 130 | + expect( |
| 131 | + recommendedGuards({ |
| 132 | + isPackaged: false, |
| 133 | + env: { MY_APP_MCP: "1" }, |
| 134 | + envVar: "MY_APP_MCP", |
| 135 | + }), |
| 136 | + ).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it("blocks startup when the opt-in env var is not enabled", () => { |
| 140 | + expect( |
| 141 | + recommendedGuards({ |
| 142 | + isPackaged: false, |
| 143 | + env: {}, |
| 144 | + envVar: "MY_APP_MCP", |
| 145 | + }), |
| 146 | + ).toBe(false); |
| 147 | + }); |
| 148 | + |
| 149 | + it("blocks startup in packaged builds even when the opt-in env var is enabled", () => { |
| 150 | + expect( |
| 151 | + recommendedGuards({ |
| 152 | + isPackaged: true, |
| 153 | + env: { MY_APP_MCP: "1" }, |
| 154 | + envVar: "MY_APP_MCP", |
| 155 | + }), |
| 156 | + ).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + it("accepts an Electron app-like object for the packaged-state check", () => { |
| 160 | + expect( |
| 161 | + recommendedGuards({ |
| 162 | + app: { isPackaged: false }, |
| 163 | + env: { MY_APP_MCP: "1" }, |
| 164 | + envVar: "MY_APP_MCP", |
| 165 | + }), |
| 166 | + ).toBe(true); |
| 167 | + }); |
| 168 | + |
| 169 | + it("blocks startup when an Electron app-like object reports a packaged build", () => { |
| 170 | + expect( |
| 171 | + recommendedGuards({ |
| 172 | + app: { isPackaged: true }, |
| 173 | + env: { MY_APP_MCP: "1" }, |
| 174 | + envVar: "MY_APP_MCP", |
| 175 | + }), |
| 176 | + ).toBe(false); |
| 177 | + }); |
| 178 | + |
| 179 | + it("throws when no packaged-state source is provided", () => { |
| 180 | + expect(() => |
| 181 | + recommendedGuards({ |
| 182 | + env: { MY_APP_MCP: "1" }, |
| 183 | + envVar: "MY_APP_MCP", |
| 184 | + }), |
| 185 | + ).toThrow(/isPackaged/i); |
| 186 | + }); |
| 187 | +}); |
0 commit comments