diff --git a/java/src/main/java/com/github/copilot/rpc/AgentMode.java b/java/src/main/java/com/github/copilot/rpc/AgentMode.java index 2210bc431..4a286dca3 100644 --- a/java/src/main/java/com/github/copilot/rpc/AgentMode.java +++ b/java/src/main/java/com/github/copilot/rpc/AgentMode.java @@ -4,6 +4,7 @@ package com.github.copilot.rpc; +import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; /** @@ -44,4 +45,28 @@ public enum AgentMode { public String getValue() { return value; } + + /** + * Deserializes a JSON string value into the corresponding {@code AgentMode} + * enum constant. + * + * @param value + * the JSON string value + * @return the matching {@code AgentMode}, or {@code null} if value is + * {@code null} + * @throws IllegalArgumentException + * if the value does not match any known agent mode + */ + @JsonCreator + public static AgentMode fromValue(String value) { + if (value == null) { + return null; + } + for (AgentMode mode : values()) { + if (mode.value.equals(value)) { + return mode; + } + } + throw new IllegalArgumentException("Unknown AgentMode value: " + value); + } } diff --git a/java/src/test/java/com/github/copilot/AgentModeTest.java b/java/src/test/java/com/github/copilot/AgentModeTest.java new file mode 100644 index 000000000..dd1aa01de --- /dev/null +++ b/java/src/test/java/com/github/copilot/AgentModeTest.java @@ -0,0 +1,70 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + *--------------------------------------------------------------------------------------------*/ + +package com.github.copilot; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.copilot.rpc.AgentMode; + +/** + * Unit tests for {@link AgentMode} serialization, deserialization, and + * unknown-value behavior. + */ +public class AgentModeTest { + + private final ObjectMapper mapper = new ObjectMapper(); + + @ParameterizedTest + @EnumSource(AgentMode.class) + void jsonRoundTrip_allValues(AgentMode mode) throws Exception { + String json = mapper.writeValueAsString(mode); + AgentMode deserialized = mapper.readValue(json, AgentMode.class); + assertEquals(mode, deserialized); + } + + @Test + void getValue_returnsExpectedStrings() { + assertEquals("interactive", AgentMode.INTERACTIVE.getValue()); + assertEquals("plan", AgentMode.PLAN.getValue()); + assertEquals("autopilot", AgentMode.AUTOPILOT.getValue()); + assertEquals("shell", AgentMode.SHELL.getValue()); + } + + @Test + void fromValue_knownValues_returnsCorrectEnum() { + assertEquals(AgentMode.INTERACTIVE, AgentMode.fromValue("interactive")); + assertEquals(AgentMode.PLAN, AgentMode.fromValue("plan")); + assertEquals(AgentMode.AUTOPILOT, AgentMode.fromValue("autopilot")); + assertEquals(AgentMode.SHELL, AgentMode.fromValue("shell")); + } + + @Test + void fromValue_null_returnsNull() { + assertNull(AgentMode.fromValue(null)); + } + + @Test + void fromValue_unknownValue_throwsWithConsistentMessage() { + var ex = assertThrows(IllegalArgumentException.class, () -> AgentMode.fromValue("unknown")); + assertEquals("Unknown AgentMode value: unknown", ex.getMessage()); + } + + @Test + void jsonDeserialize_unknownValue_throws() { + String json = "\"not-a-mode\""; + assertThrows(Exception.class, () -> mapper.readValue(json, AgentMode.class)); + } + + @Test + void jsonSerialize_writesStringValue() throws Exception { + String json = mapper.writeValueAsString(AgentMode.AUTOPILOT); + assertEquals("\"autopilot\"", json); + } +}