|
| 1 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { AndroidMCPServer } from '../src/server.js'; |
| 3 | + |
| 4 | +describe('AndroidMCPServer HTTP mode', () => { |
| 5 | + let server: AndroidMCPServer; |
| 6 | + const testPort = 13580; // Use different port than web-bridge-mcp |
| 7 | + const testHost = 'localhost'; |
| 8 | + |
| 9 | + beforeAll(async () => { |
| 10 | + server = new AndroidMCPServer(); |
| 11 | + }); |
| 12 | + |
| 13 | + afterAll(async () => { |
| 14 | + // Cleanup will be handled by process exit |
| 15 | + }); |
| 16 | + |
| 17 | + it('should start HTTP server successfully', async () => { |
| 18 | + // Mock process.exit to prevent test exit |
| 19 | + const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => { |
| 20 | + throw new Error('process.exit called'); |
| 21 | + }) as any); |
| 22 | + |
| 23 | + try { |
| 24 | + // Start server in background |
| 25 | + const serverPromise = server.launchHttp({ |
| 26 | + port: testPort, |
| 27 | + host: testHost, |
| 28 | + }); |
| 29 | + |
| 30 | + // Give server time to start |
| 31 | + await new Promise((resolve) => setTimeout(resolve, 1500)); |
| 32 | + |
| 33 | + // Simply verify the server is listening by trying to connect |
| 34 | + const response = await fetch(`http://${testHost}:${testPort}/mcp`, { |
| 35 | + method: 'POST', |
| 36 | + headers: { |
| 37 | + 'Content-Type': 'application/json', |
| 38 | + }, |
| 39 | + body: JSON.stringify({ |
| 40 | + jsonrpc: '2.0', |
| 41 | + method: 'initialize', |
| 42 | + params: { |
| 43 | + protocolVersion: '2024-11-05', |
| 44 | + capabilities: {}, |
| 45 | + clientInfo: { |
| 46 | + name: 'test-client', |
| 47 | + version: '1.0.0', |
| 48 | + }, |
| 49 | + }, |
| 50 | + id: 1, |
| 51 | + }), |
| 52 | + }); |
| 53 | + |
| 54 | + // Server should respond (even if initialization fails without device) |
| 55 | + expect(response.status).toBeGreaterThanOrEqual(200); |
| 56 | + expect(response.status).toBeLessThan(600); |
| 57 | + |
| 58 | + console.log('✓ Android MCP server started and responding'); |
| 59 | + } finally { |
| 60 | + exitSpy.mockRestore(); |
| 61 | + } |
| 62 | + }, 10000); |
| 63 | + |
| 64 | + it('should reject invalid port numbers', async () => { |
| 65 | + const invalidServer = new AndroidMCPServer(); |
| 66 | + |
| 67 | + await expect( |
| 68 | + invalidServer.launchHttp({ |
| 69 | + port: -1, |
| 70 | + host: testHost, |
| 71 | + }), |
| 72 | + ).rejects.toThrow(/Invalid port number/); |
| 73 | + |
| 74 | + await expect( |
| 75 | + invalidServer.launchHttp({ |
| 76 | + port: 99999, |
| 77 | + host: testHost, |
| 78 | + }), |
| 79 | + ).rejects.toThrow(/Invalid port number/); |
| 80 | + }); |
| 81 | +}); |
0 commit comments