mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
fix(action): correct action chain fixing error
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package work.slhaf.partner.module.action.planner;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
||||
import work.slhaf.partner.framework.agent.support.Result;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ActionPlannerAssemblyHelperTest {
|
||||
|
||||
private static void injectField(Object target, String fieldName, Object value) throws Exception {
|
||||
Field field = target.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
field.set(target, value);
|
||||
}
|
||||
|
||||
private static Object getField(Object target, String fieldName) throws Exception {
|
||||
Field field = target.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return field.get(target);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFixNonZeroBasedOrdersWithoutInjectingNullActionKeyList() throws Exception {
|
||||
ActionCapability actionCapability = Mockito.mock(ActionCapability.class);
|
||||
Mockito.when(actionCapability.loadMetaActionInfo(Mockito.anyString()))
|
||||
.thenReturn(Result.success(new MetaActionInfo(
|
||||
false,
|
||||
null,
|
||||
Map.of(),
|
||||
"desc",
|
||||
Set.of(),
|
||||
Set.of(),
|
||||
Set.of(),
|
||||
false,
|
||||
new JSONObject()
|
||||
)));
|
||||
|
||||
ActionPlanner planner = new ActionPlanner();
|
||||
injectField(planner, "actionCapability", actionCapability);
|
||||
|
||||
Object helper = getField(planner, "assemblyHelper");
|
||||
Method fixDependencies = helper.getClass().getDeclaredMethod("fixDependencies", Map.class);
|
||||
fixDependencies.setAccessible(true);
|
||||
|
||||
Map<Integer, List<String>> chain = new LinkedHashMap<>();
|
||||
chain.put(1, new ArrayList<>(List.of("action_a")));
|
||||
chain.put(2, new ArrayList<>(List.of("action_b")));
|
||||
|
||||
boolean fixed = (boolean) fixDependencies.invoke(helper, chain);
|
||||
|
||||
assertTrue(fixed);
|
||||
assertEquals(List.of(1, 2), new ArrayList<>(chain.keySet()));
|
||||
assertEquals(List.of("action_a"), chain.get(1));
|
||||
assertEquals(List.of("action_b"), chain.get(2));
|
||||
assertFalse(chain.containsValue(null));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package work.slhaf.partner.module.action.planner.evaluator.entity;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EvaluatorResultTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnEmptyMapWhenPrimaryActionChainIsNull() {
|
||||
EvaluatorResult result = new EvaluatorResult();
|
||||
Map<Integer, List<String>> chain = result.getPrimaryActionChainAsMap();
|
||||
assertNotNull(chain);
|
||||
assertTrue(chain.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNormalizeNullActionKeysToEmptyList() {
|
||||
EvaluatorResult result = new EvaluatorResult();
|
||||
EvaluatorResult.ChainElement element = new EvaluatorResult.ChainElement();
|
||||
element.setOrder(1);
|
||||
element.setActionKeys(null);
|
||||
result.setPrimaryActionChain(List.of(element));
|
||||
|
||||
Map<Integer, List<String>> chain = result.getPrimaryActionChainAsMap();
|
||||
assertEquals(1, chain.size());
|
||||
assertNotNull(chain.get(1));
|
||||
assertTrue(chain.get(1).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCopyActionKeyListDefensively() {
|
||||
EvaluatorResult result = new EvaluatorResult();
|
||||
EvaluatorResult.ChainElement element = new EvaluatorResult.ChainElement();
|
||||
element.setOrder(1);
|
||||
List<String> keys = new ArrayList<>(List.of("a"));
|
||||
element.setActionKeys(keys);
|
||||
result.setPrimaryActionChain(List.of(element));
|
||||
|
||||
Map<Integer, List<String>> chain = result.getPrimaryActionChainAsMap();
|
||||
keys.add("b");
|
||||
|
||||
assertEquals(new LinkedHashMap<>(Map.of(1, List.of("a"))), new LinkedHashMap<>(chain));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user