forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotToolAnnotationTest.java
More file actions
155 lines (125 loc) · 6.16 KB
/
Copy pathCopilotToolAnnotationTest.java
File metadata and controls
155 lines (125 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.tool;
import static org.junit.jupiter.api.Assertions.*;
import java.io.InputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.Test;
import com.github.copilot.CopilotExperimental;
import com.github.copilot.rpc.ToolDefer;
/**
* Unit tests for {@link CopilotTool} and {@link CopilotToolParam} annotations.
*/
public class CopilotToolAnnotationTest {
// --- @CopilotTool attribute verification ---
@Test
void copilotToolHasRuntimeRetention() {
Retention retention = CopilotTool.class.getAnnotation(Retention.class);
assertNotNull(retention);
assertEquals(RetentionPolicy.RUNTIME, retention.value());
}
@Test
void copilotToolTargetsMethod() {
Target target = CopilotTool.class.getAnnotation(Target.class);
assertNotNull(target);
assertArrayEquals(new ElementType[]{ElementType.METHOD}, target.value());
}
@Test
void copilotExperimentalTargetsTypeForAnnotationDeclarations() {
Target expTarget = CopilotExperimental.class.getAnnotation(Target.class);
assertNotNull(expTarget);
boolean includesType = false;
for (ElementType et : expTarget.value()) {
if (et == ElementType.TYPE) {
includesType = true;
break;
}
}
assertTrue(includesType, "@CopilotExperimental must target TYPE to be applicable to annotation declarations");
}
@Test
void copilotToolDeclaresCopilotExperimentalInClassFile() throws Exception {
String classFileResourcePath = "/" + CopilotTool.class.getName().replace('.', '/') + ".class";
try (InputStream classFile = CopilotTool.class.getResourceAsStream(classFileResourcePath)) {
assertNotNull(classFile, "CopilotTool class file must be readable as a resource");
String classFileText = new String(classFile.readAllBytes(), StandardCharsets.ISO_8859_1);
assertTrue(classFileText.contains("com/github/copilot/CopilotExperimental"));
}
}
@Test
void copilotToolDefaultValues() throws Exception {
Method nameMethod = CopilotTool.class.getDeclaredMethod("name");
assertEquals("", nameMethod.getDefaultValue());
Method overridesMethod = CopilotTool.class.getDeclaredMethod("overridesBuiltInTool");
assertEquals(false, overridesMethod.getDefaultValue());
Method skipMethod = CopilotTool.class.getDeclaredMethod("skipPermission");
assertEquals(false, skipMethod.getDefaultValue());
Method deferMethod = CopilotTool.class.getDeclaredMethod("defer");
assertEquals(ToolDefer.NONE, deferMethod.getDefaultValue());
}
// --- @CopilotToolParam attribute verification ---
@Test
void paramHasRuntimeRetention() {
Retention retention = CopilotToolParam.class.getAnnotation(Retention.class);
assertNotNull(retention);
assertEquals(RetentionPolicy.RUNTIME, retention.value());
}
@Test
void paramTargetsParameter() {
Target target = CopilotToolParam.class.getAnnotation(Target.class);
assertNotNull(target);
assertArrayEquals(new ElementType[]{ElementType.PARAMETER}, target.value());
}
@Test
void paramDefaultValues() throws Exception {
Method valueMethod = CopilotToolParam.class.getDeclaredMethod("value");
assertEquals("", valueMethod.getDefaultValue());
Method nameMethod = CopilotToolParam.class.getDeclaredMethod("name");
assertEquals("", nameMethod.getDefaultValue());
Method requiredMethod = CopilotToolParam.class.getDeclaredMethod("required");
assertEquals(true, requiredMethod.getDefaultValue());
Method defaultValueMethod = CopilotToolParam.class.getDeclaredMethod("defaultValue");
assertEquals("", defaultValueMethod.getDefaultValue());
}
// --- Applicability test ---
@SuppressWarnings("unused")
static class SampleToolHolder {
@CopilotTool(value = "Get weather for a location", name = "get_weather", defer = ToolDefer.AUTO)
public CompletableFuture<String> getWeather(
@CopilotToolParam(value = "City name", required = true) String location,
@CopilotToolParam(value = "Temperature unit", required = false, defaultValue = "celsius") String unit) {
return CompletableFuture.completedFuture("Sunny in " + location);
}
}
@Test
void annotationsAreAccessibleViaReflection() throws Exception {
Method method = SampleToolHolder.class.getDeclaredMethod("getWeather", String.class, String.class);
CopilotTool toolAnnotation = method.getAnnotation(CopilotTool.class);
assertNotNull(toolAnnotation);
assertEquals("Get weather for a location", toolAnnotation.value());
assertEquals("get_weather", toolAnnotation.name());
assertFalse(toolAnnotation.overridesBuiltInTool());
assertFalse(toolAnnotation.skipPermission());
assertEquals(ToolDefer.AUTO, toolAnnotation.defer());
Parameter[] params = method.getParameters();
assertEquals(2, params.length);
CopilotToolParam locationParam = params[0].getAnnotation(CopilotToolParam.class);
assertNotNull(locationParam);
assertEquals("City name", locationParam.value());
assertTrue(locationParam.required());
assertEquals("", locationParam.defaultValue());
CopilotToolParam unitParam = params[1].getAnnotation(CopilotToolParam.class);
assertNotNull(unitParam);
assertEquals("Temperature unit", unitParam.value());
assertFalse(unitParam.required());
assertEquals("celsius", unitParam.defaultValue());
}
}