forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_event_side_effects.rs
More file actions
353 lines (326 loc) · 12.7 KB
/
Copy pathrpc_event_side_effects.rs
File metadata and controls
353 lines (326 loc) · 12.7 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
use github_copilot_sdk::generated::SessionMode;
use github_copilot_sdk::generated::api_types::{
HistoryTruncateRequest, ModeSetRequest, NameSetRequest, PlanUpdateRequest,
WorkspacesCreateFileRequest,
};
use github_copilot_sdk::generated::session_events::{
PlanChangedOperation, SessionEventType, SessionModeChangedData, SessionPlanChangedData,
SessionSnapshotRewindData, SessionTitleChangedData, SessionWorkspaceFileChangedData,
};
use super::support::{assistant_message_content, wait_for_event, with_e2e_context};
#[tokio::test]
async fn should_emit_mode_changed_event_when_mode_set() {
with_e2e_context(
"rpc_event_side_effects",
"should_emit_mode_changed_event_when_mode_set",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let changed = wait_for_event(session.subscribe(), "mode changed", |event| {
if event.parsed_type() != SessionEventType::SessionModeChanged {
return false;
}
let data = event
.typed_data::<SessionModeChangedData>()
.expect("mode changed data");
data.previous_mode == SessionMode::Interactive
&& data.new_mode == SessionMode::Plan
});
session
.rpc()
.mode()
.set(ModeSetRequest {
mode: SessionMode::Plan,
})
.await
.expect("set mode");
changed.await;
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_emit_plan_changed_event_for_update_and_delete() {
with_e2e_context(
"rpc_event_side_effects",
"should_emit_plan_changed_event_for_update_and_delete",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let create = wait_for_plan_event(&session, PlanChangedOperation::Create);
session
.rpc()
.plan()
.update(PlanUpdateRequest {
content: "# Test plan\n- item".to_string(),
})
.await
.expect("create plan");
create.await;
let delete = wait_for_plan_event(&session, PlanChangedOperation::Delete);
session.rpc().plan().delete().await.expect("delete plan");
delete.await;
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_emit_plan_changed_update_operation_on_second_update() {
with_e2e_context(
"rpc_event_side_effects",
"should_emit_plan_changed_update_operation_on_second_update",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
session
.rpc()
.plan()
.update(PlanUpdateRequest {
content: "# initial".to_string(),
})
.await
.expect("create plan");
let update = wait_for_plan_event(&session, PlanChangedOperation::Update);
session
.rpc()
.plan()
.update(PlanUpdateRequest {
content: "# updated".to_string(),
})
.await
.expect("update plan");
update.await;
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_emit_workspace_file_changed_event_when_file_created() {
with_e2e_context(
"rpc_event_side_effects",
"should_emit_workspace_file_changed_event_when_file_created",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let path = "side-effect-rust.txt";
let changed =
wait_for_event(session.subscribe(), "workspace file changed", |event| {
if event.parsed_type() != SessionEventType::SessionWorkspaceFileChanged {
return false;
}
event
.typed_data::<SessionWorkspaceFileChangedData>()
.expect("workspace file changed data")
.path
== path
});
session
.rpc()
.workspaces()
.create_file(WorkspacesCreateFileRequest {
path: path.to_string(),
content: "hello".to_string(),
})
.await
.expect("create workspace file");
changed.await;
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_emit_title_changed_event_when_name_set() {
with_e2e_context(
"rpc_event_side_effects",
"should_emit_title_changed_event_when_name_set",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let title = "Renamed-Rust";
let changed = wait_for_event(session.subscribe(), "title changed", |event| {
if event.parsed_type() != SessionEventType::SessionTitleChanged {
return false;
}
event
.typed_data::<SessionTitleChangedData>()
.expect("title changed data")
.title
== title
});
session
.rpc()
.name()
.set(NameSetRequest {
name: title.to_string(),
})
.await
.expect("set name");
changed.await;
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_emit_snapshot_rewind_event_and_remove_events_on_truncate() {
with_e2e_context(
"rpc_event_side_effects",
"should_emit_snapshot_rewind_event_and_remove_events_on_truncate",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let answer = session
.send_and_wait("Say SNAPSHOT_REWIND_TARGET exactly.")
.await
.expect("send")
.expect("assistant message");
assert!(assistant_message_content(&answer).contains("SNAPSHOT_REWIND_TARGET"));
let user_event = session
.get_events()
.await
.expect("messages")
.into_iter()
.find(|event| event.parsed_type() == SessionEventType::UserMessage)
.expect("user.message event");
let target_event_id = user_event.id.clone();
let rewind = wait_for_event(session.subscribe(), "snapshot rewind", |event| {
if event.parsed_type() != SessionEventType::SessionSnapshotRewind {
return false;
}
event
.typed_data::<SessionSnapshotRewindData>()
.expect("snapshot rewind data")
.up_to_event_id
== target_event_id
});
let result = session
.rpc()
.history()
.truncate(HistoryTruncateRequest {
event_id: target_event_id.clone(),
})
.await
.expect("truncate history");
assert!(result.events_removed >= 1);
rewind.await;
let remaining = session.get_events().await.expect("messages after truncate");
assert!(!remaining.iter().any(|event| event.id == target_event_id));
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_allow_session_use_after_truncate() {
with_e2e_context(
"rpc_event_side_effects",
"should_allow_session_use_after_truncate",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
session
.send_and_wait("Say SNAPSHOT_REWIND_TARGET exactly.")
.await
.expect("send");
let user_event = session
.get_events()
.await
.expect("messages")
.into_iter()
.find(|event| event.parsed_type() == SessionEventType::UserMessage)
.expect("user.message event");
let result = session
.rpc()
.history()
.truncate(HistoryTruncateRequest {
event_id: user_event.id,
})
.await
.expect("truncate history");
assert!(result.events_removed >= 1);
session
.rpc()
.mode()
.get()
.await
.expect("mode after truncate");
session
.rpc()
.workspaces()
.get_workspace()
.await
.expect("workspace after truncate");
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
fn wait_for_plan_event(
session: &github_copilot_sdk::session::Session,
operation: PlanChangedOperation,
) -> impl std::future::Future<Output = github_copilot_sdk::SessionEvent> {
let events = session.subscribe();
wait_for_event(events, "plan changed", move |event| {
if event.parsed_type() != SessionEventType::SessionPlanChanged {
return false;
}
event
.typed_data::<SessionPlanChangedData>()
.expect("plan changed data")
.operation
== operation
})
}