|
| 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