Skip to content

Commit 102cd3e

Browse files
edburnsCopilot
andauthored
Use jsoncreator for AgentMode ctor, per review comments. (#1465)
* Apply review comments * Address PR review comments on AgentMode - Revert @SInCE tag from 1.1.0 to 1.0.0 for consistency with SendMessageRequest which already references AgentMode - Fix error message format to 'Unknown AgentMode value: ...' matching the convention used by generated enum fromValue methods - Add AgentModeTest covering serialization, deserialization, round-trip, null handling, and unknown-value error behavior Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7b62bfb commit 102cd3e

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

java/src/main/java/com/github/copilot/rpc/AgentMode.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package com.github.copilot.rpc;
66

7+
import com.fasterxml.jackson.annotation.JsonCreator;
78
import com.fasterxml.jackson.annotation.JsonValue;
89

910
/**
@@ -44,4 +45,28 @@ public enum AgentMode {
4445
public String getValue() {
4546
return value;
4647
}
48+
49+
/**
50+
* Deserializes a JSON string value into the corresponding {@code AgentMode}
51+
* enum constant.
52+
*
53+
* @param value
54+
* the JSON string value
55+
* @return the matching {@code AgentMode}, or {@code null} if value is
56+
* {@code null}
57+
* @throws IllegalArgumentException
58+
* if the value does not match any known agent mode
59+
*/
60+
@JsonCreator
61+
public static AgentMode fromValue(String value) {
62+
if (value == null) {
63+
return null;
64+
}
65+
for (AgentMode mode : values()) {
66+
if (mode.value.equals(value)) {
67+
return mode;
68+
}
69+
}
70+
throw new IllegalArgumentException("Unknown AgentMode value: " + value);
71+
}
4772
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------------------------------------------*/
4+
5+
package com.github.copilot;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.EnumSource;
12+
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
import com.github.copilot.rpc.AgentMode;
15+
16+
/**
17+
* Unit tests for {@link AgentMode} serialization, deserialization, and
18+
* unknown-value behavior.
19+
*/
20+
public class AgentModeTest {
21+
22+
private final ObjectMapper mapper = new ObjectMapper();
23+
24+
@ParameterizedTest
25+
@EnumSource(AgentMode.class)
26+
void jsonRoundTrip_allValues(AgentMode mode) throws Exception {
27+
String json = mapper.writeValueAsString(mode);
28+
AgentMode deserialized = mapper.readValue(json, AgentMode.class);
29+
assertEquals(mode, deserialized);
30+
}
31+
32+
@Test
33+
void getValue_returnsExpectedStrings() {
34+
assertEquals("interactive", AgentMode.INTERACTIVE.getValue());
35+
assertEquals("plan", AgentMode.PLAN.getValue());
36+
assertEquals("autopilot", AgentMode.AUTOPILOT.getValue());
37+
assertEquals("shell", AgentMode.SHELL.getValue());
38+
}
39+
40+
@Test
41+
void fromValue_knownValues_returnsCorrectEnum() {
42+
assertEquals(AgentMode.INTERACTIVE, AgentMode.fromValue("interactive"));
43+
assertEquals(AgentMode.PLAN, AgentMode.fromValue("plan"));
44+
assertEquals(AgentMode.AUTOPILOT, AgentMode.fromValue("autopilot"));
45+
assertEquals(AgentMode.SHELL, AgentMode.fromValue("shell"));
46+
}
47+
48+
@Test
49+
void fromValue_null_returnsNull() {
50+
assertNull(AgentMode.fromValue(null));
51+
}
52+
53+
@Test
54+
void fromValue_unknownValue_throwsWithConsistentMessage() {
55+
var ex = assertThrows(IllegalArgumentException.class, () -> AgentMode.fromValue("unknown"));
56+
assertEquals("Unknown AgentMode value: unknown", ex.getMessage());
57+
}
58+
59+
@Test
60+
void jsonDeserialize_unknownValue_throws() {
61+
String json = "\"not-a-mode\"";
62+
assertThrows(Exception.class, () -> mapper.readValue(json, AgentMode.class));
63+
}
64+
65+
@Test
66+
void jsonSerialize_writesStringValue() throws Exception {
67+
String json = mapper.writeValueAsString(AgentMode.AUTOPILOT);
68+
assertEquals("\"autopilot\"", json);
69+
}
70+
}

0 commit comments

Comments
 (0)