Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
rtk/proxy/tests/smoke_binary.rs at develop · rtk-ai/rtk · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
rtk-ai
rtk
Repository navigation
Code
Issues
900
(900)
Pull requests
725
(725)
Discussions
Actions
Projects
Security and quality
3
(3)
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
rtk
/
proxy
/
tests
/
smoke_binary.rs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
223 lines (201 loc) · 8.08 KB
develop
Breadcrumbs
rtk
/
proxy
/
tests
/
smoke_binary.rs
Copy path
Top
File metadata and controls
Code
Blame
223 lines (201 loc) · 8.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//! Real-binary smoke test: spawn the **compiled** `rtk-proxy` with env-only
//! config and a wiremock upstream, then drive it over real HTTP. This covers the
//! boot path the in-process router tests (`functional.rs`) bypass — `main.rs`
//! env→config parsing, store open, and the real TCP listener — end to end.
use
std
::
process
::
{
Child
,
Command
,
Stdio
}
;
use
std
::
time
::
Duration
;
use
wiremock
::
matchers
::
method
;
use
wiremock
::
{
Mock
,
MockServer
,
ResponseTemplate
}
;
/// Kills the spawned proxy when the test ends (pass or panic).
struct
Kill
(
Child
)
;
impl
Drop
for
Kill
{
fn
drop
(
&
mut
self
)
{
let
_ =
self
.
0
.
kill
(
)
;
let
_ =
self
.
0
.
wait
(
)
;
}
}
/// Grab a free TCP port by binding :0 and releasing it (tiny race, fine for CI).
fn
free_port
(
)
->
u16
{
std
::
net
::
TcpListener
::
bind
(
"127.0.0.1:0"
)
.
unwrap
(
)
.
local_addr
(
)
.
unwrap
(
)
.
port
(
)
}
/// Poll `/health` until the freshly-spawned binary answers, or fail.
async
fn
wait_healthy
(
client
:
&
reqwest
::
Client
,
base
:
&
str
)
{
for
_
in
0
..
80
{
if
let
Ok
(
r
)
= client
.
get
(
format
!
(
"{base}/health"
)
)
.
send
(
)
.
await
{
if
r
.
status
(
)
.
is_success
(
)
{
return
;
}
}
tokio
::
time
::
sleep
(
Duration
::
from_millis
(
250
)
)
.
await
;
}
panic
!
(
"rtk-proxy did not become healthy in time"
)
;
}
#
[
tokio
::
test
]
async
fn
real_binary_boots_forwards_and_records
(
)
{
let
up =
MockServer
::
start
(
)
.
await
;
Mock
::
given
(
method
(
"POST"
)
)
.
respond_with
(
ResponseTemplate
::
new
(
200
)
.
set_body_json
(
serde_json
::
json!
(
{
"id"
:
"msg_1"
,
"type"
:
"message"
,
"role"
:
"assistant"
,
"model"
:
"claude-haiku-4-5"
,
"content"
:
[
{
"type"
:
"text"
,
"text"
:
"ok"
}
]
,
"stop_reason"
:
"end_turn"
,
"usage"
:
{
"input_tokens"
:
10
,
"output_tokens"
:
7
}
}
)
)
)
.
mount
(
&
up
)
.
await
;
let
port =
free_port
(
)
;
let
base =
format
!
(
"http://127.0.0.1:{port}"
)
;
let
child =
Command
::
new
(
env
!
(
"CARGO_BIN_EXE_rtk-proxy"
)
)
.
arg
(
"start"
)
.
env
(
"RTK_PROXY_BIND"
,
format
!
(
"127.0.0.1:{port}"
)
)
.
env
(
"RTK_PROXY_ANTHROPIC_UPSTREAM"
,
up
.
uri
(
)
)
.
env
(
"RTK_PROXY_OPENAI_UPSTREAM"
,
up
.
uri
(
)
)
.
env
(
"RTK_PROXY_CCR"
,
"true"
)
.
env
(
"RTK_PROXY_DB"
,
":memory:"
)
// Both stores in memory: the ICM default is cwd-relative, so leaving
// it unset drops a real SQLite file into the crate root on every run.
.
env
(
"RTK_PROXY_ICM_DB"
,
":memory:"
)
.
env
(
"RUST_LOG"
,
"warn"
)
.
stdout
(
Stdio
::
null
(
)
)
.
spawn
(
)
.
expect
(
"spawn rtk-proxy binary"
)
;
let
_guard =
Kill
(
child
)
;
let
client = reqwest
::
Client
::
new
(
)
;
wait_healthy
(
&
client
,
&
base
)
.
await
;
// 1) /health reports the local edition.
let
h
:
serde_json
::
Value
= client
.
get
(
format
!
(
"{base}/health"
)
)
.
send
(
)
.
await
.
unwrap
(
)
.
json
(
)
.
await
.
unwrap
(
)
;
assert_eq
!
(
h
[
"edition"
]
,
"local"
,
"health edition"
)
;
assert_eq
!
(
h
[
"service"
]
,
"rtk-proxy"
,
"the identity the rtk CLI verifies before trusting the port"
)
;
// 2) A forward relays the upstream response, caller key passed through.
let
r = client
.
post
(
format
!
(
"{base}/v1/messages"
)
)
.
header
(
"content-type"
,
"application/json"
)
.
header
(
"x-api-key"
,
"sk-ant-CALLER"
)
.
body
(
r#"{"model":"claude-haiku-4-5","max_tokens":5,"messages":[{"role":"user","content":"hi"}]}"#
)
.
send
(
)
.
await
.
unwrap
(
)
;
assert_eq
!
(
r
.
status
(
)
,
200
,
"forward status"
)
;
let
j
:
serde_json
::
Value
= r
.
json
(
)
.
await
.
unwrap
(
)
;
assert_eq
!
(
j
[
"content"
]
[
0
]
[
"text"
]
,
"ok"
,
"relayed upstream body"
)
;
let
recv = up
.
received_requests
(
)
.
await
.
unwrap
(
)
;
assert
!
(
recv
.
iter
(
)
.
any
(
|req| req
.
headers
.
get
(
"x-api-key"
)
.
and_then
(
|v| v
.
to_str
(
)
.
ok
(
)
)
==
Some
(
"sk-ant-CALLER"
)
)
,
"the caller's own key reached the upstream"
)
;
// 3) Usage was recorded with the real output-token count.
tokio
::
time
::
sleep
(
Duration
::
from_millis
(
500
)
)
.
await
;
// record is spawned
let
s
:
serde_json
::
Value
= client
.
get
(
format
!
(
"{base}/stats/summary"
)
)
.
send
(
)
.
await
.
unwrap
(
)
.
json
(
)
.
await
.
unwrap
(
)
;
assert_eq
!
(
s
[
"total_requests"
]
,
1
,
"request recorded"
)
;
assert_eq
!
(
s
[
"tokens_out"
]
,
7
,
"real output tokens tallied"
)
;
// 4) No embedded dashboard: an unmatched path is a plain 404.
let
d = client
.
get
(
format
!
(
"{base}/"
)
)
.
send
(
)
.
await
.
unwrap
(
)
;
assert_eq
!
(
d
.
status
(
)
,
404
,
"no dashboard fallback"
)
;
}
/// Run `rtk-proxy <args...>` as a real child process and return its stdout.
fn
run_cli
(
args
:
&
[
&
str
]
)
->
String
{
let
out =
Command
::
new
(
env
!
(
"CARGO_BIN_EXE_rtk-proxy"
)
)
.
args
(
args
)
.
output
(
)
.
unwrap_or_else
(
|e|
panic
!
(
"failed to run rtk-proxy {args:?}: {e}"
)
)
;
assert
!
(
out
.
status
.
success
(
)
,
"rtk-proxy {args:?} failed: {}"
,
String
::
from_utf8_lossy
(
&
out
.
stderr
)
)
;
String
::
from_utf8_lossy
(
&
out
.
stdout
)
.
into_owned
(
)
}
#
[
tokio
::
test
]
async
fn
cli_subcommands_talk_to_a_running_daemon_then_stop_it
(
)
{
let
port =
free_port
(
)
;
let
bind =
format
!
(
"127.0.0.1:{port}"
)
;
let
base =
format
!
(
"http://127.0.0.1:{port}"
)
;
// The daemon's transforms come from the active pipeline: without
// proxy.request rules it forwards unchanged, so give it some. This test
// drives the CCR runtime toggle, and `rtk:proxy` deliberately does NOT
// enable CCR, so name the method explicitly.
let
pipeline_path = std
::
env
::
temp_dir
(
)
.
join
(
format
!
(
"rtk-proxy-smoke-{port}.toml"
)
)
;
std
::
fs
::
write
(
&
pipeline_path
,
"[pipeline]
\n
extends =
\"
rtk:proxy
\"
\n
\
[[proxy.request.tool_result]]
\n
name =
\"
ccr
\"
\n
\
when = { tool_not_in = [
\"
read
\"
], tokens_gt = 1500 }
\n
use = [
\"
ccr
\"
]
\n
"
,
)
.
expect
(
"write smoke pipeline"
)
;
let
child =
Command
::
new
(
env
!
(
"CARGO_BIN_EXE_rtk-proxy"
)
)
.
arg
(
"start"
)
.
env
(
"RTK_PROXY_BIND"
,
&
bind
)
.
env
(
"RTK_PROXY_DB"
,
":memory:"
)
.
env
(
"RTK_PROXY_ICM_DB"
,
":memory:"
)
.
env
(
"RTK_PROXY_PIPELINE"
,
&
pipeline_path
)
.
env
(
"RUST_LOG"
,
"warn"
)
.
stdout
(
Stdio
::
null
(
)
)
.
spawn
(
)
.
expect
(
"spawn rtk-proxy binary"
)
;
let
_guard =
Kill
(
child
)
;
let
client = reqwest
::
Client
::
new
(
)
;
wait_healthy
(
&
client
,
&
base
)
.
await
;
let
status_out =
run_cli
(
&
[
"status"
,
"--bind"
,
&
bind
]
)
;
assert
!
(
status_out
.
contains
(
"running"
)
,
"status should report running, got: {status_out}"
)
;
let
stats_out =
run_cli
(
&
[
"stats"
,
"--bind"
,
&
bind
]
)
;
assert
!
(
stats_out
.
contains
(
"total requests : 0"
)
,
"stats should show zero requests, got: {stats_out}"
)
;
// `config get` (no key) returns the full settings JSON. CCR is on unless
// turned off, and RTK_PROXY_CCR was left unset for this daemon.
let
cfg_out =
run_cli
(
&
[
"config"
,
"get"
,
"--bind"
,
&
bind
]
)
;
let
cfg_json
:
serde_json
::
Value
= serde_json
::
from_str
(
&
cfg_out
)
.
unwrap_or_else
(
|e|
panic
!
(
"config get output wasn't JSON ({e}): {cfg_out}"
)
)
;
assert_eq
!
(
cfg_json
[
"ccr"
]
,
true
,
"ccr defaults on"
)
;
run_cli
(
&
[
"config"
,
"set"
,
"--bind"
,
&
bind
,
"ccr"
,
"false"
]
)
;
assert_eq
!
(
run_cli
(
&
[
"config"
,
"get"
,
"--bind"
,
&
bind
,
"ccr"
]
)
.
trim
(
)
,
"false"
)
;
run_cli
(
&
[
"config"
,
"set"
,
"--bind"
,
&
bind
,
"ccr"
,
"true"
]
)
;
assert_eq
!
(
run_cli
(
&
[
"config"
,
"get"
,
"--bind"
,
&
bind
,
"ccr"
]
)
.
trim
(
)
,
"true"
)
;
// `config set` on an unrecognized key is rejected client-side, not silently
// dropped by the server.
let
bad =
Command
::
new
(
env
!
(
"CARGO_BIN_EXE_rtk-proxy"
)
)
.
args
(
[
"config"
,
"set"
,
"--bind"
,
&
bind
,
"not_a_real_key"
,
"x"
]
)
.
output
(
)
.
unwrap
(
)
;
assert
!
(
!bad
.
status
.
success
(
)
,
"unknown setting key should fail"
)
;
let
stop_out =
run_cli
(
&
[
"stop"
,
"--bind"
,
&
bind
]
)
;
assert
!
(
stop_out
.
contains
(
"stopped"
)
,
"got: {stop_out}"
)
;
tokio
::
time
::
sleep
(
Duration
::
from_millis
(
300
)
)
.
await
;
let
after = client
.
get
(
format
!
(
"{base}/health"
)
)
.
send
(
)
.
await
;
assert
!
(
after
.
is_err
(
)
,
"daemon should no longer be reachable after `stop`"
)
;
}
You can’t perform that action at this time.