wasm-rquickjs-cli 0.1.3

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
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
import assert from 'node:assert';
import { AssertionError } from 'node:assert';

export const testOk = () => {
    try {
        assert(true);
        assert.ok(true);
        assert.ok(1);
        assert.ok('non-empty');

        let caught = false;
        try { assert(false); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert(false) should have thrown');

        caught = false;
        try { assert.ok(0); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.ok(0) should have thrown');

        caught = false;
        try { assert.ok(''); } catch (e) { caught = true; }
        if (!caught) throw new Error("assert.ok('') should have thrown");

        caught = false;
        try { assert.ok(null); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.ok(null) should have thrown');

        caught = false;
        try { assert.ok(undefined); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.ok(undefined) should have thrown');

        // Test Error-as-message: should throw the Error directly
        caught = false;
        var customErr = new TypeError('custom');
        try { assert.ok(false, customErr); } catch (e) {
            caught = true;
            if (e !== customErr) throw new Error('Expected the custom Error to be thrown directly');
        }
        if (!caught) throw new Error('assert.ok(false, Error) should have thrown');

        // Test message factory
        caught = false;
        try {
            assert.ok(false, function() { return 'lazy message'; });
        } catch (e) {
            caught = true;
            if (e.message !== 'lazy message') throw new Error('Expected lazy message but got: ' + e.message);
        }
        if (!caught) throw new Error('assert.ok(false, factory) should have thrown');

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testEqual = () => {
    try {
        assert.equal(1, 1);
        assert.equal('a', 'a');
        assert.equal(1, '1'); // loose equality

        let caught = false;
        try { assert.equal(1, 2); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.equal(1, 2) should have thrown');

        assert.notEqual(1, 2);

        caught = false;
        try { assert.notEqual(1, 1); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.notEqual(1, 1) should have thrown');

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testStrictEqual = () => {
    try {
        assert.strictEqual(1, 1);
        assert.strictEqual('hello', 'hello');

        let caught = false;
        try { assert.strictEqual(1, '1'); } catch (e) { caught = true; }
        if (!caught) throw new Error("assert.strictEqual(1, '1') should have thrown");

        assert.notStrictEqual(1, '1');

        caught = false;
        try { assert.notStrictEqual(1, 1); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.notStrictEqual(1, 1) should have thrown');

        // NaN should be strictly equal to NaN (Object.is semantics)
        assert.strictEqual(NaN, NaN);

        // +0 and -0 should not be strictly equal
        caught = false;
        try { assert.strictEqual(0, -0); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.strictEqual(0, -0) should have thrown');

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testDeepEqual = () => {
    try {
        assert.deepEqual({ a: 1, b: 2 }, { a: 1, b: 2 });
        assert.deepEqual([1, 2, 3], [1, 2, 3]);
        assert.deepEqual({ a: { b: 1 } }, { a: { b: 1 } });

        // Loose deep equal: 1 == '1'
        assert.deepEqual({ a: 1 }, { a: '1' });

        let caught = false;
        try { assert.deepEqual({ a: 1 }, { a: 2 }); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.deepEqual({a:1}, {a:2}) should have thrown');

        assert.notDeepEqual({ a: 1 }, { a: 2 });

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testDeepStrictEqual = () => {
    try {
        assert.deepStrictEqual({ a: 1 }, { a: 1 });
        assert.deepStrictEqual(new Date(0), new Date(0));
        assert.deepStrictEqual(/abc/g, /abc/g);

        // Strict: type matters
        let caught = false;
        try { assert.deepStrictEqual({ a: 1 }, { a: '1' }); } catch (e) { caught = true; }
        if (!caught) throw new Error("assert.deepStrictEqual({a:1}, {a:'1'}) should have thrown");

        assert.notDeepStrictEqual({ a: 1 }, { a: '1' });

        // WeakMap: only reference-equal
        var wm = new WeakMap();
        assert.deepStrictEqual(wm, wm); // same ref
        caught = false;
        try { assert.deepStrictEqual(new WeakMap(), new WeakMap()); } catch (e) { caught = true; }
        if (!caught) throw new Error('distinct WeakMaps should not be deepStrictEqual');

        // WeakSet: only reference-equal
        caught = false;
        try { assert.deepStrictEqual(new WeakSet(), new WeakSet()); } catch (e) { caught = true; }
        if (!caught) throw new Error('distinct WeakSets should not be deepStrictEqual');

        // Boxed primitives
        assert.deepStrictEqual(Object(1), Object(1));
        assert.deepStrictEqual(Object('hello'), Object('hello'));
        assert.deepStrictEqual(Object(true), Object(true));
        caught = false;
        try { assert.deepStrictEqual(Object(1), Object(2)); } catch (e) { caught = true; }
        if (!caught) throw new Error('Object(1) !== Object(2) should have thrown');
        caught = false;
        try { assert.deepStrictEqual(Object(1), Object('1')); } catch (e) { caught = true; }
        if (!caught) throw new Error('Object(1) !== Object("1") should have thrown');

        // RegExp lastIndex
        var r1 = /abc/g;
        var r2 = /abc/g;
        r1.lastIndex = 5;
        caught = false;
        try { assert.deepStrictEqual(r1, r2); } catch (e) { caught = true; }
        if (!caught) throw new Error('RegExp with different lastIndex should not be equal');

        // Error with cause
        var e1 = new Error('msg');
        e1.cause = 'reason';
        var e2 = new Error('msg');
        e2.cause = 'reason';
        assert.deepStrictEqual(e1, e2);

        var e3 = new Error('msg');
        e3.cause = 'different';
        caught = false;
        try { assert.deepStrictEqual(e1, e3); } catch (e) { caught = true; }
        if (!caught) throw new Error('Errors with different cause should not be equal');

        // Map with object keys (deep matching)
        var key1 = { id: 1 };
        var key2 = { id: 1 }; // structurally equal
        var m1 = new Map([[key1, 'val']]);
        var m2 = new Map([[key2, 'val']]);
        assert.deepStrictEqual(m1, m2);

        // Symbol-keyed properties in strict mode
        var sym = Symbol('test');
        var obj1 = {};
        obj1[sym] = 42;
        var obj2 = {};
        obj2[sym] = 42;
        assert.deepStrictEqual(obj1, obj2);

        var obj3 = {};
        obj3[sym] = 99;
        caught = false;
        try { assert.deepStrictEqual(obj1, obj3); } catch (e) { caught = true; }
        if (!caught) throw new Error('Objects with different Symbol values should not be equal');

        // Sparse arrays vs explicit undefined
        caught = false;
        try {
            var sparse = new Array(1); // [<empty>]
            var explicit = [undefined]; // [undefined]
            assert.deepStrictEqual(sparse, explicit);
        } catch (e) { caught = true; }
        if (!caught) throw new Error('Sparse array vs [undefined] should not be strictEqual');

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testThrows = () => {
    try {
        assert.throws(() => { throw new Error('test'); });
        assert.throws(() => { throw new Error('hello world'); }, /hello/);
        assert.throws(() => { throw new TypeError('test'); }, TypeError);
        assert.throws(() => { throw new Error('test'); }, { message: 'test' });

        // Object matching with regex for message
        assert.throws(() => { throw new Error('hello world'); }, { message: /hello/ });

        // Validation function
        assert.throws(() => { throw new Error('custom'); }, function(err) {
            return err.message === 'custom';
        });

        let caught = false;
        try { assert.throws(() => {}); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.throws with no error should have thrown');

        // String as second arg becomes message, not error matcher
        caught = false;
        try { assert.throws(() => {}, 'custom message'); } catch (e) {
            caught = true;
            if (e.message !== 'Missing expected exception: custom message') throw new Error('Expected "Missing expected exception: custom message" but got: ' + e.message);
        }
        if (!caught) throw new Error('assert.throws with string message should have thrown');

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testDoesNotThrow = () => {
    try {
        assert.doesNotThrow(() => { return 1; });

        let caught = false;
        try { assert.doesNotThrow(() => { throw new Error('oops'); }); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.doesNotThrow with throwing fn should have thrown');

        // doesNotThrow with non-matching error type should rethrow original
        caught = false;
        try {
            assert.doesNotThrow(() => { throw new RangeError('out of range'); }, TypeError);
        } catch (e) {
            caught = true;
            // Should be the original RangeError, not an AssertionError
            if (!(e instanceof RangeError)) throw new Error('Expected RangeError to be rethrown, got: ' + e.constructor.name);
        }
        if (!caught) throw new Error('doesNotThrow should rethrow non-matching errors');

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testIfError = () => {
    try {
        assert.ifError(null);
        assert.ifError(undefined);

        let caught = false;
        try { assert.ifError(new Error('test')); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.ifError(new Error) should have thrown');

        caught = false;
        try { assert.ifError(1); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.ifError(1) should have thrown');

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testMatch = () => {
    try {
        assert.match('hello world', /hello/);

        let caught = false;
        try { assert.match('hello', /goodbye/); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.match should have thrown');

        assert.doesNotMatch('hello', /goodbye/);

        caught = false;
        try { assert.doesNotMatch('hello', /hello/); } catch (e) { caught = true; }
        if (!caught) throw new Error('assert.doesNotMatch should have thrown');

        // Non-string first arg should throw AssertionError (not TypeError)
        caught = false;
        try { assert.match(123, /123/); } catch (e) {
            caught = true;
            if (!(e instanceof AssertionError)) throw new Error('Expected AssertionError for non-string arg, got: ' + e.constructor.name);
        }
        if (!caught) throw new Error('assert.match(number) should have thrown');

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testFail = () => {
    try {
        let caught = false;
        try { assert.fail('custom message'); } catch (e) {
            caught = true;
            if (e.message !== 'custom message') {
                throw new Error('Expected message "custom message" but got "' + e.message + '"');
            }
        }
        if (!caught) throw new Error('assert.fail should have thrown');

        caught = false;
        try { assert.fail(); } catch (e) {
            caught = true;
            if (e.message !== 'Failed') throw new Error('Expected default "Failed" message');
        }
        if (!caught) throw new Error('assert.fail() should have thrown');

        // Error-as-message should throw it directly
        caught = false;
        var customErr = new TypeError('direct throw');
        try { assert.fail(customErr); } catch (e) {
            caught = true;
            if (e !== customErr) throw new Error('Expected the Error to be thrown directly');
        }
        if (!caught) throw new Error('assert.fail(Error) should have thrown');

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testRejects = async () => {
    try {
        await assert.rejects(async () => { throw new Error('async error'); });
        await assert.rejects(async () => { throw new TypeError('type err'); }, TypeError);
        await assert.rejects(Promise.reject(new Error('rejected')));

        let caught = false;
        try {
            await assert.rejects(async () => 42);
        } catch (e) {
            caught = true;
        }
        if (!caught) throw new Error('assert.rejects should have thrown for resolved promise');

        await assert.doesNotReject(async () => 42);
        await assert.doesNotReject(Promise.resolve(42));

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testStrictMode = () => {
    try {
        assert.strict.equal(1, 1);

        let caught = false;
        try { assert.strict.equal(1, '1'); } catch (e) { caught = true; }
        if (!caught) throw new Error("assert.strict.equal(1, '1') should have thrown");

        assert.strict.deepEqual({ a: 1 }, { a: 1 });

        caught = false;
        try { assert.strict.deepEqual({ a: 1 }, { a: '1' }); } catch (e) { caught = true; }
        if (!caught) throw new Error("assert.strict.deepEqual({a:1}, {a:'1'}) should have thrown");

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};

export const testAssertionError = () => {
    try {
        const err = new AssertionError({
            actual: 1,
            expected: 2,
            operator: 'strictEqual',
            message: 'values not equal',
        });

        if (err.name !== 'AssertionError') throw new Error('Expected name AssertionError but got ' + err.name);
        if (err.actual !== 1) throw new Error('Expected actual 1 but got ' + err.actual);
        if (err.expected !== 2) throw new Error('Expected expected 2 but got ' + err.expected);
        if (err.operator !== 'strictEqual') throw new Error('Expected operator strictEqual but got ' + err.operator);
        // Node.js appends the diff to the user message
        if (!err.message.startsWith('values not equal')) throw new Error('Expected message to start with "values not equal" but got ' + err.message);
        if (err.code !== 'ERR_ASSERTION') throw new Error('Expected code ERR_ASSERTION but got ' + err.code);
        if (!(err instanceof Error)) throw new Error('Expected instance of Error');
        if (!(err instanceof AssertionError)) throw new Error('Expected instance of AssertionError');
        if (err.generatedMessage !== false) throw new Error('Expected generatedMessage false');

        // Test auto-generated message
        const err2 = new AssertionError({
            actual: 'foo',
            expected: 'bar',
            operator: 'strictEqual',
        });
        if (err2.generatedMessage !== true) throw new Error('Expected generatedMessage true');
        if (!err2.message.includes('foo')) throw new Error('Auto message should include actual value');

        // toString should include ERR_ASSERTION
        var str = err.toString();
        if (!str.includes('ERR_ASSERTION')) throw new Error('toString should include ERR_ASSERTION, got: ' + str);

        return true;
    } catch (e) {
        console.error(e);
        return false;
    }
};