jstime_core 0.66.0

Another JS Runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Streams API Conformance Tests
// Based on https://streams.spec.whatwg.org/

use jstime_core as jstime;

mod common;

#[cfg(test)]
mod conformance_streams {
    use super::*;

    // Test that ReadableStream exists as a global constructor
    #[test]
    fn readable_stream_exists() {
        let result = common::get_type_of("ReadableStream");
        assert_eq!(result.unwrap(), "function");
    }

    // Test that WritableStream exists as a global constructor
    #[test]
    fn writable_stream_exists() {
        let result = common::get_type_of("WritableStream");
        assert_eq!(result.unwrap(), "function");
    }

    // Test that TransformStream exists as a global constructor
    #[test]
    fn transform_stream_exists() {
        let result = common::get_type_of("TransformStream");
        assert_eq!(result.unwrap(), "function");
    }

    // Test that ReadableStreamDefaultReader exists
    #[test]
    fn readable_stream_default_reader_exists() {
        let result = common::get_type_of("ReadableStreamDefaultReader");
        assert_eq!(result.unwrap(), "function");
    }

    // Test that WritableStreamDefaultWriter exists
    #[test]
    fn writable_stream_default_writer_exists() {
        let result = common::get_type_of("WritableStreamDefaultWriter");
        assert_eq!(result.unwrap(), "function");
    }

    // Test ReadableStream constructor
    #[test]
    fn readable_stream_constructor() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script("const rs = new ReadableStream(); typeof rs;", "test");
        assert_eq!(result.unwrap(), "object");
    }

    // Test ReadableStream with start callback
    #[test]
    fn readable_stream_with_start() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            let called = false;
            const rs = new ReadableStream({
              start(controller) {
                called = true;
                controller.close();
              }
            });
            called;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "true");
    }

    // Test ReadableStream enqueue
    #[test]
    fn readable_stream_enqueue() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            let enqueued = [];
            const rs = new ReadableStream({
              start(controller) {
                enqueued.push("chunk1");
                controller.enqueue("chunk1");
                enqueued.push("chunk2");
                controller.enqueue("chunk2");
                controller.close();
              }
            });
            enqueued.join(',');
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "chunk1,chunk2");
    }

    // Test ReadableStream locked property
    #[test]
    fn readable_stream_locked() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const rs = new ReadableStream();
            const before = rs.locked;
            const reader = rs.getReader();
            const after = rs.locked;
            before + ',' + after;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "false,true");
    }

    // Test ReadableStream getReader
    #[test]
    fn readable_stream_get_reader() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const rs = new ReadableStream();
            const reader = rs.getReader();
            typeof reader;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "object");
    }

    // Test reader.read() returns a promise
    #[test]
    fn readable_stream_reader_read_returns_promise() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const rs = new ReadableStream({
              start(controller) {
                controller.enqueue("data");
                controller.close();
              }
            });
            const reader = rs.getReader();
            const promise = reader.read();
            promise instanceof Promise;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "true");
    }

    // Test WritableStream constructor
    #[test]
    fn writable_stream_constructor() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script("const ws = new WritableStream(); typeof ws;", "test");
        assert_eq!(result.unwrap(), "object");
    }

    // Test WritableStream getWriter
    #[test]
    fn writable_stream_get_writer() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const ws = new WritableStream();
            const writer = ws.getWriter();
            typeof writer;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "object");
    }

    // Test WritableStream write
    #[test]
    fn writable_stream_write() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            let written = [];
            const ws = new WritableStream({
              write(chunk) {
                written.push(chunk);
              }
            });
            const writer = ws.getWriter();
            writer.write("hello");
            writer.write("world");
            written.join(',');
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "hello,world");
    }

    // Test WritableStream locked property
    #[test]
    fn writable_stream_locked() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const ws = new WritableStream();
            const before = ws.locked;
            const writer = ws.getWriter();
            const after = ws.locked;
            before + ',' + after;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "false,true");
    }

    // Test TransformStream constructor
    #[test]
    fn transform_stream_constructor() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script("const ts = new TransformStream(); typeof ts;", "test");
        assert_eq!(result.unwrap(), "object");
    }

    // Test TransformStream readable property
    #[test]
    fn transform_stream_readable() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const ts = new TransformStream();
            ts.readable instanceof ReadableStream;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "true");
    }

    // Test TransformStream writable property
    #[test]
    fn transform_stream_writable() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const ts = new TransformStream();
            ts.writable instanceof WritableStream;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "true");
    }

    // Test TransformStream with transform function
    #[test]
    fn transform_stream_with_transform() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            let transformed = [];
            const ts = new TransformStream({
              transform(chunk, controller) {
                const upper = chunk.toUpperCase();
                transformed.push(upper);
                controller.enqueue(upper);
              }
            });
            const writer = ts.writable.getWriter();
            writer.write("hello");
            transformed[0];
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "HELLO");
    }

    // Test TransformStream passthrough (no transform)
    #[test]
    fn transform_stream_passthrough() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const ts = new TransformStream();
            const writer = ts.writable.getWriter();
            const reader = ts.readable.getReader();
            writer.write("data");
            // Check that readable stream has data queued
            typeof reader;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "object");
    }

    // Test Response.body returns ReadableStream
    #[test]
    fn response_body_is_readable_stream() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const response = new Response("Hello");
            response.body instanceof ReadableStream;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "true");
    }

    // Test Response.body can be read
    #[test]
    fn response_body_can_be_read() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const response = new Response("Hello, World!");
            const reader = response.body.getReader();
            // Check that reader exists and is an object
            typeof reader;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "object");
    }

    // Test reader.releaseLock()
    #[test]
    fn readable_stream_reader_release_lock() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const rs = new ReadableStream();
            const reader = rs.getReader();
            const locked1 = rs.locked;
            reader.releaseLock();
            const locked2 = rs.locked;
            locked1 + ',' + locked2;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "true,false");
    }

    // Test writer.releaseLock()
    #[test]
    fn writable_stream_writer_release_lock() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const ws = new WritableStream();
            const writer = ws.getWriter();
            const locked1 = ws.locked;
            writer.releaseLock();
            const locked2 = ws.locked;
            locked1 + ',' + locked2;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "true,false");
    }

    // Test ReadableStream cancel
    #[test]
    fn readable_stream_cancel() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const rs = new ReadableStream({
              start(controller) {
                controller.enqueue("data");
              }
            });
            const cancelPromise = rs.cancel();
            cancelPromise instanceof Promise;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "true");
    }

    // Test WritableStream abort
    #[test]
    fn writable_stream_abort() {
        let _setup_guard = common::setup();
        let options = jstime::Options::default();
        let mut jstime = jstime::JSTime::new(options);
        let result = jstime.run_script(
            r#"
            const ws = new WritableStream();
            const abortPromise = ws.abort();
            abortPromise instanceof Promise;
            "#,
            "test",
        );
        assert_eq!(result.unwrap(), "true");
    }
}