Skip to content

Commit d3932bd

Browse files
test(jco): add test stubs
1 parent bb832b3 commit d3932bd

3 files changed

Lines changed: 97 additions & 6 deletions

File tree

crates/test-components/src/bin/stream_rx.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use wit_bindgen::StreamReader;
1010

1111
use bindings::exports::jco::test_components::use_stream_async;
1212
use bindings::exports::jco::test_components::use_stream_sync;
13+
use bindings::jco::test_components::resources;
1314

1415
use bindings::exports::jco::test_components::use_stream_async::{
1516
ExampleEnum, ExampleFlags, ExampleRecord, ExampleVariant,
@@ -129,6 +130,29 @@ impl use_stream_async::Guest for Component {
129130
) -> Vec<Vec<ExampleRecord>> {
130131
read_async_values(rx).await
131132
}
133+
134+
async fn read_stream_values_example_resource_own(rx: StreamReader<resources::ExampleResource>) {
135+
let _ = read_async_values(rx).await;
136+
// All vals dropped at the end of this function
137+
}
138+
139+
async fn read_stream_values_example_resource_own_attr(
140+
rx: StreamReader<resources::ExampleResource>,
141+
) -> Vec<u32> {
142+
let vals = read_async_values::<resources::ExampleResource>(rx).await;
143+
vals.into_iter().map(|r| r.get_id()).collect()
144+
}
145+
146+
async fn read_stream_values_stream_string(
147+
mut rx: StreamReader<StreamReader<String>>,
148+
) -> Vec<Vec<String>> {
149+
let mut vals = Vec::new();
150+
while let Some(inner_rx) = rx.next().await {
151+
let inner_vals = read_async_values(inner_rx).await;
152+
vals.push(inner_vals);
153+
}
154+
vals
155+
}
132156
}
133157

134158
async fn read_async_values<T>(mut rx: StreamReader<T>) -> Vec<T> {

crates/test-components/wit/all.wit

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ interface use-stream-sync {
187187
}
188188

189189
interface use-stream-async {
190-
// use resources.{example-resource};
190+
use resources.{example-resource};
191191
use example-types.{example-variant, example-enum, example-record, example-flags};
192192

193193
stream-passthrough: async func(s: stream<u32>) -> stream<u32>;
@@ -231,18 +231,18 @@ interface use-stream-async {
231231
read-stream-values-fixed-list-u32: async func(s: stream<list<list<u32, 5>>>) -> list<list<list<u32, 5>>>;
232232
read-stream-values-list-record: async func(s: stream<list<example-record>>) -> list<list<example-record>>;
233233

234-
// // NOTE: the check here will be whether the resources are disposed inside the component properly
235-
// read-stream-values-example-resource-own: async func(s: stream<example-resource>);
234+
// NOTE: the check here will be whether the resources are disposed inside the component properly
235+
read-stream-values-example-resource-own: async func(s: stream<example-resource>);
236236

237-
// read-stream-values-example-resource-own-attr: async func(s: stream<example-resource>) -> list<u32>;
237+
read-stream-values-example-resource-own-attr: async func(s: stream<example-resource>) -> list<u32>;
238238

239-
// read-stream-values-stream-string: async func(s: stream<stream<string>>) -> list<list<string>>;
239+
read-stream-values-stream-string: async func(s: stream<stream<string>>) -> list<list<string>>;
240240

241241
//read-stream-values-future-string: async func(vals: list<future<string>>) -> result<stream<future<string>>, string>;
242242
}
243243

244244
world stream-rx {
245-
// import resources;
245+
import resources;
246246

247247
export use-stream-sync;
248248
export use-stream-async;

packages/jco/test/p3/stream-lowers.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ import { WASIShim } from "@bytecodealliance/preview2-shim/instantiation";
1010
suite("stream<T> lowers", () => {
1111
let esModule, cleanup, instance;
1212

13+
class ExampleResource {
14+
#id;
15+
16+
dropped = false;
17+
18+
constructor(id) {
19+
this.#id = id;
20+
}
21+
22+
getId() {
23+
return this.#id;
24+
}
25+
26+
[Symbol.for('dispose')]() {
27+
this.dropped = true;
28+
}
29+
}
30+
1331
beforeAll(async () => {
1432
const name = "stream-rx";
1533
const setupRes = await setupAsyncTest({
@@ -39,6 +57,9 @@ suite("stream<T> lowers", () => {
3957
beforeEach(async () => {
4058
instance = await esModule.instantiate(undefined, {
4159
...new WASIShim().getImportObject(),
60+
"jco:test-components/resources": {
61+
ExampleResource,
62+
},
4263
});
4364
});
4465

@@ -390,4 +411,50 @@ suite("stream<T> lowers", () => {
390411
assert.deepEqual(returnedVals, vals);
391412
});
392413

414+
test.only("example-resource", async () => {
415+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesExampleResourceOwn, AsyncFunction);
416+
417+
let vals = [
418+
new ExampleResource(0),
419+
new ExampleResource(1),
420+
new ExampleResource(2),
421+
];
422+
await instance["jco:test-components/use-stream-async"].readStreamValuesExampleResourceOwn(
423+
createReadableStreamFromValues(vals),
424+
);
425+
assert(vals.every(r => r.dropped === true));
426+
});
427+
428+
test.skip("example-resource#get-id", async () => {
429+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesExampleResourceOwn, AsyncFunction);
430+
431+
let vals = [
432+
new ExampleResource(2),
433+
new ExampleResource(1),
434+
new ExampleResource(0),
435+
];
436+
const returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesExampleResourceOwn(
437+
createReadableStreamFromValues(vals),
438+
);
439+
assert.deepEqual(returnedVals, [2, 1, 0]);
440+
});
441+
442+
test.skip("stream<string>", async () => {
443+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesStreamString, AsyncFunction);
444+
445+
let vals = [
446+
createReadableStreamFromValues(["first", "stream", "values"]),
447+
createReadableStreamFromValues(["second", "stream", "here"]),
448+
createReadableStreamFromValues(["third", "values", "in stream"]),
449+
];
450+
const returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesStreamString(
451+
createReadableStreamFromValues(vals),
452+
);
453+
assert.deepEqual(returnedVals, [
454+
["first", "stream", "values"],
455+
["second", "stream", "here"],
456+
["third", "values", "in stream"],
457+
]);
458+
});
459+
393460
});

0 commit comments

Comments
 (0)