Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions java/src/main/java/com/github/copilot/rpc/AgentMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package com.github.copilot.rpc;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
Expand Down Expand Up @@ -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);
}
Comment thread
edburns marked this conversation as resolved.
}
70 changes: 70 additions & 0 deletions java/src/test/java/com/github/copilot/AgentModeTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading