mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 08:39:02 +03:00
test(cli-rs): inspect drive requests at the WebSocket boundary
requests_drive_a_headless_app checks typed text, modifiers, arrows, resize and unsupported requests against a running app. Check the string response ID, success and error envelopes, fractional resize and unsupported click there instead of comparing request enums or calling response constructors. malformed_requests_are_rejected keeps the bad-schema cases.
This commit is contained in:
@@ -203,7 +203,7 @@ mod tests {
|
||||
|
||||
let requests = [
|
||||
r#"{"jsonrpc":"2.0","id":1,"method":"simulation.handshake","params":{"client":{"name":"t","version":"1"},"expectedRole":"ui","offeredVersions":[1],"requiredCapabilities":["ui.capture"],"optionalCapabilities":[]}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":2,"method":"ui.type","params":{"text":"Adx"}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":"type","method":"ui.type","params":{"text":"Adx"}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":3,"method":"ui.press","params":{"key":"backspace"}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":4,"method":"ui.press","params":{"key":"a"}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":5,"method":"ui.press","params":{"key":"c","modifiers":{"ctrl":true}}}"#,
|
||||
@@ -217,9 +217,11 @@ mod tests {
|
||||
r#"{"jsonrpc":"2.0","id":13,"method":"ui.mouse","params":{"action":"scroll","x":0,"y":0,"direction":"down"}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":14,"method":"ui.mouse","params":{"action":"move","x":30,"y":0}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":15,"method":"ui.resize","params":{"cols":0,"rows":3}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":20,"method":"ui.resize","params":{"cols":20.5,"rows":3}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":16,"method":"ui.resize","params":{"cols":20,"rows":3}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":17,"method":"ui.capture"}"#,
|
||||
r#"{"jsonrpc":"2.0","id":18,"method":"ui.state"}"#,
|
||||
r#"{"jsonrpc":"2.0","id":21,"method":"ui.click","params":{"target":1,"x":0,"y":0}}"#,
|
||||
r#"{"jsonrpc":"2.0","method":"ui.press","params":{"key":"!"}}"#,
|
||||
r#"{"jsonrpc":"2.0","id":19,"method":"ui.nope"}"#,
|
||||
];
|
||||
@@ -246,13 +248,17 @@ mod tests {
|
||||
};
|
||||
|
||||
assert_eq!(result(1).unwrap()["server"]["version"], "1.2.3");
|
||||
assert_eq!(result(2).unwrap(), json!({ "focused": { "editor": true }, "elements": [] }));
|
||||
assert_eq!(
|
||||
responses[1],
|
||||
json!({"jsonrpc": "2.0", "id": "type", "result": {"focused": {"editor": true}, "elements": []}})
|
||||
);
|
||||
assert_eq!(result(7), Ok(json!(true)));
|
||||
assert_eq!(result(9), Ok(json!(true)));
|
||||
// The input under the pointer takes the wheel step, so no app mouse handler sees it.
|
||||
assert_eq!(result(13).unwrap()["elements"], json!([]));
|
||||
assert_eq!(result(14), Err("mouse position must be within the terminal viewport".into()));
|
||||
assert_eq!(result(15), Err("resize cols and rows must be positive integers".into()));
|
||||
assert_eq!(result(20), result(15));
|
||||
let frame = result(17).unwrap();
|
||||
assert_eq!((frame["cols"].clone(), frame["rows"].clone()), (json!(20), json!(3)));
|
||||
let row = |y: usize| -> String {
|
||||
@@ -261,9 +267,12 @@ mod tests {
|
||||
assert_eq!(row(0).trim_end(), "Ada");
|
||||
assert_eq!(row(1).trim_end(), "submitted Ada");
|
||||
assert!(result(18).unwrap_err().starts_with("ui.state is not supported"));
|
||||
assert!(result(21).unwrap_err().starts_with("ui.click is not supported"));
|
||||
let invalid = responses.last().unwrap();
|
||||
assert_eq!(invalid["jsonrpc"], "2.0");
|
||||
assert_eq!(invalid["id"], Value::Null);
|
||||
assert_eq!(responses.len(), 19);
|
||||
assert_eq!(invalid["error"]["code"], -32000);
|
||||
assert_eq!(responses.len(), 21);
|
||||
assert_eq!(
|
||||
*events.borrow(),
|
||||
[
|
||||
|
||||
@@ -245,35 +245,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requests_decode_as_the_frontend_schema() {
|
||||
let decode_request = |input: &str| decode(input).map(|envelope| envelope.request);
|
||||
assert_eq!(
|
||||
decode(r#"{"jsonrpc":"2.0","id":"a","method":"ui.type","params":{"text":"hi"}}"#).unwrap(),
|
||||
Envelope { id: Some(json!("a")), request: Request::Type { text: "hi".into() } }
|
||||
);
|
||||
assert_eq!(decode(r#"{"jsonrpc":"2.0","method":"ui.enter"}"#).unwrap().id, None);
|
||||
assert_eq!(
|
||||
decode_request(
|
||||
r#"{"jsonrpc":"2.0","id":1,"method":"ui.press","params":{"key":"c","modifiers":{"ctrl":true}}}"#
|
||||
),
|
||||
Ok(Request::Press { key: "c".into(), modifiers: KeyModifiers { ctrl: true, ..KeyModifiers::default() } })
|
||||
);
|
||||
assert_eq!(
|
||||
decode_request(r#"{"jsonrpc":"2.0","id":1,"method":"ui.arrow","params":{"direction":"left"}}"#),
|
||||
Ok(Request::Arrow(Direction::Left))
|
||||
);
|
||||
assert_eq!(
|
||||
decode_request(r#"{"jsonrpc":"2.0","id":1,"method":"ui.resize","params":{"cols":80.5,"rows":24}}"#),
|
||||
Ok(Request::Resize { cols: 80.5, rows: 24.0 })
|
||||
);
|
||||
assert_eq!(
|
||||
decode_request(r#"{"jsonrpc":"2.0","id":1,"method":"ui.state"}"#),
|
||||
Ok(Request::Unsupported("ui.state"))
|
||||
);
|
||||
assert_eq!(
|
||||
decode_request(r#"{"jsonrpc":"2.0","id":1,"method":"ui.click","params":{"target":1,"x":0,"y":0}}"#),
|
||||
Ok(Request::Unsupported("ui.click"))
|
||||
);
|
||||
fn malformed_requests_are_rejected() {
|
||||
for bad in [
|
||||
"nope",
|
||||
r#"{"jsonrpc":"1.0","id":1,"method":"ui.enter"}"#,
|
||||
@@ -286,14 +258,4 @@ mod tests {
|
||||
assert!(decode(bad).is_err(), "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn responses_are_json_rpc() {
|
||||
assert_eq!(success(None, json!(true)), None);
|
||||
assert_eq!(success(Some(json!(3)), json!(true)), Some(json!({ "jsonrpc": "2.0", "id": 3, "result": true })));
|
||||
assert_eq!(
|
||||
failure(None, "bad"),
|
||||
json!({ "jsonrpc": "2.0", "id": null, "error": { "code": -32000, "message": "bad" } })
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user