diff --git a/app/__test__/app/mcp/mcp.server.test.ts b/app/__test__/app/mcp/mcp.server.test.ts new file mode 100644 index 0000000..29113fd --- /dev/null +++ b/app/__test__/app/mcp/mcp.server.test.ts @@ -0,0 +1,62 @@ +import { describe, test, expect, beforeAll, mock } from "bun:test"; +import { type App, createApp, createMcpToolCaller } from "core/test/utils"; +import { getSystemMcp } from "modules/mcp/system-mcp"; + +/** + * - [x] config_server_get + * - [x] config_server_update + */ +describe("mcp system", async () => { + let app: App; + let server: ReturnType; + beforeAll(async () => { + app = createApp({ + initialConfig: { + server: { + mcp: { + enabled: true, + }, + }, + }, + }); + await app.build(); + server = getSystemMcp(app); + }); + + const tool = createMcpToolCaller(); + + test("config_server_get", async () => { + const result = await tool(server, "config_server_get", {}); + expect(result).toEqual({ + path: "", + secrets: false, + partial: false, + value: app.toJSON().server, + }); + }); + + test("config_server_update", async () => { + const original = app.toJSON().server; + const result = await tool(server, "config_server_update", { + value: { + cors: { + origin: "http://localhost", + }, + }, + return_config: true, + }); + + expect(result).toEqual({ + success: true, + module: "server", + config: { + ...original, + cors: { + ...original.cors, + origin: "http://localhost", + }, + }, + }); + expect(app.toJSON().server.cors.origin).toBe("http://localhost"); + }); +}); diff --git a/app/__test__/app/mcp/mcp.system.test.ts b/app/__test__/app/mcp/mcp.system.test.ts index 9808a51..60be89b 100644 --- a/app/__test__/app/mcp/mcp.system.test.ts +++ b/app/__test__/app/mcp/mcp.system.test.ts @@ -1,16 +1,15 @@ -import { describe, test, expect, beforeAll } from "bun:test"; +import { AppEvents } from "App"; +import { describe, test, expect, beforeAll, mock } from "bun:test"; import { type App, createApp, createMcpToolCaller } from "core/test/utils"; import { getSystemMcp } from "modules/mcp/system-mcp"; import { inspect } from "node:util"; inspect.defaultOptions.depth = 10; /** - * - [ ] system_config - * - [ ] system_build - * - [ ] system_ping - * - [ ] system_info - * - [ ] config_server_get - * - [ ] config_server_update + * - [x] system_config + * - [x] system_build + * - [x] system_ping + * - [x] system_info */ describe("mcp system", async () => { let app: App; @@ -35,4 +34,25 @@ describe("mcp system", async () => { const result = await tool(server, "system_ping", {}); expect(result).toEqual({ pong: true }); }); + + test("system_info", async () => { + const result = await tool(server, "system_info", {}); + expect(Object.keys(result).length).toBeGreaterThan(0); + expect(Object.keys(result)).toContainValues(["version", "runtime", "connection"]); + }); + + test("system_build", async () => { + const called = mock(() => null); + + app.emgr.onEvent(AppEvents.AppBuiltEvent, () => void called(), { once: true }); + + const result = await tool(server, "system_build", {}); + expect(called).toHaveBeenCalledTimes(1); + expect(result.success).toBe(true); + }); + + test("system_config", async () => { + const result = await tool(server, "system_config", {}); + expect(result).toEqual(app.toJSON()); + }); }); diff --git a/app/src/core/test/utils.ts b/app/src/core/test/utils.ts index 30e0642..5702724 100644 --- a/app/src/core/test/utils.ts +++ b/app/src/core/test/utils.ts @@ -22,6 +22,11 @@ export function createMcpToolCaller() { arguments: args, }, }); + + if ((res.result as any)?.isError) { + throw new Error((res.result as any)?.content?.[0]?.text ?? "Unknown error"); + } + return JSON.parse((res.result as any)?.content?.[0]?.text ?? "null"); }; }