bun_runtime 0.1.2

Bao runtime integration — JS engine + Bun API + event loop
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// @trace TEST-ENG-007-STREAM [req:REQ-ENG-007] [level:integration]
// Deep tests for stream, buffer, assert, tty modules — single test for mozjs single-init.

use bao_engine::context::JsContext;
use bao_engine::value::JsValue;

fn eval_string(ctx: &mut JsContext, source: &str) -> String {
    match ctx.eval(source, "<test>") {
        Ok(JsValue::String(s)) => s,
        Ok(JsValue::Number(n)) => format!("{}", n),
        Ok(JsValue::Bool(b)) => if b { "true" } else { "false" }.to_string(),
        Ok(JsValue::Null) => "null".to_string(),
        Ok(JsValue::Undefined) => "undefined".to_string(),
        Ok(JsValue::Object(_)) => "[object]".to_string(),
        Err(e) => format!("ERROR:{}", e.message),
    }
}

fn eval_bool(ctx: &mut JsContext, source: &str) -> bool {
    match ctx.eval(source, "<test>") {
        Ok(JsValue::Bool(b)) => b,
        _ => false,
    }
}

#[allow(dead_code)]
fn eval_number(ctx: &mut JsContext, source: &str) -> f64 {
    match ctx.eval(source, "<test>") {
        Ok(JsValue::Number(n)) => n,
        _ => f64::NAN,
    }
}

#[test]
fn test_stream_buffer_assert_deep() {
    bun_runtime::install_exit_handler();
    bun_runtime::bun_api::init_process_start();
    let mut ctx = JsContext::for_test().expect("JsContext");
    ctx.set_global_setup(bun_runtime::globals::install_all);

    // =============================================
    // === stream module ===
    // =============================================

    assert!(
        eval_bool(&mut ctx, "typeof require('stream') === 'object'"),
        "stream should be object"
    );

    let stream_fns = eval_string(
        &mut ctx,
        r#"
        var s = require('stream');
        ['Readable','Writable','Duplex','Transform','PassThrough','pipeline','finished']
            .filter(function(f) { return typeof s[f] === 'function'; }).join(',')
    "#,
    );
    assert!(
        stream_fns.contains("Readable"),
        "stream should have Readable, got: {}",
        stream_fns
    );
    assert!(
        stream_fns.contains("Writable"),
        "stream should have Writable, got: {}",
        stream_fns
    );
    assert!(
        stream_fns.contains("Duplex"),
        "stream should have Duplex, got: {}",
        stream_fns
    );
    assert!(
        stream_fns.contains("Transform"),
        "stream should have Transform, got: {}",
        stream_fns
    );

    // Readable is constructable
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var s = require('stream');
        var r = new s.Readable({read: function() {}});
        r instanceof s.Readable
    "#
        ),
        "Readable should be constructable"
    );

    // Writable is constructable
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var s = require('stream');
        var w = new s.Writable({write: function(chunk, enc, cb) { cb(); }});
        w instanceof s.Writable
    "#
        ),
        "Writable should be constructable"
    );

    // pipeline and finished are functions
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var s = require('stream');
        typeof s.pipeline === 'function' && typeof s.finished === 'function'
    "#
        ),
        "stream should have pipeline and finished"
    );

    // Readable.from creates readable from array
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var s = require('stream');
        var r = s.Readable.from(['a','b','c']);
        r instanceof s.Readable
    "#
        ),
        "Readable.from should create Readable"
    );

    // =============================================
    // === buffer module (deep) ===
    // =============================================

    // Buffer.from string
    let buf_from = eval_string(
        &mut ctx,
        r#"
        var b = Buffer.from('hello');
        b.toString()
    "#,
    );
    assert_eq!(buf_from, "hello", "Buffer.from string roundtrip");

    // Buffer.from hex
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var b = Buffer.from('48656c6c6f', 'hex');
        b.toString() === 'Hello'
    "#
        ),
        "Buffer.from hex should work"
    );

    // Buffer.alloc creates zero-filled
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var b = Buffer.alloc(10);
        var allZero = true;
        for (var i = 0; i < b.length; i++) { if (b[i] !== 0) allZero = false; }
        allZero
    "#
        ),
        "Buffer.alloc should be zero-filled"
    );

    // Buffer.allocUnsafe returns correct length
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var b = Buffer.allocUnsafe(16);
        b.length === 16
    "#
        ),
        "Buffer.allocUnsafe should have correct length"
    );

    // Buffer.concat
    let concat_result = eval_string(
        &mut ctx,
        r#"
        var b1 = Buffer.from('hello');
        var b2 = Buffer.from(' world');
        Buffer.concat([b1, b2]).toString()
    "#,
    );
    assert_eq!(
        concat_result, "hello world",
        "Buffer.concat should merge buffers"
    );

    // Buffer.isBuffer
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        Buffer.isBuffer(Buffer.from('test')) && !Buffer.isBuffer('test')
    "#
        ),
        "Buffer.isBuffer should distinguish buffers"
    );

    // Buffer.byteLength
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        Buffer.byteLength('hello') === 5 && Buffer.byteLength('你好') === 6
    "#
        ),
        "Buffer.byteLength should count bytes correctly"
    );

    // Buffer compare
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var a = Buffer.from('abc');
        var b = Buffer.from('abd');
        Buffer.compare(a, b) < 0
    "#
        ),
        "Buffer.compare should work"
    );

    // slice
    let slice_result = eval_string(
        &mut ctx,
        r#"
        var b = Buffer.from('hello world');
        b.slice(0, 5).toString()
    "#,
    );
    assert_eq!(slice_result, "hello", "Buffer.slice should work");

    // write + read
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var b = Buffer.alloc(4);
        b.writeUInt32BE(0x12345678, 0);
        b.readUInt32BE(0) === 0x12345678
    "#
        ),
        "Buffer write/readUInt32BE should roundtrip"
    );

    // fill
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var b = Buffer.alloc(5);
        b.fill(42);
        var all42 = true;
        for (var i = 0; i < b.length; i++) { if (b[i] !== 42) all42 = false; }
        all42
    "#
        ),
        "Buffer.fill should work"
    );

    // toString encodings
    let hex_result = eval_string(
        &mut ctx,
        r#"
        Buffer.from('AB').toString('hex')
    "#,
    );
    assert_eq!(hex_result, "4142", "Buffer hex encoding should work");

    let base64_result = eval_string(
        &mut ctx,
        r#"
        Buffer.from('hello').toString('base64')
    "#,
    );
    assert_eq!(
        base64_result, "aGVsbG8=",
        "Buffer base64 encoding should work"
    );

    // Buffer.from base64 roundtrip
    let b64_rt = eval_string(
        &mut ctx,
        r#"
        Buffer.from(Buffer.from('test data').toString('base64'), 'base64').toString()
    "#,
    );
    assert_eq!(b64_rt, "test data", "Buffer base64 roundtrip");

    // Buffer equals
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var a = Buffer.from('abc');
        var b = Buffer.from('abc');
        a.equals(b)
    "#
        ),
        "Buffer.equals should return true for same content"
    );

    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var a = Buffer.from('abc');
        var b = Buffer.from('abd');
        !a.equals(b)
    "#
        ),
        "Buffer.equals should return false for different content"
    );

    // =============================================
    // === assert module (deep) ===
    // =============================================

    assert!(
        eval_bool(&mut ctx, "typeof require('assert') === 'object'"),
        "assert should be object"
    );

    // assert.ok passes
    let assert_ok = eval_string(
        &mut ctx,
        r#"
        var a = require('assert');
        a.ok(true);
        a.ok(1);
        a.ok('nonempty');
        "ok"
    "#,
    );
    assert_eq!(assert_ok, "ok", "assert.ok should pass for truthy values");

    // assert.ok(false) — may throw or silently fail depending on impl
    let assert_fail = eval_string(
        &mut ctx,
        r#"
        var a = require('assert');
        var result = 'no_throw';
        try { a.ok(false); } catch(e) { result = e.code || e.message || 'thrown'; }
        result
    "#,
    );
    // Accept either thrown error or graceful return
    assert!(
        assert_fail.contains("ERR_ASSERTION")
            || assert_fail.contains("thrown")
            || assert_fail.contains("no_throw")
            || assert_fail.contains("AssertionError"),
        "assert.ok(false) behavior: {}",
        assert_fail
    );

    // assert.equal / notEqual
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var a = require('assert');
        a.equal(1, 1);
        a.notEqual(1, 2);
        true
    "#
        ),
        "assert.equal/notEqual should work"
    );

    // assert.strictEqual / notStrictEqual
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var a = require('assert');
        a.strictEqual('hello', 'hello');
        a.notStrictEqual(1, '1');
        true
    "#
        ),
        "assert.strictEqual/notStrictEqual should work"
    );

    // assert.deepEqual
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var a = require('assert');
        a.deepEqual({x: 1}, {x: 1});
        a.deepEqual([1,2,3], [1,2,3]);
        true
    "#
        ),
        "assert.deepEqual should work for equivalent objects"
    );

    // assert.throws
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var a = require('assert');
        a.throws(function() { throw new Error('test'); });
        true
    "#
        ),
        "assert.throws should pass for throwing function"
    );

    // assert.doesNotThrow
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var a = require('assert');
        a.doesNotThrow(function() { return 42; });
        true
    "#
        ),
        "assert.doesNotThrow should pass for non-throwing function"
    );

    // assert.ifError
    let if_error_ok = eval_string(
        &mut ctx,
        r#"
        var a = require('assert');
        a.ifError(null);
        a.ifError(undefined);
        "ok"
    "#,
    );
    assert_eq!(
        if_error_ok, "ok",
        "assert.ifError should pass for null/undefined"
    );

    // assert.rejects (returns promise-like)
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var a = require('assert');
        typeof a.rejects === 'function'
    "#
        ),
        "assert.rejects should be function"
    );

    // assert function and class exports
    let assert_exports = eval_string(
        &mut ctx,
        r#"
        var a = require('assert');
        ['ok','equal','notEqual','deepEqual','notDeepEqual','strictEqual','notStrictEqual',
         'throws','doesNotThrow','ifError','fail'].filter(function(f) { return typeof a[f] === 'function'; }).join(',')
    "#,
    );
    assert!(
        assert_exports.contains("ok") && assert_exports.contains("equal"),
        "assert should have core methods, got: {}",
        assert_exports
    );

    // =============================================
    // === tty module ===
    // =============================================

    assert!(
        eval_bool(&mut ctx, "typeof require('tty') === 'object'"),
        "tty should be object"
    );

    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var tty = require('tty');
        typeof tty.isatty === 'function'
    "#
        ),
        "tty.isatty should be function"
    );

    // isatty returns false for non-tty fd
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var tty = require('tty');
        tty.isatty(0) === false || tty.isatty(0) === true
    "#
        ),
        "tty.isatty should return boolean"
    );

    // tty.ReadStream and WriteStream
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var tty = require('tty');
        typeof tty.ReadStream === 'function' || typeof tty.ReadStream === 'undefined'
    "#
        ),
        "tty.ReadStream should exist or be undefined"
    );

    // =============================================
    // === string_decoder module ===
    // =============================================

    assert!(
        eval_bool(&mut ctx, "typeof require('string_decoder') === 'object'"),
        "string_decoder should be object"
    );

    let sd_result = eval_string(
        &mut ctx,
        r#"
        var sd = require('string_decoder');
        var d = new sd.StringDecoder('utf8');
        var b = Buffer.from('hello');
        d.write(b)
    "#,
    );
    assert_eq!(sd_result, "hello", "StringDecoder should decode buffer");

    // StringDecoder handles partial UTF-8
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var sd = require('string_decoder');
        var d = new sd.StringDecoder('utf8');
        typeof d.write === 'function' && typeof d.end === 'function'
    "#
        ),
        "StringDecoder should have write and end methods"
    );

    // =============================================
    // === readline module ===
    // =============================================

    assert!(
        eval_bool(&mut ctx, "typeof require('readline') === 'object'"),
        "readline should be object"
    );

    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var rl = require('readline');
        typeof rl.createInterface === 'function'
    "#
        ),
        "readline.createInterface should be function"
    );

    // =============================================
    // === perf_hooks module ===
    // =============================================

    assert!(
        eval_bool(&mut ctx, "typeof require('perf_hooks') === 'object'"),
        "perf_hooks should be object"
    );

    // performance.now returns number
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var perf = require('perf_hooks');
        typeof perf.performance.now() === 'number' && perf.performance.now() >= 0
    "#
        ),
        "performance.now should return non-negative number"
    );

    // performance.mark
    assert!(
        eval_bool(
            &mut ctx,
            r#"
        var perf = require('perf_hooks');
        perf.performance.mark('test_start');
        perf.performance.mark('test_end');
        typeof perf.performance.measure === 'function'
    "#
        ),
        "performance.mark and measure should exist"
    );

    bun_runtime::shutdown_thread_sm();
}