From e941709ea3e398f5d07ba4b95409fb146c2f144c Mon Sep 17 00:00:00 2001 From: Sebastian Herrlinger Date: Wed, 18 Nov 2026 21:23:23 +0100 Subject: [PATCH] test(codemode): mirror the reference's host-call argument tests tools.rs mirrors codemode.test.ts: a tool's arguments leave a function (a toJSON included) out and await a promise, pending ones too, and a rejection fails the call before the tool runs. extensions.rs mirrors extensions.test.ts and language.test.ts: a host function from a result arrives back as itself wherever it sits, a program function or a global's function is left out, a promise that never settles waits until the timeout, and a result holding functions is handed to a tool as its data. The host-args-promise case mirrors promise.test.ts: a tool's promise inside another call's arguments is awaited. --- .../conformance/cases/host-args-promise.js | 8 ++ .../conformance/cases/host-args-promise.json | 12 ++ .../crates/psychoscript/tests/extensions.rs | 127 ++++++++++++++++++ .../crates/psychoscript/tests/tools.rs | 38 ++++++ 4 files changed, 185 insertions(+) create mode 100644 packages/codemode-rust/conformance/cases/host-args-promise.js create mode 100644 packages/codemode-rust/conformance/cases/host-args-promise.json diff --git a/packages/codemode-rust/conformance/cases/host-args-promise.js b/packages/codemode-rust/conformance/cases/host-args-promise.js new file mode 100644 index 00000000000..a1843736660 --- /dev/null +++ b/packages/codemode-rust/conformance/cases/host-args-promise.js @@ -0,0 +1,8 @@ +// A tool's arguments await a promise inside them, a pending one first, and leave a function out; a rejection fails +// the call with its reason. +const nested = await tools.echo({ id: tools.echo({ id: 1 }) }) +const slow = await tools.echo({ id: (async () => { await null; await null; return 2 })() }) +const fn = await tools.git.status({ f: () => 1 }).then((s) => s.branch, (e) => e.message) +const rejected = await tools.echo({ id: tools.fail({}) }).catch((e) => e.message) +const unknown = await tools.git.stat({ id: tools.echo({ id: 3 }) }).catch((e) => e.message) +return [nested, slow, fn, rejected, unknown] diff --git a/packages/codemode-rust/conformance/cases/host-args-promise.json b/packages/codemode-rust/conformance/cases/host-args-promise.json new file mode 100644 index 00000000000..4d62692815b --- /dev/null +++ b/packages/codemode-rust/conformance/cases/host-args-promise.json @@ -0,0 +1,12 @@ +{ + "bindings": {}, + "ok": true, + "value": [ + 1, + 2, + "main", + "boom", + "Unknown tool 'git.stat'. Did you mean tools.git.status?" + ], + "logs": [] +} diff --git a/packages/codemode-rust/crates/psychoscript/tests/extensions.rs b/packages/codemode-rust/crates/psychoscript/tests/extensions.rs index a0f468ad13f..76d3379dc37 100644 --- a/packages/codemode-rust/crates/psychoscript/tests/extensions.rs +++ b/packages/codemode-rust/crates/psychoscript/tests/extensions.rs @@ -307,6 +307,133 @@ fn host_errors_become_program_errors() { ); } +#[test] +fn a_host_function_passed_back_arrives_as_itself_wherever_it_sits() { + // Each file's `text` is named after the file, so the host can tell which function it got back. + let rt = Runtime::new().extension( + Extension::new("files") + .function( + "file", + HostFunction::new("file", 1, |args| { + let name = args[0].as_str().unwrap_or_default().to_owned(); + let text = HostFunction::new(format!("text:{name}"), 0, |_| Ok("text".into())); + Ok(HostValue::object([ + ("name", HostValue::String(name)), + ("text", text.into()), + ])) + }), + ) + .function( + "copy", + HostFunction::new_async("copy", 2, |args| async move { + let text = match &args[1] { + HostValue::Object(fields) => fields.iter().find(|(k, _)| k == "text"), + _ => None, + }; + let name = match text { + Some((_, HostValue::Function(f))) => f.name().to_owned(), + _ => "no function".to_owned(), + }; + Ok(HostValue::array([args[0].clone(), HostValue::String(name)])) + }), + ), + ); + assert_eq!( + value(&rt, r#"return await copy("b", file("a"))"#), + json!(["b", "text:a"]) + ); + assert_eq!( + value( + &rt, + r#"const f = file("a"); return await copy("b", { text: f.text })"# + ), + json!(["b", "text:a"]) + ); +} + +#[test] +fn a_program_function_or_a_global_passed_to_the_host_is_left_out() { + let taken = Held::default(); + let into = taken.clone(); + let rt = Runtime::new().extension( + Extension::new("gated") + .object( + "fs", + HostValue::object([( + "rm", + HostFunction::new("rm", 0, |_| Ok(HostValue::Undefined)).into(), + )]), + ) + .function( + "take", + HostFunction::new("take", 1, move |args| { + into.lock() + .unwrap() + .push(args.into_iter().next().unwrap_or_default()); + Ok(HostValue::Undefined) + }), + ), + ); + value( + &rt, + r#"take({ text: async () => "x" }); take({ text: take }); take(fs.rm); take({ a: fs.rm }); take(fs)"#, + ); + let taken: Vec> = taken + .lock() + .unwrap() + .iter() + .map(HostValue::to_json) + .collect(); + assert_eq!( + taken, + [ + Some(json!({})), + Some(json!({})), + None, + Some(json!({})), + Some(json!({})) + ] + ); +} + +#[test] +fn a_promise_in_the_arguments_that_never_settles_waits_until_the_timeout() { + let held = Held::default(); + let rt = Runtime::new() + .extension(web(&held).function( + "never", + HostFunction::new_async("never", 0, |_| std::future::pending()), + )) + .limits(Limits { + timeout_ms: Some(50), + ..Limits::default() + }); + let d = rt + .execute("keep({ n: never() })", &serde_json::Map::new()) + .result + .unwrap_err(); + assert_eq!(d.kind.name(), "TimeoutExceeded"); + assert!(held.lock().unwrap().is_empty()); +} + +#[test] +fn a_result_with_functions_is_handed_to_a_tool_as_its_data() { + let held = Held::default(); + let rt = Runtime::new().extension(web(&held)).with_tool( + "echo", + psychoscript::Tool::new("Echo", |input, _| Ok(input["v"].clone())) + .input(psychoscript::Schema::validated(json!({ "type": "object" }))) + .output(psychoscript::Schema::validated(json!({}))), + ); + assert_eq!( + value( + &rt, + r#"return await tools.echo({ v: await fetch("https://a.test/") })"# + ), + json!({ "status": 200, "ok": true, "headers": {}, "handlers": [null] }) + ); +} + #[test] fn extension_calls_are_not_tool_calls() { let held = Held::default(); diff --git a/packages/codemode-rust/crates/psychoscript/tests/tools.rs b/packages/codemode-rust/crates/psychoscript/tests/tools.rs index ae9653ca547..ae55e76fff5 100644 --- a/packages/codemode-rust/crates/psychoscript/tests/tools.rs +++ b/packages/codemode-rust/crates/psychoscript/tests/tools.rs @@ -142,6 +142,44 @@ fn arguments_follow_json_serialization_and_drop_undefined() { assert_eq!(*observed.lock().unwrap(), vec![json!({ "limit": 5 })]); } +#[test] +fn tool_arguments_leave_functions_out_and_await_promises_as_json_did() { + let observed = Arc::new(Mutex::new(Vec::new())); + let seen = observed.clone(); + let rt = Runtime::new().with_tool( + "shop.show", + Tool::new("Show raw input", move |input, _| { + seen.lock().unwrap().push(input); + Ok(json!("ok")) + }) + .input(Schema::validated(json!({ "type": "object" }))), + ); + for code in [ + r#"await tools.shop.show({ q: "x", format(v) { return v } })"#, + "await tools.shop.show({ n: Promise.resolve(5) })", + // A toJSON method is a function too: left out, not run. + r#"await tools.shop.show({ item: "book", price: { cents: 1999, toJSON() { return "$19.99" } } })"#, + "await tools.shop.show({ later: (async () => { await null; return [1, () => 2] })() })", + ] { + assert!(run(&rt, code).result.is_ok(), "{code}"); + } + assert_eq!( + *observed.lock().unwrap(), + vec![ + json!({ "q": "x" }), + json!({ "n": 5 }), + json!({ "item": "book", "price": { "cents": 1999 } }), + json!({ "later": [1, null] }), + ] + ); + let d = failure( + &rt, + r#"await tools.shop.show({ n: Promise.reject(new Error("no price")) })"#, + ); + assert!(d.message.contains("no price"), "{}", d.message); + assert_eq!(observed.lock().unwrap().len(), 4); +} + #[test] fn validated_input_drops_excess_properties_before_the_tool_sees_it() { let observed = Arc::new(Mutex::new(Vec::new()));