-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathrpc_queue.rs
More file actions
225 lines (208 loc) · 8.15 KB
/
Copy pathrpc_queue.rs
File metadata and controls
225 lines (208 loc) · 8.15 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
use github_copilot_sdk::rpc::{
CommandsRespondToQueuedCommandRequest, EnqueueCommandParams, QueuePendingItems,
QueuePendingItemsKind, RegisterEventInterestParams, ReleaseEventInterestParams,
};
use github_copilot_sdk::session::Session;
use github_copilot_sdk::session_events::{CommandQueuedData, SessionEventType};
use serde_json::json;
use uuid::Uuid;
use super::support::{wait_for_condition, wait_for_event, with_e2e_context};
fn is_pending_command(item: &QueuePendingItems, command: &str) -> bool {
item.kind == QueuePendingItemsKind::Command
&& (item.display_text == command
|| item.display_text.contains(command.trim_start_matches('/')))
}
async fn wait_for_command_in_pending_items(session: &Session, command: &str) {
wait_for_condition(
"queued command to appear in pending items",
move || async move {
session
.rpc()
.queue()
.pending_items()
.await
.expect("pending queued command")
.items
.iter()
.any(|item| is_pending_command(item, command))
},
)
.await;
}
async fn wait_for_command_not_in_pending_items(session: &Session, command: &str) {
wait_for_condition(
"queued command to leave pending items",
move || async move {
!session
.rpc()
.queue()
.pending_items()
.await
.expect("pending queued command")
.items
.iter()
.any(|item| is_pending_command(item, command))
},
)
.await;
}
async fn wait_for_queue_empty(session: &Session) {
wait_for_condition("queue to empty", move || async move {
let pending = session
.rpc()
.queue()
.pending_items()
.await
.expect("pending after clear");
pending.items.is_empty() && pending.steering_messages.is_empty()
})
.await;
}
#[tokio::test]
async fn fresh_queue_is_empty_and_empty_mutations_are_noops() {
with_e2e_context(
"rpc_queue",
"fresh_queue_is_empty_and_empty_mutations_are_noops",
|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 pending = session
.rpc()
.queue()
.pending_items()
.await
.expect("pending items");
assert!(pending.items.is_empty());
assert!(pending.steering_messages.is_empty());
assert!(
!session
.rpc()
.queue()
.remove_most_recent()
.await
.expect("remove most recent")
.removed
);
session.rpc().queue().clear().await.expect("clear queue");
let after = session
.rpc()
.queue()
.pending_items()
.await
.expect("pending after clear");
assert!(after.items.is_empty());
assert!(after.steering_messages.is_empty());
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn pendingitems_reports_queued_command_and_remove_and_clear_update_queue() {
with_e2e_context(
"rpc_queue",
"pendingitems_reports_queued_command_and_remove_and_clear_update_queue",
|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 first_command = format!("/sdk-queue-first-{}", Uuid::new_v4());
let second_command = format!("/sdk-queue-second-{}", Uuid::new_v4());
let third_command = format!("/sdk-queue-third-{}", Uuid::new_v4());
let interest = session
.rpc()
.event_log()
.register_interest(RegisterEventInterestParams {
event_type: "command.queued".to_string(),
})
.await
.expect("register command interest")
.handle;
let first_command_for_event = first_command.clone();
let queued_event =
wait_for_event(session.subscribe(), "command queued", move |event| {
event.parsed_type() == SessionEventType::CommandQueued
&& event.data.get("command").and_then(|value| value.as_str())
== Some(first_command_for_event.as_str())
});
let enqueue = session
.rpc()
.commands()
.enqueue(EnqueueCommandParams {
command: first_command,
})
.await
.expect("enqueue command");
assert!(enqueue.queued);
let queued = queued_event
.await
.typed_data::<CommandQueuedData>()
.expect("command queued data");
let second = session
.rpc()
.commands()
.enqueue(EnqueueCommandParams {
command: second_command.clone(),
})
.await
.expect("enqueue second command");
assert!(second.queued);
wait_for_command_in_pending_items(&session, &second_command).await;
let removed = session
.rpc()
.queue()
.remove_most_recent()
.await
.expect("remove second command");
assert!(removed.removed);
wait_for_command_not_in_pending_items(&session, &second_command).await;
let third = session
.rpc()
.commands()
.enqueue(EnqueueCommandParams {
command: third_command.clone(),
})
.await
.expect("enqueue third command");
assert!(third.queued);
wait_for_command_in_pending_items(&session, &third_command).await;
session.rpc().queue().clear().await.expect("clear queue");
wait_for_command_not_in_pending_items(&session, &third_command).await;
let completed = session
.rpc()
.commands()
.respond_to_queued_command(CommandsRespondToQueuedCommandRequest {
request_id: queued.request_id,
result: json!({
"handled": true,
"stopProcessingQueue": true
}),
})
.await
.expect("respond to first command");
assert!(completed.success);
wait_for_queue_empty(&session).await;
session
.rpc()
.event_log()
.release_interest(ReleaseEventInterestParams { handle: interest })
.await
.expect("release command interest");
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}