1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const child_process = require('child_process');
5const { once } = require('events');
6const { inspect } = require('util');
7
8if (process.argv[2] !== 'child') {
9  for (const value of [null, 42, Infinity, 'foo']) {
10    assert.throws(() => {
11      child_process.spawn(process.execPath, [], { serialization: value });
12    }, {
13      code: 'ERR_INVALID_ARG_VALUE',
14      message: "The property 'options.serialization' " +
15        "must be one of: undefined, 'json', 'advanced'. " +
16        `Received ${inspect(value)}`
17    });
18  }
19
20  (async () => {
21    const cp = child_process.spawn(process.execPath, [__filename, 'child'],
22                                   {
23                                     stdio: ['ipc', 'inherit', 'inherit'],
24                                     serialization: 'advanced'
25                                   });
26
27    const circular = {};
28    circular.circular = circular;
29    for await (const message of [
30      { uint8: new Uint8Array(4) },
31      { float64: new Float64Array([ Math.PI ]) },
32      { buffer: Buffer.from('Hello!') },
33      { map: new Map([{ a: 1 }, { b: 2 }]) },
34      { bigInt: 1337n },
35      circular,
36      new Error('Something went wrong'),
37      new RangeError('Something range-y went wrong'),
38    ]) {
39      cp.send(message);
40      const [ received ] = await once(cp, 'message');
41      assert.deepStrictEqual(received, message);
42    }
43
44    cp.disconnect();
45  })().then(common.mustCall());
46} else {
47  process.on('message', (msg) => process.send(msg));
48}
49