Skip to content

Commit e7ddf2c

Browse files
test(jco): add more stream lower tests
1 parent aaad87e commit e7ddf2c

3 files changed

Lines changed: 173 additions & 16 deletions

File tree

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use wit_bindgen::StreamReader;
1111
use bindings::exports::jco::test_components::use_stream_async;
1212
use bindings::exports::jco::test_components::use_stream_sync;
1313

14-
use bindings::exports::jco::test_components::use_stream_async::{ExampleRecord, ExampleVariant};
14+
use bindings::exports::jco::test_components::use_stream_async::{
15+
ExampleEnum, ExampleFlags, ExampleRecord, ExampleVariant,
16+
};
1517

1618
struct Component;
1719

@@ -81,6 +83,46 @@ impl use_stream_async::Guest for Component {
8183
async fn read_stream_values_variant(rx: StreamReader<ExampleVariant>) -> Vec<ExampleVariant> {
8284
read_async_values(rx).await
8385
}
86+
87+
async fn read_stream_values_option_string(
88+
rx: StreamReader<Option<String>>,
89+
) -> Vec<Option<String>> {
90+
read_async_values(rx).await
91+
}
92+
93+
async fn read_stream_values_result_string(
94+
rx: StreamReader<Result<String, String>>,
95+
) -> Vec<Result<String, String>> {
96+
read_async_values(rx).await
97+
}
98+
99+
async fn read_stream_values_tuple(
100+
rx: StreamReader<(u32, i32, String)>,
101+
) -> Vec<(u32, i32, String)> {
102+
read_async_values(rx).await
103+
}
104+
105+
async fn read_stream_values_flags(rx: StreamReader<ExampleFlags>) -> Vec<ExampleFlags> {
106+
read_async_values(rx).await
107+
}
108+
109+
async fn read_stream_values_enum(rx: StreamReader<ExampleEnum>) -> Vec<ExampleEnum> {
110+
read_async_values(rx).await
111+
}
112+
113+
async fn read_stream_values_list_u8(rx: StreamReader<Vec<u8>>) -> Vec<Vec<u8>> {
114+
read_async_values(rx).await
115+
}
116+
117+
async fn read_stream_values_list_string(rx: StreamReader<Vec<String>>) -> Vec<Vec<String>> {
118+
read_async_values(rx).await
119+
}
120+
121+
async fn read_stream_values_fixed_list_u32(
122+
rx: StreamReader<Vec<[u32; 5]>>,
123+
) -> Vec<Vec<[u32; 5]>> {
124+
read_async_values(rx).await
125+
}
84126
}
85127

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

crates/test-components/wit/all.wit

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,20 @@ interface use-stream-async {
216216

217217
read-stream-values-variant: async func(s: stream<example-variant>) -> list<example-variant>;
218218

219-
// read-stream-values-list-u8: async func(s: stream<list<u8>>) -> list<list<u8>>;
220-
// read-stream-values-list-string: async func(s: stream<list<string>>) -> list<list<string>>;
221-
// read-stream-values-list-record: async func(s: stream<list<example-record>>) -> list<list<example-record>>;
222-
// read-stream-values-fixed-list-u32: async func(s: stream<list<list<u32, 5>>>) -> list<list<list<u32, 5>>>;
219+
read-stream-values-tuple: async func(s: stream<tuple<u32, s32, string>>) -> list<tuple<u32, s32, string>>;
223220

224-
// read-stream-values-tuple: async func(s: stream<tuple<u32, s32, string>>) -> list<tuple<u32, s32, string>>;
221+
read-stream-values-flags: async func(s: stream<example-flags>) -> list<example-flags>;
225222

226-
// read-stream-values-flags: async func(s: stream<example-flags>) -> list<example-flags>;
223+
read-stream-values-enum: async func(s: stream<example-enum>) -> list<example-enum>;
227224

228-
// read-stream-values-enum: async func(s: stream<example-enum>) -> list<example-enum>;
225+
read-stream-values-option-string: async func(s: stream<option<string>>) -> list<option<string>>;
229226

230-
// read-stream-values-option-string: async func(s: stream<option<string>>) -> list<option<string>>;
227+
read-stream-values-result-string: async func(s: stream<result<string, string>>) -> list<result<string, string>>;
231228

232-
// read-stream-values-result-string: async func(s: stream<result<string, string>>) -> list<result<string, string>>;
229+
read-stream-values-list-u8: async func(s: stream<list<u8>>) -> list<list<u8>>;
230+
read-stream-values-list-string: async func(s: stream<list<string>>) -> list<list<string>>;
231+
read-stream-values-fixed-list-u32: async func(s: stream<list<list<u32, 5>>>) -> list<list<list<u32, 5>>>;
232+
// read-stream-values-list-record: async func(s: stream<list<example-record>>) -> list<list<example-record>>;
233233

234234
// // NOTE: the check here will be whether the resources are disposed inside the component properly
235235
// read-stream-values-example-resource-own: async func(s: stream<example-resource>);
@@ -243,7 +243,7 @@ interface use-stream-async {
243243

244244
world stream-rx {
245245
// import resources;
246-
246+
247247
export use-stream-sync;
248248
export use-stream-async;
249249
}

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

Lines changed: 120 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,134 @@ suite("stream<T> lowers", () => {
242242
);
243243
assert.deepEqual(returnedVals, [
244244
// TODO: wit type representation smoothing mismatch
245-
{ tag: "maybe-u32", val: { tag: 'some', val: 123 }},
246-
{ tag: "maybe-u32", val: { tag: 'none' }},
245+
{ tag: "maybe-u32", val: { tag: "some", val: 123 } },
246+
{ tag: "maybe-u32", val: { tag: "none" } },
247247
{ tag: "str", val: "string-value" },
248248
{ tag: "num", val: 1 },
249249
]);
250250

251-
vals = [
252-
{ tag: "float", val: 123.1 },
253-
];
251+
vals = [{ tag: "float", val: 123.1 }];
254252
returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesVariant(
255253
createReadableStreamFromValues(vals),
256254
);
257255
assert.closeTo(returnedVals[0].val, 123.1, 0.01);
256+
});
257+
258+
test.concurrent("tuple", async () => {
259+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesTuple, AsyncFunction);
260+
261+
let vals = [
262+
[1, -1, "one"],
263+
[2, -2, "two"],
264+
[3, -3, "two"],
265+
];
266+
let returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesTuple(
267+
createReadableStreamFromValues(vals),
268+
);
269+
assert.deepEqual(returnedVals, vals);
270+
});
271+
272+
test.concurrent("flags", async () => {
273+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesFlags, AsyncFunction);
274+
275+
let vals = [
276+
{ first: true, second: false, third: false },
277+
{ first: false, second: true, third: false },
278+
{ first: false, second: false, third: true },
279+
];
280+
let returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesFlags(
281+
createReadableStreamFromValues(vals),
282+
);
283+
assert.deepEqual(returnedVals, vals);
284+
});
285+
286+
test.concurrent("enum", async () => {
287+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesEnum, AsyncFunction);
288+
289+
let vals = ["first", "second", "third"];
290+
let returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesEnum(
291+
createReadableStreamFromValues(vals),
292+
);
293+
assert.deepEqual(returnedVals, ["first", "second", "third"]);
294+
});
295+
296+
test.concurrent("option<string>", async () => {
297+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesOptionString, AsyncFunction);
298+
299+
let vals = ["present string", null];
300+
let returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesOptionString(
301+
createReadableStreamFromValues(vals),
302+
);
303+
assert.deepEqual(returnedVals, [
304+
// TODO: wit type representation smoothing mismatch
305+
{ tag: "some", val: "present string" },
306+
{ tag: "none" },
307+
]);
308+
});
309+
310+
test.concurrent("result<string>", async () => {
311+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesResultString, AsyncFunction);
312+
313+
let vals = [{ tag: "ok", val: "present string" }, { tag: "err", val: "nope" }, "bare string (ok)"];
314+
let returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesResultString(
315+
createReadableStreamFromValues(vals),
316+
);
317+
assert.deepEqual(returnedVals, [
318+
// TODO: wit type representation smoothing mismatch
319+
{ tag: "ok", val: "present string" },
320+
{ tag: "err", val: "nope" },
321+
{ tag: "ok", val: "bare string (ok)" },
322+
]);
323+
});
324+
325+
test.concurrent("list<u8>", async () => {
326+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesListU8, AsyncFunction);
327+
328+
let vals = [[0x01, 0x02, 0x03, 0x04, 0x05], new Uint8Array([0x05, 0x04, 0x03, 0x02, 0x01]), []];
329+
let returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesListU8(
330+
createReadableStreamFromValues(vals),
331+
);
332+
assert.deepEqual(returnedVals, [
333+
// TODO: wit type representation smoothing mismatch
334+
vals[0],
335+
[...vals[1]],
336+
[],
337+
]);
338+
});
339+
340+
test.concurrent("list<string>", async () => {
341+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesListString, AsyncFunction);
342+
343+
let vals = [["first", "second", "third"], []];
344+
let returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesListString(
345+
createReadableStreamFromValues(vals),
346+
);
347+
assert.deepEqual(returnedVals, vals);
348+
});
258349

350+
test.concurrent("list<list<u32, 5>>", async () => {
351+
assert.instanceOf(instance["jco:test-components/use-stream-async"].readStreamValuesFixedListU32, AsyncFunction);
352+
353+
let vals = [
354+
[
355+
[1, 2, 3, 4, 5],
356+
[0, 0, 0, 0, 0],
357+
],
358+
[[0, 0, 0, 0, 0], new Uint32Array([0x05, 0x04, 0x03, 0x02, 0x01])],
359+
];
360+
let returnedVals = await instance["jco:test-components/use-stream-async"].readStreamValuesFixedListU32(
361+
createReadableStreamFromValues(vals),
362+
);
363+
assert.deepEqual(returnedVals, [
364+
// TODO(fix): wit type representation smoothing mismatch
365+
[
366+
[1, 2, 3, 4, 5],
367+
[0, 0, 0, 0, 0],
368+
],
369+
[
370+
[0, 0, 0, 0, 0],
371+
[0x05, 0x04, 0x03, 0x02, 0x01],
372+
],
373+
]);
259374
});
260375
});

0 commit comments

Comments
 (0)