|
| 1 | +import { spawn, ChildProcess, spawnSync } from 'child_process'; |
| 2 | +import { describe, it, before, after } from 'mocha'; |
| 3 | +import { strict as assert } from 'assert'; |
| 4 | +import path from 'path'; |
| 5 | + |
| 6 | +describe('jsonrpc api common', () => { |
| 7 | + let serverProcess: ChildProcess; |
| 8 | + const serverUrl = 'http://localhost:12000'; |
| 9 | + const timeout = 10000; |
| 10 | + |
| 11 | + before(async function () { |
| 12 | + this.timeout(timeout); |
| 13 | + |
| 14 | + // Start the mobilecli server |
| 15 | + serverProcess = spawn('../mobilecli', ['server', 'start'], { |
| 16 | + cwd: path.dirname(__filename), |
| 17 | + stdio: ['ignore', 'pipe', 'pipe'] |
| 18 | + }); |
| 19 | + |
| 20 | + // Wait for server to start by polling the endpoint |
| 21 | + await waitForServer(serverUrl, 8000); // Wait up to 8 seconds for server to start |
| 22 | + }); |
| 23 | + |
| 24 | + after(() => { |
| 25 | + // Clean up the server process |
| 26 | + if (serverProcess) { |
| 27 | + serverProcess.kill('SIGTERM'); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return status "ok" from root endpoint', async () => { |
| 32 | + const response = await fetch(serverUrl); |
| 33 | + assert.equal(response.status, 200); |
| 34 | + |
| 35 | + const data = await response.json(); |
| 36 | + assert.deepEqual(data, { status: 'ok' }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return method_not_allowed for GET /rpc endpoint', async () => { |
| 40 | + const response = await fetch(`${serverUrl}/rpc`); |
| 41 | + assert.equal(response.status, 405); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return method_not_allowed for POST /rpc endpoint', async () => { |
| 45 | + const response = await fetch(`${serverUrl}/rpc`, { method: 'POST' }); |
| 46 | + assert.equal(response.status, 200); |
| 47 | + const json = await response.json(); |
| 48 | + assert.equal(json.jsonrpc, '2.0'); |
| 49 | + assert.equal(json.error.code, -32700); |
| 50 | + assert.equal(json.error.data, 'expecting jsonrpc payload'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return error if jsonrpc is not 2.0', async () => { |
| 54 | + const response = await fetch(`${serverUrl}/rpc`, { method: 'POST', body: JSON.stringify({ jsonrpc: '1.0' }) }); |
| 55 | + assert.equal(response.status, 200); |
| 56 | + const json = await response.json(); |
| 57 | + assert.equal(json.jsonrpc, '2.0'); |
| 58 | + assert.equal(json.error.code, -32600); |
| 59 | + assert.equal(json.error.data, "'jsonrpc' must be '2.0'"); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should return error if id is missing', async () => { |
| 63 | + const response = await fetch(`${serverUrl}/rpc`, { method: 'POST', body: JSON.stringify({ jsonrpc: '2.0', method: 'devices', params: {} }) }); |
| 64 | + assert.equal(response.status, 200); |
| 65 | + const json = await response.json(); |
| 66 | + assert.equal(json.jsonrpc, '2.0'); |
| 67 | + assert.equal(json.error.code, -32600); |
| 68 | + assert.equal(json.error.data, "'id' field is required"); |
| 69 | + }); |
| 70 | + |
| 71 | + false && it('should accept "devices" call without params', async () => { |
| 72 | + const response = await fetch(`${serverUrl}/rpc`, { |
| 73 | + method: 'POST', |
| 74 | + body: JSON.stringify({ |
| 75 | + jsonrpc: '2.0', |
| 76 | + method: 'devices', |
| 77 | + id: 1 |
| 78 | + }), |
| 79 | + signal: AbortSignal.timeout(30000), |
| 80 | + }); |
| 81 | + |
| 82 | + assert.equal(response.status, 200); |
| 83 | + const json = await response.json(); |
| 84 | + assert.equal(json.jsonrpc, '2.0'); |
| 85 | + assert.equal(json.id, 1); |
| 86 | + assert.ok(Array.isArray(json.result.devices)); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return error for "device_info" call without params', async () => { |
| 90 | + const response = await fetch(`${serverUrl}/rpc`, { |
| 91 | + method: 'POST', |
| 92 | + body: JSON.stringify({ |
| 93 | + jsonrpc: '2.0', |
| 94 | + method: 'device_info', |
| 95 | + id: 1 |
| 96 | + }) |
| 97 | + }); |
| 98 | + |
| 99 | + assert.equal(response.status, 200); |
| 100 | + const json = await response.json(); |
| 101 | + assert.equal(json.jsonrpc, '2.0'); |
| 102 | + assert.equal(json.id, 1); |
| 103 | + assert.equal(json.error.code, -32000); |
| 104 | + assert.equal(json.error.data, "'params' is required with fields: deviceId"); |
| 105 | + }); |
| 106 | + |
| 107 | + const waitForServer = async (url: string, timeoutMs: number): Promise<void> => { |
| 108 | + const startTime = Date.now(); |
| 109 | + const expirationTime = startTime + timeoutMs; |
| 110 | + |
| 111 | + while (Date.now() < expirationTime) { |
| 112 | + try { |
| 113 | + const response = await fetch(url); |
| 114 | + if (response.status === 200) { |
| 115 | + return; // Server is ready |
| 116 | + } |
| 117 | + } catch (error) { |
| 118 | + // Server not ready yet, continue waiting |
| 119 | + } |
| 120 | + |
| 121 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 122 | + } |
| 123 | + |
| 124 | + throw new Error(`Server did not start within ${timeoutMs}ms`); |
| 125 | + }; |
| 126 | +}); |
0 commit comments