test_r::enable!();
use self::common::test_server::start_test_server;
use crate::common::{CompiledTest, TestInstance, invoke_and_capture_output};
use anyhow::anyhow;
use camino::Utf8Path;
use indoc::{formatdoc, indoc};
use pretty_assertions::assert_eq;
use rand::Rng;
use std::slice;
use test_r::{test, test_dep};
use wasmtime::component::Val;
#[allow(dead_code)]
mod common;
#[test_dep(tagged_as = "example1")]
fn compiled_example1() -> CompiledTest {
let path = Utf8Path::new("examples/example1");
CompiledTest::new(path, true).expect("Failed to compile example1")
}
#[test_dep(tagged_as = "example2")]
fn compiled_example2() -> CompiledTest {
let path = Utf8Path::new("examples/example2");
CompiledTest::new(path, true).expect("Failed to compile example2")
}
#[test_dep(tagged_as = "example3")]
fn compiled_example3() -> CompiledTest {
let path = Utf8Path::new("examples/example3");
CompiledTest::new(path, true).expect("Failed to compile example3")
}
#[test_dep(tagged_as = "console")]
fn compiled_console() -> CompiledTest {
let path = Utf8Path::new("examples/console");
CompiledTest::new(path, true).expect("Failed to compile console")
}
#[test_dep(tagged_as = "encoding")]
fn compiled_encoding() -> CompiledTest {
let path = Utf8Path::new("examples/encoding");
CompiledTest::new(path, true).expect("Failed to compile encoding")
}
#[test_dep(tagged_as = "export_from_inner_package")]
fn compiled_export_from_inner_package() -> CompiledTest {
let path = Utf8Path::new("examples/export-from-inner-package");
CompiledTest::new(path, true).expect("Failed to compile export-from-inner-package")
}
#[test_dep(tagged_as = "fetch")]
fn compiled_fetch() -> CompiledTest {
let path = Utf8Path::new("examples/fetch");
CompiledTest::new(path, true).expect("Failed to compile fetch")
}
#[test_dep(tagged_as = "imports1")]
fn compiled_imports1() -> CompiledTest {
let path = Utf8Path::new("examples/imports1");
CompiledTest::new(path, true).expect("Failed to compile imports1")
}
#[test_dep(tagged_as = "imports2")]
fn compiled_imports2() -> CompiledTest {
let path = Utf8Path::new("examples/imports2");
CompiledTest::new(path, true).expect("Failed to compile imports2")
}
#[test_dep(tagged_as = "imports3")]
fn compiled_imports3() -> CompiledTest {
let path = Utf8Path::new("examples/imports3");
CompiledTest::new(path, true).expect("Failed to compile imports3")
}
#[test_dep(tagged_as = "types_in_exports")]
fn compiled_types_in_exports() -> CompiledTest {
let path = Utf8Path::new("examples/types-in-exports");
CompiledTest::new(path, true).expect("Failed to compile types-in-exports")
}
#[test_dep(tagged_as = "stateful1")]
fn compiled_stateful1() -> CompiledTest {
let path = Utf8Path::new("examples/stateful1");
CompiledTest::new(path, true).expect("Failed to compile stateful1")
}
#[test_dep(tagged_as = "streams")]
fn compiled_streams() -> CompiledTest {
let path = Utf8Path::new("examples/streams");
CompiledTest::new(path, true).expect("Failed to compile streams")
}
#[test_dep(tagged_as = "timeout")]
fn compiled_timeout() -> CompiledTest {
let path = Utf8Path::new("examples/timeout");
CompiledTest::new(path, true).expect("Failed to compile timeout")
}
#[test_dep(tagged_as = "bigint_roundtrip")]
fn compiled_bigint_roundtrip() -> CompiledTest {
let path = Utf8Path::new("examples/bigint-roundtrip");
CompiledTest::new(path, true).expect("Failed to compile bigint-roundtrip")
}
#[test_dep(tagged_as = "pollable")]
fn compiled_pollable() -> CompiledTest {
let path = Utf8Path::new("examples/pollable");
CompiledTest::new(path, true).expect("Failed to compile pollable")
}
#[test_dep(tagged_as = "fs")]
fn compiled_fs() -> CompiledTest {
let path = Utf8Path::new("examples/fs");
CompiledTest::new(path, false).expect("Failed to compile fs")
}
#[test_dep(tagged_as = "url")]
fn compiled_url() -> CompiledTest {
let path = Utf8Path::new("examples/url");
CompiledTest::new(path, false).expect("Failed to compile url")
}
#[test_dep(tagged_as = "crypto")]
fn compiled_crypto() -> CompiledTest {
let path = Utf8Path::new("examples/crypto");
CompiledTest::new(path, false).expect("Failed to compile crypto")
}
#[test_dep(tagged_as = "response_static")]
fn compiled_response_static() -> CompiledTest {
let path = Utf8Path::new("examples/response-static");
CompiledTest::new(path, true).expect("Failed to compile response-static")
}
#[test_dep(tagged_as = "abort_controller")]
fn compiled_abort_controller() -> CompiledTest {
let path = Utf8Path::new("examples/abort-controller");
CompiledTest::new(path, true).expect("Failed to compile abort-controller")
}
#[test_dep(tagged_as = "xhr")]
fn compiled_xhr() -> CompiledTest {
let path = Utf8Path::new("examples/xhr");
CompiledTest::new(path, true).expect("Failed to compile xhr")
}
#[test_dep(tagged_as = "os")]
fn compiled_os() -> CompiledTest {
let path = Utf8Path::new("examples/os");
CompiledTest::new(path, true).expect("Failed to compile os")
}
#[test_dep(tagged_as = "structured_clone")]
fn compiled_structured_clone() -> CompiledTest {
let path = Utf8Path::new("examples/structured-clone");
CompiledTest::new(path, true).expect("Failed to compile structured-clone")
}
#[test]
async fn example1_sync(#[tagged_as("example1")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (result, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"hello",
&[Val::String("world".to_string())],
)
.await;
let result = result?;
assert_eq!(result, Some(Val::String("Hello, world! (123)".to_string())));
assert_eq!(output, "hello called with world\n");
Ok(())
}
#[test]
async fn example1_async(#[tagged_as("example1")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (result, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"async-hello",
&[Val::String("world".to_string())],
)
.await;
let result = result?;
assert_eq!(result, Some(Val::String("Hello, world! (123)".to_string())));
assert_eq!(output, "hello called with world\n");
Ok(())
}
#[test]
async fn example2_sync(#[tagged_as("example2")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (result, output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("quickjs:example2/exp1"),
"hello",
&[Val::String("world".to_string())],
)
.await;
let result = result?;
assert_eq!(result, Some(Val::String("Hello, world! (123)".to_string())));
assert_eq!(output, "hello called with world\n");
Ok(())
}
#[test]
async fn example2_async(#[tagged_as("example2")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (result, output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("quickjs:example2/exp2"),
"async-hello",
&[Val::String("world".to_string())],
)
.await;
let result = result?;
assert_eq!(result, Some(Val::String("Hello, world!".to_string())));
assert_eq!(output, "hello called with world\n");
Ok(())
}
#[test]
async fn example3(#[tagged_as("example3")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut test_instance = TestInstance::new(compiled.wasm_path()).await?;
let (h1, _) = test_instance
.invoke_and_capture_output(
Some("quickjs:example3/iface"),
"[constructor]hello",
&[Val::String("user1".to_string())],
)
.await;
let h1 = h1?;
let Val::Resource(h1) = h1.unwrap() else {
panic!("Expected a resource handle")
};
let (name1, _) = test_instance
.invoke_and_capture_output(
Some("quickjs:example3/iface"),
"[method]hello.get-name",
&[Val::Resource(h1)],
)
.await;
let name1 = name1?;
let (h2, _) = test_instance
.invoke_and_capture_output(
Some("quickjs:example3/iface"),
"[constructor]hello",
&[Val::String("user2".to_string())],
)
.await;
let h2 = h2?;
let Val::Resource(h2) = h2.unwrap() else {
panic!("Expected a resource handle")
};
let (name2, _) = test_instance
.invoke_and_capture_output(
Some("quickjs:example3/iface"),
"[method]hello.get-name",
&[Val::Resource(h2)],
)
.await;
let name2 = name2?;
let (compare, _) = test_instance
.invoke_and_capture_output(
Some("quickjs:example3/iface"),
"[static]hello.compare",
&[Val::Resource(h1), Val::Resource(h2)],
)
.await;
let compare = compare?;
let (merged, _) = test_instance
.invoke_and_capture_output(
Some("quickjs:example3/iface"),
"[static]hello.merge",
&[Val::Resource(h1), Val::Resource(h2)],
)
.await;
let merged = merged?;
let Val::Resource(merged) = merged.unwrap() else {
panic!("Expected a resource handle")
};
let (name3, _) = test_instance
.invoke_and_capture_output(
Some("quickjs:example3/iface"),
"[method]hello.get-name",
&[Val::Resource(merged)],
)
.await;
let name3 = name3?;
test_instance.drop_resource(merged).await?;
assert_eq!(name1, Some(Val::String("user1".to_string())));
assert_eq!(name2, Some(Val::String("user2".to_string())));
assert_eq!(compare, Some(Val::S32(-1)));
assert_eq!(name3, Some(Val::String("user1 & user2".to_string())));
Ok(())
}
#[test]
async fn console(#[tagged_as("console")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (r, output) = invoke_and_capture_output(compiled.wasm_path(), None, "run", &[]).await;
let _ = r?;
println!("{output}");
let lines = output.lines().collect::<Vec<_>>();
let (output, timer_output) = lines.split_at(lines.len() - 3);
let output_str = output.join("\n");
let timer_output = timer_output.join("\n");
assert_eq!(
output_str,
formatdoc!(
r#"
default: 1
logged message 1 2 {{ key: 'value' }}
TRACE: This is a trace message
DEBUG: This is an debug message
INFO: This is an info message
WARN: This is a warning message
ERROR: This is an error message
default: 2
WARN: Assertion failed: This is an assertion failure
Group 1
Inside Group 1
Group 2
Inside Group 2
default: 3
test: 1
test: 2
default: 1
{colored}
{{ key: 'value', nested: {{ a: 1, b: 2 }} }}
======================
(index) │ 0 │
----------------------
0 │ apples │
1 │ oranges │
2 │ bananas │
======================
==============================
(index) │ 0 │ 1 │
------------------------------
0 │ Tyrone │ Jones │
1 │ Janet │ Smith │
2 │ Maria │ Cruz │
==============================
=======================
(index) │ 0 │
-----------------------
firstName │ Tyrone │
lastName │ Jones │
=======================
========================
(index) │ firstName │
------------------------
0 │ Tyrone │
1 │ Janet │
2 │ Maria │
========================
--- Map Examples ---
Empty map: Map(0) {{}}
Simple map: Map(3) {{ 'name' => 'John', 'age' => 30, 'city' => 'New York' }}
Map with mixed keys: Map(3) {{ 'string' => 'value', 42 => 'number key', {{ key: 'obj' }} => 'object key' }}
Map with object values: Map(2) {{ 'user' => {{ name: 'Alice', age: 25 }}, 'config' => {{ debug: true, timeout: 5000 }} }}
Map with array values: Map(2) {{ 'colors' => [ 'red', 'green', 'blue' ], 'numbers' => [ 1, 2, 3, 4, 5 ] }}
--- Map in dir() ---
Map(3) {{ 'name' => 'John', 'age' => 30, 'city' => 'New York' }}
--- Map in inspect() ---
Map(2) {{ 'user' => {{ name: 'Alice', age: 25 }}, 'config' => {{ debug: true, timeout: 5000 }} }}
--- Set Examples ---
Empty set: Set(0) {{}}
Simple set: Set(3) {{ 'apple', 'banana', 'cherry' }}
Set with mixed types: Set(4) {{ 'string', 42, {{ key: 'obj' }}, [ 1, 2, 3 ] }}
Set with objects: Set(2) {{ {{ name: 'Alice', age: 25 }}, {{ name: 'Bob', age: 30 }} }}
--- Set in dir() ---
Set(3) {{ 'apple', 'banana', 'cherry' }}
--- Set in inspect() ---
Set(2) {{ {{ name: 'Alice', age: 25 }}, {{ name: 'Bob', age: 30 }} }}
--- WeakSet Examples ---
WeakSet: WeakSet {{ <items unknown> }}
--- WeakMap Examples ---
WeakMap: WeakMap {{ <items unknown> }}"#,
colored = "{ key: \u{1b}[32m'value'\u{1b}[39m, nested: { a: \u{1b}[33m1\u{1b}[39m, b: \u{1b}[33m2\u{1b}[39m } }"
)
);
assert!(timer_output.contains("after 1 second"));
assert!(timer_output.contains("after 2 seconds"));
assert!(timer_output.contains("- timer ended"));
Ok(())
}
#[test]
async fn encoding(#[tagged_as("encoding")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (r, output) = invoke_and_capture_output(compiled.wasm_path(), None, "test1", &[]).await;
let _ = r?;
assert_eq!(
output,
indoc!(
r#"
Decoded text: ð ®·
Encoded array: {"0":226,"1":130,"2":172}
Enqueued [Message 0]
Encoded chunk: {"0":91,"1":77,"2":101,"3":115,"4":115,"5":97,"6":103,"7":101,"8":32,"9":48,"10":93}
Enqueued [Message 1]
Encoded chunk: {"0":91,"1":77,"2":101,"3":115,"4":115,"5":97,"6":103,"7":101,"8":32,"9":49,"10":93}
Enqueued [Message 2]
Encoded chunk: {"0":91,"1":77,"2":101,"3":115,"4":115,"5":97,"6":103,"7":101,"8":32,"9":50,"10":93}
Enqueued [Message 3]
Encoded chunk: {"0":91,"1":77,"2":101,"3":115,"4":115,"5":97,"6":103,"7":101,"8":32,"9":51,"10":93}
Enqueued [Message 4]
Encoded chunk: {"0":91,"1":77,"2":101,"3":115,"4":115,"5":97,"6":103,"7":101,"8":32,"9":52,"10":93}
Enqueued [Message 5]
Encoded chunk: {"0":91,"1":77,"2":101,"3":115,"4":115,"5":97,"6":103,"7":101,"8":32,"9":53,"10":93}
Enqueued [Message 6]
Encoded chunk: {"0":91,"1":77,"2":101,"3":115,"4":115,"5":97,"6":103,"7":101,"8":32,"9":54,"10":93}
Enqueued [Message 7]
Encoded chunk: {"0":91,"1":77,"2":101,"3":115,"4":115,"5":97,"6":103,"7":101,"8":32,"9":55,"10":93}
Enqueued [Message 8]
Encoded chunk: {"0":91,"1":77,"2":101,"3":115,"4":115,"5":97,"6":103,"7":101,"8":32,"9":56,"10":93}
Encoded buffer from stream: [91,77,101,115,115,97,103,101,32,48,93,91,77,101,115,115,97,103,101,32,49,93,91,77,101,115,115,97,103,101,32,50,93,91,77,101,115,115,97,103,101,32,51,93,91,77,101,115,115,97,103,101,32,52,93,91,77,101,115,115,97,103,101,32,53,93,91,77,101,115,115,97,103,101,32,54,93,91,77,101,115,115,97,103,101,32,55,93,91,77,101,115,115,97,103,101,32,56,93]
Decoded chunk: "[Message 0][Mess"
Decoded chunk: "age 1][Message 2"
Decoded chunk: "][Message 3][Mes"
Decoded chunk: "sage 4][Message "
Decoded chunk: "5][Message 6][Me"
Decoded chunk: "ssage 7][Message"
Decoded chunk: " 8]"
"#
)
);
Ok(())
}
#[test]
async fn export_from_inner_package(
#[tagged_as("export_from_inner_package")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("quickjs:inner/exp1@0.0.1"),
"hello",
&[Val::String("world".to_string())],
)
.await;
let result = result?;
assert_eq!(result, Some(Val::String("Hello, world!".to_string())));
assert_eq!(output, "hello called with world\n");
Ok(())
}
#[test]
async fn fetch_post_json_and_get(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-json-and-get",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains(&format!(
"Response from http://localhost:{port}/todos: 201 Created (ok=true)\n"
)));
assert!(output.contains(&format!(
"Response from http://localhost:{port}/todos/0: 200 OK (ok=true)\n"
)));
assert!(output.contains(
"Body: {\"id\":0,\"userId\":1,\"title\":\"foo\",\"body\":\"bar\",\"completed\":false}"
));
Ok(())
}
#[test]
async fn fetch_post_and_get_as_array_buffer(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-and-get-as-array-buffer",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert_eq!(
output,
indoc!(
r#"
fetch test 2
Response body as ArrayBuffer: {}
"#
)
);
Ok(())
}
#[test]
async fn fetch_streaming_response_body(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"streaming-response-body",
&[Val::U16(port)],
)
.await;
let _ = r?;
let chunk_count = output
.lines()
.filter(|l| l.starts_with("Received chunk: "))
.count();
assert!(chunk_count > 0);
Ok(())
}
#[test]
async fn fetch_pipe_response_body_to_request(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"pipe-response-body-to-request",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains(&format!(
"Response from http://localhost:{port}/echo: 200 OK (ok=true)"
)));
assert!(output.contains("Body: [{\"id\":"));
Ok(())
}
#[test]
async fn fetch_pipe_buffered_response_body_to_request(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"pipe-buffered-response-body-to-request",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains(&format!(
"Response from http://localhost:{port}/echo: 200 OK (ok=true)"
)));
assert!(output.contains("Body: [{\"id\":"));
Ok(())
}
#[test]
async fn fetch_redirects(#[tagged_as("fetch")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"redirect-follow",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains("Redirect followed successfully"));
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"redirect-manual",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains("Manual redirect handled correctly"));
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"redirect-error",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains("Caught expected error for redirect: error"));
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"redirect-loop",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains("Caught expected error for loop"));
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-with-redirect",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains("Status: 200"));
assert!(output.contains("Redirected: true"));
assert!(output.contains("Body: hello world"));
Ok(())
}
#[test]
async fn fetch_concurrent_post_and_get(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"concurrent-post-and-get",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains("200 '{\"id\":0,\""));
assert!(output.contains("200 '{\"id\":1,\""));
assert!(output.contains("200 '{\"id\":2,\""));
assert!(output.contains("200 '{\"id\":3,\""));
assert!(output.contains("200 '{\"id\":4,\""));
Ok(())
}
#[test]
async fn fetch_post_with_slow_streaming_body(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-with-slow-streaming-body",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains(&format!(
"Response from http://localhost:{port}/todos: 201 Created (ok=true)"
)));
assert!(output.contains(
"Body: {\"id\":0,\"userId\":1,\"title\":\"foo\",\"body\":\"bar\",\"completed\":false}"
));
Ok(())
}
#[test]
async fn fetch_blob_operations(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "blob-operations", &[]).await;
let _ = r?;
assert!(output.contains("Blob text: hello, world"));
assert!(output.contains("Blob array buffer length: 12"));
assert!(output.contains("FormData keys: [\"f1\",\"f2\"]"));
assert!(output.contains("f1 {}"));
assert!(output.contains("f2 {\"size\":123,\"type\":\"\",\"name\":\"cat-video.mp4\"}"));
Ok(())
}
#[test]
async fn fetch_post_with_blob_body(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-with-blob-body",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains(&format!(
"Response from http://localhost:{port}/todos: 201 Created (ok=true)"
)));
assert!(output.contains(
"Body: {\"id\":0,\"userId\":1,\"title\":\"foo\",\"body\":\"bar\",\"completed\":false}"
));
Ok(())
}
#[test]
async fn fetch_post_form_data_with_files(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-form-data-with-files",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains(&format!(
"http://localhost:{port}/echo-form: 200 OK (ok=true)"
)));
assert!(output.contains("Body: [{\"name\":\"f1\",\"data\":[97,98,99]},{\"name\":\"f2\",\"data\":[123,34,116,105,116,108,101,34,58,34,102,111,111,34,44,34,98,111,100,121,34,58,34,98,97,114,34,44,34,117,115,101,114,73,100,34,58,49,125]}]"));
Ok(())
}
#[test]
async fn fetch_with_request_object(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"fetch-with-request-object",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains(&format!(
"Response from http://localhost:{port}/todos: 201 Created (ok=true)\n"
)));
assert!(output.contains(&format!(
"Response from http://localhost:{port}/todos/0: 200 OK (ok=true)\n"
)));
assert!(output.contains(
"Body: {\"id\":0,\"userId\":1,\"title\":\"foo\",\"body\":\"bar\",\"completed\":false}"
));
Ok(())
}
#[test]
async fn fetch_post_with_data_view_body(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-with-data-view-body",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 11 (DataView)"));
assert!(output.contains(&format!(
"Response from http://localhost:{port}/todos: 201 Created (ok=true)\n"
)));
assert!(output.contains(
"Body: {\"id\":0,\"userId\":1,\"title\":\"foo\",\"body\":\"bar\",\"completed\":false}"
));
Ok(())
}
#[test]
async fn fetch_post_with_url_search_params(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-with-url-search-params",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 12 (URLSearchParams)"));
assert!(output.contains("URLSearchParams toString: title=foo&body=bar&userId=1"));
assert!(output.contains(&format!(
"Response from http://localhost:{port}/form-echo: 200 OK (ok=true)"
)));
assert!(output.contains("\"body\":\"title=foo&body=bar&userId=1\""));
Ok(())
}
#[test]
async fn fetch_request_with_url_search_params(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"request-with-url-search-params",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 13 (URLSearchParams in Request)"));
assert!(output.contains("Request body used: false"));
assert!(output.contains(&format!(
"Response from http://localhost:{port}/form-echo: 200 OK (ok=true)"
)));
assert!(output.contains("\"body\":\"name=John&email=john%40example.com\""));
Ok(())
}
#[test]
async fn fetch_with_referrer(#[tagged_as("fetch")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"fetch-with-referrer",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 14 (referrer with fetch)"));
assert!(output.contains("Test 1 referer sent: ''"));
assert!(output.contains(&format!(
"Test 2 referer sent: 'http://localhost:{port}/source'"
)));
assert!(output.contains("Test 3 referer sent: ''"));
Ok(())
}
#[test]
async fn fetch_with_referrer_policy(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"fetch-with-referrer-policy",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 15 (referrerPolicy with fetch)"));
assert!(output.contains("Test 1 referer sent: ''"));
assert!(output.contains(&format!("Test 2 referer sent: 'http://localhost:{port}'")));
assert!(output.contains(&format!(
"Test 3 referer sent: 'http://localhost:{port}/source'"
)));
assert!(output.contains(&format!(
"Test 4 referer sent: 'http://localhost:{port}/source'"
)));
Ok(())
}
#[test]
async fn fetch_with_credentials(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"fetch-with-credentials",
&[Val::U16(port)],
)
.await;
let _ = r?;
assert!(output.contains("fetch test 16 (credentials option with fetch)"));
assert!(output.contains("Test 1: credentials 'omit'"));
assert!(output.contains("Test 1 authorization: '', cookie: ''"));
assert!(output.contains("Test 1 set-cookie: ''"));
assert!(output.contains("Test 2: credentials 'same-origin'"));
assert!(
output.contains(
"Test 2 authorization: 'Bearer test-token', cookie: 'session=test-session-id'"
)
);
assert!(output.contains("Test 2 set-cookie: 'test-cookie=test-value'"));
assert!(output.contains("Test 3: credentials 'include'"));
assert!(
output.contains(
"Test 3 authorization: 'Bearer test-token', cookie: 'session=test-session-id'"
)
);
assert!(output.contains("Test 3 set-cookie: 'test-cookie=test-value'"));
Ok(())
}
#[test]
async fn fetch_response_clone_basic(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"response-clone-basic",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 22 (response clone - basic)"));
assert!(output.contains("Basic clone test passed"));
Ok(())
}
#[test]
async fn fetch_response_clone_streaming_body(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"response-clone-streaming-body",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 23 (response clone - streaming body)"));
assert!(output.contains("Original status: 200"));
assert!(output.contains("Cloned status: 200"));
assert!(output.contains("Streaming clone test passed"));
Ok(())
}
#[test]
async fn fetch_response_clone_reuse_bodies(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"response-clone-reuse-bodies",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 24 (response clone - reuse bodies)"));
assert!(output.contains("All clones have matching bodies"));
Ok(())
}
#[test]
async fn fetch_response_clone_headers(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"response-clone-headers",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 25 (response clone - headers preservation)"));
assert!(output.contains("Headers preserved in clone"));
Ok(())
}
#[test]
async fn fetch_response_form_data(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"response-form-data",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("response-form-data test"));
assert!(output.contains("Response status: 200"));
assert!(output.contains("Response Content-Type: multipart/form-data"));
assert!(output.contains("username: john_doe"));
assert!(output.contains("email: john@example.com"));
assert!(output.contains("file: File(test.txt"));
assert!(output.contains("response-form-data test completed"));
Ok(())
}
#[test]
async fn imports1(#[tagged_as("imports1")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (result, _) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"test",
&[Val::String("someone".to_string())],
)
.await;
let result = result?;
let Some(Val::String(result)) = result else {
return Err(anyhow!("Expected a string result"));
};
let parts = result.split('-').map(|s| s.trim()).collect::<Vec<_>>();
assert_eq!(parts.len(), 2);
assert_eq!(parts[0], "someone");
assert!(
parts[1].parse::<u64>().is_ok(),
"Expected a number in the second part"
);
Ok(())
}
#[test]
async fn imports2(
#[tagged_as("imports2")] compiled: &CompiledTest,
#[tagged_as("example3")] example3: &CompiledTest,
) -> anyhow::Result<()> {
let composed = example3.plug_into(compiled)?;
let (result, output) = invoke_and_capture_output(
composed.wasm_path(),
None,
"test",
&[Val::String("someone".to_string())],
)
.await;
let result = result?;
let Some(Val::String(result)) = result else {
return Err(anyhow!("Expected a string result"));
};
assert_eq!(result, "someone & World");
assert_eq!(
output,
indoc! { r#"
Comparison 1: 1
Comparison 2: -1
Dump 1: someone
Dump 2: ?
Dump 3: [someone & World]
"# }
);
Ok(())
}
#[test]
async fn imports2_static_create(
#[tagged_as("imports2")] compiled: &CompiledTest,
#[tagged_as("example3")] example3: &CompiledTest,
) -> anyhow::Result<()> {
let composed = example3.plug_into(compiled)?;
let (result, output) = invoke_and_capture_output(
composed.wasm_path(),
None,
"test-static-create",
&[Val::String("someone".to_string())],
)
.await;
let result = result?;
let Some(Val::String(result)) = result else {
return Err(anyhow!("Expected a string result"));
};
assert_eq!(result, "someone");
assert_eq!(
output,
indoc! { r#"
{ [Function: HelloWithStaticCreate] create: [Function], compare: [Function], merge: [Function] }
[Function]
[Function]
[Function]
"# }
);
Ok(())
}
#[test]
async fn imports3(
#[tagged_as("imports3")] compiled: &CompiledTest,
#[tagged_as("types_in_exports")] types_in_exports: &CompiledTest,
) -> anyhow::Result<()> {
let composed = types_in_exports.plug_into(compiled)?;
let (r, output) = invoke_and_capture_output(composed.wasm_path(), None, "test", &[]).await;
let _ = r?;
println!("{output}");
assert_eq!(
output,
indoc!(
r#"
a: [0.10000000149011612,0.20000000298023224,0.30000001192092896]
b: ["a","b"]
c: ["c","d"]
f1: 0.10000000149011612 - a - c,0.20000000298023224 - b - d,0.30000001192092896 - undefined - undefined
a: hello world
f2: 11
a: true (boolean)
b: -8 (number)
c: -16 (number)
d: -32 (number)
e: -64 (bigint)
f: 8 (number)
g: 16 (number)
h: 32 (number)
i: 64 (bigint)
j: 3.140000104904175 (number)
k: 2.718281828459045 (number)
l: c (string)
m: hello world (string)
f3: true,-8,-16,-32,-64,8,16,32,64,3.140000104904175,2.718281828459045,c,hello world
a: {"tag":"ok","val":42}
a.tag: ok
a.val: 42
f4: {"tag":"ok","val":42}
a: {"tag":"err","val":"error message"}
a.tag: err
a.val: error message
f5: {"tag":"err","val":"error message"}
a: {"tag":"ok","val":"success"}
a.tag: ok
a.val: success
f6: {"tag":"ok","val":"success"}
a: {"tag":"ok"}
a.tag: ok
a.val: undefined
f7: {"tag":"ok"}
a: ["example",123,4.559999942779541]
f8: undefined
a: {"a":"test","b":42,"c":3.140000104904175,"d":{"x":1,"y":2},"f":{"tag":"ok","val":100},"g":[],"h":["one","two"],"i":["tuple",1,2.5]}
f9: {"a":"test","b":42,"c":3.140000104904175,"d":{"x":1,"y":2},"f":{"tag":"ok","val":100},"g":[],"h":["one","two"],"i":["tuple",1,2.5]}
a: {"tag":"specific","val":"example"}
a.tag: specific
a.val: example
f10: {"tag":"specific","val":"example"}
a: "blue"
f11: the color is blue
a: "green"
f12: "green"
a: {"read":true,"write":false,"execute":true}
f13: {"read":true,"write":false,"execute":true}
a: 1234567890
f14: 1234567890
bytes: 1,2,3,4,5
f15: 1,2,3,4,5
f15 is a Uint8Array with length: 5
"#
)
);
Ok(())
}
#[test]
async fn stateful1(#[tagged_as("stateful1")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut test_instance = TestInstance::new(compiled.wasm_path()).await?;
let (v0, _) = test_instance
.invoke_and_capture_output(None, "get", &[])
.await;
let v0 = v0?;
let Val::S32(v0) = v0.unwrap() else {
panic!("Expected s32")
};
let (r, output1) = test_instance
.invoke_and_capture_output(None, "inc", &[Val::S32(1)])
.await;
let _ = r?;
let (r, output2) = test_instance
.invoke_and_capture_output(None, "inc", &[Val::S32(3)])
.await;
let _ = r?;
let (v1, _) = test_instance
.invoke_and_capture_output(None, "get", &[])
.await;
let v1 = v1?;
let Val::S32(v1) = v1.unwrap() else {
panic!("Expected s32")
};
assert_eq!(v0, 0);
assert_eq!(output1, "inc by (1)\n");
assert_eq!(output2, "inc by (1)\ninc by (3)\n");
assert_eq!(v1, 4);
Ok(())
}
#[test]
async fn streams(#[tagged_as("streams")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (r, output) = invoke_and_capture_output(compiled.wasm_path(), None, "test1", &[]).await;
let _ = r?;
assert_eq!(
output,
indoc!(
r#"
Enqueued 0
Read 1 characters so far
Most recently read chunk: 0
Enqueued 1
Read 2 characters so far
Most recently read chunk: 1
Enqueued 2
Read 3 characters so far
Most recently read chunk: 2
Enqueued 3
Read 4 characters so far
Most recently read chunk: 3
Enqueued 4
Read 5 characters so far
Most recently read chunk: 4
Enqueued 5
Read 6 characters so far
Most recently read chunk: 5
Enqueued 6
Read 7 characters so far
Most recently read chunk: 6
Enqueued 7
Read 8 characters so far
Most recently read chunk: 7
Enqueued 8
Read 9 characters so far
Most recently read chunk: 8
Stream complete 012345678
"#
)
);
Ok(())
}
#[test]
async fn node_stream1(#[tagged_as("streams")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (r, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-node-stream1", &[]).await;
let r = r?;
assert_eq!(r, Some(Val::String("Good morning!".to_string())));
Ok(())
}
#[test]
async fn timeout_1(#[tagged_as("timeout")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (r, output) = invoke_and_capture_output(compiled.wasm_path(), None, "run", &[]).await;
let _ = r?;
assert_eq!(
output,
indoc!(
r#"
timeout test starts
Message from setImmediate #1
Message from setImmediate #2
This is a repeated message every 250ms
This is a repeated message every 250ms
This is a repeated message every 250ms
This is a delayed message after 1s, with params x, 100
This is a repeated message every 250ms
This is a repeated message every 250ms
This is a repeated message every 250ms
This is a repeated message every 250ms
This is a delayed message after 2s
This is a repeated message every 250ms
This is a repeated message every 250ms
This is a repeated message every 250ms
This is a repeated message every 250ms
This is a followup delayed message after 1s
"#
)
);
Ok(())
}
#[test]
#[ignore] async fn timeout_2(#[tagged_as("timeout")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (r, output) = invoke_and_capture_output(compiled.wasm_path(), None, "parallel", &[]).await;
let _ = r?;
for i in 0..1000 {
assert!(output.contains(&format!("test {i}")));
}
Ok(())
}
#[test]
async fn timeout_3(#[tagged_as("timeout")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "use-next-tick", &[]).await;
let _ = r?;
assert_eq!(
output,
indoc!(
r#"
start
end
nextTick callback 1
nextTick callback 2
setImmediate callback 1
"#
)
);
Ok(())
}
#[test]
async fn roundtrip_u64(
#[tagged_as("bigint_roundtrip")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let mut rng = rand::rng();
let mut cases = Vec::new();
for _ in 0..5 {
cases.push(rng.random());
}
cases.push(u64::MAX);
cases.push(u64::MIN);
for case in cases {
let input = Val::U64(case);
let (result, _) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"roundtrip-u64",
slice::from_ref(&input),
)
.await;
assert_eq!(result?, Some(input));
}
Ok(())
}
#[test]
async fn roundtrip_s64(
#[tagged_as("bigint_roundtrip")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let mut rng = rand::rng();
let mut cases = Vec::new();
for _ in 0..5 {
cases.push(rng.random());
}
cases.push(i64::MAX);
cases.push(i64::MIN);
for case in cases {
let input = Val::S64(case);
let (result, _) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"roundtrip-s64",
slice::from_ref(&input),
)
.await;
assert_eq!(result?, Some(input));
}
Ok(())
}
#[test]
async fn await_pollable(#[tagged_as("pollable")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (r, _) = invoke_and_capture_output(compiled.wasm_path(), None, "test", &[]).await;
let result = r?;
let Some(Val::U64(n)) = result else {
return Err(anyhow!("Expected a u64 result"));
};
assert!(n > 2000000000);
Ok(())
}
#[test]
async fn fs(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance.invoke_and_capture_output(None, "run", &[]).await;
let _result = r?;
let result_file = String::from_utf8(std::fs::read(
instance.temp_dir_path().join("test").join("output.txt"),
)?)?;
assert_eq!(
output,
"Current working directory: /\nArguments: [ 'first-arg', 'second-arg' ]\nEnvironment variables:\nTEST_KEY: TEST_VALUE\nTEST_KEY_2: TEST_VALUE_2\n@@ TEST_KEY: TEST_VALUE\n@@ TEST_KEY_2: TEST_VALUE_2\n"
);
assert_eq!(result_file, "test file contents - Processed by test");
Ok(())
}
#[test]
async fn fs_async(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "run-async", &[])
.await;
let _result = r?;
let result_file = String::from_utf8(std::fs::read(
instance.temp_dir_path().join("test").join("output.txt"),
)?)?;
assert_eq!(output, "test file contents\n");
assert_eq!(result_file, "test file contents - Processed by test");
Ok(())
}
#[test]
async fn fs_promises_write_file(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-fs-promises-write-file", &[])
.await;
let _result = r?;
assert!(output.contains("writeFile succeeded"));
let result_file = String::from_utf8(std::fs::read(
instance
.temp_dir_path()
.join("test")
.join("promises-output.txt"),
)?)?;
assert_eq!(result_file, "written via fs/promises");
Ok(())
}
#[test]
async fn fs_promises_rename(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-fs-promises-rename", &[])
.await;
let _result = r?;
assert!(
!instance
.temp_dir_path()
.join("test")
.join("before-rename.txt")
.exists()
);
let result_file = String::from_utf8(std::fs::read(
instance
.temp_dir_path()
.join("test")
.join("after-rename.txt"),
)?)?;
assert!(output.contains("rename succeeded"));
assert_eq!(result_file, "content to rename");
Ok(())
}
#[test]
async fn fs_promises_mkdir(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-fs-promises-mkdir", &[])
.await;
let _result = r?;
assert!(output.contains("mkdir succeeded"));
assert!(
instance
.temp_dir_path()
.join("test")
.join("new-dir")
.exists()
);
assert!(
instance
.temp_dir_path()
.join("test")
.join("new-dir")
.is_dir()
);
Ok(())
}
#[test]
async fn fs_promises_mkdir_recursive(
#[tagged_as("fs")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-fs-promises-mkdir-recursive", &[])
.await;
let _result = r?;
assert!(output.contains("mkdir recursive succeeded"));
assert!(
instance
.temp_dir_path()
.join("test")
.join("nested")
.join("deep")
.join("dir")
.exists()
);
Ok(())
}
#[test]
async fn fs_promises_unlink(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-fs-promises-unlink", &[])
.await;
let _result = r?;
assert!(output.contains("unlink succeeded"));
assert!(
!instance
.temp_dir_path()
.join("test")
.join("to-delete.txt")
.exists()
);
Ok(())
}
#[test]
async fn fs_rename_sync(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-rename-sync", &[])
.await;
let _result = r?;
assert!(output.contains("renameSync succeeded"));
assert!(
!instance
.temp_dir_path()
.join("test")
.join("rename-sync-before.txt")
.exists()
);
assert!(
instance
.temp_dir_path()
.join("test")
.join("rename-sync-after.txt")
.exists()
);
Ok(())
}
#[test]
async fn fs_rename_callback(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-rename-callback", &[])
.await;
let _result = r?;
assert!(output.contains("rename callback succeeded"));
assert!(
!instance
.temp_dir_path()
.join("test")
.join("rename-cb-before.txt")
.exists()
);
assert!(
instance
.temp_dir_path()
.join("test")
.join("rename-cb-after.txt")
.exists()
);
Ok(())
}
#[test]
async fn fs_mkdir_sync(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-mkdir-sync", &[])
.await;
let _result = r?;
assert!(output.contains("mkdirSync succeeded"));
assert!(
instance
.temp_dir_path()
.join("test")
.join("mkdir-sync-dir")
.is_dir()
);
Ok(())
}
#[test]
async fn fs_mkdir_sync_recursive(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-mkdir-sync-recursive", &[])
.await;
let _result = r?;
assert!(output.contains("mkdirSync recursive succeeded"));
assert!(
instance
.temp_dir_path()
.join("test")
.join("mkdir-sync-nested")
.join("deep")
.join("dir")
.is_dir()
);
Ok(())
}
#[test]
async fn fs_mkdir_callback(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-mkdir-callback", &[])
.await;
let _result = r?;
assert!(output.contains("mkdir callback succeeded"));
assert!(
instance
.temp_dir_path()
.join("test")
.join("mkdir-cb-dir")
.is_dir()
);
Ok(())
}
#[test]
async fn fs_mkdir_callback_recursive(
#[tagged_as("fs")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-mkdir-callback-recursive", &[])
.await;
let _result = r?;
assert!(output.contains("mkdir callback recursive succeeded"));
assert!(
instance
.temp_dir_path()
.join("test")
.join("mkdir-cb-nested")
.join("deep")
.join("dir")
.is_dir()
);
Ok(())
}
#[test]
async fn fs_unlink_sync(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-unlink-sync", &[])
.await;
let _result = r?;
assert!(output.contains("unlinkSync succeeded"));
assert!(
!instance
.temp_dir_path()
.join("test")
.join("unlink-sync-file.txt")
.exists()
);
Ok(())
}
#[test]
async fn fs_unlink_callback(#[tagged_as("fs")] compiled: &CompiledTest) -> anyhow::Result<()> {
let mut instance = TestInstance::new(compiled.wasm_path()).await?;
let (r, output) = instance
.invoke_and_capture_output(None, "test-unlink-callback", &[])
.await;
let _result = r?;
assert!(output.contains("unlink callback succeeded"));
assert!(
!instance
.temp_dir_path()
.join("test")
.join("unlink-cb-file.txt")
.exists()
);
Ok(())
}
#[test]
async fn url_test1(#[tagged_as("url")] compiled_test: &CompiledTest) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test1", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn url_test2(#[tagged_as("url")] compiled_test: &CompiledTest) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test2", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn url_test3(#[tagged_as("url")] compiled_test: &CompiledTest) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test3", &[]).await;
let _ = r?;
println!("Output:\n{}", output);
assert_eq!(
output,
indoc! {
r#"[ 'q', 'URLUtils.searchParams' ]
[ 'topic', 'api' ]
true
false
true
[ 'api' ]
true
undefined
q=URLUtils.searchParams&topic=api&topic=webdev
undefined
q=URLUtils.searchParams&topic=More+webdev
undefined
q=URLUtils.searchParams
"#
}
);
Ok(())
}
#[test]
async fn url_test4(#[tagged_as("url")] compiled_test: &CompiledTest) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test4", &[]).await;
let _ = r?;
println!("Output:\n{}", output);
assert_eq!(
output,
indoc! {
r#"https:
test
pass
example.com
1234
/path
#fragment
api
URLUtils.searchParams
https://test:pass@example.com:1234/path?query=URLUtils.searchParams&topic=api#fragment
"#
}
);
Ok(())
}
#[test]
async fn url_test5(#[tagged_as("url")] compiled_test: &CompiledTest) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test5", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn web_crypto_random_uuid(
#[tagged_as("crypto")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "new-uuids", &[]).await;
let result = result?;
match result {
Some(Val::Tuple(vals)) => {
let strings = vals
.into_iter()
.filter_map(|v| match v {
Val::String(s) => Some(s),
_ => None,
})
.collect::<Vec<_>>();
assert_eq!(strings.len(), 2);
strings
.into_iter()
.map(|s| uuid::Uuid::parse_str(&s))
.collect::<Result<Vec<_>, _>>()?;
Ok(())
}
_ => Err(anyhow!("Expected tuple result")),
}
}
#[test]
async fn web_crypto_random_s8(
#[tagged_as("crypto")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "random-s8", &[Val::U32(10)]).await;
let result = result?;
match result {
Some(Val::List(vals)) => {
let bytes = vals
.into_iter()
.filter_map(|v| match v {
Val::S8(b) => Some(b),
_ => None,
})
.collect::<Vec<_>>();
assert_eq!(bytes.len(), 10);
assert!(
bytes.iter().any(|b| b != &bytes[0]),
"There should be some different bytes in the list"
);
Ok(())
}
_ => Err(anyhow!("Expected list<s8> result")),
}
}
#[test]
async fn web_crypto_random_u32(
#[tagged_as("crypto")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "random-u32", &[Val::U32(10)]).await;
let result = result?;
match result {
Some(Val::List(vals)) => {
let numbers = vals
.into_iter()
.filter_map(|v| match v {
Val::U32(n) => Some(n),
_ => None,
})
.collect::<Vec<_>>();
assert_eq!(numbers.len(), 10);
assert!(
numbers.iter().any(|b| b != &numbers[0]),
"There should be some different numbers in the list"
);
Ok(())
}
_ => Err(anyhow!("Expected list<u32> result")),
}
}
#[test]
async fn crypto_create_hash_sha256(
#[tagged_as("crypto")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"sha256-hex",
&[Val::String("some data to hash".to_string())],
)
.await;
let result = result?;
match result {
Some(Val::String(hex)) => {
assert_eq!(
hex,
"6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50"
);
Ok(())
}
_ => Err(anyhow!("Expected string result")),
}
}
#[test]
async fn crypto_create_hash_multi_update(
#[tagged_as("crypto")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"sha256-multi-update",
&[Val::List(vec![
Val::String("some ".to_string()),
Val::String("data ".to_string()),
Val::String("to hash".to_string()),
])],
)
.await;
let result = result?;
match result {
Some(Val::String(hex)) => {
assert_eq!(
hex,
"6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50"
);
Ok(())
}
_ => Err(anyhow!("Expected string result")),
}
}
#[test]
async fn response_static_error(
#[tagged_as("response_static")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("example:response-static/response-exports"),
"test-response-error",
&[],
)
.await;
let result = result?;
match result {
Some(Val::Record(vals)) => {
let status = vals
.iter()
.find(|(k, _)| k == "status")
.and_then(|(_, v)| match v {
Val::U16(s) => Some(*s),
_ => None,
})
.ok_or_else(|| anyhow!("No status field found"))?;
assert_eq!(status, 500, "error() should have status 500");
Ok(())
}
_ => Err(anyhow!("Expected record result")),
}
}
#[test]
async fn response_static_redirect(
#[tagged_as("response_static")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("example:response-static/response-exports"),
"test-response-redirect",
&[],
)
.await;
let result = result?;
match result {
Some(Val::Record(vals)) => {
let status = vals
.iter()
.find(|(k, _)| k == "status")
.and_then(|(_, v)| match v {
Val::U16(s) => Some(*s),
_ => None,
})
.ok_or_else(|| anyhow!("No status field found"))?;
let location = vals
.iter()
.find(|(k, _)| k == "location")
.and_then(|(_, v)| match v {
Val::Option(Some(val)) => match &**val {
Val::String(s) => Some(s.clone()),
_ => None,
},
_ => None,
})
.ok_or_else(|| anyhow!("No location field found"))?;
assert_eq!(status, 301, "redirect(url, 301) should have status 301");
assert_eq!(location, "https://example.com/new-path");
Ok(())
}
_ => Err(anyhow!("Expected record result")),
}
}
#[test]
async fn response_static_redirect_default(
#[tagged_as("response_static")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("example:response-static/response-exports"),
"test-response-redirect-default",
&[],
)
.await;
let result = result?;
match result {
Some(Val::Record(vals)) => {
let status = vals
.iter()
.find(|(k, _)| k == "status")
.and_then(|(_, v)| match v {
Val::U16(s) => Some(*s),
_ => None,
})
.ok_or_else(|| anyhow!("No status field found"))?;
assert_eq!(status, 302, "redirect() should default to status 302");
Ok(())
}
_ => Err(anyhow!("Expected record result")),
}
}
#[test]
async fn response_static_json(
#[tagged_as("response_static")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("example:response-static/response-exports"),
"test-response-json",
&[],
)
.await;
let result = result?;
match result {
Some(Val::Record(vals)) => {
let status = vals
.iter()
.find(|(k, _)| k == "status")
.and_then(|(_, v)| match v {
Val::U16(s) => Some(*s),
_ => None,
})
.ok_or_else(|| anyhow!("No status field found"))?;
let content_type = vals
.iter()
.find(|(k, _)| k == "content-type")
.and_then(|(_, v)| match v {
Val::String(s) => Some(s.clone()),
_ => None,
})
.ok_or_else(|| anyhow!("No content-type field found"))?;
let text = vals
.iter()
.find(|(k, _)| k == "text")
.and_then(|(_, v)| match v {
Val::String(s) => Some(s.clone()),
_ => None,
})
.ok_or_else(|| anyhow!("No text field found"))?;
assert_eq!(status, 200);
assert_eq!(content_type, "application/json");
assert!(text.contains("Hello, World!"));
assert!(text.contains("count"));
Ok(())
}
_ => Err(anyhow!("Expected record result")),
}
}
#[test]
async fn response_static_json_custom_status(
#[tagged_as("response_static")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("example:response-static/response-exports"),
"test-response-json-custom-status",
&[],
)
.await;
let result = result?;
match result {
Some(Val::Record(vals)) => {
let status = vals
.iter()
.find(|(k, _)| k == "status")
.and_then(|(_, v)| match v {
Val::U16(s) => Some(*s),
_ => None,
})
.ok_or_else(|| anyhow!("No status field found"))?;
assert_eq!(
status, 404,
"json() with init.status should set that status"
);
Ok(())
}
_ => Err(anyhow!("Expected record result")),
}
}
#[test]
async fn response_static_json_with_headers(
#[tagged_as("response_static")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("example:response-static/response-exports"),
"test-response-json-with-headers",
&[],
)
.await;
let result = result?;
match result {
Some(Val::Record(vals)) => {
let content_type = vals
.iter()
.find(|(k, _)| k == "content-type")
.and_then(|(_, v)| match v {
Val::String(s) => Some(s.clone()),
_ => None,
})
.ok_or_else(|| anyhow!("No content-type field found"))?;
let custom_header =
vals.iter()
.find(|(k, _)| k == "custom-header")
.and_then(|(_, v)| match v {
Val::Option(Some(val)) => match &**val {
Val::String(s) => Some(s.clone()),
_ => None,
},
_ => None,
});
assert_eq!(content_type, "application/json");
assert_eq!(custom_header, Some("CustomValue".to_string()));
Ok(())
}
_ => Err(anyhow!("Expected record result")),
}
}
#[test]
async fn response_static_redirect_invalid_status(
#[tagged_as("response_static")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("example:response-static/response-exports"),
"test-response-redirect-invalid-status",
&[],
)
.await;
let result = result?;
match result {
Some(Val::Record(vals)) => {
let success = vals
.iter()
.find(|(k, _)| k == "success")
.and_then(|(_, v)| match v {
Val::Bool(b) => Some(*b),
_ => None,
})
.ok_or_else(|| anyhow!("No success field found"))?;
let error = vals
.iter()
.find(|(k, _)| k == "error")
.and_then(|(_, v)| match v {
Val::String(s) => Some(s.clone()),
_ => None,
})
.ok_or_else(|| anyhow!("No error field found"))?;
assert!(
success,
"Should have caught the error. Error message: {}",
error
);
assert!(
error.contains("RangeError"),
"Should throw RangeError, got: {}",
error
);
Ok(())
}
_ => Err(anyhow!("Expected record result")),
}
}
#[test]
async fn response_static_json_string(
#[tagged_as("response_static")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) = invoke_and_capture_output(
compiled.wasm_path(),
Some("example:response-static/response-exports"),
"test-response-json-string",
&[],
)
.await;
let result = result?;
match result {
Some(Val::Record(vals)) => {
let content_type = vals
.iter()
.find(|(k, _)| k == "content-type")
.and_then(|(_, v)| match v {
Val::String(s) => Some(s.clone()),
_ => None,
})
.ok_or_else(|| anyhow!("No content-type field found"))?;
let text = vals
.iter()
.find(|(k, _)| k == "text")
.and_then(|(_, v)| match v {
Val::String(s) => Some(s.clone()),
_ => None,
})
.ok_or_else(|| anyhow!("No text field found"))?;
assert_eq!(content_type, "application/json");
assert!(text.contains("name"));
Ok(())
}
_ => Err(anyhow!("Expected record result")),
}
}
#[test_dep(tagged_as = "path")]
fn compiled_path() -> CompiledTest {
let path = Utf8Path::new("examples/path");
CompiledTest::new(path, false).expect("Failed to compile path")
}
#[test]
async fn path_test_basename(
#[tagged_as("path")] compiled_test: &CompiledTest,
) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-basename", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn path_test_dirname(
#[tagged_as("path")] compiled_test: &CompiledTest,
) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-dirname", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn path_test_extname(
#[tagged_as("path")] compiled_test: &CompiledTest,
) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-extname", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn path_test_is_absolute(
#[tagged_as("path")] compiled_test: &CompiledTest,
) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-is-absolute", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn path_test_join(#[tagged_as("path")] compiled_test: &CompiledTest) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-join", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn path_test_normalize(
#[tagged_as("path")] compiled_test: &CompiledTest,
) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-normalize", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn path_test_relative(
#[tagged_as("path")] compiled_test: &CompiledTest,
) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-relative", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn path_test_resolve(
#[tagged_as("path")] compiled_test: &CompiledTest,
) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-resolve", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn path_test_parse_format(
#[tagged_as("path")] compiled_test: &CompiledTest,
) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-parse-format", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn path_test_delimiter(
#[tagged_as("path")] compiled_test: &CompiledTest,
) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-delimiter", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn path_test_sep(#[tagged_as("path")] compiled_test: &CompiledTest) -> anyhow::Result<()> {
let (r, output) =
invoke_and_capture_output(compiled_test.wasm_path(), None, "test-sep", &[]).await;
let r = r?;
println!("Output:\n{}", output);
assert_eq!(r, Some(Val::Bool(true)));
Ok(())
}
#[test]
async fn fetch_headers_iterator(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"headers-iterator",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 26 (Headers iterator)"));
assert!(output.contains("Testing Headers Symbol.iterator:"));
assert!(output.contains("Total headers via Symbol.iterator:"));
assert!(output.contains("Total headers via entries():"));
assert!(output.contains("Symbol.iterator test passed"));
Ok(())
}
#[test]
async fn fetch_headers_constructor_iterator(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"headers-constructor-iterator",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 27 (Headers constructor iterator)"));
assert!(output.contains("Testing Headers constructor with for-of loop:"));
assert!(output.contains("x-custom-1 = value1"));
assert!(output.contains("x-custom-2 = value2"));
assert!(output.contains("x-custom-3 = value3"));
assert!(output.contains("Total headers from for-of loop: 3"));
assert!(output.contains("Total headers from entries(): 3"));
assert!(output.contains("Headers constructor iterator test passed"));
Ok(())
}
#[test]
async fn fetch_with_url_object(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"fetch-with-url-object",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 28 (URL object parameter)"));
assert!(output.contains(&format!("URL object test: status=200")));
assert!(output.contains("URL object fetch successful:"));
Ok(())
}
#[test]
async fn post_with_url_object(#[tagged_as("fetch")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-with-url-object",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 29 (POST with URL object)"));
assert!(output.contains("POST with URL object: status=201"));
assert!(output.contains("POST with URL object successful: title=url_object_test"));
Ok(())
}
#[test]
async fn fetch_url_object_with_query_params(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"fetch-url-object-with-query-params",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 30 (URL object with query parameters)"));
assert!(output.contains("URL with query params: status=200"));
assert!(output.contains("Fetched"));
assert!(output.contains("item(s) with URL query params"));
Ok(())
}
#[test]
async fn abort_controller_basic(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-abort-basic", &[]).await;
assert!(output.contains("Created AbortController"));
assert!(output.contains("Signal aborted (before abort): false"));
assert!(output.contains("Signal aborted (after abort): true"));
assert!(output.contains("test-abort-basic passed"));
Ok(())
}
#[test]
async fn abort_signal_static(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-abort-signal", &[]).await;
assert!(output.contains("Created aborted signal"));
assert!(output.contains("Signal aborted: true"));
assert!(output.contains("Signal reason: Custom abort reason"));
assert!(output.contains("test-abort-signal passed"));
Ok(())
}
#[test]
async fn abort_signal_timeout(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-abort-timeout", &[]).await;
assert!(output.contains("Created timeout signal (10ms)"));
assert!(output.contains("Signal aborted (immediately): false"));
assert!(output.contains("test-abort-timeout passed"));
Ok(())
}
#[test]
async fn abort_controller_event(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-abort-event", &[]).await;
assert!(output.contains("Added abort event listener"));
assert!(output.contains("Abort event fired"));
assert!(output.contains("Event fired: true"));
assert!(output.contains("test-abort-event passed"));
Ok(())
}
#[test]
async fn abort_controller_reason(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-abort-reason", &[]).await;
assert!(output.contains("Signal reason (before abort): undefined"));
assert!(output.contains("Signal reason message: Custom error"));
assert!(output.contains("Reasons match: true"));
assert!(output.contains("test-abort-reason passed"));
Ok(())
}
#[test]
async fn abort_controller_multiple_listeners(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"test-abort-multiple-listeners",
&[],
)
.await;
assert!(output.contains("Added 2 abort event listeners"));
assert!(output.contains("Listener 1 fired"));
assert!(output.contains("Listener 2 fired"));
assert!(output.contains("Both listeners called: true"));
assert!(output.contains("test-abort-multiple-listeners passed"));
Ok(())
}
#[test]
async fn abort_controller_throw_if_aborted(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-throw-if-aborted", &[]).await;
assert!(output.contains("throwIfAborted: Caught error: Custom reason"));
assert!(output.contains("test-throw-if-aborted passed"));
Ok(())
}
#[test]
async fn abort_controller_throw_if_aborted_not_aborted(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"test-throw-if-aborted-not-aborted",
&[],
)
.await;
assert!(output.contains("throwIfAborted: Did not throw (correct)"));
assert!(output.contains("throwIfAborted when not aborted - threw: false"));
assert!(output.contains("test-throw-if-aborted-not-aborted passed"));
Ok(())
}
#[test]
async fn abort_controller_onabort_handler(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-onabort-handler", &[]).await;
assert!(output.contains("Set onabort handler"));
assert!(output.contains("onabort handler fired"));
assert!(output.contains("onabort called: true"));
assert!(output.contains("test-onabort-handler passed"));
Ok(())
}
#[test]
async fn abort_controller_once_option(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-once-option", &[]).await;
assert!(output.contains("Added listener with once: true"));
assert!(output.contains("Listener called"));
assert!(output.contains("After first abort, call count: 1"));
assert!(output.contains("test-once-option passed"));
Ok(())
}
#[test]
async fn abort_controller_remove_event_listener(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"test-remove-event-listener",
&[],
)
.await;
assert!(output.contains("Added listener"));
assert!(output.contains("Removed listener"));
assert!(output.contains("After abort, listener called: false"));
assert!(output.contains("test-remove-event-listener passed"));
Ok(())
}
#[test]
async fn abort_controller_idempotent(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-abort-idempotent", &[]).await;
assert!(output.contains("First abort"));
assert!(output.contains("Second abort"));
assert!(output.contains("Listener called: 1 times"));
assert!(output.contains("Reasons match (should stay same): true"));
assert!(output.contains("test-abort-idempotent passed"));
Ok(())
}
#[test]
async fn abort_controller_no_reason(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-abort-no-reason", &[]).await;
assert!(output.contains("Abort without reason - reason type: DOMException"));
assert!(output.contains("Abort without reason - reason name: AbortError"));
assert!(output.contains("test-abort-no-reason passed"));
Ok(())
}
#[test]
async fn abort_controller_duplicate_listeners(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-duplicate-listeners", &[])
.await;
assert!(output.contains("Added same handler twice"));
assert!(output.contains("Handler call count: 1"));
assert!(output.contains("test-duplicate-listeners passed"));
Ok(())
}
#[test]
async fn fetch_abort_already_aborted(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"test-fetch-abort-already-aborted",
&[],
)
.await;
assert!(output.contains("Testing fetch with already-aborted signal"));
assert!(output.contains("Caught abort error"));
assert!(output.contains("test-fetch-abort-already-aborted passed"));
Ok(())
}
#[test]
async fn fetch_abort_during_request(
#[tagged_as("abort_controller")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (_, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"test-fetch-abort-during-request",
&[],
)
.await;
assert!(output.contains("Testing fetch with signal aborted during request"));
assert!(output.contains("Aborting fetch"));
assert!(output.contains("Caught abort error"));
assert!(output.contains("Result: aborted"));
assert!(output.contains("test-fetch-abort-during-request passed"));
Ok(())
}
#[test]
async fn fetch_request_body_get_reader_after_access(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"request-body-get-reader-after-access",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("fetch test 31 (Request body getReader after body access)"));
assert!(output.contains("Stored body reference: exists"));
assert!(output.contains("Got reader from request.body"));
assert!(output.contains("Body content: test body content"));
assert!(output.contains("Request body getReader after access test passed"));
Ok(())
}
#[test]
async fn fetch_response_body_get_reader_after_access(
#[tagged_as("fetch")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"response-body-get-reader-after-access",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains(
"fetch test 32 (Response body getReader after body access - TanStack AI pattern)"
));
assert!(output.contains("Stored body reference: exists"));
assert!(output.contains("Got reader from response.body"));
assert!(output.contains("Response body getReader after access test passed"));
Ok(())
}
#[test]
async fn xhr_simple_get(#[tagged_as("xhr")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "simple-get", &[Val::U16(port)])
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 1: simple GET"));
assert!(output.contains("Status: 200"));
assert!(output.contains("Response length:"));
Ok(())
}
#[test]
async fn xhr_simple_post(#[tagged_as("xhr")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) =
invoke_and_capture_output(compiled.wasm_path(), None, "simple-post", &[Val::U16(port)])
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 2: simple POST"));
assert!(output.contains("POST Status:") || output.contains("POST Error occurred"));
Ok(())
}
#[test]
async fn xhr_set_request_headers(
#[tagged_as("xhr")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"set-request-headers",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 3: set request headers"));
assert!(output.contains("Headers test status:") || output.contains("ReadyState changed"));
Ok(())
}
#[test]
async fn xhr_get_response_headers(
#[tagged_as("xhr")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"get-response-headers",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 4: get response headers"));
assert!(output.contains("Content-Type header:") || output.contains("All headers length:"));
Ok(())
}
#[test]
async fn xhr_response_types(#[tagged_as("xhr")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"response-types",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 5: response types"));
assert!(output.contains("Text response type:") || output.contains("JSON response type:"));
Ok(())
}
#[test]
async fn xhr_readystate_events(#[tagged_as("xhr")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"readystate-events",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 6: readystate events"));
assert!(output.contains("ReadyState changed to:") || output.contains("States encountered:"));
Ok(())
}
#[test]
async fn xhr_error_handling(#[tagged_as("xhr")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"error-handling",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 7: error handling"));
assert!(
output.contains("Error handler called")
|| output.contains("Request succeeded")
|| output.contains("Status on error:")
);
Ok(())
}
#[test]
async fn xhr_abort_request(#[tagged_as("xhr")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"abort-request",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 8: abort request"));
assert!(output.contains("Abort handler called") || output.contains("Request completed"));
Ok(())
}
#[test]
async fn xhr_timeout_handling(#[tagged_as("xhr")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"timeout-handling",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 9: timeout handling"));
assert!(output.contains("Timeout handler called") || output.contains("Request completed"));
Ok(())
}
#[test]
async fn xhr_post_with_form_data(
#[tagged_as("xhr")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-with-form-data",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 11: POST with FormData"));
assert!(output.contains("FormData POST status:") || output.contains("ReadyState changed"));
Ok(())
}
#[test]
async fn xhr_post_with_json_body(
#[tagged_as("xhr")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"post-with-json-body",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 12: POST with JSON body"));
assert!(output.contains("JSON POST status:") || output.contains("ReadyState changed"));
Ok(())
}
#[test]
async fn xhr_basic_auth(#[tagged_as("xhr")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"request-with-basic-auth",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 10: basic auth"));
assert!(output.contains("Auth request status:") || output.contains("Auth request error"));
Ok(())
}
#[test]
async fn xhr_status_is_number(#[tagged_as("xhr")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (port, _) = start_test_server().await;
let (r, output) = invoke_and_capture_output(
compiled.wasm_path(),
None,
"status-is-number",
&[Val::U16(port)],
)
.await;
let _ = r?;
println!("{output}");
assert!(output.contains("XMLHttpRequest test 13: status is number"));
assert!(output.contains("Status typeof: number"));
assert!(output.contains("Status is number: true"));
assert!(output.contains("Status === 200: true"));
assert!(output.contains("Status >= 200 && status < 300: true"));
Ok(())
}
#[test]
async fn os_eol_constant(#[tagged_as("os")] compiled: &CompiledTest) -> anyhow::Result<()> {
let (result, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test", &[]).await;
let result = result?;
if let Some(Val::String(json_str)) = result {
let result_obj: serde_json::Value = serde_json::from_str(&json_str)?;
assert!(
result_obj["hasEOL"].as_bool().unwrap_or(false),
"os.EOL should exist"
);
assert!(
result_obj["canImportAsOs"].as_bool().unwrap_or(false),
"os module should be importable"
);
assert!(
result_obj["hasArch"].as_bool().unwrap_or(false),
"os.arch should exist"
);
assert!(
result_obj["hasPlatform"].as_bool().unwrap_or(false),
"os.platform should exist"
);
assert!(
result_obj["hasHostname"].as_bool().unwrap_or(false),
"os.hostname should exist"
);
assert!(
result_obj["hasUptime"].as_bool().unwrap_or(false),
"os.uptime should exist"
);
assert!(
result_obj["hasHomedir"].as_bool().unwrap_or(false),
"os.homedir should exist"
);
assert!(
result_obj["hasTmpdir"].as_bool().unwrap_or(false),
"os.tmpdir should exist"
);
} else {
anyhow::bail!("Expected string result from test function");
}
Ok(())
}
#[test]
async fn structured_clone_primitives(
#[tagged_as("structured_clone")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-primitives", &[]).await;
let result = result?;
if let Some(Val::String(s)) = result {
assert!(s.contains("number: true"), "number cloning failed");
assert!(s.contains("string: true"), "string cloning failed");
assert!(s.contains("boolean: true"), "boolean cloning failed");
assert!(s.contains("null: true"), "null cloning failed");
assert!(s.contains("undefined: true"), "undefined cloning failed");
assert!(s.contains("bigint: true"), "bigint cloning failed");
} else {
anyhow::bail!("Expected string result");
}
Ok(())
}
#[test]
async fn structured_clone_objects(
#[tagged_as("structured_clone")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-objects", &[]).await;
let result = result?;
if let Some(Val::String(s)) = result {
assert!(
s.contains("object different ref: true"),
"object should be cloned by value"
);
assert!(s.contains("object.a: true"), "object.a should be cloned");
assert!(s.contains("object.b: true"), "object.b should be cloned");
assert!(
s.contains("nested different ref: true"),
"nested object should be cloned"
);
assert!(
s.contains("mutation isolated: true"),
"mutation should be isolated"
);
} else {
anyhow::bail!("Expected string result");
}
Ok(())
}
#[test]
async fn structured_clone_arrays(
#[tagged_as("structured_clone")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-arrays", &[]).await;
let result = result?;
if let Some(Val::String(s)) = result {
assert!(
s.contains("array different ref: true"),
"array should be cloned by value"
);
assert!(
s.contains("array length: true"),
"array length should match"
);
assert!(
s.contains("typed array different ref: true"),
"typed array should be cloned"
);
assert!(
s.contains("typed array length: true"),
"typed array length should match"
);
} else {
anyhow::bail!("Expected string result");
}
Ok(())
}
#[test]
async fn structured_clone_collections(
#[tagged_as("structured_clone")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-collections", &[]).await;
let result = result?;
if let Some(Val::String(s)) = result {
assert!(
s.contains("map different ref: true"),
"map should be cloned"
);
assert!(s.contains("map size: true"), "map size should match");
assert!(
s.contains("set different ref: true"),
"set should be cloned"
);
assert!(s.contains("set size: true"), "set size should match");
} else {
anyhow::bail!("Expected string result");
}
Ok(())
}
#[test]
async fn structured_clone_special_types(
#[tagged_as("structured_clone")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-special-types", &[]).await;
let result = result?;
if let Some(Val::String(s)) = result {
assert!(
s.contains("date different ref: true"),
"date should be cloned"
);
assert!(s.contains("date value: true"), "date value should match");
assert!(
s.contains("regex different ref: true"),
"regex should be cloned"
);
assert!(
s.contains("regex source: true"),
"regex source should match"
);
assert!(s.contains("regex flags: true"), "regex flags should match");
assert!(
s.contains("error different ref: true"),
"error should be cloned"
);
assert!(
s.contains("error message: true"),
"error message should match"
);
} else {
anyhow::bail!("Expected string result");
}
Ok(())
}
#[test]
async fn structured_clone_circular_refs(
#[tagged_as("structured_clone")] compiled: &CompiledTest,
) -> anyhow::Result<()> {
let (result, _output) =
invoke_and_capture_output(compiled.wasm_path(), None, "test-circular-refs", &[]).await;
let result = result?;
if let Some(Val::String(s)) = result {
assert!(
s.contains("circular clone different ref: true"),
"circular ref should be cloned"
);
assert!(
s.contains("circular self ref: true"),
"circular self ref should be preserved"
);
assert!(
s.contains("circular array different ref: true"),
"circular array should be cloned"
);
assert!(
s.contains("circular array self ref: true"),
"circular array self ref should be preserved"
);
} else {
anyhow::bail!("Expected string result");
}
Ok(())
}