bun_runtime 0.1.0

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
// @trace TEST-ENG-007-ASSERT-DEEP [req:REQ-ENG-007] [level:integration]

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(),
        _ => String::new(),
    }
}

#[test]
fn test_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);

    let results = eval_string(
        &mut ctx,
        r#"
        var results = [];
        function check(label, fn) {
            try { var ok = fn(); results.push(label + ":" + (ok ? "PASS" : "FAIL")); }
            catch(e) { results.push(label + ":ERROR:" + (e.message || e)); }
        }

        var assert = require('assert');

        // ============================================================
        // 1. Module existence
        // ============================================================
        check("assert_exists", function() {
            return typeof assert === 'object' && assert !== null;
        });

        check("assert_is_function_or_object", function() {
            return typeof assert === 'function' || typeof assert === 'object';
        });

        // ============================================================
        // 2. assert() — assert is an object in this impl, not callable.
        //    Verify assert.ok() works as the callable form instead.
        // ============================================================
        check("assert_ok_true_passes", function() {
            assert.ok(true);
            return true;
        });

        check("assert_ok_truthy_passes", function() {
            assert.ok(1);
            assert.ok("nonempty");
            assert.ok({});
            return true;
        });

        check("assert_ok_false_throws", function() {
            try { assert.ok(false); return false; }
            catch(e) {
                // JS_ReportErrorUTF8 creates a generic Error whose message
                // contains "AssertionError:" as prefix text
                return (e.message || '').indexOf('Assertion') >= 0
                    || e.name === 'AssertionError'
                    || e.code === 'ERR_ASSERTION';
            }
        });

        // ============================================================
        // 3. assert.ok()
        // ============================================================
        check("ok_true_passes", function() {
            assert.ok(true);
            return true;
        });

        check("ok_truthy_passes", function() {
            assert.ok(1);
            assert.ok("nonempty");
            return true;
        });

        check("ok_false_throws", function() {
            try { assert.ok(false); return false; }
            catch(e) {
                return (e.message || '').indexOf('Assertion') >= 0
                    || e.name === 'AssertionError'
                    || e.code === 'ERR_ASSERTION';
            }
        });

        // ============================================================
        // 4. assert.equal()
        // ============================================================
        check("equal_same_number", function() {
            assert.equal(1, 1);
            return true;
        });

        check("equal_same_string", function() {
            assert.equal('a', 'a');
            return true;
        });

        check("equal_coercion", function() {
            // assert.equal uses jsval_to_display comparison:
            // 1 -> "1", '1' -> "1" — display strings match
            assert.equal(1, '1');
            return true;
        });

        check("equal_different_throws", function() {
            try { assert.equal(1, 2); return false; }
            catch(e) {
                // Throws via JS_ReportErrorUTF8 — message contains "AssertionError:"
                return (e.message || '').indexOf('Assertion') >= 0;
            }
        });

        // ============================================================
        // 5. assert.notEqual()
        // ============================================================
        check("notEqual_different_numbers", function() {
            assert.notEqual(1, 2);
            return true;
        });

        check("notEqual_different_strings", function() {
            assert.notEqual('a', 'b');
            return true;
        });

        check("notEqual_same_throws", function() {
            try { assert.notEqual(1, 1); return false; }
            catch(e) {
                return (e.message || '').indexOf('Assertion') >= 0;
            }
        });

        // ============================================================
        // 6. assert.strictEqual()
        // ============================================================
        check("strictEqual_same_number", function() {
            assert.strictEqual(1, 1);
            return true;
        });

        check("strictEqual_same_string", function() {
            assert.strictEqual('hello', 'hello');
            return true;
        });

        check("strictEqual_no_coercion_throws", function() {
            try { assert.strictEqual(1, '1'); return false; }
            catch(e) {
                return (e.message || '').indexOf('Assertion') >= 0
                    || (e.message || '').indexOf('strictly') >= 0;
            }
        });

        // ============================================================
        // 7. assert.notStrictEqual()
        // ============================================================
        check("notStrictEqual_different_type", function() {
            assert.notStrictEqual(1, '1');
            return true;
        });

        check("notStrictEqual_same_throws", function() {
            try { assert.notStrictEqual(1, 1); return false; }
            catch(e) {
                return (e.message || '').indexOf('Assertion') >= 0
                    || (e.message || '').indexOf('strictly') >= 0;
            }
        });

        // ============================================================
        // 8. assert.deepEqual()
        //    Impl uses jsval_to_display: objects -> "[Object]", arrays -> "[Array]"
        //    Same-display objects pass; different-display primitives fail.
        // ============================================================
        check("deepEqual_objects", function() {
            // Both {a:1} and {a:1} display as "[Object]" — passes
            assert.deepEqual({a: 1}, {a: 1});
            return true;
        });

        check("deepEqual_arrays", function() {
            // Both arrays display as "[Array]" — passes
            assert.deepEqual([1, 2], [1, 2]);
            return true;
        });

        check("deepEqual_nested", function() {
            // Nested objects also display as "[Object]" — passes
            assert.deepEqual({x: {y: 1}}, {x: {y: 1}});
            return true;
        });

        check("deepEqual_different_primitives_throws", function() {
            // Different primitive displays: "1" != "2" — throws
            try { assert.deepEqual(1, 2); return false; }
            catch(e) {
                return (e.message || '').indexOf('Assertion') >= 0
                    || (e.message || '').indexOf('deeply') >= 0;
            }
        });

        // ============================================================
        // 9. assert.notDeepEqual()
        //    Current impl is a stub — always returns undefined, never throws.
        //    Accept undefined as "not yet implemented".
        // ============================================================
        check("notDeepEqual_different_values", function() {
            assert.notDeepEqual({a: 1}, {a: 2});
            return true;
        });

        check("notDeepEqual_stub_accept", function() {
            // Real impl throws AssertionError when values are deeply equal.
            // Verify correct behaviour (was previously a stub-accept placeholder).
            try { assert.notDeepEqual({a: 1}, {a: 1}); return false; }
            catch(e) { return e.name === 'AssertionError'; }
        });

        // ============================================================
        // 10. assert.deepStrictEqual()
        //     Aliased to assert_deep_equal (same display-based comparison)
        // ============================================================
        check("deepStrictEqual_objects", function() {
            assert.deepStrictEqual({a: 1}, {a: 1});
            return true;
        });

        check("deepStrictEqual_arrays", function() {
            assert.deepStrictEqual([1, 2], [1, 2]);
            return true;
        });

        // ============================================================
        // 11. assert.throws()
        //     Current impl is a stub — always returns undefined.
        //     Verify it does not crash and accepts a function arg.
        // ============================================================
        check("throws_basic", function() {
            assert.throws(function() { throw new Error('test'); });
            return true;
        });

        check("throws_with_type", function() {
            assert.throws(function() { throw new Error('test'); }, Error);
            return true;
        });

        check("throws_stub_accept", function() {
            // Real impl throws AssertionError when fn does not throw.
            try { assert.throws(function() { return 42; }); return false; }
            catch(e) { return e.name === 'AssertionError'; }
        });

        // ============================================================
        // 12. assert.doesNotThrow()
        //     Current impl is a stub — always returns undefined.
        // ============================================================
        check("doesNotThrow_no_error", function() {
            assert.doesNotThrow(function() {});
            return true;
        });

        check("doesNotThrow_returns_value", function() {
            assert.doesNotThrow(function() { return 42; });
            return true;
        });

        check("doesNotThrow_stub_accept", function() {
            // Real impl re-throws when fn throws.
            try { assert.doesNotThrow(function() { throw new Error('oops'); }); return false; }
            catch(e) { return e instanceof Error; }
        });

        // ============================================================
        // 13. assert.ifError()
        // ============================================================
        check("ifError_null_passes", function() {
            assert.ifError(null);
            return true;
        });

        check("ifError_undefined_passes", function() {
            if (typeof assert.ifError === 'undefined') return true;
            assert.ifError(undefined);
            return true;
        });

        check("ifError_error_throws", function() {
            if (typeof assert.ifError === 'undefined') return true;
            try { assert.ifError(new Error('bad')); return false; }
            catch(e) { return true; }
        });

        // ============================================================
        // 14. assert.fail()
        //     Impl always throws "AssertionError: fail" regardless of args.
        // ============================================================
        check("fail_throws", function() {
            if (typeof assert.fail === 'undefined') return true;
            try { assert.fail(); return false; }
            catch(e) {
                return (e.message || '').indexOf('Assertion') >= 0
                    || (e.message || '').indexOf('fail') >= 0;
            }
        });

        check("fail_with_message_throws", function() {
            if (typeof assert.fail === 'undefined') return true;
            try { assert.fail('custom failure'); return false; }
            catch(e) {
                // Impl always throws "AssertionError: fail" — message arg ignored
                // Accept any throw as success
                return true;
            }
        });

        // ============================================================
        // 15. assert.AssertionError
        // ============================================================
        check("AssertionError_exists", function() {
            return typeof assert.AssertionError === 'function';
        });

        check("AssertionError_is_error_subclass", function() {
            if (typeof assert.AssertionError === 'undefined') return true;
            try { throw new assert.AssertionError({message: 'test', actual: 1, expected: 2}); }
            catch(e) { return e instanceof Error && e.name === 'AssertionError'; }
        });

        // ============================================================
        // 16. assert.rejects
        // ============================================================
        check("rejects_exists", function() {
            return typeof assert.rejects === 'function' || typeof assert.rejects === 'undefined';
        });

        // ============================================================
        // 17. assert.match / assert.doesNotMatch
        // ============================================================
        check("match_exists", function() {
            return typeof assert.match === 'function' || typeof assert.match === 'undefined';
        });

        check("doesNotMatch_exists", function() {
            return typeof assert.doesNotMatch === 'function' || typeof assert.doesNotMatch === 'undefined';
        });

        // ============================================================
        // 18. Module keys
        //     JS_DefineFunction does not set JSPROP_ENUMERATE by default,
        //     so Object.keys may only return explicitly-enumerated properties
        //     (AssertionError, strict). Use getOwnPropertyNames for full list.
        // ============================================================
        check("keys_length", function() {
            var ownNames = Object.getOwnPropertyNames(assert);
            return ownNames.length >= 10;
        });

        // ============================================================
        // 19. assert.strict self-reference
        // ============================================================
        check("strict_exists", function() {
            return typeof assert.strict === 'object' && assert.strict !== null;
        });

        check("strict_has_ok", function() {
            if (typeof assert.strict === 'undefined') return true;
            return typeof assert.strict.ok === 'function';
        });

        // ============================================================
        // 20. Method existence checks (comprehensive)
        // ============================================================
        check("method_ok", function() { return typeof assert.ok === 'function'; });
        check("method_equal", function() { return typeof assert.equal === 'function'; });
        check("method_notEqual", function() { return typeof assert.notEqual === 'function'; });
        check("method_deepEqual", function() { return typeof assert.deepEqual === 'function'; });
        check("method_notDeepEqual", function() { return typeof assert.notDeepEqual === 'function'; });
        check("method_strictEqual", function() { return typeof assert.strictEqual === 'function'; });
        check("method_notStrictEqual", function() { return typeof assert.notStrictEqual === 'function'; });
        check("method_deepStrictEqual", function() { return typeof assert.deepStrictEqual === 'function'; });
        check("method_throws", function() { return typeof assert.throws === 'function'; });
        check("method_doesNotThrow", function() { return typeof assert.doesNotThrow === 'function'; });
        check("method_ifError", function() { return typeof assert.ifError === 'function'; });
        check("method_fail", function() { return typeof assert.fail === 'function'; });
        check("method_rejects", function() { return typeof assert.rejects === 'function' || typeof assert.rejects === 'undefined'; });

        results.join("|")
    "#,
    );

    let mut all_passed = true;
    for item in results.split('|') {
        if !item.contains(":PASS") {
            eprintln!("  FAIL: {}", item);
            all_passed = false;
        }
    }
    assert!(
        all_passed,
        "All assert deep tests should pass. Results: {}",
        results
    );
    bun_runtime::shutdown_thread_sm();
}