-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathSessionEventSerializationTests.cs
More file actions
180 lines (169 loc) · 6.66 KB
/
SessionEventSerializationTests.cs
File metadata and controls
180 lines (169 loc) · 6.66 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using System.Collections.Generic;
using System.Text.Json;
using Xunit;
namespace GitHub.Copilot.SDK.Test;
public class SessionEventSerializationTests
{
public static TheoryData<SessionEvent, string> JsonElementBackedEvents => new()
{
{
new AssistantMessageEvent
{
Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
Timestamp = DateTimeOffset.Parse("2026-03-15T21:26:02.642Z"),
ParentId = Guid.Parse("22222222-2222-2222-2222-222222222222"),
Data = new AssistantMessageData
{
MessageId = "msg-1",
Content = "",
ToolRequests =
[
new AssistantMessageDataToolRequestsItem
{
ToolCallId = "call-1",
Name = "view",
Arguments = ParseJsonElement("""{"path":"README.md"}"""),
Type = AssistantMessageDataToolRequestsItemType.Function,
},
],
},
},
"assistant.message"
},
{
new ToolExecutionStartEvent
{
Id = Guid.Parse("33333333-3333-3333-3333-333333333333"),
Timestamp = DateTimeOffset.Parse("2026-03-15T21:26:02.642Z"),
ParentId = Guid.Parse("44444444-4444-4444-4444-444444444444"),
Data = new ToolExecutionStartData
{
ToolCallId = "call-1",
ToolName = "view",
Arguments = ParseJsonElement("""{"path":"README.md"}"""),
},
},
"tool.execution_start"
},
{
new ToolExecutionCompleteEvent
{
Id = Guid.Parse("55555555-5555-5555-5555-555555555555"),
Timestamp = DateTimeOffset.Parse("2026-03-15T21:26:02.642Z"),
ParentId = Guid.Parse("66666666-6666-6666-6666-666666666666"),
Data = new ToolExecutionCompleteData
{
ToolCallId = "call-1",
Success = true,
Result = new ToolExecutionCompleteDataResult
{
Content = "ok",
DetailedContent = "ok",
},
ToolTelemetry = new Dictionary<string, object>
{
["properties"] = ParseJsonElement("""{"command":"view"}"""),
["metrics"] = ParseJsonElement("""{"resultLength":2}"""),
},
},
},
"tool.execution_complete"
},
{
new SessionShutdownEvent
{
Id = Guid.Parse("77777777-7777-7777-7777-777777777777"),
Timestamp = DateTimeOffset.Parse("2026-03-15T21:26:52.987Z"),
ParentId = Guid.Parse("88888888-8888-8888-8888-888888888888"),
Data = new SessionShutdownData
{
ShutdownType = SessionShutdownDataShutdownType.Routine,
TotalPremiumRequests = 1,
TotalApiDurationMs = 100,
SessionStartTime = 1773609948932,
CodeChanges = new SessionShutdownDataCodeChanges
{
LinesAdded = 1,
LinesRemoved = 0,
FilesModified = ["README.md"],
},
ModelMetrics = new Dictionary<string, object>
{
["gpt-5.4"] = ParseJsonElement("""
{
"requests": {
"count": 1,
"cost": 1
},
"usage": {
"inputTokens": 10,
"outputTokens": 5,
"cacheReadTokens": 0,
"cacheWriteTokens": 0
}
}
"""),
},
CurrentModel = "gpt-5.4",
},
},
"session.shutdown"
}
};
private static JsonElement ParseJsonElement(string json)
{
using var document = JsonDocument.Parse(json);
return document.RootElement.Clone();
}
[Theory]
[MemberData(nameof(JsonElementBackedEvents))]
public void SessionEvent_ToJson_RoundTrips_JsonElementBackedPayloads(SessionEvent sessionEvent, string expectedType)
{
var serialized = sessionEvent.ToJson();
using var document = JsonDocument.Parse(serialized);
var root = document.RootElement;
Assert.Equal(expectedType, root.GetProperty("type").GetString());
switch (expectedType)
{
case "assistant.message":
Assert.Equal(
"README.md",
root.GetProperty("data")
.GetProperty("toolRequests")[0]
.GetProperty("arguments")
.GetProperty("path")
.GetString());
break;
case "tool.execution_start":
Assert.Equal(
"README.md",
root.GetProperty("data")
.GetProperty("arguments")
.GetProperty("path")
.GetString());
break;
case "tool.execution_complete":
Assert.Equal(
"view",
root.GetProperty("data")
.GetProperty("toolTelemetry")
.GetProperty("properties")
.GetProperty("command")
.GetString());
break;
case "session.shutdown":
Assert.Equal(
1,
root.GetProperty("data")
.GetProperty("modelMetrics")
.GetProperty("gpt-5.4")
.GetProperty("requests")
.GetProperty("count")
.GetInt32());
break;
}
}
}