Skip to content

Commit d8f133a

Browse files
committed
test(stream): lock canonical delta semantics
1 parent a31bbde commit d8f133a

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.github.easy4j.opencode.api;
2+
3+
import io.github.easy4j.opencode.OpenCodeHttpClientConfig;
4+
import io.github.easy4j.opencode.api.sse.SseEvent;
5+
import io.github.easy4j.opencode.api.sse.StreamingChatResponse;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.lang.reflect.Method;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
class OpenCodeChatClientStreamingContractTest {
15+
16+
@Test
17+
void messagePartDeltaMustAppendTextExactlyOnce() throws Exception {
18+
OpenCodeChatClient client = new OpenCodeChatClient(new OpenCodeHttpClientConfig());
19+
try {
20+
StreamingChatResponse stream = new StreamingChatResponse();
21+
SseEvent event = event("message.part.delta", "sess-1");
22+
event.getProperties().put("field", "text");
23+
event.getProperties().put("delta", "hello");
24+
25+
invokeHandleEvent(client, "sess-1", event, stream);
26+
27+
assertEquals("hello", stream.getAccumulatedContent());
28+
} finally {
29+
client.close();
30+
}
31+
}
32+
33+
@Test
34+
void messagePartUpdatedSnapshotMustNotBeAppendedAsDelta() throws Exception {
35+
OpenCodeChatClient client = new OpenCodeChatClient(new OpenCodeHttpClientConfig());
36+
try {
37+
StreamingChatResponse stream = new StreamingChatResponse();
38+
SseEvent event = event("message.part.updated", "sess-1");
39+
Map<String, Object> part = new HashMap<String, Object>();
40+
part.put("type", "text");
41+
part.put("text", "whole snapshot");
42+
event.getProperties().put("part", part);
43+
44+
invokeHandleEvent(client, "sess-1", event, stream);
45+
46+
assertEquals("", stream.getAccumulatedContent());
47+
} finally {
48+
client.close();
49+
}
50+
}
51+
52+
private static SseEvent event(String type, String sessionId) {
53+
SseEvent event = new SseEvent();
54+
event.setType(type);
55+
Map<String, Object> properties = new HashMap<String, Object>();
56+
properties.put("sessionID", sessionId);
57+
event.setProperties(properties);
58+
return event;
59+
}
60+
61+
private static void invokeHandleEvent(OpenCodeChatClient client, String sessionId,
62+
SseEvent event, StreamingChatResponse stream) throws Exception {
63+
Method method = OpenCodeChatClient.class.getDeclaredMethod(
64+
"handleEvent", String.class, SseEvent.class, StreamingChatResponse.class);
65+
method.setAccessible(true);
66+
method.invoke(client, sessionId, event, stream);
67+
}
68+
}

0 commit comments

Comments
 (0)