1// Flags: --expose-internals --no-warnings
2
3import '../common/index.mjs';
4import { describe, it, beforeEach } from 'node:test';
5import assert from 'node:assert';
6import { finished } from 'node:stream/promises';
7import { DefaultSerializer } from 'node:v8';
8import serializer from 'internal/test_runner/reporter/v8-serializer';
9import runner from 'internal/test_runner/runner';
10
11async function toArray(chunks) {
12  const arr = [];
13  for await (const i of chunks) arr.push(i);
14  return arr;
15}
16
17const chunks = await toArray(serializer([
18  { type: 'test:diagnostic', data: { nesting: 0, details: {}, message: 'diagnostic' } },
19]));
20const defaultSerializer = new DefaultSerializer();
21defaultSerializer.writeHeader();
22const headerLength = defaultSerializer.releaseBuffer().length;
23
24describe('v8 deserializer', () => {
25  let fileTest;
26  let reported;
27  beforeEach(() => {
28    reported = [];
29    fileTest = new runner.FileTest({ name: 'filetest' });
30    fileTest.reporter.on('data', (data) => reported.push(data));
31    assert(fileTest.isClearToSend());
32  });
33
34  async function collectReported(chunks) {
35    chunks.forEach((chunk) => fileTest.parseMessage(chunk));
36    fileTest.drain();
37    fileTest.reporter.end();
38    await finished(fileTest.reporter);
39    return reported;
40  }
41
42  it('should do nothing when no chunks', async () => {
43    const reported = await collectReported([]);
44    assert.deepStrictEqual(reported, []);
45  });
46
47  it('should deserialize a chunk with no serialization', async () => {
48    const reported = await collectReported([Buffer.from('unknown')]);
49    assert.deepStrictEqual(reported, [
50      { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' },
51    ]);
52  });
53
54  it('should deserialize a serialized chunk', async () => {
55    const reported = await collectReported(chunks);
56    assert.deepStrictEqual(reported, [
57      { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' },
58    ]);
59  });
60
61  it('should deserialize a serialized chunk after non-serialized chunk', async () => {
62    const reported = await collectReported([Buffer.concat([Buffer.from('unknown'), ...chunks])]);
63    assert.deepStrictEqual(reported, [
64      { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' },
65      { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' },
66    ]);
67  });
68
69  it('should deserialize a serialized chunk before non-serialized output', async () => {
70    const reported = await collectReported([Buffer.concat([ ...chunks, Buffer.from('unknown')])]);
71    assert.deepStrictEqual(reported, [
72      { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' },
73      { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' },
74    ]);
75  });
76
77  const headerPosition = headerLength * 2 + 4;
78  for (let i = 0; i < headerPosition + 5; i++) {
79    const message = `should deserialize a serialized message split into two chunks {...${i},${i + 1}...}`;
80    it(message, async () => {
81      const data = chunks[0];
82      const reported = await collectReported([data.subarray(0, i), data.subarray(i)]);
83      assert.deepStrictEqual(reported, [
84        { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' },
85      ]);
86    });
87
88    it(`${message} wrapped by non-serialized data`, async () => {
89      const data = chunks[0];
90      const reported = await collectReported([
91        Buffer.concat([Buffer.from('unknown'), data.subarray(0, i)]),
92        Buffer.concat([data.subarray(i), Buffer.from('unknown')]),
93      ]);
94      assert.deepStrictEqual(reported, [
95        { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' },
96        { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' },
97        { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' },
98      ]);
99    }
100    );
101  }
102
103});
104