forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventFidelityTest.java
More file actions
111 lines (87 loc) · 4.08 KB
/
EventFidelityTest.java
File metadata and controls
111 lines (87 loc) · 4.08 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.github.copilot.sdk.generated.AssistantUsageEvent;
import com.github.copilot.sdk.generated.SessionEvent;
import com.github.copilot.sdk.generated.SessionUsageInfoEvent;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.PermissionHandler;
import com.github.copilot.sdk.json.SessionConfig;
/**
* E2E tests for event fidelity — verifying the shape, ordering, and presence of
* key events emitted from the runtime.
*
* <p>
* Snapshots are stored in {@code test/snapshots/event_fidelity/}.
* </p>
*/
public class EventFidelityTest {
private static E2ETestContext ctx;
@BeforeAll
static void setup() throws Exception {
ctx = E2ETestContext.create();
}
@AfterAll
static void teardown() throws Exception {
if (ctx != null) {
ctx.close();
}
}
/**
* Verifies that an {@code assistant.usage} event is emitted after the model
* processes a prompt.
*
* @see Snapshot:
* event_fidelity/should_emit_assistant_usage_event_after_model_call
*/
@Test
void testShouldEmitAssistantUsageEventAfterModelCall() throws Exception {
ctx.configureForTest("event_fidelity", "should_emit_assistant_usage_event_after_model_call");
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client
.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
List<SessionEvent> events = new ArrayList<>();
session.on(events::add);
session.sendAndWait(new MessageOptions().setPrompt("What is 5+5? Reply with just the number.")).get(60,
TimeUnit.SECONDS);
List<AssistantUsageEvent> usageEvents = events.stream().filter(e -> e instanceof AssistantUsageEvent)
.map(e -> (AssistantUsageEvent) e).toList();
assertFalse(usageEvents.isEmpty(), "Should have received an assistant.usage event after model call");
AssistantUsageEvent lastUsage = usageEvents.get(usageEvents.size() - 1);
assertNotNull(lastUsage.getData().model(), "Usage event should have a model field");
assertFalse(lastUsage.getData().model().isEmpty(), "Model field should not be empty");
session.close();
}
}
/**
* Verifies that a {@code session.usage_info} event is emitted after the model
* processes a prompt.
*
* @see Snapshot:
* event_fidelity/should_emit_session_usage_info_event_after_model_call
*/
@Test
void testShouldEmitSessionUsageInfoEventAfterModelCall() throws Exception {
ctx.configureForTest("event_fidelity", "should_emit_session_usage_info_event_after_model_call");
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client
.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
List<SessionEvent> events = new ArrayList<>();
session.on(events::add);
session.sendAndWait(new MessageOptions().setPrompt("What is 5+5? Reply with just the number.")).get(60,
TimeUnit.SECONDS);
List<SessionUsageInfoEvent> usageInfoEvents = events.stream()
.filter(e -> e instanceof SessionUsageInfoEvent).map(e -> (SessionUsageInfoEvent) e).toList();
assertFalse(usageInfoEvents.isEmpty(), "Should have received a session.usage_info event after model call");
session.close();
}
}
}