|
7 | 7 | import assert from 'node:assert'; |
8 | 8 | import {describe, it, afterEach} from 'node:test'; |
9 | 9 |
|
10 | | -import {startDaemon, stopDaemon} from '../../src/daemon/client.js'; |
| 10 | +import { |
| 11 | + handleResponse, |
| 12 | + startDaemon, |
| 13 | + stopDaemon, |
| 14 | +} from '../../src/daemon/client.js'; |
11 | 15 | import {isDaemonRunning} from '../../src/daemon/utils.js'; |
12 | 16 |
|
13 | 17 | describe('daemon client', () => { |
14 | | - afterEach(async () => { |
15 | | - if (isDaemonRunning()) { |
| 18 | + describe('start/stop', () => { |
| 19 | + afterEach(async () => { |
| 20 | + if (isDaemonRunning()) { |
| 21 | + await stopDaemon(); |
| 22 | + // Wait a bit for the daemon to fully terminate and clean up its files. |
| 23 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + it('should start and stop daemon', async () => { |
| 28 | + assert.ok(!isDaemonRunning(), 'Daemon should not be running initially'); |
| 29 | + |
| 30 | + await startDaemon(); |
| 31 | + assert.ok(isDaemonRunning(), 'Daemon should be running after start'); |
| 32 | + |
16 | 33 | await stopDaemon(); |
17 | | - // Wait a bit for the daemon to fully terminate and clean up its files. |
18 | 34 | await new Promise(resolve => setTimeout(resolve, 1000)); |
19 | | - } |
20 | | - }); |
| 35 | + assert.ok(!isDaemonRunning(), 'Daemon should not be running after stop'); |
| 36 | + }); |
21 | 37 |
|
22 | | - it('should start and stop daemon', async () => { |
23 | | - assert.ok(!isDaemonRunning(), 'Daemon should not be running initially'); |
| 38 | + it('should handle starting daemon when already running', async () => { |
| 39 | + await startDaemon(); |
| 40 | + assert.ok(isDaemonRunning(), 'Daemon should be running'); |
24 | 41 |
|
25 | | - await startDaemon(); |
26 | | - assert.ok(isDaemonRunning(), 'Daemon should be running after start'); |
| 42 | + // Starting again should be a no-op |
| 43 | + await startDaemon(); |
| 44 | + assert.ok(isDaemonRunning(), 'Daemon should still be running'); |
| 45 | + }); |
27 | 46 |
|
28 | | - await stopDaemon(); |
29 | | - await new Promise(resolve => setTimeout(resolve, 1000)); |
30 | | - assert.ok(!isDaemonRunning(), 'Daemon should not be running after stop'); |
| 47 | + it('should handle stopping daemon when not running', async () => { |
| 48 | + assert.ok(!isDaemonRunning(), 'Daemon should not be running initially'); |
| 49 | + |
| 50 | + // Stopping when not running should be a no-op |
| 51 | + await stopDaemon(); |
| 52 | + assert.ok(!isDaemonRunning(), 'Daemon should still not be running'); |
| 53 | + }); |
31 | 54 | }); |
32 | 55 |
|
33 | | - it('should handle starting daemon when already running', async () => { |
34 | | - await startDaemon(); |
35 | | - assert.ok(isDaemonRunning(), 'Daemon should be running'); |
| 56 | + describe('parsing', () => { |
| 57 | + it('handles MCP response with text format', () => { |
| 58 | + const textResponse = {content: [{type: 'text' as const, text: 'test'}]}; |
| 59 | + assert.strictEqual(handleResponse(textResponse, 'text'), 'test'); |
| 60 | + }); |
36 | 61 |
|
37 | | - // Starting again should be a no-op |
38 | | - await startDaemon(); |
39 | | - assert.ok(isDaemonRunning(), 'Daemon should still be running'); |
40 | | - }); |
| 62 | + it('handles JSON response', () => { |
| 63 | + const jsonResponse = { |
| 64 | + content: [], |
| 65 | + structuredContent: { |
| 66 | + test: 'data', |
| 67 | + number: 123, |
| 68 | + }, |
| 69 | + }; |
| 70 | + assert.strictEqual( |
| 71 | + handleResponse(jsonResponse, 'json'), |
| 72 | + JSON.stringify(jsonResponse.structuredContent), |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('handles error response when isError is true', () => { |
| 77 | + const errorResponse = { |
| 78 | + isError: true, |
| 79 | + content: [{type: 'text' as const, text: 'Something went wrong'}], |
| 80 | + }; |
| 81 | + assert.strictEqual( |
| 82 | + handleResponse(errorResponse, 'text'), |
| 83 | + JSON.stringify(errorResponse.content), |
| 84 | + ); |
| 85 | + }); |
41 | 86 |
|
42 | | - it('should handle stopping daemon when not running', async () => { |
43 | | - assert.ok(!isDaemonRunning(), 'Daemon should not be running initially'); |
| 87 | + it('handles text response when json format is requested but no structured content', () => { |
| 88 | + const textResponse = { |
| 89 | + content: [{type: 'text' as const, text: 'Fall through text'}], |
| 90 | + }; |
| 91 | + assert.deepStrictEqual( |
| 92 | + handleResponse(textResponse, 'json'), |
| 93 | + JSON.stringify(['Fall through text']), |
| 94 | + ); |
| 95 | + }); |
44 | 96 |
|
45 | | - // Stopping when not running should be a no-op |
46 | | - await stopDaemon(); |
47 | | - assert.ok(!isDaemonRunning(), 'Daemon should still not be running'); |
| 97 | + it('throws error for unsupported content type', () => { |
| 98 | + const unsupportedContentResponse = { |
| 99 | + content: [ |
| 100 | + { |
| 101 | + type: 'resource' as const, |
| 102 | + resource: { |
| 103 | + uri: 'data:image/png;base64,base64data', |
| 104 | + blob: 'base64data', |
| 105 | + mimeType: 'image/png', |
| 106 | + }, |
| 107 | + }, |
| 108 | + ], |
| 109 | + structuredContent: {}, |
| 110 | + }; |
| 111 | + assert.throws( |
| 112 | + () => handleResponse(unsupportedContentResponse, 'text'), |
| 113 | + new Error('Not supported response content type'), |
| 114 | + ); |
| 115 | + }); |
48 | 116 | }); |
49 | 117 | }); |
0 commit comments