Skip to content

Commit b97aaaf

Browse files
committed
add unit tests
Signed-off-by: Jiaping Zeng <[email protected]>
1 parent 7b6cb32 commit b97aaaf

File tree

12 files changed

+1707
-18
lines changed

12 files changed

+1707
-18
lines changed

common/src/test/java/org/opensearch/ml/common/agui/AGUIInputConverterTest.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,7 @@ public void testConvertFromAGUIInput_WithTemplates() {
246246
);
247247
}
248248

249-
@Test
250-
public void testReconstructAGUIInput() {
251-
// Create parameters that would result from conversion
252-
java.util.Map<String, String> parameters = new java.util.HashMap<>();
253-
parameters.put("agui_thread_id", "reconstruct_thread");
254-
parameters.put("agui_run_id", "reconstruct_run");
255-
parameters.put("agui_state", "{\"status\":\"active\"}");
256-
parameters.put("agui_messages", "[]");
257-
parameters.put("agui_tools", "[]");
258-
259-
JsonObject result = AGUIInputConverter.reconstructAGUIInput(parameters);
260-
261-
assertNotNull("Reconstructed result should not be null", result);
262-
assertTrue("Should contain threadId", result.has("threadId"));
263-
assertEquals("Thread ID should match", "reconstruct_thread", result.get("threadId").getAsString());
264-
assertTrue("Should contain runId", result.has("runId"));
265-
assertEquals("Run ID should match", "reconstruct_run", result.get("runId").getAsString());
266-
}
249+
267250

268251
@Test
269252
public void testConvertFromAGUIInput_WithContext() {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.ml.common.agui;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNotNull;
10+
import static org.junit.Assert.assertTrue;
11+
12+
import java.io.IOException;
13+
14+
import org.junit.Test;
15+
import org.opensearch.common.io.stream.BytesStreamOutput;
16+
import org.opensearch.core.common.io.stream.StreamInput;
17+
public class RunFinishedEventTests {
18+
19+
@Test
20+
public void testConstructor() {
21+
String threadId = "thread_123";
22+
String runId = "run_456";
23+
24+
RunFinishedEvent event = new RunFinishedEvent(threadId, runId, null);
25+
26+
assertNotNull("Event should not be null", event);
27+
assertEquals("Type should be RUN_FINISHED", "RUN_FINISHED", event.getType());
28+
assertEquals("Thread ID should match", threadId, event.getThreadId());
29+
assertEquals("Run ID should match", runId, event.getRunId());
30+
assertNotNull("Timestamp should be set", event.getTimestamp());
31+
}
32+
33+
@Test
34+
public void testSerialization() throws IOException {
35+
RunFinishedEvent original = new RunFinishedEvent("thread_test", "run_test", null);
36+
37+
BytesStreamOutput output = new BytesStreamOutput();
38+
original.writeTo(output);
39+
40+
StreamInput input = output.bytes().streamInput();
41+
RunFinishedEvent deserialized = new RunFinishedEvent(input);
42+
43+
assertEquals("Type should match", original.getType(), deserialized.getType());
44+
assertEquals("Thread ID should match", original.getThreadId(), deserialized.getThreadId());
45+
assertEquals("Run ID should match", original.getRunId(), deserialized.getRunId());
46+
assertEquals("Timestamp should match", original.getTimestamp(), deserialized.getTimestamp());
47+
}
48+
49+
@Test
50+
public void testToXContent() throws IOException {
51+
RunFinishedEvent event = new RunFinishedEvent("thread_xcontent", "run_xcontent", null);
52+
53+
String json = event.toJsonString();
54+
55+
assertNotNull("JSON should not be null", json);
56+
assertTrue("JSON should contain type", json.contains("\"type\":\"RUN_FINISHED\""));
57+
assertTrue("JSON should contain threadId", json.contains("\"threadId\":\"thread_xcontent\""));
58+
assertTrue("JSON should contain runId", json.contains("\"runId\":\"run_xcontent\""));
59+
assertTrue("JSON should contain timestamp", json.contains("\"timestamp\""));
60+
}
61+
62+
@Test
63+
public void testToJsonString() {
64+
RunFinishedEvent event = new RunFinishedEvent("thread_json", "run_json", null);
65+
66+
String json = event.toJsonString();
67+
68+
assertNotNull("JSON string should not be null", json);
69+
assertTrue("JSON should be valid", json.startsWith("{") && json.endsWith("}"));
70+
}
71+
72+
@Test
73+
public void testTimestampGeneration() {
74+
long before = System.currentTimeMillis();
75+
RunFinishedEvent event = new RunFinishedEvent("thread_time", "run_time", null);
76+
long after = System.currentTimeMillis();
77+
78+
assertNotNull("Timestamp should be set", event.getTimestamp());
79+
assertTrue("Timestamp should be in valid range",
80+
event.getTimestamp() >= before && event.getTimestamp() <= after);
81+
}
82+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.ml.common.agui;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNotNull;
10+
import static org.junit.Assert.assertTrue;
11+
12+
import java.io.IOException;
13+
14+
import org.junit.Test;
15+
import org.opensearch.common.io.stream.BytesStreamOutput;
16+
import org.opensearch.core.common.io.stream.StreamInput;
17+
public class RunStartedEventTests {
18+
19+
@Test
20+
public void testConstructor() {
21+
String threadId = "thread_123";
22+
String runId = "run_456";
23+
24+
RunStartedEvent event = new RunStartedEvent(threadId, runId);
25+
26+
assertNotNull("Event should not be null", event);
27+
assertEquals("Type should be RUN_STARTED", "RUN_STARTED", event.getType());
28+
assertEquals("Thread ID should match", threadId, event.getThreadId());
29+
assertEquals("Run ID should match", runId, event.getRunId());
30+
assertNotNull("Timestamp should be set", event.getTimestamp());
31+
assertTrue("Timestamp should be recent", event.getTimestamp() > 0);
32+
}
33+
34+
@Test
35+
public void testSerialization() throws IOException {
36+
RunStartedEvent original = new RunStartedEvent("thread_test", "run_test");
37+
38+
BytesStreamOutput output = new BytesStreamOutput();
39+
original.writeTo(output);
40+
41+
StreamInput input = output.bytes().streamInput();
42+
RunStartedEvent deserialized = new RunStartedEvent(input);
43+
44+
assertEquals("Type should match", original.getType(), deserialized.getType());
45+
assertEquals("Thread ID should match", original.getThreadId(), deserialized.getThreadId());
46+
assertEquals("Run ID should match", original.getRunId(), deserialized.getRunId());
47+
assertEquals("Timestamp should match", original.getTimestamp(), deserialized.getTimestamp());
48+
}
49+
50+
@Test
51+
public void testToXContent() throws IOException {
52+
RunStartedEvent event = new RunStartedEvent("thread_xcontent", "run_xcontent");
53+
54+
String json = event.toJsonString();
55+
56+
assertNotNull("JSON should not be null", json);
57+
assertTrue("JSON should contain type", json.contains("\"type\":\"RUN_STARTED\""));
58+
assertTrue("JSON should contain threadId", json.contains("\"threadId\":\"thread_xcontent\""));
59+
assertTrue("JSON should contain runId", json.contains("\"runId\":\"run_xcontent\""));
60+
assertTrue("JSON should contain timestamp", json.contains("\"timestamp\""));
61+
}
62+
63+
@Test
64+
public void testToJsonString() {
65+
RunStartedEvent event = new RunStartedEvent("thread_json", "run_json");
66+
67+
String json = event.toJsonString();
68+
69+
assertNotNull("JSON string should not be null", json);
70+
assertTrue("JSON should be valid", json.startsWith("{") && json.endsWith("}"));
71+
assertTrue("JSON should contain all fields",
72+
json.contains("type") && json.contains("threadId") && json.contains("runId"));
73+
}
74+
75+
@Test
76+
public void testTimestampGeneration() throws InterruptedException {
77+
long before = System.currentTimeMillis();
78+
Thread.sleep(10); // Small delay to ensure different timestamps
79+
80+
RunStartedEvent event = new RunStartedEvent("thread_time", "run_time");
81+
82+
Thread.sleep(10);
83+
long after = System.currentTimeMillis();
84+
85+
assertNotNull("Timestamp should be set", event.getTimestamp());
86+
assertTrue("Timestamp should be after 'before'", event.getTimestamp() >= before);
87+
assertTrue("Timestamp should be before 'after'", event.getTimestamp() <= after);
88+
}
89+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.ml.common.agui;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNotNull;
10+
import static org.junit.Assert.assertTrue;
11+
12+
import java.io.IOException;
13+
14+
import org.junit.Test;
15+
import org.opensearch.common.io.stream.BytesStreamOutput;
16+
import org.opensearch.core.common.io.stream.StreamInput;
17+
public class TextMessageContentEventTests {
18+
19+
@Test
20+
public void testConstructor() {
21+
String messageId = "msg_123";
22+
String delta = "Hello, ";
23+
24+
TextMessageContentEvent event = new TextMessageContentEvent(messageId, delta);
25+
26+
assertNotNull("Event should not be null", event);
27+
assertEquals("Type should be TEXT_MESSAGE_CONTENT", "TEXT_MESSAGE_CONTENT", event.getType());
28+
assertEquals("Message ID should match", messageId, event.getMessageId());
29+
assertEquals("Delta should match", delta, event.getDelta());
30+
assertNotNull("Timestamp should be set", event.getTimestamp());
31+
}
32+
33+
@Test
34+
public void testSerialization() throws IOException {
35+
TextMessageContentEvent original = new TextMessageContentEvent("msg_test", "world!");
36+
37+
BytesStreamOutput output = new BytesStreamOutput();
38+
original.writeTo(output);
39+
40+
StreamInput input = output.bytes().streamInput();
41+
TextMessageContentEvent deserialized = new TextMessageContentEvent(input);
42+
43+
assertEquals("Type should match", original.getType(), deserialized.getType());
44+
assertEquals("Message ID should match", original.getMessageId(), deserialized.getMessageId());
45+
assertEquals("Delta should match", original.getDelta(), deserialized.getDelta());
46+
assertEquals("Timestamp should match", original.getTimestamp(), deserialized.getTimestamp());
47+
}
48+
49+
@Test
50+
public void testToXContent() throws IOException {
51+
TextMessageContentEvent event = new TextMessageContentEvent("msg_xcontent", "test content");
52+
53+
String json = event.toJsonString();
54+
55+
assertNotNull("JSON should not be null", json);
56+
assertTrue("JSON should contain type", json.contains("\"type\":\"TEXT_MESSAGE_CONTENT\""));
57+
assertTrue("JSON should contain messageId", json.contains("\"messageId\":\"msg_xcontent\""));
58+
assertTrue("JSON should contain delta", json.contains("\"delta\":\"test content\""));
59+
assertTrue("JSON should contain timestamp", json.contains("\"timestamp\""));
60+
}
61+
62+
@Test
63+
public void testToJsonString() {
64+
TextMessageContentEvent event = new TextMessageContentEvent("msg_json", "content");
65+
66+
String json = event.toJsonString();
67+
68+
assertNotNull("JSON string should not be null", json);
69+
assertTrue("JSON should be valid", json.startsWith("{") && json.endsWith("}"));
70+
}
71+
72+
@Test
73+
public void testTimestampGeneration() {
74+
long before = System.currentTimeMillis();
75+
TextMessageContentEvent event = new TextMessageContentEvent("msg_time", "delta");
76+
long after = System.currentTimeMillis();
77+
78+
assertNotNull("Timestamp should be set", event.getTimestamp());
79+
assertTrue("Timestamp should be in valid range",
80+
event.getTimestamp() >= before && event.getTimestamp() <= after);
81+
}
82+
83+
@Test
84+
public void testEmptyDelta() {
85+
TextMessageContentEvent event = new TextMessageContentEvent("msg_empty", "");
86+
87+
assertEquals("Empty delta should be preserved", "", event.getDelta());
88+
89+
String json = event.toJsonString();
90+
assertTrue("JSON should contain empty delta", json.contains("\"delta\":\"\""));
91+
}
92+
93+
@Test
94+
public void testSpecialCharactersInDelta() {
95+
String specialChars = "Hello \"world\" with\nnewlines\tand\ttabs";
96+
TextMessageContentEvent event = new TextMessageContentEvent("msg_special", specialChars);
97+
98+
assertEquals("Special characters should be preserved", specialChars, event.getDelta());
99+
}
100+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.ml.common.agui;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNotNull;
10+
import static org.junit.Assert.assertTrue;
11+
12+
import java.io.IOException;
13+
14+
import org.junit.Test;
15+
import org.opensearch.common.io.stream.BytesStreamOutput;
16+
import org.opensearch.core.common.io.stream.StreamInput;
17+
public class TextMessageStartEventTests {
18+
19+
@Test
20+
public void testConstructor() {
21+
String messageId = "msg_123";
22+
String role = "assistant";
23+
24+
TextMessageStartEvent event = new TextMessageStartEvent(messageId, role);
25+
26+
assertNotNull("Event should not be null", event);
27+
assertEquals("Type should be TEXT_MESSAGE_START", "TEXT_MESSAGE_START", event.getType());
28+
assertEquals("Message ID should match", messageId, event.getMessageId());
29+
assertEquals("Role should match", role, event.getRole());
30+
assertNotNull("Timestamp should be set", event.getTimestamp());
31+
}
32+
33+
@Test
34+
public void testSerialization() throws IOException {
35+
TextMessageStartEvent original = new TextMessageStartEvent("msg_test", "assistant");
36+
37+
BytesStreamOutput output = new BytesStreamOutput();
38+
original.writeTo(output);
39+
40+
StreamInput input = output.bytes().streamInput();
41+
TextMessageStartEvent deserialized = new TextMessageStartEvent(input);
42+
43+
assertEquals("Type should match", original.getType(), deserialized.getType());
44+
assertEquals("Message ID should match", original.getMessageId(), deserialized.getMessageId());
45+
assertEquals("Role should match", original.getRole(), deserialized.getRole());
46+
assertEquals("Timestamp should match", original.getTimestamp(), deserialized.getTimestamp());
47+
}
48+
49+
@Test
50+
public void testToXContent() throws IOException {
51+
TextMessageStartEvent event = new TextMessageStartEvent("msg_xcontent", "assistant");
52+
53+
String json = event.toJsonString();
54+
55+
assertNotNull("JSON should not be null", json);
56+
assertTrue("JSON should contain type", json.contains("\"type\":\"TEXT_MESSAGE_START\""));
57+
assertTrue("JSON should contain messageId", json.contains("\"messageId\":\"msg_xcontent\""));
58+
assertTrue("JSON should contain role", json.contains("\"role\":\"assistant\""));
59+
assertTrue("JSON should contain timestamp", json.contains("\"timestamp\""));
60+
}
61+
62+
@Test
63+
public void testToJsonString() {
64+
TextMessageStartEvent event = new TextMessageStartEvent("msg_json", "user");
65+
66+
String json = event.toJsonString();
67+
68+
assertNotNull("JSON string should not be null", json);
69+
assertTrue("JSON should be valid", json.startsWith("{") && json.endsWith("}"));
70+
assertTrue("JSON should contain messageId and role",
71+
json.contains("messageId") && json.contains("role"));
72+
}
73+
74+
@Test
75+
public void testTimestampGeneration() {
76+
long before = System.currentTimeMillis();
77+
TextMessageStartEvent event = new TextMessageStartEvent("msg_time", "assistant");
78+
long after = System.currentTimeMillis();
79+
80+
assertNotNull("Timestamp should be set", event.getTimestamp());
81+
assertTrue("Timestamp should be in valid range",
82+
event.getTimestamp() >= before && event.getTimestamp() <= after);
83+
}
84+
}

0 commit comments

Comments
 (0)