AI-native design editor. Open-source Figma alternative.
Add MCP server integration tests (closes #19)
Danila Poyarkov7a5f6b370746
diff --git a/bun.lock b/bun.lockindex 9dd63803d..8c5aa8c35 100644--- a/bun.lock+++ b/bun.lock@@ -40,6 +40,7 @@},"devDependencies": {"@iconify-json/lucide": "^1.2.94",+ "@modelcontextprotocol/sdk": "^1.27.1","@playwright/test": "^1.58.2","@tauri-apps/cli": "^2","@types/culori": "^4.0.1",@@ -60,7 +61,7 @@},"packages/cli": {"name": "@open-pencil/cli",- "version": "0.4.0",+ "version": "0.4.2","bin": {"openpencil": "./src/index.ts",},@@ -76,7 +77,7 @@},"packages/core": {"name": "@open-pencil/core",- "version": "0.4.0",+ "version": "0.4.2","dependencies": {"canvaskit-wasm": "^0.40.0","culori": "^4.0.2",@@ -98,7 +99,7 @@},"packages/mcp": {"name": "@open-pencil/mcp",- "version": "0.4.0",+ "version": "0.4.2","bin": {"openpencil-mcp": "./dist/index.js","openpencil-mcp-http": "./dist/http.js",@@ -106,7 +107,7 @@"dependencies": {"@hono/node-server": "^1.19.9","@modelcontextprotocol/sdk": "^1.25.2",- "@open-pencil/core": "^0.3.4",+ "@open-pencil/core": "workspace:*","canvaskit-wasm": "^0.40.0","hono": "^4.11.4","zod": "^3.25.0",@@ -1833,8 +1834,6 @@"@multiformats/dns/@libp2p/interface": ["@libp2p/[email protected]", "", { "dependencies": { "@multiformats/dns": "^1.0.6", "@multiformats/multiaddr": "^13.0.1", "main-event": "^1.0.1", "multiformats": "^13.4.0", "progress-events": "^1.0.1", "uint8arraylist": "^2.4.8" } }, "sha512-RE7/XyvC47fQBe1cHxhMvepYKa5bFCUyFrrpj8PuM0E7JtzxU7F+Du5j4VXbg2yLDcToe0+j8mB7jvwE2AThYw=="],- "@open-pencil/mcp/@open-pencil/core": ["@open-pencil/[email protected]", "", { "dependencies": { "canvaskit-wasm": "^0.40.0", "culori": "^4.0.2", "fflate": "^0.8.2", "fzstd": "^0.1.1", "yoga-layout": "^3.2.1" } }, "sha512-IF/FIKdBCYASYF9tUL2iHXbpiLybgpq8KPh+9uZb6/7BQainqmAnu1DGi633LOUGHr6QXWjJ5OTNMxOJltAPjQ=="],-"@open-pencil/mcp/@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-akNQMv0wW5uyRpD2v2IEyRSZiR+BeGuoB6L310EgGObO44HSMNT8z1xzio28V8qOrgYaopIDNA18YgdXd+qTiw=="],"@tailwindcss/oxide-wasm32-wasi/@emnapi/core": ["@emnapi/[email protected]", "", { "dependencies": { "@emnapi/wasi-threads": "1.1.0", "tslib": "^2.4.0" }, "bundled": true }, "sha512-AvT9QFpxK0Zd8J0jopedNm+w/2fIzvtPKPjqyw9jwvBaReTTqPBk9Hixaz7KbjimP+QNz605/XnjFcDAL2pqBg=="],diff --git a/components.d.ts b/components.d.tsindex dfbbd5b11..ed334ae4b 100644--- a/components.d.ts+++ b/components.d.ts@@ -92,6 +92,7 @@ declare module 'vue' {SafariBanner: typeof import('./src/components/SafariBanner.vue')['default']ScrubInput: typeof import('./src/components/ScrubInput.vue')['default']StrokeSection: typeof import('./src/components/properties/StrokeSection.vue')['default']+ TabBar: typeof import('./src/components/TabBar.vue')['default']Toolbar: typeof import('./src/components/Toolbar.vue')['default']TypographySection: typeof import('./src/components/properties/TypographySection.vue')['default']VariablesDialog: typeof import('./src/components/VariablesDialog.vue')['default']diff --git a/package.json b/package.jsonindex 911002ef2..94dd4dc6d 100644--- a/package.json+++ b/package.json@@ -64,6 +64,7 @@},"devDependencies": {"@iconify-json/lucide": "^1.2.94",+ "@modelcontextprotocol/sdk": "^1.27.1","@playwright/test": "^1.58.2","@tauri-apps/cli": "^2","@types/culori": "^4.0.1",diff --git a/packages/mcp/package.json b/packages/mcp/package.jsonindex d5cc2c984..1d88cae0e 100644--- a/packages/mcp/package.json+++ b/packages/mcp/package.json@@ -27,7 +27,7 @@},"dependencies": {"@modelcontextprotocol/sdk": "^1.25.2",- "@open-pencil/core": "^0.3.4",+ "@open-pencil/core": "workspace:*","canvaskit-wasm": "^0.40.0","@hono/node-server": "^1.19.9","hono": "^4.11.4",diff --git a/tests/engine/mcp-server.test.ts b/tests/engine/mcp-server.test.tsnew file mode 100644index 000000000..f19c657e2--- /dev/null+++ b/tests/engine/mcp-server.test.ts@@ -0,0 +1,220 @@+import { describe, expect, test, beforeEach, afterEach } from 'bun:test'+import { join } from 'node:path'+import { writeFile, unlink } from 'node:fs/promises'++import { Client } from '@modelcontextprotocol/sdk/client/index.js'+import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'++import { createServer } from '../../packages/mcp/src/server'+import { SceneGraph, exportFigFile } from '@open-pencil/core'++function parseResult(result: { content: { type: string; text: string }[] }): unknown {+ return JSON.parse(result.content[0].text)+}++describe('MCP server', () => {+ let client: Client+ let cleanup: () => Promise<void>++ beforeEach(async () => {+ const server = createServer('0.0.0-test')+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair()+ await server.connect(serverTransport)+ client = new Client({ name: 'test-client', version: '0.0.0' })+ await client.connect(clientTransport)+ cleanup = async () => {+ await client.close()+ await server.close()+ }+ })++ afterEach(async () => {+ await cleanup()+ })++ test('lists all registered tools', async () => {+ const { tools } = await client.listTools()+ const names = tools.map((t) => t.name)+ expect(names).toContain('new_document')+ expect(names).toContain('open_file')+ expect(names).toContain('save_file')+ expect(names).toContain('create_shape')+ expect(names).toContain('set_fill')+ expect(names).toContain('get_page_tree')+ expect(tools.length).toBeGreaterThan(70)+ })++ test('tools have descriptions and input schemas', async () => {+ const { tools } = await client.listTools()+ for (const tool of tools) {+ expect(tool.description).toBeTruthy()+ expect(tool.inputSchema).toBeDefined()+ }+ })++ test('new_document creates an empty document', async () => {+ const result = await client.callTool({ name: 'new_document', arguments: {} })+ const data = parseResult(result) as { page: string; id: string }+ expect(data.page).toBe('Page 1')+ expect(data.id).toBeTruthy()+ })++ test('tools fail without a loaded document', async () => {+ const result = await client.callTool({ name: 'get_page_tree', arguments: {} })+ expect(result.isError).toBe(true)+ const data = parseResult(result) as { error: string }+ expect(data.error).toContain('No document loaded')+ })++ test('create_shape after new_document', async () => {+ await client.callTool({ name: 'new_document', arguments: {} })+ const result = await client.callTool({+ name: 'create_shape',+ arguments: { type: 'FRAME', x: 0, y: 0, width: 200, height: 100, name: 'Test' }+ })+ const data = parseResult(result) as { id: string; name: string; type: string }+ expect(data.name).toBe('Test')+ expect(data.type).toBe('FRAME')+ expect(data.id).toBeTruthy()+ })++ test('set_fill validates color string', async () => {+ await client.callTool({ name: 'new_document', arguments: {} })+ const shape = parseResult(+ await client.callTool({+ name: 'create_shape',+ arguments: { type: 'RECTANGLE', x: 0, y: 0, width: 50, height: 50 }+ })+ ) as { id: string }++ await client.callTool({+ name: 'set_fill',+ arguments: { id: shape.id, color: '#00ff00' }+ })++ const node = parseResult(+ await client.callTool({ name: 'get_node', arguments: { id: shape.id } })+ ) as { fills: { color: { r: number; g: number; b: number } }[] }+ expect(node.fills[0].color.g).toBeCloseTo(1)+ })++ test('delete_node removes created node', async () => {+ await client.callTool({ name: 'new_document', arguments: {} })+ const shape = parseResult(+ await client.callTool({+ name: 'create_shape',+ arguments: { type: 'RECTANGLE', x: 0, y: 0, width: 50, height: 50 }+ })+ ) as { id: string }++ await client.callTool({ name: 'delete_node', arguments: { id: shape.id } })++ const result = await client.callTool({ name: 'get_node', arguments: { id: shape.id } })+ const data = parseResult(result) as { error?: string }+ expect(data.error).toContain('not found')+ })++ test('get_node on nonexistent ID returns error in response', async () => {+ await client.callTool({ name: 'new_document', arguments: {} })+ const result = await client.callTool({ name: 'get_node', arguments: { id: 'bogus' } })+ const data = parseResult(result) as { error: string }+ expect(data.error).toContain('not found')+ })++ test('open_file loads a .fig file', async () => {+ const fixturePath = join(import.meta.dir, '..', 'fixtures', 'nuxtui.fig')+ const result = await client.callTool({ name: 'open_file', arguments: { path: fixturePath } })+ const data = parseResult(result) as { pages: { name: string }[]; currentPage: string }+ expect(data.pages.length).toBeGreaterThan(0)+ expect(data.currentPage).toBeTruthy()+ })++ test('open_file with invalid path returns error', async () => {+ const result = await client.callTool({+ name: 'open_file',+ arguments: { path: '/nonexistent/file.fig' }+ })+ expect(result.isError).toBe(true)+ })++ test('save_file roundtrips a document', async () => {+ const tmpPath = join(import.meta.dir, '..', `_mcp_test_${Date.now()}.fig`)+ try {+ await client.callTool({ name: 'new_document', arguments: {} })+ await client.callTool({+ name: 'create_shape',+ arguments: { type: 'FRAME', x: 0, y: 0, width: 300, height: 200, name: 'Saved' }+ })++ const saveResult = await client.callTool({+ name: 'save_file',+ arguments: { path: tmpPath }+ })+ const saved = parseResult(saveResult) as { saved: string; bytes: number }+ expect(saved.bytes).toBeGreaterThan(0)++ await client.callTool({ name: 'open_file', arguments: { path: tmpPath } })+ const tree = parseResult(+ await client.callTool({ name: 'get_page_tree', arguments: {} })+ ) as { children: { name: string }[] }+ expect(tree.children.some((c) => c.name === 'Saved')).toBe(true)+ } finally {+ await unlink(tmpPath).catch(() => {})+ }+ })++ test('save_file without document returns error', async () => {+ const result = await client.callTool({+ name: 'save_file',+ arguments: { path: '/tmp/_mcp_no_doc.fig' }+ })+ expect(result.isError).toBe(true)+ })++ test('full workflow: new → create → query → delete', async () => {+ await client.callTool({ name: 'new_document', arguments: {} })++ const frame = parseResult(+ await client.callTool({+ name: 'create_shape',+ arguments: { type: 'FRAME', x: 10, y: 20, width: 400, height: 300, name: 'Container' }+ })+ ) as { id: string }++ const child = parseResult(+ await client.callTool({+ name: 'create_shape',+ arguments: {+ type: 'TEXT',+ x: 0,+ y: 0,+ width: 200,+ height: 30,+ name: 'Label',+ parent_id: frame.id+ }+ })+ ) as { id: string }++ await client.callTool({+ name: 'set_fill',+ arguments: { id: frame.id, color: '#336699' }+ })++ const tree = parseResult(+ await client.callTool({ name: 'get_page_tree', arguments: {} })+ ) as { children: { id: string; name: string; children?: { name: string }[] }[] }++ const container = tree.children.find((c) => c.name === 'Container')+ expect(container).toBeDefined()+ expect(container!.children?.some((c) => c.name === 'Label')).toBe(true)++ await client.callTool({ name: 'delete_node', arguments: { id: child.id } })++ const tree2 = parseResult(+ await client.callTool({ name: 'get_page_tree', arguments: {} })+ ) as { children: { id: string; children?: unknown[] }[] }+ const container2 = tree2.children.find((c) => c.id === frame.id)+ expect(container2!.children ?? []).toHaveLength(0)+ })+})