|
1 | 1 | package io.github.fabb.wigai.mcp.tool; |
2 | 2 |
|
| 3 | +import com.bitwig.extension.controller.api.*; |
3 | 4 | import io.github.fabb.wigai.WigAIExtensionDefinition; |
4 | 5 | import io.github.fabb.wigai.common.Logger; |
5 | 6 | import io.modelcontextprotocol.server.McpServerFeatures; |
|
10 | 11 | import org.mockito.Mock; |
11 | 12 | import org.mockito.MockitoAnnotations; |
12 | 13 |
|
13 | | -import java.util.Collections; |
14 | | -import java.util.List; |
15 | 14 | import java.util.Map; |
16 | 15 |
|
17 | 16 | import static org.junit.jupiter.api.Assertions.*; |
18 | 17 | import static org.mockito.Mockito.*; |
19 | 18 |
|
20 | | -/** |
21 | | - * Unit tests for the StatusTool class. |
22 | | - */ |
23 | | -public class StatusToolTest { |
| 19 | +class StatusToolTest { |
24 | 20 |
|
25 | 21 | @Mock |
26 | 22 | private WigAIExtensionDefinition mockExtensionDefinition; |
27 | | - |
28 | 23 | @Mock |
29 | 24 | private Logger mockLogger; |
30 | | - |
| 25 | + @Mock |
| 26 | + private ControllerHost mockHost; |
31 | 27 | @Mock |
32 | 28 | private McpSyncServerExchange mockExchange; |
33 | 29 |
|
| 30 | + // Mocks for Bitwig API objects that ControllerHost would return |
| 31 | + @Mock |
| 32 | + private Project mockProject; |
| 33 | + @Mock |
| 34 | + private Application mockApplication; |
| 35 | + @Mock |
| 36 | + private Transport mockTransport; |
| 37 | + |
| 38 | + // Mocks for Bitwig Value objects |
| 39 | + @Mock |
| 40 | + private StringValue mockProjectNameValue; |
| 41 | + @Mock |
| 42 | + private BooleanValue mockEngineActiveValue; |
| 43 | + @Mock |
| 44 | + private BooleanValue mockPlayingValue; |
| 45 | + @Mock |
| 46 | + private BooleanValue mockRecordingValue; |
| 47 | + @Mock |
| 48 | + private BooleanValue mockRepeatActiveValue; |
| 49 | + @Mock |
| 50 | + private BooleanValue mockMetronomeActiveValue; |
| 51 | + @Mock |
| 52 | + private SettableBeatTimeValue mockTempoValue; // This is the type for transport.tempo() |
| 53 | + @Mock |
| 54 | + private Parameter mockTempoParameter; // This is the type for tempo().value() |
| 55 | + @Mock |
| 56 | + private StringValue mockTimeSignatureValue; |
| 57 | + @Mock |
| 58 | + private BeatTimeValue mockPositionValue; // For both current_beat_str and current_time_str |
| 59 | + |
| 60 | + |
34 | 61 | @BeforeEach |
35 | 62 | void setUp() { |
36 | 63 | MockitoAnnotations.openMocks(this); |
37 | | - when(mockExtensionDefinition.getVersion()).thenReturn("0.2.0"); |
38 | | - } |
39 | 64 |
|
40 | | - @Test |
41 | | - void testStatusToolSpecification() { |
42 | | - // Get the tool specification |
43 | | - McpServerFeatures.SyncToolSpecification toolSpec = |
44 | | - StatusTool.specification(mockExtensionDefinition, mockLogger); |
45 | | - |
46 | | - // Verify the tool properties |
47 | | - assertNotNull(toolSpec); |
48 | | - assertEquals("status", toolSpec.tool().name()); |
49 | | - assertEquals("Get WigAI operational status and version information.", |
50 | | - toolSpec.tool().description()); |
| 65 | + // Setup ControllerHost mocks to return other Bitwig API mocks |
| 66 | + when(mockHost.getProject()).thenReturn(mockProject); |
| 67 | + when(mockHost.getApplication()).thenReturn(mockApplication); |
| 68 | + when(mockHost.getTransport()).thenReturn(mockTransport); |
| 69 | + |
| 70 | + // Setup Bitwig API object mocks to return Value mocks |
| 71 | + when(mockProject.getName()).thenReturn(mockProjectNameValue); |
| 72 | + when(mockApplication.isEngineActive()).thenReturn(mockEngineActiveValue); |
| 73 | + when(mockTransport.isPlaying()).thenReturn(mockPlayingValue); |
| 74 | + when(mockTransport.isArrangerRecordEnabled()).thenReturn(mockRecordingValue); |
| 75 | + when(mockTransport.isArrangerLoopEnabled()).thenReturn(mockRepeatActiveValue); |
| 76 | + when(mockTransport.isMetronomeEnabled()).thenReturn(mockMetronomeActiveValue); |
| 77 | + when(mockTransport.tempo()).thenReturn(mockTempoValue); |
| 78 | + when(mockTempoValue.value()).thenReturn(mockTempoParameter); // tempo().value() |
| 79 | + when(mockTransport.timeSignature()).thenReturn(mockTimeSignatureValue); |
| 80 | + when(mockTransport.getPosition()).thenReturn(mockPositionValue); |
51 | 81 | } |
52 | 82 |
|
53 | 83 | @Test |
54 | | - void testStatusToolHandler() { |
55 | | - // Initialize the handler by calling specification |
56 | | - StatusTool.specification(mockExtensionDefinition, mockLogger); |
57 | | - |
58 | | - // Execute the handler with an empty arguments map |
59 | | - Map<String, Object> args = Collections.emptyMap(); |
60 | | - McpSchema.CallToolResult result = StatusTool.getHandler().apply(mockExchange, args); |
61 | | - |
62 | | - // Verify the result |
63 | | - assertNotNull(result); |
| 84 | + void testGetStatusReturnsCorrectInformation() { |
| 85 | + // Define expected values |
| 86 | + String expectedVersion = "1.0.0"; |
| 87 | + String expectedProjectName = "Test Project"; |
| 88 | + boolean expectedEngineActive = true; |
| 89 | + boolean expectedPlaying = false; |
| 90 | + boolean expectedRecording = false; |
| 91 | + boolean expectedRepeatActive = true; |
| 92 | + boolean expectedMetronomeActive = true; |
| 93 | + double expectedTempo = 125.0; |
| 94 | + String expectedTimeSignature = "3/4"; |
| 95 | + String expectedBeatStr = "2.1.1:0"; |
| 96 | + |
| 97 | + // Stub mock methods to return these expected values |
| 98 | + when(mockExtensionDefinition.getVersion()).thenReturn(expectedVersion); |
| 99 | + when(mockProjectNameValue.get()).thenReturn(expectedProjectName); |
| 100 | + when(mockEngineActiveValue.get()).thenReturn(expectedEngineActive); |
| 101 | + when(mockPlayingValue.get()).thenReturn(expectedPlaying); |
| 102 | + when(mockRecordingValue.get()).thenReturn(expectedRecording); |
| 103 | + when(mockRepeatActiveValue.get()).thenReturn(expectedRepeatActive); |
| 104 | + when(mockMetronomeActiveValue.get()).thenReturn(expectedMetronomeActive); |
| 105 | + when(mockTempoParameter.get()).thenReturn(expectedTempo); // tempo().value().get() |
| 106 | + when(mockTimeSignatureValue.get()).thenReturn(expectedTimeSignature); |
| 107 | + when(mockPositionValue.get()).thenReturn(expectedBeatStr); |
| 108 | + |
| 109 | + // Get the StatusTool specification and handler |
| 110 | + McpServerFeatures.SyncToolSpecification spec = StatusTool.specification(mockExtensionDefinition, mockLogger, mockHost); |
| 111 | + var handler = spec.handler(); |
| 112 | + |
| 113 | + // Call the handler |
| 114 | + McpSchema.CallToolResult result = handler.apply(mockExchange, Map.of()); |
| 115 | + |
| 116 | + // Assert the results |
64 | 117 | assertFalse(result.isError()); |
65 | | - |
66 | | - // Check content structure |
67 | | - List<?> content = result.content(); |
68 | | - assertNotNull(content); |
69 | | - assertEquals(1, content.size()); |
70 | | - |
71 | | - // Check the text content |
72 | | - Object first = content.get(0); |
73 | | - assertTrue(first instanceof McpSchema.TextContent); |
74 | | - McpSchema.TextContent textContent = (McpSchema.TextContent) first; |
75 | | - assertEquals("WigAI v0.2.0 is operational", textContent.text()); |
76 | | - |
77 | | - // Verify logging |
78 | | - verify(mockLogger).info("Received 'status' tool call"); |
79 | | - verify(mockLogger).info("Responding with: WigAI v0.2.0 is operational"); |
| 118 | + assertNotNull(result.response()); |
| 119 | + assertTrue(result.response() instanceof Map); |
| 120 | + |
| 121 | + @SuppressWarnings("unchecked") |
| 122 | + Map<String, Object> responseMap = (Map<String, Object>) result.response(); |
| 123 | + assertEquals(expectedVersion, responseMap.get("wigai_version")); |
| 124 | + assertEquals(expectedProjectName, responseMap.get("project_name")); |
| 125 | + assertEquals(expectedEngineActive, responseMap.get("audio_engine_active")); |
| 126 | + |
| 127 | + assertTrue(responseMap.get("transport") instanceof Map); |
| 128 | + @SuppressWarnings("unchecked") |
| 129 | + Map<String, Object> transportMap = (Map<String, Object>) responseMap.get("transport"); |
| 130 | + assertEquals(expectedPlaying, transportMap.get("playing")); |
| 131 | + assertEquals(expectedRecording, transportMap.get("recording")); |
| 132 | + assertEquals(expectedRepeatActive, transportMap.get("repeat_active")); |
| 133 | + assertEquals(expectedMetronomeActive, transportMap.get("metronome_active")); |
| 134 | + assertEquals(expectedTempo, transportMap.get("current_tempo")); |
| 135 | + assertEquals(expectedTimeSignature, transportMap.get("time_signature")); |
| 136 | + assertEquals(expectedBeatStr, transportMap.get("current_beat_str")); |
| 137 | + assertEquals(expectedBeatStr, transportMap.get("current_time_str")); // As per story current_time_str is same as current_beat_str |
| 138 | + |
| 139 | + // Verify logger interactions |
| 140 | + verify(mockLogger, times(1)).info("Received 'status' tool call"); |
| 141 | + verify(mockLogger, times(1)).info(startsWith("Responding with: {")); |
80 | 142 | } |
81 | 143 | } |
0 commit comments